| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- // Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
- // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
- //
- // based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
- //
- // Author: Jean-Marc Zingg
- //
- // Version: see library.properties
- //
- // Library: https://github.com/ZinggJM/GxEPD2
- #include "GxEPD2_290.h"
- #include "WaveTables.h"
- GxEPD2_290::GxEPD2_290(int8_t cs, int8_t dc, int8_t rst, int8_t busy) :
- GxEPD2_EPD(cs, dc, rst, busy, HIGH, 10000000, WIDTH, HEIGHT, panel, hasColor, hasPartialUpdate, hasFastPartialUpdate)
- {
- _initial = true;
- _power_is_on = false;
- _using_partial_mode = false;
- }
- void GxEPD2_290::init(uint32_t serial_diag_bitrate)
- {
- GxEPD2_EPD::init(serial_diag_bitrate);
- _initial = true;
- _power_is_on = false;
- _using_partial_mode = false;
- }
- void GxEPD2_290::clearScreen(uint8_t value)
- {
- if (_initial)
- {
- _Init_Full();
- _setPartialRamArea(0, 0, WIDTH, HEIGHT);
- _writeCommand(0x24);
- for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
- {
- _writeData(value);
- }
- _Update_Full();
- }
- else
- {
- if (!_using_partial_mode) _Init_Part();
- _setPartialRamArea(0, 0, WIDTH, HEIGHT);
- _writeCommand(0x24);
- for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
- {
- _writeData(value);
- }
- _Update_Part();
- }
- if (!_using_partial_mode) _Init_Part();
- _setPartialRamArea(0, 0, WIDTH, HEIGHT);
- _writeCommand(0x24);
- for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
- {
- _writeData(value);
- }
- _Update_Part();
- _initial = false;
- }
- void GxEPD2_290::writeScreenBuffer(uint8_t value)
- {
- if (_initial) clearScreen(value);
- else _writeScreenBuffer(value);
- }
- void GxEPD2_290::_writeScreenBuffer(uint8_t value)
- {
- if (!_using_partial_mode) _Init_Part();
- _setPartialRamArea(0, 0, WIDTH, HEIGHT);
- _writeCommand(0x24);
- for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
- {
- _writeData(value);
- }
- }
- void GxEPD2_290::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- {
- delay(1); // yield() to avoid WDT on ESP8266 and ESP32
- int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
- x -= x % 8; // byte boundary
- w = wb * 8; // byte boundary
- int16_t x1 = x < 0 ? 0 : x; // limit
- int16_t y1 = y < 0 ? 0 : y; // limit
- int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
- int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
- int16_t dx = x1 - x;
- int16_t dy = y1 - y;
- w1 -= dx;
- h1 -= dy;
- if ((w1 <= 0) || (h1 <= 0)) return;
- if (!_using_partial_mode) _Init_Part();
- _setPartialRamArea(x1, y1, w1, h1);
- _writeCommand(0x24);
- for (int16_t i = 0; i < h1; i++)
- {
- for (int16_t j = 0; j < w1 / 8; j++)
- {
- uint8_t data;
- // use wb, h of bitmap for index!
- int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
- if (pgm)
- {
- #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
- data = pgm_read_byte(&bitmap[idx]);
- #else
- data = bitmap[idx];
- #endif
- }
- else
- {
- data = bitmap[idx];
- }
- if (invert) data = ~data;
- else _writeData(data);
- }
- }
- delay(1); // yield() to avoid WDT on ESP8266 and ESP32
- }
- void GxEPD2_290::writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- {
- if (black)
- {
- writeImage(black, x, y, w, h, invert, mirror_y, pgm);
- }
- }
- void GxEPD2_290::writeNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- {
- if (data1)
- {
- writeImage(data1, x, y, w, h, invert, mirror_y, pgm);
- }
- }
- void GxEPD2_290::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- {
- writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
- refresh(x, y, w, h);
- }
- void GxEPD2_290::drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- {
- writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
- refresh(x, y, w, h);
- }
- void GxEPD2_290::drawNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
- {
- writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
- refresh(x, y, w, h);
- }
- void GxEPD2_290::refresh(bool partial_update_mode)
- {
- if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
- else
- {
- if (_using_partial_mode) _Init_Full();
- _Update_Full();
- }
- }
- void GxEPD2_290::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
- {
- x -= x % 8; // byte boundary
- w -= x % 8; // byte boundary
- int16_t x1 = x < 0 ? 0 : x; // limit
- int16_t y1 = y < 0 ? 0 : y; // limit
- int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
- int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
- w1 -= x1 - x;
- h1 -= y1 - y;
- if (!_using_partial_mode) _Init_Part();
- _setPartialRamArea(x1, y1, w1, h1);
- _Update_Part();
- }
- void GxEPD2_290::powerOff(void)
- {
- _PowerOff();
- }
- void GxEPD2_290::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
- {
- _writeCommand(0x11); // set ram entry mode
- _writeData(0x03); // x increase, y increase : normal mode
- _writeCommand(0x44);
- _writeData(x / 8);
- _writeData((x + w - 1) / 8);
- _writeCommand(0x45);
- _writeData(y % 256);
- _writeData(y / 256);
- _writeData((y + h - 1) % 256);
- _writeData((y + h - 1) / 256);
- _writeCommand(0x4e);
- _writeData(x / 8);
- _writeCommand(0x4f);
- _writeData(y % 256);
- _writeData(y / 256);
- }
- void GxEPD2_290::_PowerOn()
- {
- if (!_power_is_on)
- {
- _writeCommand(0x22);
- _writeData(0xc0);
- _writeCommand(0x20);
- _waitWhileBusy("_PowerOn", power_on_time);
- }
- _power_is_on = true;
- }
- void GxEPD2_290::_PowerOff()
- {
- _writeCommand(0x22);
- _writeData(0xc3);
- _writeCommand(0x20);
- _waitWhileBusy("_PowerOff", power_off_time);
- _power_is_on = false;
- _using_partial_mode = false;
- }
- void GxEPD2_290::_InitDisplay()
- {
- _writeCommand(0x01); // Panel configuration, Gate selection
- _writeData((HEIGHT - 1) % 256);
- _writeData((HEIGHT - 1) / 256);
- _writeData(0x00);
- _writeCommand(0x0c); // softstart
- _writeData(0xd7);
- _writeData(0xd6);
- _writeData(0x9d);
- _writeCommand(0x2c); // VCOM setting
- _writeData(0xa8); // * different
- _writeCommand(0x3a); // DummyLine
- _writeData(0x1a); // 4 dummy line per gate
- _writeCommand(0x3b); // Gatetime
- _writeData(0x08); // 2us per line
- _setPartialRamArea(0, 0, WIDTH, HEIGHT);
- }
- void GxEPD2_290::_Init_Full()
- {
- _InitDisplay();
- _writeCommandDataPGM(GxGDEH029A1_LUTDefault_full, sizeof(GxGDEH029A1_LUTDefault_full));
- _PowerOn();
- _using_partial_mode = false;
- }
- void GxEPD2_290::_Init_Part()
- {
- _InitDisplay();
- _writeCommandDataPGM(GxGDEH029A1_LUTDefault_part, sizeof(GxGDEH029A1_LUTDefault_part));
- _PowerOn();
- _using_partial_mode = true;
- }
- void GxEPD2_290::_Update_Full()
- {
- _writeCommand(0x22);
- _writeData(0xc4);
- _writeCommand(0x20);
- _waitWhileBusy("_Update_Full", full_refresh_time);
- _writeCommand(0xff);
- }
- void GxEPD2_290::_Update_Part()
- {
- _writeCommand(0x22);
- _writeData(0x04);
- _writeCommand(0x20);
- _waitWhileBusy("_Update_Part", partial_refresh_time);
- _writeCommand(0xff);
- }
|