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

+ Here is the caller graph for this function: