FastLED 3.9.15
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 121 of file sk9822.h.

122 {
123 if (first == last) {
124 // Empty range - just write start frame
125 *out++ = 0x00; *out++ = 0x00; *out++ = 0x00; *out++ = 0x00;
126 return;
127 }
128
129 // Start frame
130 *out++ = 0x00;
131 *out++ = 0x00;
132 *out++ = 0x00;
133 *out++ = 0x00;
134
135 // Extract global brightness from first pixel
136 const fl::array<u8, BYTES_PER_PIXEL_RGB>& first_pixel = *first;
137 const u16 maxBrightness = 0x1F;
138 // Use sequential comparisons to avoid nested fl::max (helps AVR register allocation)
139 u8 max_rg = (first_pixel[2] > first_pixel[1]) ? first_pixel[2] : first_pixel[1];
140 u8 max_component = (max_rg > first_pixel[0]) ? max_rg : first_pixel[0];
141 u16 brightness = ((((u16)max_component + 1) * maxBrightness - 1) >> 8) + 1;
142 u8 global_brightness = static_cast<u8>(brightness);
143
144 // Write first LED with extracted brightness (BGR order: 0=B, 1=G, 2=R)
145 u8 s0 = (maxBrightness * first_pixel[2] + (brightness >> 1)) / brightness; // Red
146 u8 s1 = (maxBrightness * first_pixel[1] + (brightness >> 1)) / brightness; // Green
147 u8 s2 = (maxBrightness * first_pixel[0] + (brightness >> 1)) / brightness; // Blue
148
149 *out++ = 0xE0 | (global_brightness & 0x1F);
150 *out++ = s2; // Blue
151 *out++ = s1; // Green
152 *out++ = s0; // Red
153 ++first;
154
155 // Write remaining LEDs (count as we go)
156 size_t num_leds = 1; // Already wrote first pixel
157 while (first != last) {
158 const fl::array<u8, BYTES_PER_PIXEL_RGB>& pixel = *first;
159 *out++ = 0xE0 | (global_brightness & 0x1F);
160 *out++ = pixel[0]; // Index 0 (BGR order: Blue)
161 *out++ = pixel[1]; // Index 1 (BGR order: Green)
162 *out++ = pixel[2]; // Index 2 (BGR order: Red)
163 ++first;
164 ++num_leds;
165 }
166
167 // End frame (SK9822 uses 0x00)
168 size_t end_dwords = (num_leds / 32) + 1;
169 for (size_t i = 0; i < end_dwords * 4; i++) {
170 *out++ = 0x00;
171 }
172}
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_NOEXCEPT.

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

+ Here is the caller graph for this function: