diff --git a/CLAUDE.md b/CLAUDE.md index 343891c..1d69a14 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -160,6 +160,9 @@ See README.md for the overview. `web/web_ui.cpp` is the JSON API (endpoints listed in `web_ui.h`), `web/web_page.h` the single HTML/JS page (raw string literal in PROGMEM). Keep image work in the browser; the ESP32 only handles 418-character hex bitmaps (`logic/bitmap_format.h`, MSB = left dot). +- The Random effect's list (`Settings::random_effects`) is only known to the ESP32: + `FlipdotFrames::_useEffect()` picks an effect per frame and sends that, so the Nano doesn't + run its own `EFFECT_RANDOM` (which picks from all effects). - Settings: `logic/settings.h` struct with defaults = original behaviour, stored as an NVS blob with a version (`system/settings_store.cpp`). When the struct changes, bump `VERSION` there (old records then fall back to defaults). Only the current version is diff --git a/docs/web-ui.md b/docs/web-ui.md index 527d18f..80af253 100644 --- a/docs/web-ui.md +++ b/docs/web-ui.md @@ -141,7 +141,8 @@ The display schedule (on/off, when the display is on, when it switches off); slo and is switched off in between, for less noise); the "No WiFi" screen; the flip time, the pause after each pulse, pulses per dot and the refresh effect (instant, sparkle, wipe, rain, from the middle out, diagonal, spiral, flip all then reveal, push, Matrix (yellow streaks fall down the columns -that change and leave the new image behind them), random; optionally only for small changes +that change and leave the new image behind them), random (with a list of the effects it can +pick from, also for pages set to random); optionally only for small changes like the clock's minutes; pages can choose their own effect); how often every dot is flipped again ("Dot refresh", default 2 min) and how often the whole image is sent again ("Image resync", default 10 min; 0 switches either off); test patterns; the status LED. diff --git a/src/esp32/app.cpp b/src/esp32/app.cpp index f71683f..dc9d591 100644 --- a/src/esp32/app.cpp +++ b/src/esp32/app.cpp @@ -93,9 +93,14 @@ static void applyOffPage() static void apply() { controller.setSettings(settings.schedule, settings.clock, settings.slow_mode); - controller.setEffectTime(effectTime(settings.drive)); - status.setDrive(settings.flip_time, settings.drive, settings.refresh.dots); - frames.setDrive(settings.flip_time, settings.drive); + controller.setEffectTime(effectTime(settings.drive, settings.random_effects)); + // What the Nano stores and uses until the first frame. Its own Random doesn't know the + // list, so it never gets that: the first effect on the list instead. + DriveSettings stored_drive = settings.drive; + if (stored_drive.effect == DRIVE_EFFECT_RANDOM) + stored_drive.effect = pickRandomEffect(settings.random_effects, 0); + status.setDrive(settings.flip_time, stored_drive, settings.refresh.dots); + frames.setDrive(settings.flip_time, settings.drive, settings.random_effects); char user[PLEX_USER_SIZE]; plexUserSlug(settings.plex_user, user, sizeof(user)); haApiSetSettings(settings.home_assistant, user); @@ -149,6 +154,7 @@ const char *appSaveSettings(const Settings &new_settings, bool &restart_needed) bool drive_changed = settings.flip_time != new_settings.flip_time || a.flip_gap != b.flip_gap || a.pulses != b.pulses || a.effect != b.effect || a.effect_time != b.effect_time || a.effect_small_only != b.effect_small_only || + settings.random_effects != new_settings.random_effects || settings.refresh.dots != new_settings.refresh.dots; settings = new_settings; saveSettings(settings); diff --git a/src/esp32/config.h b/src/esp32/config.h index 7908b1d..95ab195 100644 --- a/src/esp32/config.h +++ b/src/esp32/config.h @@ -118,7 +118,7 @@ const uint32_t SETUP_AP_DELAY = 20000; // open the AP when WiFi fails thi const uint32_t WIFI_CONNECT_TIMEOUT = 30000; const uint32_t WIFI_RESTART_TIME = 10UL * 60 * 1000; -#define FIRMWARE_VERSION "2.21.0" +#define FIRMWARE_VERSION "2.22.0" // Home Assistant device id (do not change: Home Assistant's entities are linked to it) const uint8_t HA_DEVICE_ID[] = {0x34, 0x86, 0x5d, 0x3a, 0x08, 0x28}; diff --git a/src/esp32/display/frames.cpp b/src/esp32/display/frames.cpp index 5570507..7513a27 100644 --- a/src/esp32/display/frames.cpp +++ b/src/esp32/display/frames.cpp @@ -1,5 +1,6 @@ #include "display/frames.h" +#include #include #include @@ -20,16 +21,19 @@ uint32_t FlipdotFrames::dynamicPage(const LocalTime &time, uint32_t now, bool re bool moving = wait != 0; // The Nano has no acknowledgement: the next frame waits until it has received this one // and flipped its dots, or the next frame overflows its serial buffer - _useEffect(_page_effect, redraw); + // Instant frames don't use the effect (moving ones come fast: no E command for each) + if (redraw || !(moving || _instant_next)) + _useEffect(_page_effect, redraw); uint32_t busy = _show(bitmap, !redraw, moving || _instant_next); _instant_next = false; return moving && wait < busy ? busy : wait; } -void FlipdotFrames::setDrive(uint16_t flip_time, const DriveSettings &drive) +void FlipdotFrames::setDrive(uint16_t flip_time, const DriveSettings &drive, uint16_t random_effects) { _dot_us = (uint32_t)drive.pulses * (flip_time + drive.flip_gap); _drive = drive; + _random_effects = random_effects; _nano_effect = EFFECT_UNKNOWN; } @@ -37,6 +41,8 @@ void FlipdotFrames::_useEffect(uint8_t effect, bool full) { if (effect == PAGE_EFFECT_DEFAULT || effect >= DRIVE_EFFECT_COUNT) effect = _drive.effect; + if (effect == DRIVE_EFFECT_RANDOM) + effect = pickRandomEffect(_random_effects, esp_random()); if (effect == _nano_effect && !full) return; uint8_t flags = EFFECT_FLAG_NO_SAVE | (_drive.effect_small_only ? EFFECT_FLAG_SMALL_ONLY : 0); diff --git a/src/esp32/display/frames.h b/src/esp32/display/frames.h index 1047cd0..ef0087c 100644 --- a/src/esp32/display/frames.h +++ b/src/esp32/display/frames.h @@ -32,7 +32,7 @@ public: // block that switches instantly) void showNextInstant() { _instant_next = true; } // How the Nano flips the dots: for the time a frame takes, and the refresh effect - void setDrive(uint16_t flip_time, const DriveSettings &drive); + void setDrive(uint16_t flip_time, const DriveSettings &drive, uint16_t random_effects); // The refresh effect of the shown page (PAGE_EFFECT_DEFAULT: the one of the drive // settings). Sent to the Nano before page frames, without storing it in its EEPROM; // the other screens (off, live, No WiFi) use the drive settings' effect. @@ -52,6 +52,7 @@ private: DynamicPage _dynamic; static const uint8_t EFFECT_UNKNOWN = 254; DriveSettings _drive; + uint16_t _random_effects = RANDOM_EFFECTS_ALL; uint8_t _page_effect = PAGE_EFFECT_DEFAULT; uint8_t _nano_effect = EFFECT_UNKNOWN; // the effect last sent by _useEffect() uint32_t _dot_us = 1000; @@ -63,6 +64,7 @@ private: void _drawFullScreen(const uint8_t *bitmap); // Makes the Nano use this effect (PAGE_EFFECT_DEFAULT: the drive settings' one). Sent // when it changed, and always with full frames: a restarted Nano has the stored one. + // Random is decided here, for each frame, as only the ESP32 knows the list. void _useEffect(uint8_t effect, bool full); uint32_t _show(const uint8_t *bitmap, bool changes_only, bool instant); }; diff --git a/src/esp32/logic/settings.cpp b/src/esp32/logic/settings.cpp index bf9941f..a62071d 100644 --- a/src/esp32/logic/settings.cpp +++ b/src/esp32/logic/settings.cpp @@ -39,6 +39,8 @@ const char *validateSettings(const Settings &s) return "Unknown refresh effect"; if (d.effect_time < 100 || d.effect_time > 10000) return "The effect time must be between 100 and 10000 ms"; + if (s.random_effects == 0 || (s.random_effects & ~RANDOM_EFFECTS_ALL)) + return "Random needs at least one effect to pick from"; if (s.refresh.dots > REFRESH_MINUTES_MAX || s.refresh.image > REFRESH_MINUTES_MAX) return "Refresh intervals must be between 0 and 1440 minutes"; @@ -63,17 +65,33 @@ bool validSlowModeMinutes(int minutes) return false; } -uint32_t effectTime(const DriveSettings &drive) +uint32_t effectTime(const DriveSettings &drive, uint16_t random_effects) { if (drive.effect == DRIVE_EFFECT_INSTANT) return 0; - if (drive.effect == DRIVE_EFFECT_PUSH || drive.effect == DRIVE_EFFECT_RANDOM) + // Random: the slowest effect it can pick + bool random = drive.effect == DRIVE_EFFECT_RANDOM; + if (drive.effect == DRIVE_EFFECT_PUSH || (random && (random_effects & (1 << DRIVE_EFFECT_PUSH)))) return drive.effect_time + DRIVE_PUSH_EXTRA_TIME; - if (drive.effect == DRIVE_EFFECT_MATRIX) + if (drive.effect == DRIVE_EFFECT_MATRIX || (random && (random_effects & (1 << DRIVE_EFFECT_MATRIX)))) return drive.effect_time + DRIVE_MATRIX_EXTRA_TIME; return drive.effect_time; } +uint8_t pickRandomEffect(uint16_t random_effects, uint32_t random) +{ + uint8_t count = 0; + for (uint8_t effect = 0; effect < DRIVE_EFFECT_COUNT; effect++) + count += (random_effects >> effect) & 1; + if (count == 0) + return DRIVE_EFFECT_INSTANT; + uint8_t skip = random % count; + for (uint8_t effect = 0; effect < DRIVE_EFFECT_COUNT; effect++) + if (((random_effects >> effect) & 1) && skip-- == 0) + return effect; + return DRIVE_EFFECT_INSTANT; // not reached +} + bool parseTime(const char *text, uint16_t &minutes) { if (text == nullptr) diff --git a/src/esp32/logic/settings.h b/src/esp32/logic/settings.h index edc7061..c00b7c4 100644 --- a/src/esp32/logic/settings.h +++ b/src/esp32/logic/settings.h @@ -57,6 +57,8 @@ const uint8_t DRIVE_EFFECT_COUNT = 11; const uint32_t DRIVE_PUSH_EXTRA_TIME = 20000; // The matrix streaks flip most dots of a changed column twice, also more than effect_time const uint32_t DRIVE_MATRIX_EXTRA_TIME = 5000; +// What Random can pick from: bit n = effect n. All effects but Instant and Random itself. +const uint16_t RANDOM_EFFECTS_ALL = 0x05FE; struct DriveSettings { @@ -99,11 +101,15 @@ struct Settings // Plex user name for {plex_user} in entity ids (Tautulli's sensors of that user) char plex_user[PLEX_USER_SIZE] = ""; SlowModeSettings slow_mode; + // What the Random effect picks from (drive settings and pages), see RANDOM_EFFECTS_ALL + uint16_t random_effects = RANDOM_EFFECTS_ALL; // New fields go here, at the end (see system/settings_store.cpp) }; // How long a new frame can take to appear on the dots in ms (0 when instant) -uint32_t effectTime(const DriveSettings &drive); +uint32_t effectTime(const DriveSettings &drive, uint16_t random_effects); +// One of the effects of a valid random_effects list. random: any random number. +uint8_t pickRandomEffect(uint16_t random_effects, uint32_t random); const uint16_t MINUTES_PER_DAY = 24 * 60; const uint16_t SETTINGS_FLIP_TIME_MIN = 50; diff --git a/src/esp32/system/settings_store.cpp b/src/esp32/system/settings_store.cpp index 0e56563..c27a80a 100644 --- a/src/esp32/system/settings_store.cpp +++ b/src/esp32/system/settings_store.cpp @@ -9,7 +9,12 @@ static const char *KEY = "settings"; // Increase when the Settings struct changes; records of other versions are ignored // (defaults). 2.15.0 stored all older records again in version 7. To keep the settings over // a change, add a frozen copy of the old layout and convert it (see git history). -static const uint16_t VERSION = 7; +static const uint16_t VERSION = 8; +// Version 7 is version 8 without random_effects at the end, so no frozen copy is needed. It +// becomes version 8 with the next save. Remove this once the device has saved. +static const uint16_t OLD_VERSION = 7; +static const size_t OLD_SIZE = offsetof(Settings, random_effects); +static_assert(alignof(Settings) == alignof(uint16_t), "no padding before random_effects"); struct StoredSettings { @@ -28,6 +33,15 @@ void loadSettings(Settings &settings) size_t read = prefs.getBytes(KEY, &stored, sizeof(stored)); prefs.end(); + if (read == offsetof(StoredSettings, settings) + OLD_SIZE && stored.version == OLD_VERSION && + stored.size == OLD_SIZE) + { + stored.settings.random_effects = RANDOM_EFFECTS_ALL; + if (validateSettings(stored.settings) == nullptr) + settings = stored.settings; + return; + } + if (read == offsetof(StoredSettings, settings) + sizeof(Settings) && stored.version == VERSION && stored.size == sizeof(Settings) && validateSettings(stored.settings) == nullptr) settings = stored.settings; diff --git a/src/esp32/web/web_page.h b/src/esp32/web/web_page.h index f5be15e..a1f1838 100644 --- a/src/esp32/web/web_page.h +++ b/src/esp32/web/web_page.h @@ -172,6 +172,7 @@ details{margin-top:10px}summary{cursor:pointer;color:var(--dim);font-size:13px;m +
@@ -704,10 +705,12 @@ async function cycleTest(){clearInterval(testTimer);let n=0;const step=()=>live( await step();testTimer=setInterval(step,Math.max(4000,(settings.drive.effect?settings.drive.effect_time:0)+2000))} async function stopTest(){clearInterval(testTimer);testTimer=null;await stopLive()} // settings +function fillRandom(list){const d=$('h_random');d.textContent='';for(const o of $('h_effect').options)if(+o.value&&o.value!=9) + d.append(mk('label',{style:'display:inline-block;margin-right:1em'},mk('input',{type:'checkbox',value:o.value,checked:!!(list>>o.value&1)}),' '+o.textContent))} function fill(){const s=settings;$('s_en').checked=s.schedule.enabled;$('s_day').value=s.schedule.day_start; $('s_night').value=s.schedule.night_start;$('s_slow').checked=s.slow_mode.enabled;$('s_slowmin').value=s.slow_mode.minutes; $('c_nw').checked=s.clock.no_wifi_screen;$('h_flip').value=s.flip_time;$('h_led').checked=s.status_led; - $('h_gap').value=s.drive.flip_gap;$('h_pulses').value=s.drive.pulses;$('h_effect').value=s.drive.effect;$('h_efft').value=s.drive.effect_time;$('h_small').checked=s.drive.effect_small_only;$('h_rdots').value=s.refresh.dots;$('h_rimg').value=s.refresh.image; + $('h_gap').value=s.drive.flip_gap;$('h_pulses').value=s.drive.pulses;$('h_effect').value=s.drive.effect;$('h_efft').value=s.drive.effect_time;$('h_small').checked=s.drive.effect_small_only;fillRandom(s.drive.random_effects);$('h_rdots').value=s.refresh.dots;$('h_rimg').value=s.refresh.image; $('ha_en').checked=s.home_assistant.enabled;$('ha_host').value=s.home_assistant.host;$('ha_port').value=s.home_assistant.port;$('ha_user').value=s.home_assistant.user; $('ha_pass').placeholder=s.home_assistant.password_set?'unchanged':'none';$('ha_url').value=s.home_assistant.api_url;$('ha_plex').value=s.plex_user||''; $('ha_token').value='';$('ha_token').placeholder=s.home_assistant.token_set?'unchanged':'none'} @@ -717,7 +720,7 @@ function collect(){const s=JSON.parse(JSON.stringify(settings)); if(sm.enabled!=settings.slow_mode.enabled||sm.minutes!=settings.slow_mode.minutes)s.slow_mode=sm;else delete s.slow_mode; // HA may have switched it meanwhile s.clock={no_wifi_screen:$('c_nw').checked}; s.flip_time=+$('h_flip').value;s.status_led=$('h_led').checked; - s.drive={flip_gap:+$('h_gap').value,pulses:+$('h_pulses').value,effect:+$('h_effect').value,effect_time:+$('h_efft').value,effect_small_only:$('h_small').checked};s.refresh={dots:+$('h_rdots').value,image:+$('h_rimg').value}; + s.drive={flip_gap:+$('h_gap').value,pulses:+$('h_pulses').value,effect:+$('h_effect').value,effect_time:+$('h_efft').value,effect_small_only:$('h_small').checked,random_effects:[...$('h_random').querySelectorAll('input')].reduce((m,c)=>c.checked?m|1<(); refresh["dots"] = s.refresh.dots; refresh["image"] = s.refresh.image; @@ -760,6 +761,8 @@ static void handleSettingsPost() if (drive["effect_time"].is()) s.drive.effect_time = constrain(drive["effect_time"].as(), 0, 65535); copyBool(drive["effect_small_only"], s.drive.effect_small_only); + if (drive["random_effects"].is()) + s.random_effects = constrain(drive["random_effects"].as(), 0, 65535); JsonVariantConst refresh = body["refresh"]; if (refresh["dots"].is()) s.refresh.dots = constrain(refresh["dots"].as(), 0, 65535); diff --git a/test/test_esp32/test_main.cpp b/test/test_esp32/test_main.cpp index 7e47026..4e287d4 100644 --- a/test/test_esp32/test_main.cpp +++ b/test/test_esp32/test_main.cpp @@ -748,21 +748,75 @@ void test_settings_drive_ranges() void test_effect_time_is_zero_when_instant() { DriveSettings d; - TEST_ASSERT_EQUAL(0, (int)effectTime(d)); // default: instant + TEST_ASSERT_EQUAL(0, (int)effectTime(d, RANDOM_EFFECTS_ALL)); // default: instant d.effect = 1; d.effect_time = 2500; - TEST_ASSERT_EQUAL(2500, (int)effectTime(d)); + TEST_ASSERT_EQUAL(2500, (int)effectTime(d, RANDOM_EFFECTS_ALL)); d.effect = DRIVE_EFFECT_PUSH; // can take much longer - TEST_ASSERT_EQUAL(2500 + (int)DRIVE_PUSH_EXTRA_TIME, (int)effectTime(d)); - d.effect = DRIVE_EFFECT_RANDOM; // can be a push - TEST_ASSERT_EQUAL(2500 + (int)DRIVE_PUSH_EXTRA_TIME, (int)effectTime(d)); + TEST_ASSERT_EQUAL(2500 + (int)DRIVE_PUSH_EXTRA_TIME, (int)effectTime(d, 0x2)); d.effect = DRIVE_EFFECT_MATRIX; // flips most dots of a changed column twice - TEST_ASSERT_EQUAL(2500 + (int)DRIVE_MATRIX_EXTRA_TIME, (int)effectTime(d)); + TEST_ASSERT_EQUAL(2500 + (int)DRIVE_MATRIX_EXTRA_TIME, (int)effectTime(d, 0x2)); Settings s; s.drive.effect = DRIVE_EFFECT_MATRIX; TEST_ASSERT_NULL(validateSettings(s)); } +void test_effect_time_of_random_is_the_slowest_on_its_list() +{ + DriveSettings d; + d.effect = DRIVE_EFFECT_RANDOM; + d.effect_time = 2500; + const uint16_t push = 1 << DRIVE_EFFECT_PUSH, matrix = 1 << DRIVE_EFFECT_MATRIX, sparkle = 1 << 1; + TEST_ASSERT_EQUAL(2500 + (int)DRIVE_PUSH_EXTRA_TIME, (int)effectTime(d, RANDOM_EFFECTS_ALL)); + TEST_ASSERT_EQUAL(2500 + (int)DRIVE_PUSH_EXTRA_TIME, (int)effectTime(d, push | sparkle)); + TEST_ASSERT_EQUAL(2500 + (int)DRIVE_MATRIX_EXTRA_TIME, (int)effectTime(d, matrix | sparkle)); + TEST_ASSERT_EQUAL(2500, (int)effectTime(d, sparkle)); +} + +void test_random_effects_list_is_validated() +{ + Settings s; + TEST_ASSERT_EQUAL_HEX16(RANDOM_EFFECTS_ALL, s.random_effects); // default: everything + TEST_ASSERT_NULL(validateSettings(s)); + s.random_effects = 1 << DRIVE_EFFECT_MATRIX; + TEST_ASSERT_NULL(validateSettings(s)); + s.random_effects = 0; // nothing to pick from + TEST_ASSERT_NOT_NULL(validateSettings(s)); + s.random_effects = 1 << DRIVE_EFFECT_INSTANT; + TEST_ASSERT_NOT_NULL(validateSettings(s)); + s.random_effects = 1 << DRIVE_EFFECT_RANDOM; // would pick itself + TEST_ASSERT_NOT_NULL(validateSettings(s)); + s.random_effects = 1 << DRIVE_EFFECT_COUNT; + TEST_ASSERT_NOT_NULL(validateSettings(s)); +} + +void test_random_picks_only_from_its_list_and_all_of_it() +{ + // Each list of 2 or 3 of the effects, and the whole list + const uint16_t lists[] = {RANDOM_EFFECTS_ALL, 1 << 3, (1 << 3) | (1 << DRIVE_EFFECT_MATRIX), + (1 << 1) | (1 << 5) | (1 << DRIVE_EFFECT_PUSH)}; + for (uint16_t list : lists) + { + int seen[DRIVE_EFFECT_COUNT] = {}; + for (uint32_t r = 0; r < 990; r++) + seen[pickRandomEffect(list, r * 2654435761u)]++; + for (int effect = 0; effect < DRIVE_EFFECT_COUNT; effect++) + { + if (list & (1 << effect)) + { + TEST_ASSERT_GREATER_THAN(50, seen[effect]); + } + else + { + TEST_ASSERT_EQUAL(0, seen[effect]); + } + } + } + // The first on the list: what the Nano stores + TEST_ASSERT_EQUAL(DRIVE_EFFECT_MATRIX, pickRandomEffect(1 << DRIVE_EFFECT_MATRIX, 0)); + TEST_ASSERT_EQUAL(3, pickRandomEffect((1 << 3) | (1 << DRIVE_EFFECT_MATRIX), 0)); +} + void test_settings_off_page_blank_or_bitmap_name() { Settings s; @@ -3380,6 +3434,9 @@ int main() RUN_TEST(test_settings_slow_mode_interval); RUN_TEST(test_settings_drive_ranges); RUN_TEST(test_effect_time_is_zero_when_instant); + RUN_TEST(test_effect_time_of_random_is_the_slowest_on_its_list); + RUN_TEST(test_random_effects_list_is_validated); + RUN_TEST(test_random_picks_only_from_its_list_and_all_of_it); RUN_TEST(test_settings_off_page_blank_or_bitmap_name); RUN_TEST(test_changed_off_image_is_redrawn_while_the_display_is_on); RUN_TEST(test_settings_flip_time_and_port_ranges);