FastLED 3.9.15
Loading...
Searching...
No Matches
encoder_utils.h
Go to the documentation of this file.
1#pragma once
2
8
9#include "fl/stl/stdint.h"
10#include "fl/gfx/gamma_lut.h"
11#include "fl/stl/noexcept.h"
12
13namespace fl {
14
20inline u8 mapBrightness8to5(u8 brightness_8bit) FL_NOEXCEPT {
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}
45
53 return 0xC0 | ((~b & 0xC0) >> 2) | ((~g & 0xC0) >> 4) | ((~r & 0xC0) >> 6);
54}
55
61 // LPD8806 requires MSB set on each byte, and uses 7-bit color depth
62 return 0x80 | ((value >> 1) | ((value && (value < 254)) & 0x01));
63}
64
71
80inline void hd108BrightnessHeader(u8 brightness_8bit, u8* f0_out, u8* f1_out) FL_NOEXCEPT {
81 (void)brightness_8bit; // Unused - brightness applied to 16-bit values instead
82
83 // Use maximum gain for all channels for maximum precision
84 // Brightness control happens via 16-bit PWM values
85 constexpr u8 r_gain = 31;
86 constexpr u8 g_gain = 31;
87 constexpr u8 b_gain = 31;
88
89 // Correct HD108 per-channel encoding:
90 // f0: [1][RRRRR][GG] - marker bit, 5-bit R gain, 2 MSBs of G gain
91 // f1: [GGG][BBBBB] - 3 LSBs of G gain, 5-bit B gain
92 *f0_out = 0x80 | ((r_gain & 0x1F) << 2) | ((g_gain >> 3) & 0x03);
93 *f1_out = ((g_gain & 0x07) << 5) | (b_gain & 0x1F);
94}
95
102inline u16 lpd6803EncodeRGB(u8 r, u8 g, u8 b) FL_NOEXCEPT {
103 u16 command = 0x8000; // Start marker
104 command |= (r & 0xF8) << 7; // Red: high 5 bits
105 command |= (g & 0xF8) << 2; // Green: high 5 bits
106 command |= (b >> 3); // Blue: high 5 bits
107 return command;
108}
109
110} // namespace fl
unsigned char u8
Definition stdint.h:131
constexpr int type_rank< T >::value
u16 lpd6803EncodeRGB(u8 r, u8 g, u8 b) FL_NOEXCEPT
Convert RGB to LPD6803 16-bit format (1bbbbbgggggrrrrr)
void hd108BrightnessHeader(u8 brightness_8bit, u8 *f0_out, u8 *f1_out) FL_NOEXCEPT
Generate HD108 per-channel gain header bytes.
u16 hd108GammaCorrect(u8 value) FL_NOEXCEPT
Convert 8-bit color to HD108 16-bit gamma-corrected value (gamma 2.8)
u8 mapBrightness8to5(u8 brightness_8bit) FL_NOEXCEPT
Map 8-bit brightness to 5-bit (0-31)
u8 p9813FlagByte(u8 r, u8 g, u8 b) FL_NOEXCEPT
Generate P9813 flag byte from RGB components.
u8 lpd8806Encode(u8 value) FL_NOEXCEPT
Apply LPD8806 encoding to a single color byte.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT