#include "GxEPDDisplay.h" #ifndef DISPLAY_ROTATION #define DISPLAY_ROTATION 3 #endif #ifdef ESP32 SPIClass SPI1 = SPIClass(FSPI); #endif #ifndef EPD_WASHING_MACHINE_CYCLES #define EPD_WASHING_MACHINE_CYCLES 0 #endif // Color scheme ColorVal UIColor::window_bkg = GxEPD_WHITE; ColorVal UIColor::title_bkg = GxEPD_WHITE; ColorVal UIColor::title_txt = GxEPD_BLACK; ColorVal UIColor::primary_txt = GxEPD_BLACK; ColorVal UIColor::secondary_txt = GxEPD_BLACK; ColorVal UIColor::warning_txt = GxEPD_BLACK; ColorVal UIColor::popup_bkg = GxEPD_WHITE; ColorVal UIColor::popup_txt = GxEPD_BLACK; ColorVal UIColor::corp_blue = GxEPD_BLACK; bool GxEPDDisplay::begin() { display.epd2.selectSPI(SPI1, SPISettings(4000000, MSBFIRST, SPI_MODE0)); #ifdef ESP32 SPI1.begin(PIN_DISPLAY_SCLK, PIN_DISPLAY_MISO, PIN_DISPLAY_MOSI, PIN_DISPLAY_CS); #else SPI1.begin(); #endif display.init(115200, true, 2, false); display.setRotation(DISPLAY_ROTATION); setTextSize(1); // Default to size 1 display.setFullWindow(); for (int i = 0; i < EPD_WASHING_MACHINE_CYCLES; i++) { display.fillScreen(GxEPD_BLACK); display.display(false); delay(2000); display.fillScreen(GxEPD_WHITE); display.display(false); delay(2000); } display.setPartialWindow(0, 0, display.width(), display.height()); _partial_refresh_count = 0; #if DISP_BACKLIGHT digitalWrite(DISP_BACKLIGHT, LOW); pinMode(DISP_BACKLIGHT, OUTPUT); #endif _init = true; _isOn = true; return true; } void GxEPDDisplay::turnOn() { if (!_init) begin(); if (!_isOn) { _isOn = true; } } void GxEPDDisplay::turnOn(int max_partial) { setMaxPartialRefresh(max_partial); turnOn(); } // 24x24 sleep icon, MSB-first (matches GxEPDDisplay::drawXbm) static const uint8_t sleep_icon_24x24[] = { 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xe0, 0x0e, 0x03, 0xc0, 0x18, 0x0f, 0x80, 0x70, 0x1f, 0x80, 0xff, 0x3f, 0x80, 0xff, 0x3f, 0x00, 0x00, 0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0x00, 0xff, 0x0e, 0x00, 0xff, 0x1c, 0x7c, 0xff, 0xbf, 0x7c, 0xff, 0xbf, 0x38, 0xff, 0xc0, 0x7c, 0xff, 0xe0, 0x7c, 0xff, 0xf0, 0x00, 0x7f, 0xfc, 0x00, 0x7f, 0xff, 0xf8, 0x3f, 0xff, 0xf0, 0x1f, 0xff, 0xf0, 0x0f, 0xff, 0xe0, 0x07, 0xff, 0x80, 0x01, 0xff, 0x00 }; void GxEPDDisplay::drawSleepIcon() { const int W = 24, H = 24; int x = (int)(display.width() / scale_x) - W; // top-right, logical coordinates int y = 0; display.fillRect((x - 5) * scale_x - 1, y * scale_y, (W+5) * scale_x + 2, (H-2) * scale_y + 1, GxEPD_WHITE); _curr_color = GxEPD_BLACK; drawXbm(x, y, sleep_icon_24x24, W, H); display.display(true); } void GxEPDDisplay::turnOff(bool blank) { if (blank) { blankScreen(); } else { drawSleepIcon(); } _isOn = false; last_display_crc_value=0; } void GxEPDDisplay::blankScreen() { // do full refresh before powering off to clear screen // no full refresh needed at wakeup display.clearScreen(0xFF); // Clears microcontroller side RAM display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers _partial_refresh_count = 0; last_display_crc_value = 0; // we can hibernate as screen has been cleaned display.hibernate(); } void GxEPDDisplay::clear() { display.fillScreen(GxEPD_WHITE); display.setTextColor(GxEPD_BLACK); display_crc.reset(); } void GxEPDDisplay::startFrame(ColorVal bkg) { display.fillScreen(bkg); display.setTextColor(_curr_color = UIColor::primary_txt); display_crc.reset(); if (_force_full_refresh || (_max_partial_refresh > 0 && _partial_refresh_count >= _max_partial_refresh)) { // forces a full wipe of the screen ... display.clearScreen(0xFF); // Clears microcontroller side RAM display.writeScreenBuffer(0xFF); // Forces 0xFF (White) into the display controller's history registers // we'll need a partial refresh after that (whatever crc value is) last_display_crc_value = 0; _partial_refresh_count = 0; _force_full_refresh = false; } else { display.setPartialWindow(0, 0, display.width(), display.height()); } } void GxEPDDisplay::setTextSize(int sz) { display_crc.update(sz); switch(sz) { case 1: // Small display.setFont(&FreeSans9pt7b); break; case 2: // Medium Bold display.setFont(&FreeSansBold12pt7b); break; case 3: // Large display.setFont(&FreeSans18pt7b); break; default: display.setFont(&FreeSans9pt7b); break; } } void GxEPDDisplay::setColor(ColorVal c) { display_crc.update (c); display.setTextColor(_curr_color = c); } void GxEPDDisplay::setCursor(int x, int y) { display_crc.update(x); display_crc.update(y); display.setCursor((x+offset_x)*scale_x, (y+offset_y)*scale_y); } void GxEPDDisplay::print(const char* str) { display_crc.update(str, strlen(str)); display.print(str); } void GxEPDDisplay::fillRect(int x, int y, int w, int h) { display_crc.update(x); display_crc.update(y); display_crc.update(w); display_crc.update(h); display.fillRect(x*scale_x, y*scale_y, w*scale_x, h*scale_y, _curr_color); } void GxEPDDisplay::drawRect(int x, int y, int w, int h) { display_crc.update(x); display_crc.update(y); display_crc.update(w); display_crc.update(h); display.drawRect(x*scale_x, y*scale_y, w*scale_x, h*scale_y, _curr_color); } void GxEPDDisplay::drawXbm(int x, int y, const uint8_t* bits, int w, int h) { display_crc.update(x); display_crc.update(y); display_crc.update(w); display_crc.update(h); display_crc.update(bits, w * h / 8); // Calculate the base position in display coordinates uint16_t startX = x * scale_x; uint16_t startY = y * scale_y; // Width in bytes for bitmap processing uint16_t widthInBytes = (w + 7) / 8; // Process the bitmap row by row for (uint16_t by = 0; by < h; by++) { // Calculate the target y-coordinates for this logical row int y1 = startY + (int)(by * scale_y); int y2 = startY + (int)((by + 1) * scale_y); int block_h = y2 - y1; // Scan across the row bit by bit for (uint16_t bx = 0; bx < w; bx++) { // Calculate the target x-coordinates for this logical column int x1 = startX + (int)(bx * scale_x); int x2 = startX + (int)((bx + 1) * scale_x); int block_w = x2 - x1; // Get the current bit uint16_t byteOffset = (by * widthInBytes) + (bx / 8); uint8_t bitMask = 0x80 >> (bx & 7); bool bitSet = pgm_read_byte(bits + byteOffset) & bitMask; // If the bit is set, draw a block of pixels if (bitSet) { // Draw the block as a filled rectangle display.fillRect(x1, y1, block_w, block_h, _curr_color); } } } } uint16_t GxEPDDisplay::getTextWidth(const char* str) { int16_t x1, y1; uint16_t w, h; display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h); return ceil((w + 1) / scale_x); } void GxEPDDisplay::endFrame() { if (_isOn == false) return; uint32_t crc = display_crc.finalize(); if (crc != last_display_crc_value) { display.display(true); _partial_refresh_count++; } last_display_crc_value = crc; }