FastLED 3.10.8
Loading...
Searching...
No Matches
hd108.h
Go to the documentation of this file.
1#pragma once
2
19
20#include "fl/stl/stdint.h"
21#include "fl/stl/array.h"
24#include "fl/stl/noexcept.h"
25
26namespace fl {
27
42template <typename InputIterator, typename OutputIterator>
43void encodeHD108(InputIterator first, InputIterator last, OutputIterator out,
44 u8 global_brightness = 255) FL_NO_EXCEPT {
45 // Start frame: 64 bits (8 bytes) of 0x00
46 for (int i = 0; i < 8; i++) {
47 *out++ = 0x00;
48 }
49
50 // Compute brightness header bytes (cached for all LEDs)
51 u8 f0, f1;
52 hd108BrightnessHeader(global_brightness, &f0, &f1);
53
54 // LED data: 2-byte header + 6-byte RGB16 (count as we go)
55 size_t num_leds = 0;
56 while (first != last) {
57 const fl::array<u8, BYTES_PER_PIXEL_RGB>& pixel = *first;
58
59 // Apply gamma correction (2.8) to 16-bit (RGB order: 0=R, 1=G, 2=B)
60 u16 r16 = hd108GammaCorrect(pixel[0]); // Red
61 u16 g16 = hd108GammaCorrect(pixel[1]); // Green
62 u16 b16 = hd108GammaCorrect(pixel[2]); // Blue
63
64 // Transmit: 2 header + 6 color bytes (16-bit RGB, big-endian)
65 *out++ = f0;
66 *out++ = f1;
67 *out++ = static_cast<u8>(r16 >> 8);
68 *out++ = static_cast<u8>(r16 & 0xFF);
69 *out++ = static_cast<u8>(g16 >> 8);
70 *out++ = static_cast<u8>(g16 & 0xFF);
71 *out++ = static_cast<u8>(b16 >> 8);
72 *out++ = static_cast<u8>(b16 & 0xFF);
73
74 ++first;
75 ++num_leds;
76 }
77
78 // End frame: (num_leds / 2) + 4 bytes of 0xFF
79 const size_t latch = num_leds / 2 + 4;
80 for (size_t i = 0; i < latch; i++) {
81 *out++ = 0xFF;
82 }
83}
84
98template <typename InputIterator, typename BrightnessIterator, typename OutputIterator>
99void encodeHD108_HD(InputIterator first, InputIterator last,
100 BrightnessIterator brightness_first, OutputIterator out) FL_NO_EXCEPT {
101 FL_UNUSED(brightness_first);
102 // Start frame: 64 bits (8 bytes) of 0x00
103 for (int i = 0; i < 8; i++) {
104 *out++ = 0x00;
105 }
106
107 // One header for the whole strip. `hd108BrightnessHeader` discards its
108 // argument -- `(void)brightness_8bit`, then three `constexpr` gains of 31
109 // -- so it returns 0xFF 0xFF for every input. There was a per-LED cache
110 // here guarding against recomputing a value that cannot change, and a
111 // `*brightness_first` read feeding it; both computed the same two bytes
112 // every time. Reading the brightness per LED was the more expensive half:
113 // `ScaledPixelIteratorBrightness::load()` is out of line, so it was a call
114 // per LED to fetch a number that reached nothing. See FastLED#4402.
115 u8 f0, f1;
116 hd108BrightnessHeader(0, &f0, &f1);
117
118 // LED data: 2-byte header + 6-byte RGB16 (count as we go)
119 size_t num_leds = 0;
120 while (first != last) {
121 const fl::array<u8, BYTES_PER_PIXEL_RGB>& pixel = *first;
122
123 // Apply gamma correction (2.8) to 16-bit (RGB order: 0=R, 1=G, 2=B)
124 u16 r16 = hd108GammaCorrect(pixel[0]); // Red
125 u16 g16 = hd108GammaCorrect(pixel[1]); // Green
126 u16 b16 = hd108GammaCorrect(pixel[2]); // Blue
127
128 // Transmit: 2 header + 6 color bytes (16-bit RGB, big-endian)
129 *out++ = f0;
130 *out++ = f1;
131 *out++ = static_cast<u8>(r16 >> 8);
132 *out++ = static_cast<u8>(r16 & 0xFF);
133 *out++ = static_cast<u8>(g16 >> 8);
134 *out++ = static_cast<u8>(g16 & 0xFF);
135 *out++ = static_cast<u8>(b16 >> 8);
136 *out++ = static_cast<u8>(b16 & 0xFF);
137
138 ++first;
139 ++num_leds;
140 }
141
142 // End frame: (num_leds / 2) + 4 bytes of 0xFF
143 const size_t latch = num_leds / 2 + 4;
144 for (size_t i = 0; i < latch; i++) {
145 *out++ = 0xFF;
146 }
147}
148
149} // namespace fl
A fixed-size array implementation similar to std::array.
Definition array.h:27
#define FL_UNUSED(x)
Constants for SPI chipset encoders.
Shared utilities for SPI chipset encoders.
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
void hd108BrightnessHeader(u8 brightness_8bit, u8 *f0_out, u8 *f1_out) FL_NO_EXCEPT
Generate HD108 per-channel gain header bytes.
void encodeHD108_HD(InputIterator first, InputIterator last, BrightnessIterator brightness_first, OutputIterator out) FL_NO_EXCEPT
Encode pixel data in HD108 format with per-LED brightness.
Definition hd108.h:99
void encodeHD108(InputIterator first, InputIterator last, OutputIterator out, u8 global_brightness=255) FL_NO_EXCEPT
Encode pixel data in HD108 format with global brightness.
Definition hd108.h:43
u16 hd108GammaCorrect(u8 value) FL_NO_EXCEPT
Convert 8-bit color to HD108 16-bit gamma-corrected value (gamma 2.8)
Base definition for an LED controller.
Definition crgb.hpp:179