#pragma once #include "DisplayBuildFlags.h" #include "DisplayPowerPolicy.h" #include #include using ColorVal = uint16_t; class UIColor { public: // color definitions (by element _type_) static ColorVal window_bkg, title_bkg, title_txt, primary_txt, secondary_txt, warning_txt, popup_bkg, popup_txt, corp_blue; }; class DisplayDriver { int _w, _h; mesh::ui::DisplayPowerPolicy _power_policy; protected: DisplayDriver(int w, int h) { _w = w; _h = h; } void setDimensions(int w, int h) { _w = w; _h = h; } static size_t trimLastUTF8Codepoint(char* str, size_t length) { if (length == 0) return 0; size_t start = length - 1; while (start > 0 && ((uint8_t)str[start] & 0xC0) == 0x80) --start; str[start] = 0; return start; } public: //enum Color { DARK=0, LIGHT, RED, GREEN, BLUE, YELLOW, ORANGE }; // on b/w screen, colors will be !=0 synonym of light int width() const { return _w; } int height() const { return _h; } // Dense 5/6px body fonts are only for sub-160px panels, in either rotation. bool useSmallMessageFont() const { return _w < 160 && _h < 160; } virtual int textLineHeight() { const int glyph = getTextWidth("M"); return glyph <= 6 ? 10 : glyph * 2; } virtual bool isOn() = 0; virtual bool supportsRotation() const { return false; } virtual bool setRotationDegrees(uint16_t degrees) { (void)degrees; return false; } // Rotate 180 degrees relative to the display's compiled orientation. This is // intentionally separate from absolute rotation: fixed portrait/landscape // layouts can be flipped without changing their geometry. virtual void setFlipped(bool flipped) { (void)flipped; } virtual bool isEink() { return false; } // default to non-eink, override in eink drivers virtual void forceFullRefresh() {} // next refresh will be full for eink virtual void turnOn() = 0; virtual void turnOff() = 0; // Returns whether the panel state changed, allowing cached UIs to redraw. bool servicePower(bool usb_power, bool app_connected = false, bool pairing = false); bool wake(mesh::ui::DisplayWake reason); void dismiss(); virtual void clear() = 0; virtual void startFrame(ColorVal bkg = UIColor::window_bkg) = 0; virtual void setTextSize(int sz) = 0; // Optional fractional small-text mode for dense status chrome. Drivers that // only support integer bitmap scales retain their normal size. virtual void setCompactText(bool compact) { (void)compact; } // Optional runtime canvas scaling. Large RGB panels can retain one logical // UI geometry while selecting a different internal render resolution based // on the active transport's RAM cost. virtual bool setRenderScale(uint8_t coordinate_scale, float output_zoom) { (void)coordinate_scale; (void)output_zoom; return false; } virtual int renderWidth() const { return width(); } virtual int renderHeight() const { return height(); } virtual void setColor(ColorVal c) = 0; virtual void setCursor(int x, int y) = 0; virtual void print(const char* str) = 0; virtual void printWordWrap(const char* str, int max_width) { print(str); } // fallback to basic print() if no override virtual void fillRect(int x, int y, int w, int h) = 0; virtual void drawRect(int x, int y, int w, int h) = 0; virtual void drawXbm(int x, int y, const uint8_t* bits, int w, int h) = 0; // Optional QR renderer. Coordinates and size use the same logical units as // the rest of the UI. Drivers without QR support leave the caller's // fallback layout in place. #if defined(ESP32_PLATFORM) && defined(MESHCORE_HAS_REAL_DISPLAY) virtual bool drawQrCode(const char* text, int x, int y, int size); // Explicit pixel border for compact setup screens; -1 keeps the normal // four-module quiet zone. The matrix always uses whole pixels. virtual bool drawQrCodeWithBorder(const char* text, int x, int y, int size, int border_pixels); #else virtual bool drawQrCode(const char* text, int x, int y, int size) { (void)text; (void)x; (void)y; (void)size; return false; } virtual bool drawQrCodeWithBorder(const char* text, int x, int y, int size, int border_pixels) { (void)text; (void)x; (void)y; (void)size; (void)border_pixels; return false; } #endif virtual uint16_t getTextWidth(const char* str) = 0; virtual bool getTouch(int* x, int* y) { (void)x; (void)y; return false; } virtual void drawTextCentered(int mid_x, int y, const char* str) { // helper method (override to optimise) int w = getTextWidth(str); setCursor(mid_x - w/2, y); print(str); } virtual void drawTextRightAlign(int x_anch, int y, const char* str) { int w = getTextWidth(str); setCursor(x_anch - w, y); print(str); } virtual void drawTextLeftAlign(int x_anch, int y, const char* str) { setCursor(x_anch, y); print(str); } // convert UTF-8 characters to displayable block characters for compatibility virtual void translateUTF8ToBlocks(char* dest, const char* src, size_t dest_size) { size_t j = 0; for (size_t i = 0; src[i] != 0 && j < dest_size - 1; i++) { unsigned char c = (unsigned char)src[i]; if (c >= 32 && c <= 126) { dest[j++] = c; // ASCII printable } else if (c >= 0x80) { dest[j++] = '\xDB'; // CP437 full block # while (src[i+1] && (src[i+1] & 0xC0) == 0x80) i++; // skip UTF-8 continuation bytes } } dest[j] = 0; } // draw text with ellipsis if it exceeds max_width virtual void drawTextEllipsized(int x, int y, int max_width, const char* str) { char temp_str[256]; // reasonable buffer size size_t len = strlen(str); if (len >= sizeof(temp_str)) { len = sizeof(temp_str) - 1; // If the fixed buffer cuts through a UTF-8 sequence, omit that whole // codepoint instead of passing malformed text to the display driver. while (len > 0 && ((uint8_t)str[len] & 0xC0) == 0x80) --len; } memcpy(temp_str, str, len); temp_str[len] = 0; if (max_width <= 0) return; if (getTextWidth(temp_str) <= max_width) { setCursor(x, y); print(temp_str); return; } // for variable-width fonts (GxEPD), add space after ellipsis // for fixed-width fonts (OLED), keep tight spacing to save precious characters const char* ellipsis; // use a simple heuristic: if 'i' and 'l' have different widths, it's variable-width int i_width = getTextWidth("i"); int l_width = getTextWidth("l"); if (i_width != l_width) { ellipsis = "... "; // variable-width fonts: add space } else { ellipsis = "..."; // fixed-width fonts: no space } int ellipsis_width = getTextWidth(ellipsis); size_t ellipsis_len = strlen(ellipsis); size_t str_len = strlen(temp_str); while (str_len > 0 && (getTextWidth(temp_str) > max_width - ellipsis_width || str_len + ellipsis_len >= sizeof(temp_str))) { str_len = trimLastUTF8Codepoint(temp_str, str_len); } if (ellipsis_width <= max_width) { memcpy(temp_str + str_len, ellipsis, ellipsis_len + 1); } else { temp_str[0] = 0; } setCursor(x, y); print(temp_str); } virtual void endFrame() = 0; };