Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
611 lines (497 sloc)
17.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @filename : epd4in2.cpp | |
* @brief : Implements for Dual-color e-paper library | |
* @author : Yehui from Waveshare | |
* | |
* Copyright (C) Waveshare August 10 2017 | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documnetation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
#include <stdlib.h> | |
#include <iostream> | |
#include "epd4in2b.h" | |
#include "epdif.h" | |
Epd::~Epd() { | |
}; | |
Epd::Epd() { | |
reset_pin = RST_PIN; | |
dc_pin = DC_PIN; | |
cs_pin = CS_PIN; | |
busy_pin = BUSY_PIN; | |
width = EPD_WIDTH; | |
height = EPD_HEIGHT; | |
}; | |
int Epd::Init(void) { | |
/* this calls the peripheral hardware interface, see epdif */ | |
if (IfInit() != 0) { | |
return -1; | |
} | |
/* EPD hardware init start */ | |
Reset(); | |
SendCommand(POWER_SETTING); | |
SendData(0x03); // VDS_EN, VDG_EN | |
SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0] | |
SendData(0x2b); // VDH | |
SendData(0x2b); // VDL | |
SendData(0xff); // VDHR | |
SendCommand(BOOSTER_SOFT_START); | |
SendData(0x17); | |
SendData(0x17); | |
SendData(0x17); //07 0f 17 1f 27 2F 37 2f | |
SendCommand(POWER_ON); | |
WaitUntilIdle(); | |
SendCommand(PANEL_SETTING); | |
// SendData(0xbf); // KW-BF KWR-AF BWROTP 0f | |
// SendData(0x0b); | |
// SendData(0x0F); //300x400 Red mode, LUT from OTP | |
//SendData(0x1F); //300x400 B/W mode, LUT from OTP | |
SendData(0x3F); //300x400 B/W mode, LUT set by register | |
// SendData(0x2F); //300x400 Red mode, LUT set by register | |
SendCommand(PLL_CONTROL); | |
SendData(0x3C); // 3A 100Hz 29 150Hz 39 200Hz 31 171Hz 3C 50Hz (default) 0B 10Hz | |
//SendData(0x0B); //0B is 10Hz | |
// BVZ | |
SendCommand(VCOM_AND_DATA_INTERVAL_SETTING); | |
SendData(0x17); //border floating | |
/* EPD hardware init end */ | |
return 0; | |
} | |
/** | |
* @brief: basic function for sending commands | |
*/ | |
void Epd::SendCommand(unsigned char command) { | |
DigitalWrite(dc_pin, LOW); | |
SpiTransfer(command); | |
} | |
/** | |
* @brief: basic function for sending data | |
*/ | |
void Epd::SendData(unsigned char data) { | |
DigitalWrite(dc_pin, HIGH); | |
SpiTransfer(data); | |
} | |
/** | |
* @brief: Wait until the busy_pin goes HIGH | |
*/ | |
void Epd::WaitUntilIdle(void) { | |
while(DigitalRead(busy_pin) == 0) { //0: busy, 1: idle | |
DelayMs(1); | |
} | |
} | |
/** | |
* @brief: module reset. | |
* often used to awaken the module in deep sleep, | |
* see Epd::Sleep(); | |
*/ | |
void Epd::Reset(void) { | |
DigitalWrite(reset_pin, LOW); | |
DelayMs(200); | |
DigitalWrite(reset_pin, HIGH); | |
DelayMs(200); | |
} | |
/** | |
* @brief: transmit partial data to the SRAM. The final parameter chooses between dtm=1 and dtm=2 | |
*/ | |
void Epd::SetPartialWindow(const unsigned char* buffer_black, int x, int y, int w, int l, int dtm) { | |
SendCommand(PARTIAL_IN); | |
SendCommand(PARTIAL_WINDOW); | |
SendData(x >> 8); | |
SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored | |
SendData(((x & 0xf8) + w - 1) >> 8); | |
SendData(((x & 0xf8) + w - 1) | 0x07); | |
SendData(y >> 8); | |
SendData(y & 0xff); | |
SendData((y + l - 1) >> 8); | |
SendData((y + l - 1) & 0xff); | |
//SendData(0x01); // Gates scan both inside and outside of the partial window. (default) | |
SendData(0x00); // Gates scan both inside and outside of the partial window. (bvz) | |
// DelayMs(2); | |
SendCommand((dtm == 1) ? DATA_START_TRANSMISSION_1 : DATA_START_TRANSMISSION_2); | |
if (buffer_black != NULL) { | |
for(int i = 0; i < w / 8 * l; i++) { | |
SendData(buffer_black[i]); | |
} | |
} else { | |
for(int i = 0; i < w / 8 * l; i++) { | |
SendData(0x00); | |
} | |
} | |
// DelayMs(2); | |
SendCommand(PARTIAL_OUT); | |
} | |
/** | |
* Note: frame_buffer is the frame buffer for the _full_ screen, this function | |
* only renders the portion of it within the partial frame | |
* @brief: Renders data to a partial section of the screen | |
*/ | |
void Epd::DisplayPartialFrame(const unsigned char* frame_buffer, int x, int y, int w, int l) { | |
// This function has undergone quite a bit of tuning to do partial refreshes with minimal artifacts | |
// on the 4.2 tri-color epaper display, namely: | |
// - use a custom LUT (LutBvz): the default LUT not only does a lot of unnecessary blinking, it | |
// also creates noticible fading artifacts on pixels _outside_ the partial frame window | |
// - only scan gates inside the partial window: this reduces the impact on pixels outside the partial window | |
// - sending the data and refresh twice: with the custom LUT, the pixels don't always "stick" when going from one state to the next, this helps | |
// Prep | |
SetLutBvz(); | |
//SetLut(); | |
SendCommand(PARTIAL_IN); | |
SendCommand(PARTIAL_WINDOW); | |
// x should be the multiple of 8, the last 3 bit will always be ignored | |
x = x & 0xFF8; | |
//Horizontal start (first bit 8, then bits 7-3) | |
SendData(x >> 8); | |
SendData(x & 0x0FF); | |
//Horizontal end | |
SendData((x + w - 1) >> 8); | |
//Because we always do blocks of 8, max X always ends in 0b111 | |
SendData(((x + w - 1) | 0x07) & 0x0FF); | |
//Vertical start | |
SendData(y >> 8); | |
SendData(y & 0xff); | |
//Vertical end | |
SendData((y + l - 1) >> 8); | |
SendData((y + l - 1) & 0xff); | |
//PTScan | |
SendData(0x00); // Gates scan only inside the partial window. | |
// We get better quality by doing it twice, and the custom LUT is very fast | |
for (int repeat = 0; repeat < 2; repeat++) { | |
// Send data | |
SendCommand(DATA_START_TRANSMISSION_2); | |
if (frame_buffer != NULL) { | |
for(unsigned int i = 0; i < width * height / 8; i++) { | |
int x_i = (i * 8) % width; | |
int y_i = (i * 8) / width; | |
// If we're "in frame", send the data, otherwise pass | |
if (x_i >= x && x_i < x + w && y_i >= y && y_i < y + l) { | |
SendData(frame_buffer[i]); | |
} | |
} | |
} else { | |
for(unsigned int i = 0; i < w * l / 8; i++) { | |
SendData(0x00); | |
} | |
} | |
SendCommand(DISPLAY_REFRESH); | |
DelayMs(100); | |
WaitUntilIdle(); | |
} | |
SendCommand(PARTIAL_OUT); | |
} | |
/** | |
* @brief: set the look-up table | |
*/ | |
void Epd::SetLut(void) { | |
unsigned int count; | |
SendCommand(LUT_FOR_VCOM); //vcom | |
for(count = 0; count < 44; count++) { | |
SendData(lut_vcom0[count]); | |
} | |
SendCommand(LUT_WHITE_TO_WHITE); //ww -- | |
for(count = 0; count < 42; count++) { | |
SendData(lut_ww[count]); | |
} | |
SendCommand(LUT_BLACK_TO_WHITE); //bw r | |
for(count = 0; count < 42; count++) { | |
SendData(lut_bw[count]); | |
} | |
SendCommand(LUT_WHITE_TO_BLACK); //wb w | |
for(count = 0; count < 42; count++) { | |
SendData(lut_wb[count]); | |
} | |
SendCommand(LUT_BLACK_TO_BLACK); //bb b | |
for(count = 0; count < 42; count++) { | |
SendData(lut_bb[count]); | |
} | |
} | |
/** | |
* @brief: set the look-up table for quick display (partial refresh) | |
*/ | |
void Epd::SetLutQuick(void) { | |
unsigned int count; | |
SendCommand(LUT_FOR_VCOM); //vcom | |
for(count = 0; count < 44; count++) { | |
SendData(lut_vcom0_quick[count]); | |
} | |
SendCommand(LUT_WHITE_TO_WHITE); //ww -- | |
for(count = 0; count < 42; count++) { | |
SendData(lut_ww_quick[count]); | |
} | |
SendCommand(LUT_BLACK_TO_WHITE); //bw r | |
for(count = 0; count < 42; count++) { | |
SendData(lut_bw_quick[count]); | |
} | |
SendCommand(LUT_WHITE_TO_BLACK); //wb w | |
for(count = 0; count < 42; count++) { | |
SendData(lut_wb_quick[count]); | |
} | |
SendCommand(LUT_BLACK_TO_BLACK); //bb b | |
for(count = 0; count < 42; count++) { | |
SendData(lut_bb_quick[count]); | |
} | |
} | |
void Epd::SetLutBvz(void) { | |
unsigned int count; | |
SendCommand(LUT_FOR_VCOM); //vcom | |
for(count = 0; count < 44; count++) { | |
SendData(lut_vcom0_bvz[count]); | |
} | |
SendCommand(LUT_WHITE_TO_WHITE); //ww -- | |
for(count = 0; count < 42; count++) { | |
SendData(lut_ww_bvz[count]); | |
} | |
SendCommand(LUT_BLACK_TO_WHITE); //bw r | |
for(count = 0; count < 42; count++) { | |
SendData(lut_bw_bvz[count]); | |
} | |
SendCommand(LUT_WHITE_TO_BLACK); //wb w | |
for(count = 0; count < 42; count++) { | |
SendData(lut_wb_bvz[count]); | |
} | |
SendCommand(LUT_BLACK_TO_BLACK); //bb b | |
for(count = 0; count < 42; count++) { | |
SendData(lut_bb_bvz[count]); | |
} | |
} | |
/** | |
* @brief: refresh and displays the frame | |
*/ | |
void Epd::DisplayFrame(const unsigned char* frame_buffer) { | |
if (frame_buffer != NULL) { | |
SendCommand(DATA_START_TRANSMISSION_1); | |
for(unsigned int i = 0; i < width / 8 * height; i++) { | |
SendData(0xFF); // bit set: white, bit reset: black | |
} | |
DelayMs(2); | |
SendCommand(DATA_START_TRANSMISSION_2); | |
for(int i = 0; i < width / 8 * height; i++) { | |
SendData(frame_buffer[i]); | |
} | |
DelayMs(2); | |
} | |
SetLut(); | |
SendCommand(DISPLAY_REFRESH); | |
DelayMs(100); | |
WaitUntilIdle(); | |
} | |
/** | |
* @brief: clear the frame data from the SRAM, this won't refresh the display | |
*/ | |
void Epd::ClearFrame(void) { | |
SendCommand(RESOLUTION_SETTING); | |
SendData(width >> 8); | |
SendData(width & 0xff); | |
SendData(height >> 8); | |
SendData(height & 0xff); | |
SendCommand(DATA_START_TRANSMISSION_1); | |
DelayMs(2); | |
for(unsigned int i = 0; i < width / 8 * height; i++) { | |
SendData(0xFF); | |
} | |
DelayMs(2); | |
SendCommand(DATA_START_TRANSMISSION_2); | |
DelayMs(2); | |
for(unsigned int i = 0; i < width / 8 * height; i++) { | |
SendData(0xFF); | |
} | |
DelayMs(2); | |
} | |
/** | |
* @brief: This displays the frame data from SRAM | |
*/ | |
void Epd::DisplayFrame(void) { | |
SetLut(); | |
SendCommand(DISPLAY_REFRESH); | |
DelayMs(100); | |
WaitUntilIdle(); | |
} | |
void Epd::DisplayFrameQuick(void) { | |
SetLutQuick(); | |
SendCommand(DISPLAY_REFRESH); | |
// DelayMs(100); | |
// WaitUntilIdle(); | |
} | |
/** | |
* @brief: After this command is transmitted, the chip would enter the deep-sleep mode to save power. | |
* The deep sleep mode would return to standby by hardware reset. The only one parameter is a | |
* check code, the command would be executed if check code = 0xA5. | |
* You can use Epd::Reset() to awaken and use Epd::Init() to initialize. | |
*/ | |
void Epd::Sleep() { | |
SendCommand(VCOM_AND_DATA_INTERVAL_SETTING); | |
SendData(0x17); //border floating | |
SendCommand(VCM_DC_SETTING); //VCOM to 0V | |
SendCommand(PANEL_SETTING); | |
DelayMs(100); | |
SendCommand(POWER_SETTING); //VG&VS to 0V fast | |
SendData(0x00); | |
SendData(0x00); | |
SendData(0x00); | |
SendData(0x00); | |
SendData(0x00); | |
DelayMs(100); | |
SendCommand(POWER_OFF); //power off | |
WaitUntilIdle(); | |
SendCommand(DEEP_SLEEP); //deep sleep | |
SendData(0xA5); | |
} | |
#define TP0A 2 // sustain phase for bb and ww, change phase for bw and wb | |
#define TP0B 45 // change phase for bw and wb | |
const unsigned char lut_vcom0[] = | |
{ | |
0x40, 0x17, 0x00, 0x00, 0x00, 0x02, | |
0x00, 0x17, 0x17, 0x00, 0x00, 0x02, | |
0x00, 0x0A, 0x01, 0x00, 0x00, 0x01, | |
0x00, 0x0E, 0x0E, 0x00, 0x00, 0x02, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_vcom0_quick[] = | |
{ | |
0x00, 0x0E, 0x00, 0x00, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_vcom0_bvz[] = | |
{ | |
0x00, TP0A, TP0B, 0x01, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_ww[] ={ | |
0x40, 0x17, 0x00, 0x00, 0x00, 0x02, | |
0x90, 0x17, 0x17, 0x00, 0x00, 0x02, | |
0x40, 0x0A, 0x01, 0x00, 0x00, 0x01, | |
0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_ww_quick[] ={ | |
0xA0, 0x0E, 0x00, 0x00, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_ww_bvz[] ={ | |
0x80, // 10 00 00 00 | |
TP0A, TP0B, 0x01, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_bw[] ={ | |
0x40, 0x17, 0x00, 0x00, 0x00, 0x02, | |
0x90, 0x17, 0x17, 0x00, 0x00, 0x02, | |
0x40, 0x0A, 0x01, 0x00, 0x00, 0x01, | |
0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_bw_quick[] ={ | |
0xA0, 0x0E, 0x00, 0x00, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_bw_bvz[] ={ | |
0xA0, // 10 10 00 00 | |
TP0A, TP0B, 0x01, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_bb[] ={ | |
0x80, 0x17, 0x00, 0x00, 0x00, 0x02, | |
0x90, 0x17, 0x17, 0x00, 0x00, 0x02, | |
0x80, 0x0A, 0x01, 0x00, 0x00, 0x01, | |
0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_bb_quick[] ={ | |
0x50, 0x0E, 0x00, 0x00, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_bb_bvz[] ={ | |
0x40, // 01 00 00 00 | |
TP0A, TP0B, 0x01, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_wb[] ={ | |
0x80, 0x17, 0x00, 0x00, 0x00, 0x02, | |
0x90, 0x17, 0x17, 0x00, 0x00, 0x02, | |
0x80, 0x0A, 0x01, 0x00, 0x00, 0x01, | |
0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_wb_quick[] ={ | |
0x50, 0x0E, 0x00, 0x00, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
const unsigned char lut_wb_bvz[] ={ | |
0x50, // 01 01 00 00 | |
TP0A, TP0B, 0x01, 0x00, 0x01, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
}; | |
/* END OF FILE */ | |