From 882c927185ab8f1c893410e3dc9b72ecbcb5f433 Mon Sep 17 00:00:00 2001 From: mikecarper Date: Thu, 22 Oct 2026 00:21:35 -0700 Subject: [PATCH] Clean MeshPocket page changes and extend e-paper idle timeout --- .github/workflows/run-unit-tests.yml | 2 +- docs/cli_commands.md | 12 +- docs/mesh_pocket_gui.md | 7 + examples/companion_radio/ui-new/UITask.cpp | 17 ++- src/helpers/ui/DisplayBuildFlags.h | 12 ++ src/helpers/ui/DisplayPowerPolicy.h | 5 + src/helpers/ui/GxEPDDisplay.cpp | 10 ++ src/helpers/ui/GxEPDDisplay.h | 13 +- test/fixtures/pocket_eink_refresh/Arduino.h | 18 +++ test/fixtures/pocket_eink_refresh/CRC32.h | 21 +++ .../Fonts/FreeSans18pt7b.h | 2 + .../pocket_eink_refresh/Fonts/FreeSans9pt7b.h | 2 + .../Fonts/FreeSansBold12pt7b.h | 2 + test/fixtures/pocket_eink_refresh/GxEPD2_3C.h | 1 + test/fixtures/pocket_eink_refresh/GxEPD2_4C.h | 1 + test/fixtures/pocket_eink_refresh/GxEPD2_7C.h | 1 + test/fixtures/pocket_eink_refresh/GxEPD2_BW.h | 76 ++++++++++ test/fixtures/pocket_eink_refresh/SPI.h | 7 + test/fixtures/pocket_eink_refresh/Wire.h | 1 + test/fixtures/pocket_eink_refresh/main.cpp | 131 ++++++++++++++++++ .../pocket_gui/test_pocket_controls.cpp | 11 +- test/test_display_power.py | 80 +++++++++++ test/test_pocket_eink_refresh.py | 75 ++++++++++ 23 files changed, 491 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/pocket_eink_refresh/Arduino.h create mode 100644 test/fixtures/pocket_eink_refresh/CRC32.h create mode 100644 test/fixtures/pocket_eink_refresh/Fonts/FreeSans18pt7b.h create mode 100644 test/fixtures/pocket_eink_refresh/Fonts/FreeSans9pt7b.h create mode 100644 test/fixtures/pocket_eink_refresh/Fonts/FreeSansBold12pt7b.h create mode 100644 test/fixtures/pocket_eink_refresh/GxEPD2_3C.h create mode 100644 test/fixtures/pocket_eink_refresh/GxEPD2_4C.h create mode 100644 test/fixtures/pocket_eink_refresh/GxEPD2_7C.h create mode 100644 test/fixtures/pocket_eink_refresh/GxEPD2_BW.h create mode 100644 test/fixtures/pocket_eink_refresh/SPI.h create mode 100644 test/fixtures/pocket_eink_refresh/Wire.h create mode 100644 test/fixtures/pocket_eink_refresh/main.cpp create mode 100644 test/test_pocket_eink_refresh.py diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml index 3ed6fe7ad..06efa06a2 100644 --- a/.github/workflows/run-unit-tests.yml +++ b/.github/workflows/run-unit-tests.yml @@ -854,7 +854,7 @@ jobs: run: >- python3 -B -m unittest test_message_navigation test_companion_reader test_reader_touch_coordinates - test_pocket_gui test_pocket_world_map + test_pocket_gui test_pocket_world_map test_pocket_eink_refresh test_touch_debug_overlay test_touch_debug_cli test_color_theme test_indicator_messages_profile test_t096_message_footer_profile test_indicator_display_profile test_indicator_render_profile diff --git a/docs/cli_commands.md b/docs/cli_commands.md index fb583f810..7a42189f4 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1145,17 +1145,17 @@ saved settings. The WebConfig **Display** card exposes the same controls: | Setting | CLI | Default | | --- | --- | --- | | Battery mode | `get/set display.mode` | `button-pairing` on BLE Companions; `button` elsewhere | -| Battery timeout, seconds | `get/set display.timeout` | `15` | +| Battery timeout, seconds | `get/set display.timeout` | `900` on e-paper; `15` elsewhere | | USB mode | `get/set display.usb.mode` | Same as battery mode | -| USB timeout, seconds | `get/set display.usb.timeout` | `15` | +| USB timeout, seconds | `get/set display.usb.timeout` | `900` on e-paper; `15` elsewhere | Modes are `off`, `on`, `button`, `pairing`, `button-pairing`, and `automatic`. Pairing modes are available only on builds supporting BLE pairing. All display-equipped Companion builds show the screen at boot in the default `button` or `button-pairing` mode, on battery or USB power. It stays on for the -selected profile's timeout (15 seconds by default); button input restarts that -timer. This also applies to previously saved button modes. Saved `off` and +selected profile's timeout (15 minutes on e-paper, 15 seconds elsewhere). +Button input restarts that timer. This also applies to previously saved button modes. Saved `off` and pairing-only modes still suppress the boot screen. Repeaters, room servers, and sensors do not gain this Companion-only normal-UI boot wake. @@ -1201,6 +1201,10 @@ display rather than timeout `0`. The timeout is retained but unused in `off`, changing power source applies the other profile without carrying over a wake from the previous profile. Settings survive reboot and filesystem-preserving updates in `/display_prefs`; erasing the filesystem restores the defaults. +The 15-minute e-paper default applies to battery and USB profiles on all roles. +Existing saved timeouts stay unchanged. To use 15 minutes on an existing node, +set both `display.timeout` and `display.usb.timeout` to `900`; choose a timed +mode such as `button` or `button-pairing` if the current mode is `on` or `off`. An existing observer timeout that differs from its old 60-second default is migrated once to both power profiles; an old zero timeout becomes mode `on`. diff --git a/docs/mesh_pocket_gui.md b/docs/mesh_pocket_gui.md index 54377a66d..74fae4519 100644 --- a/docs/mesh_pocket_gui.md +++ b/docs/mesh_pocket_gui.md @@ -27,6 +27,13 @@ They use a separate checked, atomically replaced record in primary storage. Existing node, channel and contact formats are unchanged. Timezone comes from the existing node preferences. +Changing pages or map zoom uses a full clean refresh to clear the previous +image. Updates within the same page keep the faster partial refresh. + +The idle screen-off timer defaults to 15 minutes on battery and USB. Existing +saved timeouts are retained; set `display.timeout` and `display.usb.timeout` to +`900` to use 15 minutes. The return-to-board delay is a separate setting. + The map centres on this node's location, or the first contact with a location. With no locations it shows a world overview. Contacts appear as dots, with nearby names at closer zooms. The view spans 20,000 km down to 5 km vertically; diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index d5f609e61..658b45e2d 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -642,6 +642,7 @@ class HomeScreen : public UIScreen { #if UI_POCKET_GUI flushPocketSettings(); _pocket_editing = _pocket_browsing = _pocket_zooming = false; + displayForceClean(); #endif do { _page = (_page + HomePage::Count + direction) % HomePage::Count; @@ -939,6 +940,7 @@ public: void showFirstPage() { #if UI_POCKET_GUI flushPocketSettings(); + if (_page != HomePage::FIRST) displayForceClean(); _pocket_editing = _pocket_browsing = _pocket_zooming = false; _pocket.resetScroll(); _pocket_last_input = millis(); @@ -971,10 +973,7 @@ public: const uint32_t idle = mesh::ui::pocket::idleMillis(_task->pocketSettings()); if (idle && uint32_t(pocket_now - _pocket_last_input) >= idle && (_page != HomePage::FIRST || _pocket_browsing)) { - flushPocketSettings(); - _pocket_editing = _pocket_browsing = _pocket_zooming = false; - _pocket.resetScroll(); - _page = HomePage::FIRST; + showFirstPage(); _task->gotoHomeScreen(); } #endif @@ -983,6 +982,9 @@ public: } if (!isPageVisible(_page)) { _page = HomePage::RADIO; +#if UI_POCKET_GUI + displayForceClean(); +#endif resetRadioProfileDisplayPage(); } #if UI_WIFI_SETUP_HOME_PAGE == 1 @@ -1673,10 +1675,12 @@ public: return true; } if (_pocket_zooming && (key == KEY_NEXT || key == KEY_RIGHT || key == KEY_PREV || key == KEY_LEFT)) { + const uint8_t previous_zoom = settings.map_zoom; const bool closer = key == KEY_NEXT || key == KEY_RIGHT; if (closer && settings.map_zoom < 8) ++settings.map_zoom; if (!closer && settings.map_zoom > 0) --settings.map_zoom; _pocket_dirty = true; + if (previous_zoom != settings.map_zoom) displayForceClean(); return true; } } @@ -1709,6 +1713,9 @@ public: #endif )) { _radio_status_page++; +#if UI_POCKET_GUI + displayForceClean(); +#endif return true; } #ifdef COMPANION_EXCLUSIVE_WIFI_BLE @@ -2565,7 +2572,7 @@ void UITask::userLedHandler() { void UITask::setCurrScreen(UIScreen* c) { #if UI_POCKET_GUI if (curr == home && c != home) static_cast(home)->flushPocketSettings(); - if (curr != c && _pocket_settings.dark && _display) _display->forceFullRefresh(); + if (curr != c && _display) _display->forceFullRefresh(); #endif #if COMPANION_FEATURE_READER if (isReaderActive() && c != reader diff --git a/src/helpers/ui/DisplayBuildFlags.h b/src/helpers/ui/DisplayBuildFlags.h index e7f9b045d..9017398d2 100644 --- a/src/helpers/ui/DisplayBuildFlags.h +++ b/src/helpers/ui/DisplayBuildFlags.h @@ -8,6 +8,9 @@ #define MESHCORE_SMALL_DISPLAY_CLASS_SSD1306Display 1 #define MESHCORE_SMALL_DISPLAY_CLASS_ST7735Display 1 #define MESHCORE_SMALL_DISPLAY_CLASS_U8g2Display 1 +#define MESHCORE_EINK_DISPLAY_CLASS_GxEPDDisplay 1 +#define MESHCORE_EINK_DISPLAY_CLASS_E213Display 1 +#define MESHCORE_EINK_DISPLAY_CLASS_E290Display 1 #define MESHCORE_DISPLAY_TOKEN_JOIN_INNER(prefix, name) prefix##name #define MESHCORE_DISPLAY_TOKEN_JOIN(prefix, name) \ MESHCORE_DISPLAY_TOKEN_JOIN_INNER(prefix, name) @@ -17,6 +20,12 @@ #define MESHCORE_HAS_REAL_DISPLAY 1 #endif +// Shared idle defaults apply to every board and role using these drivers. +#if defined(DISPLAY_CLASS) \ + && MESHCORE_DISPLAY_TOKEN_JOIN(MESHCORE_EINK_DISPLAY_CLASS_, DISPLAY_CLASS) + #define MESHCORE_HAS_EINK_DISPLAY 1 +#endif + // These drivers back physically compact panels where the message-channel // selector consumes too much of the useful text area. Do not infer this from // DisplayDriver::height(): the 160x80 ST7735 is still physically compact. @@ -41,4 +50,7 @@ #undef MESHCORE_SMALL_DISPLAY_CLASS_ST7735Display #undef MESHCORE_SMALL_DISPLAY_CLASS_SSD1306Display #undef MESHCORE_SMALL_DISPLAY_CLASS_SH1106Display +#undef MESHCORE_EINK_DISPLAY_CLASS_E290Display +#undef MESHCORE_EINK_DISPLAY_CLASS_E213Display +#undef MESHCORE_EINK_DISPLAY_CLASS_GxEPDDisplay #undef MESHCORE_NULL_DISPLAY_CLASS_NullDisplayDriver diff --git a/src/helpers/ui/DisplayPowerPolicy.h b/src/helpers/ui/DisplayPowerPolicy.h index 121ccc549..768ce6835 100644 --- a/src/helpers/ui/DisplayPowerPolicy.h +++ b/src/helpers/ui/DisplayPowerPolicy.h @@ -2,6 +2,7 @@ #include #include +#include "DisplayBuildFlags.h" namespace mesh { namespace ui { @@ -19,7 +20,11 @@ inline const char* displayInboxModeName(DisplayInboxMode mode) { struct DisplayPowerProfile { DisplayMode mode = DisplayMode::Button; +#ifdef MESHCORE_HAS_EINK_DISPLAY + uint16_t seconds = 15 * 60; +#else uint16_t seconds = 15; +#endif }; struct DisplayPowerPrefs { DisplayPowerProfile battery; diff --git a/src/helpers/ui/GxEPDDisplay.cpp b/src/helpers/ui/GxEPDDisplay.cpp index 203f7a2f1..b105f8607 100644 --- a/src/helpers/ui/GxEPDDisplay.cpp +++ b/src/helpers/ui/GxEPDDisplay.cpp @@ -71,7 +71,11 @@ bool GxEPDDisplay::begin() { } display.setPartialWindow(0, 0, display.width(), display.height()); +#if UI_POCKET_GUI + forceFullRefresh(); +#else resetPartialRefreshCounter(); +#endif #if DISP_BACKLIGHT digitalWrite(DISP_BACKLIGHT, LOW); @@ -97,10 +101,16 @@ void GxEPDDisplay::turnOn() { #endif if (!_isOn) { _isOn = true; +#if UI_POCKET_GUI + forceFullRefresh(); +#endif } } void GxEPDDisplay::turnOff() { +#if UI_POCKET_GUI + if (!_init || !_isOn) return; +#endif #ifdef MESHCORE_TECHO_LITE_DISPLAY_DETECTION if (!_init || !_present || !_isOn) return; #endif diff --git a/src/helpers/ui/GxEPDDisplay.h b/src/helpers/ui/GxEPDDisplay.h index 7dd5f9642..1ac062043 100644 --- a/src/helpers/ui/GxEPDDisplay.h +++ b/src/helpers/ui/GxEPDDisplay.h @@ -57,7 +57,7 @@ class GxEPDDisplay : public DisplayDriver { uint16_t _curr_color; CRC32 display_crc; int last_display_crc_value = 0; - int _cycles_before_full_refresh; + int _cycles_before_full_refresh = EINK_MAX_PARTIAL_REFRESH; int max_partial_refresh = EINK_MAX_PARTIAL_REFRESH; // so this can be changed by an option after #if UI_POCKET_GUI bool _full_frame = false; @@ -79,7 +79,8 @@ public: void setMaxPartialRefresh(int count) { if (count == max_partial_refresh) return; max_partial_refresh = count; - resetPartialRefreshCounter(); + // Keep a clean refresh that is already due or explicitly requested. + if (!_full_frame && _cycles_before_full_refresh != 0) resetPartialRefreshCounter(); } void setNativeRotation(int rotation) { rotation &= 3; @@ -97,7 +98,13 @@ public: bool isOn() override { return _isOn; } bool isEink() override { return true; } - void forceFullRefresh() override { _cycles_before_full_refresh = 0; }; + void forceFullRefresh() override { + _cycles_before_full_refresh = 0; +#if UI_POCKET_GUI + // Rotation and GUI changes can request this after startFrame(). + _full_frame = true; +#endif + } void resetPartialRefreshCounter() { _cycles_before_full_refresh = max_partial_refresh; }; void turnOn() override; void turnOff() override; diff --git a/test/fixtures/pocket_eink_refresh/Arduino.h b/test/fixtures/pocket_eink_refresh/Arduino.h new file mode 100644 index 000000000..144b4ba0d --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/Arduino.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#define LOW 0 +#define HIGH 1 +#define OUTPUT 1 +#define PIN_DISPLAY_CS 1 +#define PIN_DISPLAY_DC 2 +#define PIN_DISPLAY_RST 3 +#define PIN_DISPLAY_BUSY 4 +inline void delay(unsigned) {} +inline void digitalWrite(int, int) {} +inline void pinMode(int, int) {} diff --git a/test/fixtures/pocket_eink_refresh/CRC32.h b/test/fixtures/pocket_eink_refresh/CRC32.h new file mode 100644 index 000000000..75aec8718 --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/CRC32.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +// A real byte CRC keeps changed and identical command streams distinct; no +// constant finalize() stub can conceal a missing frame or exhausted budget. +class CRC32 { + uint32_t value = 0xffffffffU; +public: + void reset() { value = 0xffffffffU; } + template void update(T input) { update(&input, 1); } + template void update(const T* input, size_t count) { + const auto* bytes = reinterpret_cast(input); + for (size_t i = 0; i < count * sizeof(T); ++i) { + value ^= bytes[i]; + for (unsigned bit = 0; bit < 8; ++bit) + value = (value >> 1) ^ ((value & 1U) ? 0xedb88320U : 0U); + } + } + uint32_t finalize() const { return value ^ 0xffffffffU; } +}; diff --git a/test/fixtures/pocket_eink_refresh/Fonts/FreeSans18pt7b.h b/test/fixtures/pocket_eink_refresh/Fonts/FreeSans18pt7b.h new file mode 100644 index 000000000..980ff911b --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/Fonts/FreeSans18pt7b.h @@ -0,0 +1,2 @@ +#pragma once +inline const int FreeSans18pt7b = 18; diff --git a/test/fixtures/pocket_eink_refresh/Fonts/FreeSans9pt7b.h b/test/fixtures/pocket_eink_refresh/Fonts/FreeSans9pt7b.h new file mode 100644 index 000000000..87c8dd8e5 --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/Fonts/FreeSans9pt7b.h @@ -0,0 +1,2 @@ +#pragma once +inline const int FreeSans9pt7b = 9; diff --git a/test/fixtures/pocket_eink_refresh/Fonts/FreeSansBold12pt7b.h b/test/fixtures/pocket_eink_refresh/Fonts/FreeSansBold12pt7b.h new file mode 100644 index 000000000..6b5877161 --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/Fonts/FreeSansBold12pt7b.h @@ -0,0 +1,2 @@ +#pragma once +inline const int FreeSansBold12pt7b = 12; diff --git a/test/fixtures/pocket_eink_refresh/GxEPD2_3C.h b/test/fixtures/pocket_eink_refresh/GxEPD2_3C.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/GxEPD2_3C.h @@ -0,0 +1 @@ +#pragma once diff --git a/test/fixtures/pocket_eink_refresh/GxEPD2_4C.h b/test/fixtures/pocket_eink_refresh/GxEPD2_4C.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/GxEPD2_4C.h @@ -0,0 +1 @@ +#pragma once diff --git a/test/fixtures/pocket_eink_refresh/GxEPD2_7C.h b/test/fixtures/pocket_eink_refresh/GxEPD2_7C.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/GxEPD2_7C.h @@ -0,0 +1 @@ +#pragma once diff --git a/test/fixtures/pocket_eink_refresh/GxEPD2_BW.h b/test/fixtures/pocket_eink_refresh/GxEPD2_BW.h new file mode 100644 index 000000000..8e7776b3e --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/GxEPD2_BW.h @@ -0,0 +1,76 @@ +#pragma once +#include + +#define GxEPD_BLACK 0 +#define GxEPD_WHITE 1 + +namespace panel { +enum class Call { Full, Partial, PowerOff, Clear, WriteWhite, Hibernate }; +inline std::vector calls; +inline std::vector images; +inline bool valid_history = false; +inline unsigned updates(Call kind) { + unsigned count = 0; + for (auto call : calls) if (call == kind) ++count; + return count; +} +} + +class Adafruit_GFX { +public: + virtual ~Adafruit_GFX() = default; +}; + +struct GxEPD2_213_B74 { + static constexpr int WIDTH = 128, WIDTH_VISIBLE = 122, HEIGHT = 250; + GxEPD2_213_B74(int, int, int, int) {} + void selectSPI(SPIClass&, SPISettings) {} +}; + +template class GxEPD2_BW : public Adafruit_GFX { + int rotation = 0; + std::string image; +public: + T epd2; + explicit GxEPD2_BW(T model) : epd2(model) { static_assert(PageHeight == 250); } + void init(int, bool, int, bool) {} + void setRotation(int value) { rotation = value & 3; } + int getRotation() const { return rotation; } + int width() const { return rotation & 1 ? T::HEIGHT : T::WIDTH_VISIBLE; } + int height() const { return rotation & 1 ? T::WIDTH_VISIBLE : T::HEIGHT; } + void setTextWrap(bool) {} + void setFullWindow() {} + void setPartialWindow(int x, int y, int w, int h) { + assert(x == 0 && y == 0 && w == width() && h == height()); + } + void setFont(const int*) {} + void setTextColor(int) {} + void setCursor(int, int) {} + void getTextBounds(const char* text, int, int, int16_t* x, int16_t* y, + uint16_t* w, uint16_t* h) { + *x = 0; *y = -10; *w = std::strlen(text) * 6; *h = 12; + } + void print(const char* text) { image += text; } + void fillScreen(int) { image.clear(); } + void fillRect(int, int, int, int, int) {} + void drawRect(int, int, int, int, int) {} + void drawBitmap(int, int, const uint8_t*, int, int, int) {} + void display(bool partial) { + if (partial) + assert(panel::valid_history && "partial refresh must follow a clean image after wake"); + panel::calls.push_back(partial ? panel::Call::Partial : panel::Call::Full); + panel::images.push_back(image); + // GxEPD2 synchronizes previous/current RAM after both update modes. + panel::valid_history = true; + } + void powerOff() { panel::calls.push_back(panel::Call::PowerOff); } + void clearScreen(int) { + panel::calls.push_back(panel::Call::Clear); + panel::valid_history = true; + } + void writeScreenBuffer(int) { panel::calls.push_back(panel::Call::WriteWhite); } + void hibernate() { + panel::calls.push_back(panel::Call::Hibernate); + panel::valid_history = false; + } +}; diff --git a/test/fixtures/pocket_eink_refresh/SPI.h b/test/fixtures/pocket_eink_refresh/SPI.h new file mode 100644 index 000000000..c1046f9d7 --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/SPI.h @@ -0,0 +1,7 @@ +#pragma once +#include +#define MSBFIRST 1 +#define SPI_MODE0 0 +struct SPISettings { SPISettings(int, int, int) {} }; +struct SPIClass { void begin() {} }; +inline SPIClass SPI1; diff --git a/test/fixtures/pocket_eink_refresh/Wire.h b/test/fixtures/pocket_eink_refresh/Wire.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/Wire.h @@ -0,0 +1 @@ +#pragma once diff --git a/test/fixtures/pocket_eink_refresh/main.cpp b/test/fixtures/pocket_eink_refresh/main.cpp new file mode 100644 index 000000000..a5968c4a8 --- /dev/null +++ b/test/fixtures/pocket_eink_refresh/main.cpp @@ -0,0 +1,131 @@ +#include "GxEPDDisplay.h" + +using panel::Call; + +static void paint(GxEPDDisplay& display, const char* text) { + display.setTextSize(1); + display.setCursor(2, 20); + display.print(text); +} + +static void frame(GxEPDDisplay& display, const char* text) { + display.startFrame(); + paint(display, text); + display.endFrame(); +} + +static void cleanBaseline(GxEPDDisplay& display, int interval = 2) { + assert(display.begin()); + assert(display.width() == 250 && display.height() == 122); + display.setMaxPartialRefresh(interval); + display.forceFullRefresh(); + frame(display, "Network"); + assert(panel::updates(Call::Full) == 1 && panel::updates(Call::Partial) == 0); + assert(panel::images.back() == "Network"); +} + +int main(int argc, char** argv) { + assert(argc == 2); + const std::string scenario = argv[1]; + GxEPDDisplay display; + if (scenario == "boot") { + display.turnOff(); + assert(panel::calls.empty() && "an uninitialized display must not access hardware"); + assert(display.begin()); + display.startFrame(); + display.setMaxPartialRefresh(30); + paint(display, "First image"); + display.endFrame(); + assert(panel::updates(Call::Full) == 1 && panel::updates(Call::Partial) == 0); + frame(display, "Clock 13:44"); + assert(panel::updates(Call::Partial) == 1); + return 0; + } + cleanBaseline(display); + + if (scenario == "manual") { + // Requesting a clean once rendering has started must clean this frame, + // including when it has exactly the same drawing commands as before. + display.startFrame(); + paint(display, "Network"); + display.forceFullRefresh(); + display.endFrame(); + assert(panel::updates(Call::Full) == 2 && panel::updates(Call::Partial) == 0); + const auto calls = panel::calls.size(); + frame(display, "Network"); + assert(panel::calls.size() == calls && "identical clean image needs no repeat SPI update"); + display.forceFullRefresh(); + frame(display, "Network"); + assert(panel::updates(Call::Full) == 3); + } else if (scenario == "periodic") { + const auto calls = panel::calls.size(); + frame(display, "Network"); + assert(panel::calls.size() == calls); + frame(display, "Clock 13:44"); + assert(panel::updates(Call::Partial) == 1 && panel::updates(Call::Full) == 1); + const auto after_first = panel::calls.size(); + for (unsigned i = 0; i < 8; ++i) frame(display, "Clock 13:44"); + assert(panel::calls.size() == after_first && "unchanged frames must not exhaust the clean interval"); + frame(display, "Clock 13:45"); + assert(panel::updates(Call::Partial) == 2 && panel::updates(Call::Full) == 1); + frame(display, "Clock 13:46"); + assert(panel::updates(Call::Full) == 2 && panel::updates(Call::Partial) == 2); + frame(display, "Clock 13:47"); + assert(panel::updates(Call::Partial) == 3 && "a clean resets the partial-update allowance"); + assert(panel::images.back() == "Clock 13:47"); + } else if (scenario == "interval") { + // A settings change must preserve a clean requested before drawing starts. + display.forceFullRefresh(); + display.setMaxPartialRefresh(60); + frame(display, "Network"); + assert(panel::updates(Call::Full) == 2 && panel::updates(Call::Partial) == 0); + // Settings may also be applied by the GUI after startFrame(). Neither + // increasing the allowance nor manual cleanup may cancel a requested clean. + for (int interval : {30, -1, 1}) { + const auto full = panel::updates(Call::Full); + display.forceFullRefresh(); + display.startFrame(); + display.setMaxPartialRefresh(interval); + paint(display, "Network"); + display.endFrame(); + assert(panel::updates(Call::Full) == full + 1); + } + display.setMaxPartialRefresh(-1); + const auto full = panel::updates(Call::Full); + for (unsigned i = 0; i < 70; ++i) { + const std::string value = "tick " + std::to_string(i); + frame(display, value.c_str()); + } + assert(panel::updates(Call::Full) == full && "manual mode allows partial updates until explicitly cleaned"); + assert(panel::updates(Call::Partial) == 70); + } else if (scenario == "wake") { + display.turnOff(); + assert(!display.isOn() && panel::updates(Call::Hibernate) == 1); + const auto full = panel::updates(Call::Full); + const auto partial = panel::updates(Call::Partial); + display.turnOn(); + assert(display.isOn()); + frame(display, "Network"); + assert(panel::updates(Call::Full) == full + 1); + assert(panel::updates(Call::Partial) == partial); + // An already-awake turnOn() should not force a clean or discard history. + display.turnOn(); + frame(display, "Clock 13:45"); + assert(panel::updates(Call::Full) == full + 1); + assert(panel::updates(Call::Partial) == partial + 1); + } else if (scenario == "rotation") { + display.startFrame(); + display.setNativeRotation(1); + paint(display, "Flipped"); + display.endFrame(); + assert(display.width() == 250 && display.height() == 122); + assert(panel::updates(Call::Full) == 2 && panel::updates(Call::Partial) == 0); + display.setNativeRotation(1); + frame(display, "Flipped"); + assert(panel::updates(Call::Full) == 2 && panel::updates(Call::Partial) == 0); + frame(display, "Flipped clock"); + assert(panel::updates(Call::Partial) == 1); + } else { + assert(false && "unknown scenario"); + } +} diff --git a/test/fixtures/pocket_gui/test_pocket_controls.cpp b/test/fixtures/pocket_gui/test_pocket_controls.cpp index b6d2cd7e9..4d505f0cb 100644 --- a/test/fixtures/pocket_gui/test_pocket_controls.cpp +++ b/test/fixtures/pocket_gui/test_pocket_controls.cpp @@ -66,8 +66,9 @@ int main() { assert(task.settings.channel == 0); key(home, KEY_ENTER); assert(!home._pocket_browsing); + assert(home.clean_count == 0); key(home, KEY_NEXT); - assert(home._page == HomeScreen::MAP); + assert(home._page == HomeScreen::MAP && home.clean_count == 1); key(home, KEY_ENTER); assert(home._pocket_zooming); key(home, KEY_NEXT); @@ -86,14 +87,16 @@ int main() { key(home, KEY_NEXT); assert(task.settings.text_large == 1 && home._pocket_dirty); home._pocket_setting = 4; + const int before_theme = home.clean_count; key(home, KEY_NEXT); - assert(task.settings.dark == 1 && home.clean_count == 1); + assert(task.settings.dark == 1 && home.clean_count == before_theme + 1); key(home, KEY_ENTER); assert(!home._pocket_editing && !home._pocket_dirty); key(home, KEY_PREV); assert(home._page == HomeScreen::STATUS); + const int before_status = home.clean_count; key(home, KEY_ENTER); - assert(home._radio_status_page == 1 && task.reader_opens == 0); + assert(home._radio_status_page == 1 && task.reader_opens == 0 && home.clean_count == before_status + 1); // Pairing can interrupt any editing/browsing mode. Returning to the first // page saves settings and leaves the next click free to navigate normally. @@ -104,12 +107,14 @@ int main() { home._pocket.state.scroll = 2; home._pocket_last_input = 0; const int saves = home.save_count; + const int cleans = home.clean_count; clock_millis += 1000; home.showFirstPage(); assert(home._page == HomeScreen::FIRST && home.save_count == saves + 1); assert(!home._pocket_editing && !home._pocket_browsing && !home._pocket_zooming); assert(!home._pocket_dirty && home._pocket.state.scroll == 0); assert(home._pocket_last_input == clock_millis); + assert(home.clean_count == cleans + (interrupted != HomeScreen::FIRST)); key(home, KEY_NEXT); assert(home._page == HomeScreen::MAP); } diff --git a/test/test_display_power.py b/test/test_display_power.py index 29f572cee..c493e8678 100644 --- a/test/test_display_power.py +++ b/test/test_display_power.py @@ -486,7 +486,87 @@ int main() { ''' +EINK_TEST = r''' +#include +#include +using namespace mesh::ui; +int main() { + for (bool ble : {false, true}) for (bool companion : {false, true}) { + FakeFS fs; + assert(!loadDisplayPowerSettings(&fs, ble, companion)); + const auto& prefs = displayPowerPrefs(); + assert(prefs.battery.seconds == 900 && prefs.usb.seconds == 900); + for (bool usb : {false, true}) { + DisplayPowerPolicy policy; + const uint32_t start = UINT32_MAX - 1000; + policy.update(prefs, usb, false, false, start); + assert(policy.on() == companion); + assert(policy.wake(DisplayWake::Button, start)); + // Redraw/service calls and an app connection do not count as input. + policy.update(prefs, usb, true, false, start + 899999); + assert(policy.on()); + policy.update(prefs, usb, true, false, start + 900000); + assert(!policy.on()); + assert(policy.wake(DisplayWake::Button, start + 900001)); + assert(policy.on()); + } + char reply[160]; + for (const char* command : {"set display.timeout 27", "set display.usb.timeout 123"}) { + assert(handleDisplayPowerCommand(command, reply, sizeof(reply))); + assert(std::string(reply) == "OK"); + } + const auto saved = fs.files.at("/display_prefs"); + assert(saved.size() == 12 && saved[2] == 2); + assert(loadDisplayPowerSettings(&fs, ble, companion)); + assert(prefs.battery.seconds == 27 && prefs.usb.seconds == 123); + assert(fs.files.at("/display_prefs") == saved); + for (bool usb : {false, true}) { + DisplayPowerPolicy policy; + policy.update(prefs, usb, false, false, 0); + policy.wake(DisplayWake::Button, 0); + const uint32_t timeout = usb ? 123000 : 27000; + policy.update(prefs, usb, false, false, timeout - 1); + assert(policy.on()); + policy.update(prefs, usb, false, false, timeout); + assert(!policy.on()); + } + // A legacy explicit always-on setting must not become a timed profile. + migrateLegacyDisplayTimeout(0); + assert(loadDisplayPowerSettings(&fs, ble, companion)); + for (bool usb : {false, true}) { + DisplayPowerPolicy policy; + policy.update(prefs, usb, false, false, 0); + policy.update(prefs, usb, false, false, 3600000); + assert(policy.on()); + } + } +} +''' + + class DisplayPowerTest(unittest.TestCase): + def test_all_eink_drivers_default_to_fifteen_minute_idle_without_overriding_saved_profiles(self): + compiler = shutil.which("g++") or shutil.which("clang++") + self.assertIsNotNone(compiler, "host C++ compiler required") + with tempfile.TemporaryDirectory(prefix="mesh-eink-timeout-test-") as name: + work = Path(name) + (work / "helpers").mkdir() + (work / "helpers/IdentityStore.h").write_text(FS_STUB) + (work / "test.cpp").write_text(EINK_TEST) + for driver in ("GxEPDDisplay", "E213Display", "E290Display"): + with self.subTest(driver=driver): + executable = work / (driver + ".exe") + result = subprocess.run([ + compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror", + "-DDISPLAY_CLASS=" + driver, "-I" + str(work), + "-I" + str(ROOT / "src"), str(work / "test.cpp"), + str(ROOT / "src/helpers/ui/DisplayPowerSettings.cpp"), + "-o", str(executable), + ], capture_output=True, text=True, timeout=60) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + result = subprocess.run([str(executable)], capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + def test_companion_boot_wake_is_transport_and_layout_independent(self): mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text() self.assertRegex(mesh, re.compile( diff --git a/test/test_pocket_eink_refresh.py b/test/test_pocket_eink_refresh.py new file mode 100644 index 000000000..1031819c0 --- /dev/null +++ b/test/test_pocket_eink_refresh.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +"""Exercise the production Pocket e-paper driver against a recording panel.""" + +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +UI = ROOT / "src/helpers/ui" +FIXTURE = ROOT / "test/fixtures/pocket_eink_refresh" + + +class PocketEinkRefreshTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + compiler = shutil.which(os.environ.get("CXX", "g++")) + if compiler is None: + raise unittest.SkipTest("host G++ required") + cls.temporary = tempfile.TemporaryDirectory(prefix="pocket-eink-refresh-") + cls.work = Path(cls.temporary.name) + # Copy the actual driver so its quoted hardware includes resolve to the + # fixture. The DisplayDriver base and Pocket feature configuration stay + # production code, while the panel records calls crossing the HW API. + for name in ("GxEPDDisplay.h", "GxEPDDisplay.cpp"): + shutil.copyfile(UI / name, cls.work / name) + cls.binary = cls.work / "refresh-test" + flags = ["-std=c++17", "-Wall", "-Wextra", "-Werror", "-g", + "-Wno-unused-parameter", "-Wno-sign-compare"] + if os.name != "nt": + flags += ["-fsanitize=address,undefined", "-fno-sanitize-recover=all", + "-fno-omit-frame-pointer", "-fno-pie", "-no-pie"] + command = [compiler, *flags, "-DHELTEC_MESH_POCKET=1", "-DCOMPANION_RADIO_FULL=1", + "-DEINK_DISPLAY_MODEL=GxEPD2_213_B74", "-DDISPLAY_ROTATION=3", + "-I" + str(cls.work), "-I" + str(FIXTURE), "-I" + str(UI), + "-I" + str(ROOT / "src"), str(FIXTURE / "main.cpp"), + str(cls.work / "GxEPDDisplay.cpp"), "-o", str(cls.binary)] + result = subprocess.run(command, capture_output=True, text=True, timeout=90) + if result.returncode: + cls.temporary.cleanup() + raise AssertionError(" ".join(command) + "\n" + result.stdout + result.stderr) + + @classmethod + def tearDownClass(cls): + cls.temporary.cleanup() + + def run_case(self, scenario): + result = subprocess.run([str(self.binary), scenario], capture_output=True, + text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + def test_manual_clean_affects_current_frame_even_when_crc_is_unchanged(self): + self.run_case("manual") + + def test_first_image_is_clean_even_when_gui_applies_its_refresh_settings(self): + self.run_case("boot") + + def test_unchanged_frames_skip_io_and_do_not_use_partial_refresh_budget(self): + self.run_case("periodic") + + def test_changing_clean_interval_preserves_a_pending_full_refresh(self): + self.run_case("interval") + + def test_waking_from_hibernate_restores_panel_history_before_partial_updates(self): + self.run_case("wake") + + def test_rotation_cleans_the_current_frame_and_keeps_native_dimensions(self): + self.run_case("rotation") + + +if __name__ == "__main__": + unittest.main()