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>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-04 02:28:34 +01:00
1 parent 35e678d78d
commit 8fcb17c395
13 files changed
+345 -19

No files matched your search

+70 -2
View File
@@ -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))
+7 -4
View File
@@ -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
+31 -2
View File
@@ -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