Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
strip.h
Go to the documentation of this file.
1
5#ifndef COMPONENT_STRIP_H
6#define COMPONENT_STRIP_H
7
8// this file is active only if LMBD_LAMP_TYPE=indexable
9#ifdef LMBD_LAMP_TYPE__INDEXABLE
10
11#include <cstdint>
12#include <cstring>
13#include <array>
14
15#include <etl/bitset.h>
16
18
19// special case: include the implementation
20#include "src/generated/hal/strip_impl.hpp"
21//
22
25
26#include "src/user/constants.h"
27
28namespace lampda {
29
30static constexpr size_t stripNbBuffers = 3;
31static constexpr float baseCurrentConsumption = 0.4f;
32static constexpr float maxCurrentConsumption = 2.7f - baseCurrentConsumption;
33static constexpr float ampPerLed = maxCurrentConsumption / (float)LED_COUNT;
34
35namespace modes::hardware {
36struct LampTy;
37}
38
39namespace component {
40
41using StripImpl_t = hal::strip::LampdaStrip<LED_COUNT, 3>;
42
44class LedStrip : private StripImpl_t
45{
46 using BufferTy = std::array<uint32_t, LED_COUNT>;
47 friend struct modes::hardware::LampTy;
48
50 static constexpr bool useColorDithering = true;
52 static constexpr bool useTemporalDithering = false;
53 static constexpr uint16_t refreshFramesCount = 10;
54
55public:
56 LedStrip(int16_t pin, neoPixelType type = NEO_RGB + NEO_KHZ800) : StripImpl_t(pin, type), shownCount(0)
57 {
58 assert(_colorErrors.size() > 0);
59
60 for (uint16_t i = 0; i < LED_COUNT; ++i)
61 {
62 if constexpr (useTemporalDithering)
63 {
64 _colorErrors[i] = utils::ColorSpace::BLACK.get_rgb().color;
65 }
66
67 // All to black
68 _colors[i] = utils::ColorSpace::BLACK.get_rgb().color;
69 }
70 }
71
72 LedStrip(const LedStrip&) = delete;
73 LedStrip& operator=(LedStrip const&) = delete;
74
75 void show()
76 {
77 if (hasSomeChanges)
78 {
79 // only show if some changes were made
80 show_now();
81 }
82 hasSomeChanges = false;
83 }
84
86 void setBrightness(uint8_t b) { brightness = b; }
87
89 uint8_t getBrightness() const { return brightness; }
90
92 void setBlackout(const bool shouldBlackout) { isBlackout = shouldBlackout; }
93
98 void begin() { StripImpl_t::begin(); }
99
102 uint32_t getRawPixelColor(uint16_t n) const
103 {
104 // ,o colors outside of strip
105 if (n >= LED_COUNT)
106 return 0;
107
108 utils::ColorSpace::RGB c(StripImpl_t::getPixelColor(n));
109 // We use brightnessAtShowTime here, or the colors can break when brightness changed
110 const uint8_t blue = restore_color_with_brightness(c.get_rgb().blue, brightnessAtShowTime);
111 const uint8_t green = restore_color_with_brightness(c.get_rgb().green, brightnessAtShowTime);
112 const uint8_t red = restore_color_with_brightness(c.get_rgb().red, brightnessAtShowTime);
113
114 return utils::ColorSpace::RGB(red, green, blue).get_rgb().color;
115 }
116
118 void write_to_led_driver(const uint8_t writeBrightness)
119 {
120 // Adjust brightness to the desired output
121 const uint8_t capedShown = shownCount % refreshFramesCount;
122 for (uint16_t i = 0; i < LED_COUNT; ++i)
123 {
124 // ignore masked leds
125 if (nextWriteMask[i])
126 {
127 // write black
128 StripImpl_t::setPixelColor(i, 0);
129
130 if constexpr (useTemporalDithering)
131 {
132 _colorErrors[i] = 0;
133 }
134 // and skip
135 continue;
136 }
137
138 if constexpr (useTemporalDithering)
139 {
140 const uint32_t c = convert_color_with_brightness(_colors[i], writeBrightness, i + capedShown, _colorErrors[i]);
141 // set strip color
142 StripImpl_t::setPixelColor(i, c);
143 }
144 else
145 {
146 const uint32_t c = convert_color_with_brightness(_colors[i], writeBrightness, i + capedShown, _colorErrors[0]);
147 // set strip color
148 StripImpl_t::setPixelColor(i, c);
149 }
150 }
151
152 // reset the mask after write
153 nextWriteMask.reset();
154 }
155
157 void show_now()
158 {
159 // copy the pattern to show to the display buffer
160 brightnessAtShowTime = isBlackout ? 0 : brightness;
161 write_to_led_driver(brightnessAtShowTime);
162 // show on hardware
163 StripImpl_t::show();
164 hasSomeChanges = false;
165 // increment show count
166 auto refCount = shownCount;
167 shownCount = refCount + 1;
168 }
169
181 static uint8_t restore_color_with_brightness(uint8_t colorIn, const uint8_t brightness)
182 {
183 // only special case
184 const uint16_t colorShifted = static_cast<uint16_t>(colorIn) << 8u;
185 if (colorShifted <= brightness or brightness == 0)
186 return 0;
187
188 uint8_t fullColor = static_cast<uint8_t>((colorShifted - brightness) / brightness);
189 return fullColor;
190 }
191
200 static std::pair<uint8_t, uint8_t> get_brightness_color_and_error(const uint8_t colorIn,
201 uint16_t errorIn,
202 const uint8_t brightness,
203 const uint16_t index)
204 {
205 // blue noise look up table
206 static constexpr std::array<uint8_t, 64> BLUE_NOISE_LUT = {
207 0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26, 12, 44, 4, 36, 14, 46,
208 6, 38, 60, 28, 52, 20, 62, 30, 54, 22, 3, 35, 11, 43, 1, 33, 9, 41, 51, 19, 59, 27,
209 49, 17, 57, 25, 15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29, 53, 21};
210
211 // only special case
212 if (colorIn == 0)
213 return {0, 0};
214
215 // scale color by brightness
216 const uint16_t scaledColor = colorIn * brightness + brightness;
217
218 // When using standard and temporal dithering, shift the pattern
219 if constexpr (useColorDithering and useTemporalDithering)
220 {
221 const uint8_t allowedError = UINT8_MAX - (scaledColor & 0xFF);
222 errorIn += min<uint16_t>(allowedError, BLUE_NOISE_LUT[index % BLUE_NOISE_LUT.size()]);
223 }
224
225 // add error
226 const uint32_t fullColor = min<uint32_t>(scaledColor + errorIn, 0xFF00);
227
228 // compute final color and new error
229 uint32_t finalColorRaw = fullColor >> 8;
230 const uint8_t finalError = fullColor & 0xFF;
231
232 // When using only color dithering, add a blue noise error to the final color
233 if constexpr (useColorDithering and not useTemporalDithering)
234 {
235 finalColorRaw += (finalError > BLUE_NOISE_LUT[index % BLUE_NOISE_LUT.size()] ? 1 : 0);
236 }
237
238 const uint8_t finalColor = (finalColorRaw > 255) ? 255 : static_cast<uint8_t>(finalColorRaw);
239 return {finalColor, finalError};
240 }
241
250 static uint32_t convert_color_with_brightness(const uint32_t color,
251 const uint8_t brightness,
252 const uint16_t index,
253 uint32_t& error)
254 {
255 // no temporal dithering: no error propagation
256 if constexpr (not useTemporalDithering)
257 {
258 error = 0;
259 }
260
261 utils::ColorSpace::RGB c(color);
262 utils::ColorSpace::RGB err(error);
263
264 // convert colors and errors, with small offsets for the different chanels
265 const auto& [red, redError] = get_brightness_color_and_error(c.get_rgb().red, err.get_rgb().red, brightness, index);
266 const auto& [green, greenError] =
267 get_brightness_color_and_error(c.get_rgb().green, err.get_rgb().green, brightness, index + 1);
268 const auto& [blue, blueError] =
269 get_brightness_color_and_error(c.get_rgb().blue, err.get_rgb().blue, brightness, index + 2);
270
271 // store error components
272 error = utils::ColorSpace::RGB(redError, greenError, blueError).get_rgb().color;
273 return utils::ColorSpace::RGB(red, green, blue).get_rgb().color;
274 }
275
276 void setPixelColor(uint16_t n, uint32_t c)
277 {
278 if (n >= LED_COUNT)
279 return;
280
281 _colors[n] = c;
282 }
283
284 uint32_t getPixelColor(uint16_t n) const { return _colors[lmpd_constrain<uint16_t>(n, 0, LED_COUNT - 1)]; }
285
286 // signal the strip that it can display the update
287 void signal_display() { hasSomeChanges = true; }
288
292 void mask_led_at_index(const size_t n, const bool shouldMask)
293 {
294 if (n >= LED_COUNT)
295 return;
296
297 nextWriteMask.set(n, shouldMask);
298 }
299
301 std::array<BufferTy, stripNbBuffers> _buffers;
302
303private:
304 volatile bool hasSomeChanges;
305
307 volatile uint8_t brightness;
308
310 volatile bool isBlackout = false;
311
313 volatile uint8_t brightnessAtShowTime;
314
316 volatile uint8_t shownCount;
317
319 etl::bitset<LED_COUNT> nextWriteMask;
320
322 std::array<uint32_t, useTemporalDithering ? LED_COUNT : 1> _colorErrors;
323
325 std::array<uint32_t, LED_COUNT> _colors;
326};
327
328} // namespace component
329} // namespace lampda
330
331#endif
332
333#endif
protected inheritence to avoid uncontroled hardware calls
Definition: strip.h:45
uint8_t getBrightness() const
Brightness is an internal flag.
Definition: strip.h:89
static uint32_t convert_color_with_brightness(const uint32_t color, const uint8_t brightness, const uint16_t index, uint32_t &error)
Convert a color to the correct brightness, with temporal dithering.
Definition: strip.h:250
void mask_led_at_index(const size_t n, const bool shouldMask)
Set a pixel to be masked at the next show.
Definition: strip.h:292
std::array< BufferTy, stripNbBuffers > _buffers
buffers for computations
Definition: strip.h:301
void setBrightness(uint8_t b)
Brightness is an internal counter.
Definition: strip.h:86
uint32_t getRawPixelColor(uint16_t n) const
Return the raw color value stored in the send buffer.
Definition: strip.h:102
void setBlackout(const bool shouldBlackout)
While set, the next shows write black to all leds, whatever the brightness or masks....
Definition: strip.h:92
static std::pair< uint8_t, uint8_t > get_brightness_color_and_error(const uint8_t colorIn, uint16_t errorIn, const uint8_t brightness, const uint16_t index)
Convert a color to the desired brightness level, with an error adjustment.
Definition: strip.h:200
static uint8_t restore_color_with_brightness(uint8_t colorIn, const uint8_t brightness)
Convert a color to the standard range, assuming it starts as brightness level.
Definition: strip.h:181
void begin()
Definition: strip.h:98
void show_now()
Show the current data, independant of changes.
Definition: strip.h:157
This class is a lightweight port of the Adafruit_Neopixel library, with compile time buffers to have ...
Definition: strip_impl.h:30
Definition: colorspace.h:50
COLOR get_rgb() const override
Get the RGB representation.
Definition: colorspace.h:62
Define commonly used color spaces.
Program scope.
Definition: control_fixed_modes.hpp:14
Implement a led strip object.
uint8_t neoPixelType
3rd arg to Adafruit_NeoPixel constructor
Definition: strip_impl.h:17
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:120
Define the system hardware constants.
User defined constants, relative to specific lamp types.