Repository navigation
Description
Summary
Temporal binary dithering lives inline in PixelController (src/pixel_controller.h) as two raw arrays (d[3], e[3]) plus ~6 member functions, with the arithmetic copied into several places. Pull the algorithm out into one small, header-only, unit-tested module that compiles to the same code when enabled and to nothing when disabled. AVR flash must not grow.
Where the algorithm lives today
| Piece | Location |
|---|---|
Per-frame setup (bit-reverse the phase, centre it, scale by 256/premixed) |
PixelController::init_binary_dithering() |
| The same setup again with a loop instead of an unrolled chain | PixelController::reseed_binary_dithering() (#4347) |
Per-pixel toggle d = e - d |
stepDithering(), preStepFirstByteDithering() |
Apply b ? qadd8(b, d) : 0 |
dither<SLOT>() (two overloads), loadAndScaleChannel() |
"Is dithering on?" (e[0] | e[1] | e[2]) != 0 |
fl/channels/channel.cpp.hpp, fl/channels/slim_spi_bridge_controller.h, fl/channels/color_managed_source.cpp.hpp |
| Disable | enable_dithering(DISABLE_DITHER) zeroes the arrays; NO_DITHERING compiles out only the setup body |
Hand-written assembly and per-platform structs read pixels.d[]/pixels.e[] directly: platforms/avr/clockless_avr.h, platforms/avr/attiny/clockless_blocking*.h, platforms/arm/{lpc,kl26,nrf51,teensy/teensy_lc}, platforms/arm/teensy/teensy4_common/block_clockless_arm_mxrt1062.h. So the fields must keep their names and stay public on PixelController.
Bug found while auditing
NO_DITHERING is forced on for every FL_IS_AVR_LIMITED chip (platforms/avr/led_sysdefs_avr.h: all ATtiny parts, ATmega32U2/16U2/8U2, AT90USB162). Under it, init_binary_dithering() has an empty body. The default dither mode is still BINARY_DITHER (fl/channels/options.h, CLEDController), so the constructor calls enable_dithering(BINARY_DITHER) → empty init → d[] and e[] are never written. The AVR clockless asm then adds those uninitialised stack bytes to every byte it sends (PRESCALEA2(d0) / MOV_ADDDE04), and stepDithering() toggles them. The result is undefined behaviour: random brightness offsets on ATtiny.
It is narrower than "every frame" because CFastLED::show() calls setDither(0) while mNFPS < FL_DITHER_ENABLE_MIN_REFRESH_HZ, and mNFPS starts at 0, so the first FastLED.show() turns dither off. It is still reachable in two ways:
- the sketch calls
FastLED.setDither(BINARY_DITHER)(documented API) after that first frame; - the sketch drives a controller directly (
controller->showLeds()/CLEDController::show()).
Plan
-
New module
src/fl/gfx/binary_dither.h. Header-only, everythingFASTLED_FORCE_INLINE, operating on caller-ownedu8 d[3]/u8 e[3]:phase(frame): bit-reverse toVIRTUAL_BITSand centre. One implementation, shared by the setup and the reseed.init(d, e, scale, frame),reseed(d, e, frame),clear(d, e),active(e).step(d, e),stepChannel(d, e, c),apply(b, d).
Two policies share this interface:
fl::BinaryDither(the algorithm) andfl::NoDither(init→clear,stepno-op,apply(b, d)→b).fl::Ditherselects between them fromNO_DITHERING. Disabling it is one switch, and once disabled the work compiles away rather than running against zeros. -
PixelControllerkeepsd[3]/e[3](layout and names unchanged) and its existing method names. Each becomes a one-line forward tofl::Dither. No platform driver changes. -
Replace the three open-coded
(e[0]|e[1]|e[2])checks withPixelController::ditherActive(). -
Fix the NO_DITHERING bug. With
NoDither,enable_dithering()always clearsd/e. This should cost nothing: the clear branch already exists forDISABLE_DITHER, and the mode compare goes away.
Acceptance
-
fl::BinaryDitherunit tests (tests/fl/gfx/binary_dither.cpp):phase()matches the documented table 16, 144, 80, 208, 48, 176, 112, 240 and wraps every 8 frames.initgivese = 256/sandd ∈ [0, e]. A zero scale givesd = e = 0.reseed(f)matchesinit(f)for every frame and scale.steptogglesdande - d, and stepping twice is the identity.applynever lifts 0 and saturates at 255.- Over one 8-frame cycle the mean of
scale8(apply(v, d), s)is closer tov·s/255than the undithered value, for a sweep ofv/s. - A golden test that the module reproduces the pre-refactor
PixelControlleroutput byte-for-byte. NoDitherclears state, never changes a byte, andactive()is false.
- Existing
tests/pixel_controller.cppdither tests pass unchanged. -
bash bloat <board> --buildfor AVR (uno, leonardo, nano_every, attiny85) × (Blink, Apa102, ColorPalette):image_flashbefore vs after posted on the PR, with no increase. -
bash test --cppandbash lintare green.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
fix(dither): don't pull led_sysdefs.h/<Arduino.h> into pixel_controll…
refactor(dither): extract binary dithering into a tested, zero-cost, …