FastLED 3.9.15
Loading...
Searching...
No Matches

◆ mapBrightness8to5()

u8 fl::mapBrightness8to5 ( u8 brightness_8bit)
inline

Map 8-bit brightness to 5-bit (0-31)

Parameters
brightness_8bitInput brightness (0-255)
Returns
5-bit brightness (0-31)
Note
Ensures non-zero input maps to non-zero output (fixes issue #1908)
Uses bit-shift approximation on AVR to avoid expensive division

Definition at line 20 of file encoder_utils.h.

20 {
21 #if defined(FL_IS_AVR)
22 // AVR-specific: Use bit shifts to avoid expensive division
23 // Approximation: (value * 31) / 255 ≈ (value * 31) >> 8
24 // Add rounding: ((value * 31) + 128) >> 8
25 u16 bri5 = ((u16)brightness_8bit * 31 + 128) >> 8;
26
27 // Ensure non-zero input doesn't map to zero output
28 if (bri5 == 0 && brightness_8bit != 0) {
29 bri5 = 1;
30 }
31
32 return static_cast<u8>(bri5);
33 #else
34 // Non-AVR: Use accurate division
35 u16 bri5 = ((u16)brightness_8bit * 31 + 128) / 255;
36
37 // Ensure non-zero input doesn't map to zero output
38 if (bri5 == 0 && brightness_8bit != 0) {
39 bri5 = 1;
40 }
41
42 return static_cast<u8>(bri5);
43 #endif
44}
unsigned char u8
Definition stdint.h:131
unsigned char u8
Definition stdint.h:131

References FL_NOEXCEPT.

Referenced by encodeAPA102_HD(), and encodeSK9822_HD().

+ Here is the caller graph for this function: