Files
Flipdot-display/test/test_driver/test_main.cpp
T
mangavalkandClaude Opus 5.5 8fcb17c395 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 <noreply@anthropic.com>
2026-11-04 02:28:34 +01:00

421 lines
11 KiB
C++

// Tests for the real display driver (lib/BROSE9323, Nano build) on the PC, with the
// simulated panel of test/driver_mocks: pio test -e test_driver
//
// What matters most: drawing never blocks loop() (the Nano must keep reading the serial
// port, or bytes are lost and the picture ends up shifted), and the dots end up right.
#include <unity.h>
#include "../../lib/BROSE9323/BROSE9323.cpp"
static const int W = 84, H = 19;
// Like the user's settings: 3 pulses of 200 us with a 300 us gap
static BROSE9323 *make()
{
BROSE9323 *d = new BROSE9323(W, H, 28, 200);
d->setGap(300);
d->setPulses(3);
panel = FakePanel(); // before begin(): it sets the pins
d->begin();
return d;
}
// Runs update() until the frame and the refresh are done. Fails when one call takes
// longer than max_us (fake time): then loop() would not read the serial port in time.
static int finish(BROSE9323 &d, uint32_t max_us = 3000)
{
int calls = 0;
while (d.busy() || d.refreshing())
{
uint32_t start = fake_micros;
d.update();
uint32_t took = fake_micros - start;
if (took > max_us)
{
char msg[64];
snprintf(msg, sizeof(msg), "update() took %u us", (unsigned)took);
TEST_FAIL_MESSAGE(msg);
}
TEST_ASSERT_LESS_THAN(1000000, ++calls);
}
return calls;
}
static void assertPanelShows(BROSE9323 &d)
{
for (int x = 0; x < W; x++)
for (int y = 0; y < H; y++)
if (panel.dot[x][y] != d.shownPixel(x, y))
{
char msg[64];
snprintf(msg, sizeof(msg), "dot %d,%d differs", x, y);
TEST_FAIL_MESSAGE(msg);
}
}
static void drawPattern(BROSE9323 &d, int seed)
{
for (int x = 0; x < W; x++)
for (int y = 0; y < H; y++)
d.drawPixel(x, y, ((x * 7 + y * 3 + seed) % 5) == 0);
}
static int dotsFlippedAtLeast(int n)
{
int count = 0;
for (int x = 0; x < W; x++)
for (int y = 0; y < H; y++)
count += panel.flips[x][y] >= n;
return count;
}
void setUp() {}
void tearDown() {}
void test_first_frame_flips_every_dot_without_blocking()
{
BROSE9323 *d = make();
drawPattern(*d, 0);
uint32_t start = fake_micros;
d->display();
TEST_ASSERT_LESS_THAN(3000, fake_micros - start); // display() itself returns at once
TEST_ASSERT_TRUE(d->busy());
int calls = finish(*d);
TEST_ASSERT_GREATER_THAN(100, calls); // spread over many loop() passes
TEST_ASSERT_EQUAL(W * H, dotsFlippedAtLeast(1));
assertPanelShows(*d);
delete d;
}
void test_instant_frame_flips_only_the_changed_dots_with_all_pulses()
{
BROSE9323 *d = make();
drawPattern(*d, 0);
d->display();
finish(*d);
panel.clearCounts();
d->drawPixel(10, 5, !d->shownPixel(10, 5));
d->drawPixel(70, 18, !d->shownPixel(70, 18));
d->display(false, true);
finish(*d);
TEST_ASSERT_EQUAL(6, panel.strobes); // 2 dots, 3 pulses each
TEST_ASSERT_EQUAL(3, panel.flips[10][5]);
assertPanelShows(*d);
delete d;
}
void test_effects_end_with_the_right_image()
{
for (uint8_t effect = EFFECT_SPARKLE; effect < EFFECT_COUNT; effect++)
{
BROSE9323 *d = make();
d->setEffect(effect, 500);
drawPattern(*d, 0);
d->display();
finish(*d);
assertPanelShows(*d);
drawPattern(*d, 2);
d->display();
finish(*d, 5000);
assertPanelShows(*d);
delete d;
}
}
// 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();
drawPattern(*d, 0);
d->display();
finish(*d);
panel.clearCounts();
d->startRefresh();
TEST_ASSERT_TRUE(d->refreshing());
TEST_ASSERT_FALSE(d->busy()); // not a frame: loop() may take new frames meanwhile
int calls = finish(*d);
TEST_ASSERT_GREATER_THAN(100, calls);
TEST_ASSERT_EQUAL(W * H, panel.strobes); // one pulse each, nothing changed
TEST_ASSERT_EQUAL(W * H, dotsFlippedAtLeast(1));
assertPanelShows(*d);
delete d;
}
void test_refresh_pauses_while_a_frame_arrives_and_then_finishes()
{
BROSE9323 *d = make();
drawPattern(*d, 0);
d->display();
finish(*d);
panel.clearCounts();
d->startRefresh();
d->update(); // a part of the dots
int before = panel.strobes;
TEST_ASSERT_GREATER_THAN(0, before);
TEST_ASSERT_LESS_THAN(W * H, before);
// A new frame is being received: the refresh must not show half of it
bool old = d->shownPixel(83, 18);
d->drawPixel(83, 18, !old);
for (int i = 0; i < 50; i++)
d->update();
TEST_ASSERT_EQUAL(before, panel.strobes);
TEST_ASSERT_EQUAL(old, panel.dot[83][18]);
d->display(false, true);
finish(*d);
assertPanelShows(*d);
TEST_ASSERT_EQUAL(W * H, dotsFlippedAtLeast(1)); // the refresh got to the end
TEST_ASSERT_FALSE(d->refreshing());
delete d;
}
void test_refresh_waits_for_a_running_effect()
{
BROSE9323 *d = make();
d->setEffect(EFFECT_WIPE, 1000);
drawPattern(*d, 0);
d->display();
finish(*d);
drawPattern(*d, 1);
d->display();
d->startRefresh();
panel.clearCounts();
// The wipe goes left to right; the refresh would start at the left too and finish
// before the wipe reaches the right side if it ran at the same time
while (d->busy())
d->update();
TEST_ASSERT_TRUE(d->refreshing());
finish(*d);
assertPanelShows(*d);
delete d;
}
void test_interrupted_first_frame_still_reaches_every_dot()
{
BROSE9323 *d = make();
drawPattern(*d, 0);
d->display(); // after boot: every dot
for (int i = 0; i < 20; i++)
d->update();
TEST_ASSERT_LESS_THAN(W * H, dotsFlippedAtLeast(1));
// The next frame (scrolling text) arrives before it is done
d->drawPixel(0, 0, !d->shownPixel(0, 0));
d->display(false, true);
finish(*d);
TEST_ASSERT_EQUAL(W * H, dotsFlippedAtLeast(1));
assertPanelShows(*d);
delete d;
}
void test_crc_of_equal_images_is_equal()
{
BROSE9323 *a = make(), *b = make();
a->fillScreen(1); // also sets the unused bits at the end of each row
for (int x = 0; x < W; x++)
for (int y = 0; y < H; y++)
b->drawPixel(x, y, 1);
TEST_ASSERT_EQUAL_HEX16(a->crc(), b->crc());
b->drawPixel(40, 9, 0);
TEST_ASSERT_NOT_EQUAL(a->crc(), b->crc());
delete a;
delete b;
}
// The fast path for B data must give the same image as drawPixel(), also at the right edge
// (B widths are rounded up to 4, so x + 3 can be past it) and below the last row
void test_draw_dots_is_like_draw_pixel()
{
BROSE9323 *a = make(), *b = make();
a->fillScreen(1);
b->fillScreen(1);
for (int y = 0; y <= H; y++)
for (int x = 0; x < W; x += 4)
{
uint8_t bits = (x * 5 + y * 3) & 0xF;
bool color = (x / 4 + y) % 3 == 0;
a->drawDots(x + 2, y, bits, color);
for (int i = 0; i < 4; i++)
if (bits & (8 >> i))
b->drawPixel(x + 2 + i, y, color);
}
TEST_ASSERT_EQUAL_HEX16(b->crc(), a->crc());
for (int x = 0; x < W; x++)
for (int y = 0; y < H; y++)
{
a->drawPixel(x, y, 0);
b->drawPixel(x, y, 0);
}
a->drawDots(-2, 0, 0xF, 1); // dots 0 and 1 of the first row
b->drawPixel(0, 0, 1);
b->drawPixel(1, 0, 1);
TEST_ASSERT_EQUAL_HEX16(b->crc(), a->crc());
delete a;
delete b;
}
int main()
{
UNITY_BEGIN();
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);
RUN_TEST(test_interrupted_first_frame_still_reaches_every_dot);
RUN_TEST(test_crc_of_equal_images_is_equal);
RUN_TEST(test_draw_dots_is_like_draw_pixel);
return UNITY_END();
}