FastLED 3.10.6
Loading...
Searching...
No Matches

◆ encodeSK9822_AutoBrightness()

template<typename InputIterator, typename OutputIterator>
FL_NO_INLINE_IF_AVR void fl::encodeSK9822_AutoBrightness ( InputIterator first,
InputIterator last,
OutputIterator out )

Encode pixel data in SK9822 format (auto-detected brightness from first pixel)

Template Parameters
InputIteratorIterator yielding fl::array<u8, 3> (3 bytes in wire order)
OutputIteratorOutput iterator accepting uint8_t
Parameters
firstIterator to first pixel
lastIterator past last pixel
outOutput iterator for encoded bytes
Note
SK9822 uses BGR wire order: pixel[0]=Blue, pixel[1]=Green, pixel[2]=Red
Uses O2 optimization to avoid AVR register allocation issues with LTO
Marked noinline on AVR to prevent register exhaustion from divisions

Definition at line 122 of file sk9822.h.

123 {
124 if (first == last) {
125 // Keep zero LEDs as a full frame, matching the non-auto encoder.
126 *out++ = 0x00; *out++ = 0x00; *out++ = 0x00; *out++ = 0x00;
127 *out++ = 0xFF; *out++ = 0xFF; *out++ = 0xFF; *out++ = 0xFF;
128 return;
129 }
130
131 // Start frame
132 *out++ = 0x00;
133 *out++ = 0x00;
134 *out++ = 0x00;
135 *out++ = 0x00;
136
137 // Extract global brightness from first pixel
138 const fl::array<u8, BYTES_PER_PIXEL_RGB>& first_pixel = *first;
139 const u16 maxBrightness = 0x1F;
140 // Use sequential comparisons to avoid nested fl::max (helps AVR register allocation)
141 u8 max_rg = (first_pixel[2] > first_pixel[1]) ? first_pixel[2] : first_pixel[1];
142 u8 max_component = (max_rg > first_pixel[0]) ? max_rg : first_pixel[0];
143 u16 brightness = ((((u16)max_component + 1) * maxBrightness - 1) >> 8) + 1;
144 u8 global_brightness = static_cast<u8>(brightness);
145
146 // Write first LED with extracted brightness (BGR order: 0=B, 1=G, 2=R)
147 u8 s0 = (maxBrightness * first_pixel[2] + (brightness >> 1)) / brightness; // Red
148 u8 s1 = (maxBrightness * first_pixel[1] + (brightness >> 1)) / brightness; // Green
149 u8 s2 = (maxBrightness * first_pixel[0] + (brightness >> 1)) / brightness; // Blue
150
151 *out++ = 0xE0 | (global_brightness & 0x1F);
152 *out++ = s2; // Blue
153 *out++ = s1; // Green
154 *out++ = s0; // Red
155 ++first;
156
157 // Write remaining LEDs (count as we go)
158 size_t num_leds = 1; // Already wrote first pixel
159 while (first != last) {
160 const fl::array<u8, BYTES_PER_PIXEL_RGB>& pixel = *first;
161 *out++ = 0xE0 | (global_brightness & 0x1F);
162 *out++ = pixel[0]; // Index 0 (BGR order: Blue)
163 *out++ = pixel[1]; // Index 1 (BGR order: Green)
164 *out++ = pixel[2]; // Index 2 (BGR order: Red)
165 ++first;
166 ++num_leds;
167 }
168
169 // End frame: all ones clock the final data through the cascade.
170 size_t end_dwords = (num_leds / 32) + 1;
171 for (size_t i = 0; i < end_dwords * 4; i++) {
172 *out++ = 0xFF;
173 }
174}
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
A fixed-size array implementation similar to std::array.
Definition array.h:27
unsigned char u8
Definition stdint.h:131

References brightness, and FL_NO_EXCEPT.

Referenced by fl::PixelIterator::writeSK9822().

+ Here is the caller graph for this function: