Skip to content

Conversation

@sayhiben
Copy link

@sayhiben sayhiben commented Jan 21, 2026

Overview

image image image

I'm designing a fancy sign for a craft fair booth with an engraved sheet of acrylic in front. In order to properly light it, I would like to define segments for various shapes in the engraved sheet such that I can apply different effects to each area. Currently, support for this is limited to ledmaps (only one across all segments), jMaps (1D -> 2D translation only), and grouping (only works for specific designs)

This PR adds support for clipping masks per segment, defined in virtual LED space. This enables overlapping segments to render distinct effects/palettes while preserving normal segment geometry and mappings.

Summary of changes

  • Add per‑segment mask clipping via segmaskX.json files (1D + 2D).
  • Expose mask selection + invert in the UI (MM‑specific items marked with 🌜).
  • Stream‑parse mask files (ledmap‑style) and enumerate available masks.
  • Make 1D mask gating work on matrix installs and add a safety reset on failed mask loads.

How it works

  • Mask storage: bit‑packed 0/1 mask loaded from /segmaskX.json (stream parser like ledmap).
  • Mask checks:
    • 2D: mask checked in setPixelColorXY_*.
    • 1D: mask checked in setPixelColor with !is2D() so 1D segments still mask on matrix installs.
  • JSON API: segment state uses mask + minv; /json/info exposes available mask IDs.
  • UI: adds “Mask 🌜” dropdown and “Invert mask 🌜”

Segmask.json

Masks are defined by 1/0 values in a list similar to ledmapN.json files:

{"w":16,"h":32,"mask":[0,0,0,1,1,1,0,0,0,0,...,0,1,1],"inv":false}
  • w: Mask width
  • h: Mask height
  • mask: JSON array of bits
  • inv: Invert mask by default (can be changed at runtime in the UI)

Other Notes

  • I'd appreciate confirmation that this works on something other than the ESP32-S3 boards I have on hand
  • Performance seems unchanged on three segments with masks over 512 LEDs despite checking each virtual LED on each segment against its segment mask. I'm using bitwise ops here and storing the masks as u_int8_t byte arrays, so lookups and memory footprint remains efficient. Still, I expected a small hit to perf, and I'm seeing a perf gain when enabling segment masks on my device. I'd love someone to double check on a larger installation
  • Documentation PR: Feat: Segment Masks WLED-Docs#13

Summary by CodeRabbit

  • New Features

    • Per-segment masks: discover, select and invert masks from the UI; masks persisted in configs and reported in device info.
    • Device-aware default limit for mask slots (different default for ESP8266 and other platforms).
    • UI controls to set/clear masks and invert mask bits per segment.
  • Bug Fixes / Reliability

    • Mask geometry validated at render time; rendering respects masks across 1D/2D update paths.
    • Mask state initialized, cleaned up and synchronized safely during segment lifecycle.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 21, 2026

📝 Walkthrough

Walkthrough

Adds per-segment masks: mask state and lifecycle in Segment, filesystem loading/parsing of segmaskN.json, UI controls and JSON (de)serialization, render-time gating to skip masked pixels, and enumeration/init hooks to register available masks.

Changes

Cohort / File(s) Summary
Header declarations
wled00/FX.h, wled00/wled.h
Added maskId, maskInvert (public) and _mask, _maskW, _maskH, _maskLen, _maskValid (private) to Segment; added setMask(), clearMask(), accessors hasMask(), maskAllows(), maskAllowsXY(); added WS2812FX::enumerateSegmasks() and global segMasks.
Configuration constants
wled00/const.h
Introduced WLED_MAX_SEGMASKS with range guard (4–32) and platform defaults (ESP8266:10, others:16).
Core masking implementation
wled00/FX_fcn.cpp
Implemented setMask() to load/parse segmaskN.json (dimensions, invert, 1-bit mask), clearMask() to free state, mask state synchronization (segmentMux / busDrawMux), lifecycle propagation (copy/move/assign). Added segMasks enumeration and finalize init hook.
2D pixel rendering
wled00/FX_2Dfcn.cpp
Validate _maskValid in startFrame(); added maskAllowsXY() gating in fast and slow setPixelColorXY() paths to skip masked pixels.
1D rendering gating & render-time checks
wled00/FX_fcn.cpp
Apply maskAllows(i) gating for 1D pixel writes and respect mask validity during rendering.
JSON (de)serialization
wled00/json.cpp
deserializeSegment() reads "mask" (clamped, apply/clear) and "minv"; serializeSegment() writes "mask" and "minv"; serializeInfo() adds "masks" array of active masks.
UI / Frontend
wled00/data/index.js
Added per-segment mask UI controls (selector, invert toggle, info), integrated mask state into segment UI/mapping logic, and added setMask() / setMaskInv() client functions.
Initialization / Settings integration
wled00/set.cpp, wled00/wled.cpp
Call strip.enumerateSegmasks() alongside enumerateLedmaps() during settings init and after LED-map load; finalizeInit() calls enumeration.

Sequence Diagram(s)

sequenceDiagram
    participant UI as Frontend UI
    participant App as WLED App
    participant Seg as Segment
    participant FS as Filesystem
    participant Render as Renderer

    UI->>App: requestJson setMask(seg, id)
    App->>Seg: deserializeSegment() with "mask"
    Seg->>Seg: setMask(maskId)
    Seg->>FS: open segmask{maskId}.json
    FS-->>Seg: return JSON (w,h,inv,bitarray)
    Seg->>Seg: parse, alloc _mask, set _maskValid
    Seg-->>App: updated segment state
    App-->>UI: update UI

    Note over Render,Seg: During render loop
    Render->>Seg: setPixelColorXY(x,y,color)
    Seg->>Seg: maskAllowsXY(x,y)?
    alt allowed
        Seg->>Seg: apply color
    else masked
        Seg-->>Seg: skip pixel
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

enhancement

Suggested reviewers

  • softhack007

Poem

🐇 I found a mask inside a file,

Bits and JSON made me smile,
I hop through pixels, skip and play,
Masked lights blink night and day,
Hoppy code and glowing style ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 52.94% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Feat: Segment masks' directly and clearly describes the main feature addition—per-segment clipping masks for LED rendering.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
wled00/FX_fcn.cpp (1)

94-103: Mask reload path missing after copy/assignment.

Copy ctor/assignment null out _mask (and _maskValid) but preserve maskId/maskInvert via memcpy. When rendering, maskAllows() checks if (!_mask || !_maskValid) return true;, so copied segments silently skip mask application even though maskId remains set. There is no automatic reload path—the effect must manually call setMask(maskId) after copying, or maskId should be cleared.

🤖 Fix all issues with AI agents
In `@wled00/FX_fcn.cpp`:
- Around line 253-260: _maskValid is only set once when loading the mask and can
become stale if virtual geometry changes; update the validity check by
recomputing it whenever geometry may change (for example at start of setUp() and
startFrame() or immediately before any mask application routine). Locate where
_maskValid is used (e.g., in FX_fcn::_maskValid checks and any applyMask/paint
routines) and replace the single-time assignment with a runtime check that
compares _maskW/_maskH to calc_virtualWidth()/calc_virtualHeight() (or reassign
_maskValid at the start of setUp()/startFrame()) so the mask validity reflects
current virtual geometry. Ensure no behavioral change besides recomputing
validity.
- Around line 150-176: The clearMask()/setMask() flow can free or replace _mask
while maskAllows() may be reading it on the other core; to fix, serialize the
pointer swap/free with the renderer by acquiring the same mutex used by the
render loop (e.g., segmentMux) around the critical section in Segment::clearMask
and Segment::setMask (before freeing or assigning _mask and updating
_maskLen/_maskW/_maskH/_maskValid/maskId) or alternatively wait for the strip to
be idle using the existing idle/wait API before changing the buffer; locate and
protect the critical sections in Segment::clearMask, Segment::setMask and
anywhere maskAllows reads _mask to prevent use-after-free.
🧹 Nitpick comments (2)
wled00/json.cpp (1)

364-372: Prefer an explicit clear path when mask is 0.
This makes intent obvious and avoids any unintended file load if setMask(0) isn’t a no-op.

♻️ Proposed tweak
 if (elem.containsKey("mask")) { // WLEDMM segment mask id
   int maskVal = elem["mask"] | 0;
   if (maskVal < 0) maskVal = 0;
   uint8_t maskId = constrain(maskVal, 0, WLED_MAX_SEGMASKS-1);
-  if (maskId != seg.maskId || (maskId != 0 && !seg.hasMask())) seg.setMask(maskId);
+  if (maskId == 0) {
+    if (seg.hasMask()) seg.clearMask();
+  } else if (maskId != seg.maskId || !seg.hasMask()) {
+    seg.setMask(maskId);
+  }
 }
 if (elem.containsKey("minv")) { // WLEDMM segment mask invert
   seg.maskInvert = elem["minv"] | seg.maskInvert;
 }
wled00/FX.h (1)

725-741: Consider adding [[gnu::hot]] attribute for hot-path optimization.

The mask accessor logic is correct:

  • Fail-open design (returns true when mask is absent/invalid) prevents black pixels on error
  • Bounds checking prevents buffer overread
  • Bit-packing/unpacking is efficient and correct (LSB-first)

Since maskAllows() and maskAllowsXY() are called for every pixel during rendering, consider adding the [[gnu::hot]] attribute (like progress() at line 692 and currentBri() at line 700) to hint the compiler for optimization.

♻️ Suggested optimization
-    inline bool hasMask(void) const { return _mask != nullptr; } // WLEDMM
-    inline bool maskAllows(uint16_t i) const { // WLEDMM
+    [[gnu::hot]] inline bool hasMask(void) const { return _mask != nullptr; } // WLEDMM
+    [[gnu::hot]] inline bool maskAllows(uint16_t i) const { // WLEDMM
       if (!_mask || !_maskValid) return true;
       if (size_t(i) >= _maskLen) return false;
       // WLEDMM: bit-packed mask (LSB-first): byte = i>>3, bit = i&7
       bool bit = (_mask[i >> 3] >> (i & 7)) & 0x01;
       return maskInvert ? !bit : bit;
     }
-    inline bool maskAllowsXY(int x, int y) const { // WLEDMM
+    [[gnu::hot]] inline bool maskAllowsXY(int x, int y) const { // WLEDMM
       if (!_mask || !_maskValid) return true;
       if (x < 0 || y < 0) return false;
       size_t idx = size_t(x) + (size_t(y) * _maskW);
       if (idx >= _maskLen) return false;
       // WLEDMM: row-major (x + y*w), bit-packed mask (LSB-first in each byte)
       bool bit = (_mask[idx >> 3] >> (idx & 7)) & 0x01;
       return maskInvert ? !bit : bit;
     }

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
wled00/FX_fcn.cpp (1)

94-103: Reset mask ID and invert flag when clearing mask buffer on segment copy.

After memcpy, both the copy constructor and copy assignment operator null _mask and reset mask buffer state, but leave maskId and maskInvert from the source. This causes the copied segment to report a mask ID that has no actual buffer applied. Reset these to match clearMask() behavior:

Fix for both locations
   _maskW = 0; // WLEDMM
   _maskH = 0; // WLEDMM
   _maskValid = false; // WLEDMM
+  maskId = 0; // WLEDMM
+  maskInvert = false; // WLEDMM

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@wled00/FX_fcn.cpp`:
- Around line 171-176: Segment::setMask currently calls clearMask() but proceeds
even if clearMask failed to release the existing mask, which can leak the old
buffer; after calling clearMask() in Segment::setMask, check the _mask member
and abort (return false) if it is still non-null to avoid overwriting/leaking
the previous buffer—use the existing Segment::clearMask() call, then immediately
guard with a check on _mask before continuing (also preserve the existing id==0
behavior).

Comment on lines +171 to +176
bool Segment::setMask(uint8_t id) { // WLEDMM
clearMask();
if (id >= WLED_MAX_SEGMASKS) {
return false;
}
if (id == 0) return true;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Abort mask load if clearMask() couldn't clear.

If clearMask() fails to acquire segmentMux, _mask can remain non-null; setMask() can then overwrite it, leaking the old buffer. Add a guard to bail out when _mask is still set.

🛠️ Suggested guard
 bool Segment::setMask(uint8_t id) { // WLEDMM
   clearMask();
+  if (_mask) {
+    DEBUG_PRINTLN(F("Segment::setMask: clearMask failed, aborting."));
+    return false;
+  }
   if (id >= WLED_MAX_SEGMASKS) {
     return false;
   }
🤖 Prompt for AI Agents
In `@wled00/FX_fcn.cpp` around lines 171 - 176, Segment::setMask currently calls
clearMask() but proceeds even if clearMask failed to release the existing mask,
which can leak the old buffer; after calling clearMask() in Segment::setMask,
check the _mask member and abort (return false) if it is still non-null to avoid
overwriting/leaking the previous buffer—use the existing Segment::clearMask()
call, then immediately guard with a check on _mask before continuing (also
preserve the existing id==0 behavior).

// * expects scaled color (final brightness) as additional input parameter, plus segment virtualWidth() and virtualHeight()
void IRAM_ATTR __attribute__((hot)) Segment::setPixelColorXY_fast(int x, int y, uint32_t col, uint32_t scaled_col, int cols, int rows) const //WLEDMM
{
if (!maskAllowsXY(x, y)) return; // WLEDMM mask gate for 2D pixels
Copy link
Collaborator

@softhack007 softhack007 Jan 23, 2026

Choose a reason for hiding this comment

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

I need to think about this one - wledMM main objective is performance/speed, so adding a new function call into the hot path of setPixelColor needs some careful thinking 🤔 especially when 95% of users might not need/use the mask feature.

I see why you put it here, maybe the call should to be guarded - something like if (_maskValid && !maskAllowsXY(...)) return;

Copy link
Author

Choose a reason for hiding this comment

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

Oh, good catch - yeah, my intent is to only add overhead iff the feature is being used (and keep it minimal in that case as well). I'll add some guards across the PR where appropriate

const int_fast16_t rows = virtualHeight();

if (x<0 || y<0 || x >= cols || y >= rows) return; // if pixel would fall out of virtual segment just exit
if (!maskAllowsXY(x, y)) return; // WLEDMM mask gate for 2D pixels
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment here

transitional = false; // copied segment cannot be in transition
if (name) delete[] name;
if (_t) delete _t;
clearMask(); // WLEDMM
Copy link
Collaborator

@softhack007 softhack007 Jan 23, 2026

Choose a reason for hiding this comment

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

Not sure if clearMask() should be called here

  • it locks the segment mutex which could cause other troubles (need to double check)
  • actually it should be enough here to free the mask (avoid mem leaks) and then reset mask-related metadata.

i &= 0xFFFF;
if (unsigned(i) >= virtualLength()) return; // if pixel would fall out of segment just exit //WLEDMM unsigned(i)>SEGLEN also catches "i<0"

if (!is2D() && !maskAllows(i)) return; // WLEDMM mask gate for 1D segments
Copy link
Collaborator

@softhack007 softhack007 Jan 23, 2026

Choose a reason for hiding this comment

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

Similar to the 2D setPixelColor case: we need to protect the hot path from any additional function calls. Instead of doing if !is2D() && !maskAllows(..) here, it's better to put it into the else branch for line 1005

wled00/wled.h Outdated
#else
WLED_GLOBAL uint16_t ledMaps _INIT(0); // bitfield representation of available ledmaps
#endif
#if WLED_MAX_SEGMASKS>16
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can simply use the uint32_t branch here, or stick with uint16_t if you think there will never be more than 15 mask files. The same ``#if` for ledMaps is actually on my "kill stupid optimizations" list for longer, as it only invites trouble for a questionable saving of two bytes in RAM.

char fileName[24] = {'\0'};
snprintf_P(fileName, sizeof(fileName), PSTR("/segmask%d.json"), id);
if (!WLED_FS.exists(fileName)) {
USER_PRINTF("Segment mask missing: %s\n", fileName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

DEBUG_PRINTF should be used here, unless "we don't have segment mask file xx" is really the smoking gun of an error. Otherwise a user who doesn't have masks files in his LittleFS would receive this message regularly.

@softhack007
Copy link
Collaborator

@sayhiben I really like the overall idea 😃 its a nice way to handle overlaying properly, and it optimizes pixel drawing by skipping masked out pixels at the same time 👍

@sayhiben
Copy link
Author

@softhack007 Thanks for the thorough review! I'll work through comments today

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@wled00/FX_fcn.cpp`:
- Around line 330-334: Direct free(_mask) is unsafe because maskAllows() reads
_mask without mutex protection; replace this with a protected pointer swap (or
call clearMask()) so readers never see a freed pointer. Either call clearMask()
from here, or replicate its sequence: esp32SemTake(&segmentMux) to lock, oldMask
= _mask; _mask = nullptr; esp32SemGive(&segmentMux) to unlock, then call
strip_wait_until_idle("Segment::operator= mask cleanup") if still desired and
free(oldMask). Also remove or update the misleading comment about deadlocks and
avoid freeing while still holding the mutex.
♻️ Duplicate comments (1)
wled00/FX_fcn.cpp (1)

171-176: Add guard to prevent memory leak if clearMask() fails.

If clearMask() fails to acquire segmentMux, _mask remains non-null. Proceeding with the load would overwrite the pointer and leak the old buffer. This was flagged in a previous review but doesn't appear to be addressed.

🛠️ Suggested fix
 bool Segment::setMask(uint8_t id) { // WLEDMM
   clearMask();
+  if (_mask) {
+    DEBUG_PRINTLN(F("Segment::setMask: clearMask failed, aborting."));
+    return false;
+  }
   if (id >= WLED_MAX_SEGMASKS) {
     return false;
   }
   if (id == 0) return true;
🧹 Nitpick comments (1)
wled00/FX_fcn.cpp (1)

1188-1189: Consider moving mask gate to the 1D-only path for efficiency.

The mask check at line 1188 runs for all segments including 2D, but 2D segments already have mask gating in setPixelColorXY_* functions. While the check is cheap (bit lookup with early _maskValid check), placing it in the 1D-only else branch (around line 1361+) would avoid redundant checks for 2D paths.

A previous reviewer suggested: "Instead of doing if !is2D() && !maskAllows(..) here, it's better to put it into the else branch for line 1005."

♻️ Alternative placement

Move the mask gate to the 1D path only (after line 1371):

   }
 `#endif`
+
+  if (_maskValid && !maskAllows(i)) return; // WLEDMM mask gate for 1D segments

   if (ledsrgb) ledsrgb[i] = col;

And remove from line 1188-1189.

Comment on lines +330 to +334
if (_mask) { // WLEDMM free mask buffer directly to avoid deadlocks
strip_wait_until_idle("Segment::operator= mask cleanup"); // WLEDMM avoid freeing while renderer is active
free(_mask);
_mask = nullptr;
}
Copy link

@coderabbitai coderabbitai bot Jan 24, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Examine clearMask, maskAllows, and segmentMux usage
rg -n "clearMask\(\)|maskAllows|segmentMux" wled00/FX_fcn.cpp wled00/FX.h --context=3 | head -100

Repository: MoonModules/WLED-MM

Length of output: 5748


🏁 Script executed:

#!/bin/bash
# Find the clearMask function definition
rg -n "void clearMask|void.*clearMask" wled00/FX_fcn.cpp wled00/FX.h -A 10

Repository: MoonModules/WLED-MM

Length of output: 1448


🏁 Script executed:

#!/bin/bash
# Find maskAllows function definition
rg -n "maskAllows" wled00/FX_fcn.cpp wled00/FX.h -B 2 -A 8

Repository: MoonModules/WLED-MM

Length of output: 1891


Use clearMask() or acquire segmentMux to protect pointer swap before freeing.

The direct free(_mask) is unsafe because maskAllows() (called from rendering tasks on other cores) reads _mask without mutex protection. While strip_wait_until_idle() ensures the renderer is idle now, it doesn't prevent the next render cycle from starting immediately after.

Compare with clearMask() (lines 152–169), which correctly:

  1. Acquires segmentMux with esp32SemTake()
  2. Swaps the pointer atomically (oldMask = _mask; _mask = nullptr)
  3. Releases the mutex
  4. Frees the old buffer after releasing the lock

Either call clearMask() directly or follow this same pattern to ensure the pointer swap is serialized with reader tasks. The comment about deadlocks is misleading—heap operations are thread-safe; the issue is protecting the pointer change itself.

🤖 Prompt for AI Agents
In `@wled00/FX_fcn.cpp` around lines 330 - 334, Direct free(_mask) is unsafe
because maskAllows() reads _mask without mutex protection; replace this with a
protected pointer swap (or call clearMask()) so readers never see a freed
pointer. Either call clearMask() from here, or replicate its sequence:
esp32SemTake(&segmentMux) to lock, oldMask = _mask; _mask = nullptr;
esp32SemGive(&segmentMux) to unlock, then call
strip_wait_until_idle("Segment::operator= mask cleanup") if still desired and
free(oldMask). Also remove or update the misleading comment about deadlocks and
avoid freeing while still holding the mutex.

Copy link
Author

Choose a reason for hiding this comment

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

@softhack007 I believe I've hit all of your other suggestions at this point, but this quirk is tripping me up a little. Do you have any recommendations?

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

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.

2 participants