From 8fcb17c395c390b4eee5d16fa10ffa2e00a85a1f Mon Sep 17 00:00:00 2001 From: MangaValk Date: Wed, 4 Nov 2026 02:28:34 +0100 Subject: [PATCH] Matrix refresh effect, and Random really picks from all effects Yellow streaks fall down the columns that changed, at random moments, and leave the new image behind them. The start ticks come from a hash, so the only new RAM is one bit per column. Only a dot's final flip gets all pulses. Random took micros() % 8, but micros() only gives multiples of 4 on the Nano, so it could only pick sparkle or diagonal. Co-Authored-By: Claude Opus 5.5 --- docs/web-ui.md | 3 +- lib/BROSE9323/BROSE9323.cpp | 72 +++++++++++++++- lib/BROSE9323/BROSE9323.h | 11 ++- lib/BROSE9323/dot_sequence.h | 33 +++++++- src/esp32/config.h | 2 +- src/esp32/logic/page.h | 2 +- src/esp32/logic/settings.cpp | 2 + src/esp32/logic/settings.h | 5 +- src/esp32/web/web_page.h | 4 +- src/nano/README.md | 9 +- test/test_driver/test_main.cpp | 146 +++++++++++++++++++++++++++++++++ test/test_esp32/test_main.cpp | 5 ++ test/test_nano/test_main.cpp | 70 +++++++++++++++- 13 files changed, 345 insertions(+), 19 deletions(-) diff --git a/docs/web-ui.md b/docs/web-ui.md index a1e055b..527d18f 100644 --- a/docs/web-ui.md +++ b/docs/web-ui.md @@ -140,7 +140,8 @@ The display schedule (on/off, when the display is on, when it switches off); slo (also a Home Assistant switch: the display only changes every 1-60 minutes, on the clock, 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, random; optionally only for small changes +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 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/lib/BROSE9323/BROSE9323.cpp b/lib/BROSE9323/BROSE9323.cpp index c951840..f6e9224 100644 --- a/lib/BROSE9323/BROSE9323.cpp +++ b/lib/BROSE9323/BROSE9323.cpp @@ -15,6 +15,9 @@ BROSE9323::BROSE9323(uint8_t w, uint8_t h, uint8_t pw, uint16_t ft) : Adafruit_G _old_buffer = (uint8_t *)calloc(_buffer_size, sizeof(uint8_t)); #endif _new_buffer = (uint8_t *)calloc(_buffer_size, sizeof(uint8_t)); +#ifndef ESPSERIAL + _changed_columns = (uint8_t *)calloc(_buffer_width, sizeof(uint8_t)); +#endif } #ifdef ESPSERIAL @@ -424,9 +427,14 @@ void BROSE9323::_startEffect(bool all, bool instant) { uint16_t total = width() * height(); uint16_t changed = 0; + memset(_changed_columns, 0, _buffer_width); for (uint8_t y = 0; y < height(); y++) for (uint8_t x = 0; x < width(); x++) - changed += _changed(x, y); + if (_changed(x, y)) + { + changed++; + _changed_columns[x >> 3] |= 1 << (x & 7); + } if (changed == 0 && !all) { _animating = false; @@ -438,7 +446,7 @@ void BROSE9323::_startEffect(bool all, bool instant) if (effect != EFFECT_INSTANT && (_effect_flags & EFFECT_FLAG_SMALL_ONLY) && (all || changed > total / 10)) effect = EFFECT_INSTANT; if (effect == EFFECT_RANDOM) - effect = 1 + micros() % (EFFECT_RANDOM - 1); + effect = randomEffect(micros()); // Pushing needs to know what is shown; after boot that is unknown if (all && effect == EFFECT_PUSH) effect = EFFECT_WIPE; @@ -462,6 +470,15 @@ void BROSE9323::_startEffect(bool all, bool instant) _push_pos = 0; steps = width(); } + else if (effect == EFFECT_MATRIX) + { + _push_shift = 0; + _push_pos = 0; + _matrix_seed = micros() >> 2; + if (all) + memset(_changed_columns, 0xFF, _buffer_width); + steps = matrixTicks(height()); + } else { _sequence.begin(effect == EFFECT_FLAP ? EFFECT_WIPE : effect, width(), height(), (uint16_t)micros()); @@ -517,6 +534,48 @@ bool BROSE9323::_pushDot(void) return ++_push_shift <= w; } +// The next flip of the current matrix tick. _push_pos walks over two places per column: the +// streak's head (turns the dot yellow), then its tail (the new image). Only the changed +// columns have a streak. Only a dot's final flip gets all pulses, so the head gives a dot +// that ends up black just one: a full screen change takes much less time. False when all +// ticks are done. +bool BROSE9323::_matrixStep(void) +{ + uint8_t w = width(), h = height(); + uint8_t spread = matrixStartSpread(h); + while (true) + { + uint8_t x = _push_pos >> 1; + bool tail = _push_pos & 1; + bool flipped = false; + if (_changed_columns[x >> 3] & (1 << (x & 7))) + { + int16_t row = (int16_t)_push_shift - matrixStartTick(x, _matrix_seed, spread); + if (tail) + row -= MATRIX_STREAK; + if (row >= 0 && row < h) + { + bool wanted = _wanted(x, row); + bool target = tail ? wanted : true; + bool changed = _shown(x, row) != target; + if (changed || _animate_all) + { + _flipTo(x, row, target, changed && (tail || wanted) ? _pulses : 1); + flipped = true; + } + } + } + if (++_push_pos >= (uint16_t)w * 2) + { + _push_pos = 0; + _next_step += _step_time; + return ++_push_shift < matrixTicks(h); + } + if (flipped) + return true; + } +} + bool BROSE9323::busy(void) const { return _animating && !_drawing; @@ -548,6 +607,15 @@ bool BROSE9323::update(void) } continue; } + if (_running_effect == EFFECT_MATRIX) + { + if (!_matrixStep()) + { + _animating = false; + break; + } + continue; + } uint8_t x, y; if (!_sequence.next(x, y)) diff --git a/lib/BROSE9323/BROSE9323.h b/lib/BROSE9323/BROSE9323.h index a87e8cb..3f3bf9a 100644 --- a/lib/BROSE9323/BROSE9323.h +++ b/lib/BROSE9323/BROSE9323.h @@ -44,8 +44,10 @@ private: bool _animating = false; uint8_t _running_effect = EFFECT_INSTANT; // _effect, or the one chosen for EFFECT_RANDOM uint8_t _phase = 0; // EFFECT_FLAP: 0 = to yellow, 1 = the new image - uint8_t _push_shift = 0; // EFFECT_PUSH: column shift 1..width - uint16_t _push_pos = 0; // EFFECT_PUSH: dot within the shift + uint8_t _push_shift = 0; // EFFECT_PUSH: column shift 1..width; EFFECT_MATRIX: tick + uint16_t _push_pos = 0; // EFFECT_PUSH: dot within the shift; EFFECT_MATRIX: see _matrixStep() + uint8_t *_changed_columns = NULL; // a bit per column, set by _startEffect() (EFFECT_MATRIX) + uint8_t _matrix_seed = 0; // EFFECT_MATRIX: different start ticks for each frame bool _animate_all = false; // flip every dot, not only the changed ones bool _drawing = false; // the buffer changed since the last display(): pause uint32_t _step_time = 0; // us per step @@ -106,6 +108,7 @@ private: void _flipTo(uint8_t x, uint8_t y, bool on, uint8_t pulses); void _flipDot(uint8_t x, uint8_t y, uint8_t pulses); bool _pushDot(void); + bool _matrixStep(void); void _startEffect(bool all, bool instant); void _refreshStep(uint32_t start); #endif @@ -138,8 +141,8 @@ public: // Pulses for each changed dot (a second pulse catches dots that didn't flip). Full // redraws use one pulse per dot. void setPulses(uint8_t); - // How display() shows a new frame (RefreshEffect), how long it takes in ms (pushing - // can take longer: it flips many dots per step) and EFFECT_FLAG_... flags. + // How display() shows a new frame (RefreshEffect), how long it takes in ms (push and + // matrix can take longer: they flip many dots) and EFFECT_FLAG_... flags. // Effects run in steps from update(), so call that often (every loop). void setEffect(uint8_t effect, uint16_t time_ms, uint8_t flags = 0); #ifndef ESPSERIAL diff --git a/lib/BROSE9323/dot_sequence.h b/lib/BROSE9323/dot_sequence.h index 45321a6..6919a55 100644 --- a/lib/BROSE9323/dot_sequence.h +++ b/lib/BROSE9323/dot_sequence.h @@ -16,10 +16,39 @@ enum RefreshEffect : uint8_t // The ones below are run by BROSE9323 itself (DotSequence uses column order for them) EFFECT_FLAP = 7, // every dot to yellow (wipe), then the new image (wipe) EFFECT_PUSH = 8, // the new image slides in from the right, pushing the old one out - EFFECT_RANDOM = 9, // one of the effects 1-8, chosen for each frame - EFFECT_COUNT = 10, + EFFECT_RANDOM = 9, // one of the effects 1-8 or 10, chosen for each frame + EFFECT_MATRIX = 10, // yellow streaks fall down the changed columns, the new image behind them + EFFECT_COUNT = 11, }; +// The effect for EFFECT_RANDOM. now: micros(), which only gives multiples of 4 on a 16 MHz +// Nano, so its lowest bits say nothing. +inline uint8_t randomEffect(uint32_t now) +{ + uint8_t pick = 1 + (now >> 2) % 9; + return pick == EFFECT_RANDOM ? (uint8_t)EFFECT_MATRIX : pick; +} + +// EFFECT_MATRIX runs in ticks: per tick every streak moves one row down. A streak's head +// starts at the top in the column's start tick and turns dots yellow; MATRIX_STREAK rows +// behind it, its tail shows the new image. +const uint8_t MATRIX_STREAK = 4; +// Columns start in the first 2/3 of the ticks, the last 1/3 is the last streak falling +inline uint8_t matrixTicks(uint8_t h) { return 3 * (h + MATRIX_STREAK); } +inline uint8_t matrixStartSpread(uint8_t h) { return 2 * (h + MATRIX_STREAK); } + +// The start tick of a column, a mixed hash so that no pattern shows between the columns +// (and no RAM per column). Fast on the AVR: only 8 bit steps and one 8 x 8 multiplication. +inline uint8_t matrixStartTick(uint8_t column, uint8_t seed, uint8_t spread) +{ + uint8_t v = (uint8_t)(column * 167) ^ seed; + v *= 29; + v ^= v >> 3; + v *= 113; + v ^= v >> 4; + return ((uint16_t)v * spread) >> 8; +} + // Flags for BROSE9323::setEffect() const uint8_t EFFECT_FLAG_SMALL_ONLY = 1; // big changes (> 1/10 of the dots) are instant // E command only: use the effect until the next E command or power cycle, without storing diff --git a/src/esp32/config.h b/src/esp32/config.h index 854246f..7908b1d 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.20.0" +#define FIRMWARE_VERSION "2.21.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/logic/page.h b/src/esp32/logic/page.h index 3aa840c..b4a7f37 100644 --- a/src/esp32/logic/page.h +++ b/src/esp32/logic/page.h @@ -26,7 +26,7 @@ const int ELEMENT_MAP_SIZE = 64; const char DEGREE_SIGN = 127; // DynamicPage::effect: the refresh effect of the Hardware settings const uint8_t PAGE_EFFECT_DEFAULT = 255; -const uint8_t PAGE_EFFECT_COUNT = 10; // DRIVE_EFFECT_COUNT (checked in logic/settings.cpp) +const uint8_t PAGE_EFFECT_COUNT = 11; // DRIVE_EFFECT_COUNT (checked in logic/settings.cpp) enum class ElementType : uint8_t { diff --git a/src/esp32/logic/settings.cpp b/src/esp32/logic/settings.cpp index 818fbeb..bf9941f 100644 --- a/src/esp32/logic/settings.cpp +++ b/src/esp32/logic/settings.cpp @@ -69,6 +69,8 @@ uint32_t effectTime(const DriveSettings &drive) return 0; if (drive.effect == DRIVE_EFFECT_PUSH || drive.effect == DRIVE_EFFECT_RANDOM) return drive.effect_time + DRIVE_PUSH_EXTRA_TIME; + if (drive.effect == DRIVE_EFFECT_MATRIX) + return drive.effect_time + DRIVE_MATRIX_EXTRA_TIME; return drive.effect_time; } diff --git a/src/esp32/logic/settings.h b/src/esp32/logic/settings.h index 5e63408..edc7061 100644 --- a/src/esp32/logic/settings.h +++ b/src/esp32/logic/settings.h @@ -51,9 +51,12 @@ struct HomeAssistantSettings const uint8_t DRIVE_EFFECT_INSTANT = 0; // numbers: RefreshEffect in lib/BROSE9323/dot_sequence.h const uint8_t DRIVE_EFFECT_PUSH = 8; const uint8_t DRIVE_EFFECT_RANDOM = 9; -const uint8_t DRIVE_EFFECT_COUNT = 10; +const uint8_t DRIVE_EFFECT_MATRIX = 10; +const uint8_t DRIVE_EFFECT_COUNT = 11; // Pushing flips many dots per column step, so it can take much longer than effect_time 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; struct DriveSettings { diff --git a/src/esp32/web/web_page.h b/src/esp32/web/web_page.h index 4b3e5c2..f5be15e 100644 --- a/src/esp32/web/web_page.h +++ b/src/esp32/web/web_page.h @@ -171,9 +171,9 @@ details{margin-top:10px}summary{cursor:pointer;color:var(--dim);font-size:13px;m + - + diff --git a/src/nano/README.md b/src/nano/README.md index 71e8a52..a6a463b 100644 --- a/src/nano/README.md +++ b/src/nano/README.md @@ -38,9 +38,10 @@ Arduino core, EEPROM and the display driver are replaced by fakes in so the coil supply can recover. - **Refresh effect** (`E`): with `0` (default) a frame appears column by column as fast as possible. The other effects (sparkle, wipe, rain, from the middle out, diagonal, - spiral, flip all then reveal, push, random) spread the flips over the effect time. - Push shifts the whole picture one column per step, so it flips many dots and can take - longer than the effect time. With flag 1 only small changes (at most 1/10 of the dots, + spiral, flip all then reveal, push, matrix, random) spread the flips over the effect + time. Push shifts the whole picture one column per step, so it flips many dots and can + take longer than the effect time. Matrix drops a yellow streak down each column that + changed, so it flips those columns' dots up to twice and can also take longer. With flag 1 only small changes (at most 1/10 of the dots, like the clock's minutes) get the effect. After boot the dot state is unknown, so push falls back to a wipe. They run in small steps from `loop()`, so serial data keeps being read; while a new frame is being drawn into the buffer the effect pauses, and the @@ -90,7 +91,7 @@ skipped and the next line starts clean. Coordinates are 2-digit **hex**. Color i | `N` | `Nn` | Pulses for each changed dot (1–3), stored in EEPROM | | `I` | `Innn` | Minutes between full redraws (decimal, 0–1440, 0 = never), stored in EEPROM | | `C` | `C` | Reply `C xxxx`: checksum of the image received so far (CRC-16/CCITT of the buffer, only the dots, `lib/BROSE9323/buffer_crc.h`), 4 hex digits | -| `E` | `Ee,nnnn[,f]` | Refresh effect `e` (0 instant, 1 sparkle, 2 wipe, 3 rain, 4 from the middle out, 5 diagonal, 6 spiral, 7 flip all then reveal, 8 push, 9 random), its time in ms (100–10000) and flags `f` (1 = only small changes, default 0), stored in EEPROM | +| `E` | `Ee,nnnn[,f]` | Refresh effect `e` (0 instant, 1 sparkle, 2 wipe, 3 rain, 4 from the middle out, 5 diagonal, 6 spiral, 7 flip all then reveal, 8 push, 9 random, 10 matrix), its time in ms (100–10000) and flags `f` (1 = only small changes, default 0), stored in EEPROM | Replies on the Nano's TX pin: diff --git a/test/test_driver/test_main.cpp b/test/test_driver/test_main.cpp index faace78..528b426 100644 --- a/test/test_driver/test_main.cpp +++ b/test/test_driver/test_main.cpp @@ -124,6 +124,147 @@ void test_effects_end_with_the_right_image() } } +// Stricter than assertPanelShows(): shownPixel() would also agree with a leftover streak +static void assertPanelHasPattern(int seed) +{ + for (int x = 0; x < W; x++) + for (int y = 0; y < H; y++) + if (panel.dot[x][y] != (((x * 7 + y * 3 + seed) % 5) == 0)) + { + char msg[64]; + snprintf(msg, sizeof(msg), "dot %d,%d is wrong", x, y); + TEST_FAIL_MESSAGE(msg); + } +} + +void test_matrix_ends_with_the_right_image_and_reaches_every_dot_after_boot() +{ + BROSE9323 *d = make(); + d->setEffect(EFFECT_MATRIX, 500); + drawPattern(*d, 0); + d->display(); + finish(*d); + TEST_ASSERT_EQUAL(W * H, dotsFlippedAtLeast(1)); + assertPanelHasPattern(0); + drawPattern(*d, 2); + d->display(); + finish(*d); + assertPanelHasPattern(2); + delete d; +} + +void test_matrix_flips_only_changed_columns_with_the_right_pulses() +{ + BROSE9323 *d = make(); + d->setEffect(EFFECT_MATRIX, 500); + d->fillScreen(0); + d->drawPixel(40, 3, 1); // stays yellow + d->drawPixel(40, 9, 1); // yellow to black + d->display(); + finish(*d); + panel.clearCounts(); + + d->drawPixel(40, 9, 0); + d->drawPixel(40, 12, 1); // black to yellow + d->display(); + finish(*d); + for (int x = 0; x < W; x++) + for (int y = 0; y < H; y++) + { + int expected = x != 40 ? 0 : y == 3 ? 0 : y == 9 ? 3 : y == 12 ? 3 : 1 + 3; + if (panel.flips[x][y] != expected) + { + char msg[64]; + snprintf(msg, sizeof(msg), "dot %d,%d: %d pulses, not %d", x, y, panel.flips[x][y], expected); + TEST_FAIL_MESSAGE(msg); + } + } + TEST_ASSERT_TRUE(panel.dot[40][3]); + TEST_ASSERT_FALSE(panel.dot[40][9]); + TEST_ASSERT_TRUE(panel.dot[40][12]); + delete d; +} + +void test_matrix_streak_falls_from_the_top_and_takes_the_effect_time() +{ + BROSE9323 *d = make(); + d->setEffect(EFFECT_MATRIX, 1000); + d->fillScreen(0); + d->display(); + finish(*d); + + d->drawPixel(40, 18, 1); + uint32_t start = fake_micros; + d->display(); + int highest = -1, longest = 0; + while (d->busy()) + { + d->update(); + int lit = 0, bottom = -1; + for (int y = 0; y < H; y++) + if (panel.dot[40][y]) + { + lit++; + bottom = y; + } + TEST_ASSERT_LESS_OR_EQUAL(highest + 1, bottom); // one row at a time, never upwards + if (bottom > highest) + highest = bottom; + if (lit > longest) + longest = lit; + } + TEST_ASSERT_EQUAL(H - 1, highest); + TEST_ASSERT_EQUAL(MATRIX_STREAK + 1, longest); // the tail goes after the head moved + uint32_t took = fake_micros - start; + TEST_ASSERT_UINT32_WITHIN(100000, 1000000, took); + delete d; +} + +void test_matrix_full_screen_change_never_blocks_and_finishes_soon_enough() +{ + BROSE9323 *d = make(); + d->setEffect(EFFECT_MATRIX, 1500); + drawPattern(*d, 0); + d->display(); + finish(*d); + drawPattern(*d, 1); + uint32_t start = fake_micros; + d->display(); + finish(*d); + assertPanelHasPattern(1); + uint32_t took = fake_micros - start; + // DRIVE_MATRIX_EXTRA_TIME on the ESP32 keeps the power on for this much longer + TEST_ASSERT_LESS_THAN(1500000 + 5000000, took); + delete d; +} + +void test_matrix_interrupted_by_a_new_frame_leaves_no_streaks() +{ + BROSE9323 *d = make(); + d->setEffect(EFFECT_MATRIX, 1000); + drawPattern(*d, 0); + d->display(); + finish(*d); + drawPattern(*d, 1); + d->display(); + // Until streaks are visible: dots yellow that are in neither image + int streak_dots = 0; + while (d->busy() && streak_dots < 10) + { + d->update(); + streak_dots = 0; + for (int x = 0; x < W; x++) + for (int y = 0; y < H; y++) + streak_dots += panel.dot[x][y] && ((x * 7 + y * 3) % 5) != 0 && ((x * 7 + y * 3 + 1) % 5) != 0; + } + TEST_ASSERT_TRUE(d->busy()); + drawPattern(*d, 2); + d->display(false, true); // a moving page: instant + finish(*d); + assertPanelHasPattern(2); + delete d; +} + void test_refresh_flips_every_dot_once_without_blocking() { BROSE9323 *d = make(); @@ -264,6 +405,11 @@ int main() RUN_TEST(test_first_frame_flips_every_dot_without_blocking); RUN_TEST(test_instant_frame_flips_only_the_changed_dots_with_all_pulses); RUN_TEST(test_effects_end_with_the_right_image); + RUN_TEST(test_matrix_ends_with_the_right_image_and_reaches_every_dot_after_boot); + RUN_TEST(test_matrix_flips_only_changed_columns_with_the_right_pulses); + RUN_TEST(test_matrix_streak_falls_from_the_top_and_takes_the_effect_time); + RUN_TEST(test_matrix_full_screen_change_never_blocks_and_finishes_soon_enough); + RUN_TEST(test_matrix_interrupted_by_a_new_frame_leaves_no_streaks); RUN_TEST(test_refresh_flips_every_dot_once_without_blocking); RUN_TEST(test_refresh_pauses_while_a_frame_arrives_and_then_finishes); RUN_TEST(test_refresh_waits_for_a_running_effect); diff --git a/test/test_esp32/test_main.cpp b/test/test_esp32/test_main.cpp index 8d7a6cd..7e47026 100644 --- a/test/test_esp32/test_main.cpp +++ b/test/test_esp32/test_main.cpp @@ -756,6 +756,11 @@ void test_effect_time_is_zero_when_instant() 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)); + 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)); + Settings s; + s.drive.effect = DRIVE_EFFECT_MATRIX; + TEST_ASSERT_NULL(validateSettings(s)); } void test_settings_off_page_blank_or_bitmap_name() diff --git a/test/test_nano/test_main.cpp b/test/test_nano/test_main.cpp index 1fe2843..1396471 100644 --- a/test/test_nano/test_main.cpp +++ b/test/test_nano/test_main.cpp @@ -478,7 +478,7 @@ void test_effect_without_saving() void test_invalid_effect_is_rejected() { - send("E10,1500\n"); // unknown effect + send("E11,1500\n"); // unknown effect send("E1,1500,4\n"); // unknown flag send("E1,50\n"); // too fast send("E1\n"); // no duration @@ -486,6 +486,18 @@ void test_invalid_effect_is_rejected() TEST_ASSERT_TRUE(replied("ERR")); } +void test_matrix_effect_is_accepted_and_stored() +{ + send("E10,2000\n"); + TEST_ASSERT_EQUAL(EFFECT_MATRIX, display.last("effect")->a); + TEST_ASSERT_FALSE(replied("ERR")); + display.calls.clear(); + setup(); + TEST_ASSERT_EQUAL(EFFECT_MATRIX, display.last("effect")->a); + TEST_ASSERT_EQUAL(2000, display.last("effect")->b); + send("E0,1500,0\n"); +} + // Every effect order visits each dot exactly once static void checkSequence(uint8_t effect, uint8_t w, uint8_t h, uint16_t seed) { @@ -574,6 +586,59 @@ void test_sparkle_order_is_scrambled() TEST_ASSERT_LESS_THAN(n / 20, neighbours); } +void test_random_effect_reaches_all_effects_with_nano_micros() +{ + // micros() on the Nano: multiples of 4. Taking the modulo of that directly only ever + // gave sparkle and diagonal. + int seen[EFFECT_COUNT] = {}; + for (uint32_t now = 0; now < 4000; now += 4) + { + uint8_t effect = randomEffect(now); + TEST_ASSERT_LESS_THAN(EFFECT_COUNT, effect); + seen[effect]++; + } + TEST_ASSERT_EQUAL(0, seen[EFFECT_INSTANT]); + TEST_ASSERT_EQUAL(0, seen[EFFECT_RANDOM]); + for (uint8_t effect = EFFECT_SPARKLE; effect < EFFECT_COUNT; effect++) + if (effect != EFFECT_RANDOM) + TEST_ASSERT_GREATER_THAN(100, seen[effect]); +} + +void test_matrix_start_ticks_are_spread_without_a_pattern() +{ + const uint8_t spread = matrixStartSpread(19); + TEST_ASSERT_EQUAL(matrixTicks(19), spread + 19 + MATRIX_STREAK); + for (int seed = 0; seed < 256; seed++) + { + int starts[46] = {}; + int steps[46] = {}; + int close = 0, previous = 0; + for (int x = 0; x < 84; x++) + { + int start = matrixStartTick(x, seed, spread); + TEST_ASSERT_LESS_THAN(spread, start); + starts[start]++; + if (x) + { + close += abs(start - previous) <= 1; + steps[(start - previous + spread) % spread]++; + } + previous = start; + } + for (int i = 0; i < spread; i++) + { + TEST_ASSERT_LESS_THAN(9, starts[i]); // no walls of streaks + TEST_ASSERT_LESS_THAN(15, steps[i]); // no diagonal lines + } + TEST_ASSERT_LESS_THAN(20, close); // no wide bands + } + // A new frame gets other columns first + int same = 0; + for (int x = 0; x < 84; x++) + same += matrixStartTick(x, 1, spread) == matrixStartTick(x, 2, spread); + TEST_ASSERT_LESS_THAN(20, same); +} + // --------------------------------------------------------------------------- // Periodic full redraw // --------------------------------------------------------------------------- @@ -750,9 +815,12 @@ int main() RUN_TEST(test_effect_is_applied_and_stored); RUN_TEST(test_effect_without_saving); RUN_TEST(test_invalid_effect_is_rejected); + RUN_TEST(test_matrix_effect_is_accepted_and_stored); RUN_TEST(test_every_effect_visits_each_dot_once); RUN_TEST(test_wipe_goes_left_to_right_and_center_starts_in_the_middle); RUN_TEST(test_sparkle_order_is_scrambled); + RUN_TEST(test_random_effect_reaches_all_effects_with_nano_micros); + RUN_TEST(test_matrix_start_ticks_are_spread_without_a_pattern); RUN_TEST(test_refresh_when_due); RUN_TEST(test_refresh_only_once_per_interval);