Skip to content

Harden LED configuration and color packing - #94

Draft
mjc wants to merge 2 commits into
lukash:mainfrom
mjc:refloat-fix-led-driver-encoding
Draft

Harden LED configuration and color packing#94
mjc wants to merge 2 commits into
lukash:mainfrom
mjc:refloat-fix-led-driver-encoding

Conversation

@mjc

@mjc mjc commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Validate LED strip color order before allocating DMA storage and report which strip is invalid.

The paint path also rejects unexpected encodings, and channel values are widened before bit packing. Valid GRB, GRBW, RGB, and WRGB output is unchanged.

Copilot AI review requested due to automatic review settings July 15, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the LED driver’s handling of LED strip color order/encoding by validating configuration earlier and making color packing/shifting behavior well-defined for 32-bit output values.

Changes:

  • Reject invalid LedColorOrder values during led_driver_setup() before calculating/allocating the DMA bitbuffer, with an error that identifies the offending strip index.
  • Make color channel bit packing explicitly use uint32_t widening before shifts to avoid undefined behavior on 32-bit targets.
  • Reject unexpected color_order encodings in the paint path (added a default case in the switch).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/led_driver.c Outdated
Comment thread src/led_driver.c Outdated
Comment thread src/led_driver.c Outdated
@mjc
mjc force-pushed the refloat-fix-led-driver-encoding branch 2 times, most recently from 8ae1c33 to 00062b3 Compare July 24, 2026 16:32
Comment thread src/leds.c
if (!leds->rear_strip.color_conv) {
log_error("Invalid rear LED color order: %u.", hw_cfg->rear.color_order);
return;
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplication is not good. It's about architecturally putting the code to the correct place. Looks like the whole color_order handling could be moved into the led_strip module, storing the bit number and the converter in the LedStrip struct. They are not really LedStrip related by nature, they should probably technically go into their own module (e.g. led_color_order.{c,h} or just make a directory for the led code at this stage). But I'd accept it if the functions were cleanly placed into led_strip.c too. More structural work and I don't really wanna spend the time iterating on this (so to be blunt if you can do it right please do, if not, let's not spend the time 😅 ).

A straightforward fix of the uninitialized pointer dereference would be to initialize it and early-return if it's NULL.

@mjc mjc Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I should not have pushed until I had a chance to re-review the larger context. I'll put this back to draft and get back to you when it's really right.

a bit of a struggle since there's so much to keep track of, but I'll get back to you when it feels right.

@mjc
mjc force-pushed the refloat-fix-led-driver-encoding branch from 00062b3 to 8dd3311 Compare July 25, 2026 21:51
@mjc
mjc marked this pull request as draft July 28, 2026 17:38
mjc added 2 commits July 28, 2026 12:51
Move color-order resolution into led_color_order and cache the bit width
and converter on each LedStrip during setup. This keeps the paint path
generic and leaves one color-order mapping.

Bind leds->cfg before the new fallible strip validation. leds_setup()
returns locally for an invalid active strip, but startup still configures
LCM in external modes. Invalid internal setup still stops before LED data
or DMA allocation.
@mjc
mjc force-pushed the refloat-fix-led-driver-encoding branch from 8dd3311 to 1d708fe Compare July 28, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants