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

◆ encodeAPA102_AutoBrightness()

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

Encode pixel data in APA102 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
Extracts global brightness from max component of first pixel
APA102 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 131 of file apa102.h.

132 {
133 if (first == last) {
134 // Preserve a complete frame even at zero LEDs. This matters when a
135 // previously longer cascade is reconfigured through the public API.
136 *out++ = 0x00; *out++ = 0x00; *out++ = 0x00; *out++ = 0x00;
137 *out++ = 0xFF; *out++ = 0xFF; *out++ = 0xFF; *out++ = 0xFF;
138 return;
139 }
140
141 // Start frame
142 *out++ = 0x00;
143 *out++ = 0x00;
144 *out++ = 0x00;
145 *out++ = 0x00;
146
147 // Extract global brightness from first pixel
148 const fl::array<u8, BYTES_PER_PIXEL_RGB>& first_pixel = *first;
149 const u16 maxBrightness = 0x1F;
150 // Use sequential comparisons to avoid nested fl::max (helps AVR register allocation)
151 u8 max_rg = (first_pixel[2] > first_pixel[1]) ? first_pixel[2] : first_pixel[1];
152 u8 max_component = (max_rg > first_pixel[0]) ? max_rg : first_pixel[0];
153 u16 brightness = ((((u16)max_component + 1) * maxBrightness - 1) >> 8) + 1;
154 u8 global_brightness = static_cast<u8>(brightness);
155
156 // Write first LED with extracted brightness (BGR order: 0=B, 1=G, 2=R)
157 u8 s0 = (maxBrightness * first_pixel[2] + (brightness >> 1)) / brightness; // Red
158 u8 s1 = (maxBrightness * first_pixel[1] + (brightness >> 1)) / brightness; // Green
159 u8 s2 = (maxBrightness * first_pixel[0] + (brightness >> 1)) / brightness; // Blue
160
161 *out++ = 0xE0 | (global_brightness & 0x1F);
162 *out++ = s2; // Blue
163 *out++ = s1; // Green
164 *out++ = s0; // Red
165 ++first;
166
167 // Write remaining LEDs (count as we go)
168 size_t num_leds = 1; // Already wrote first pixel
169 while (first != last) {
170 const fl::array<u8, BYTES_PER_PIXEL_RGB>& pixel = *first;
171 *out++ = 0xE0 | (global_brightness & 0x1F);
172 *out++ = pixel[0]; // Index 0 (BGR order: Blue)
173 *out++ = pixel[1]; // Index 1 (BGR order: Green)
174 *out++ = pixel[2]; // Index 2 (BGR order: Red)
175 ++first;
176 ++num_leds;
177 }
178
179 // End frame
180 size_t end_dwords = (num_leds / 32) + 1;
181 for (size_t i = 0; i < end_dwords * 4; i++) {
182 *out++ = 0xFF;
183 }
184}
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::writeAPA102().

+ Here is the caller graph for this function: