5#ifndef COMPONENT_STRIP_H
6#define COMPONENT_STRIP_H
9#ifdef LMBD_LAMP_TYPE__INDEXABLE
15#include <etl/bitset.h>
20#include "src/generated/hal/strip_impl.hpp"
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;
35namespace modes::hardware {
41using StripImpl_t = hal::strip::LampdaStrip<LED_COUNT, 3>;
46 using BufferTy = std::array<uint32_t, LED_COUNT>;
50 static constexpr bool useColorDithering =
true;
52 static constexpr bool useTemporalDithering =
false;
53 static constexpr uint16_t refreshFramesCount = 10;
58 assert(_colorErrors.size() > 0);
60 for (uint16_t i = 0; i < LED_COUNT; ++i)
62 if constexpr (useTemporalDithering)
64 _colorErrors[i] = utils::ColorSpace::BLACK.get_rgb().color;
68 _colors[i] = utils::ColorSpace::BLACK.get_rgb().color;
82 hasSomeChanges =
false;
92 void setBlackout(
const bool shouldBlackout) { isBlackout = shouldBlackout; }
98 void begin() { StripImpl_t::begin(); }
118 void write_to_led_driver(
const uint8_t writeBrightness)
121 const uint8_t capedShown = shownCount % refreshFramesCount;
122 for (uint16_t i = 0; i < LED_COUNT; ++i)
125 if (nextWriteMask[i])
128 StripImpl_t::setPixelColor(i, 0);
130 if constexpr (useTemporalDithering)
138 if constexpr (useTemporalDithering)
142 StripImpl_t::setPixelColor(i, c);
148 StripImpl_t::setPixelColor(i, c);
153 nextWriteMask.reset();
160 brightnessAtShowTime = isBlackout ? 0 : brightness;
161 write_to_led_driver(brightnessAtShowTime);
164 hasSomeChanges =
false;
166 auto refCount = shownCount;
167 shownCount = refCount + 1;
184 const uint16_t colorShifted =
static_cast<uint16_t
>(colorIn) << 8u;
185 if (colorShifted <= brightness or brightness == 0)
188 uint8_t fullColor =
static_cast<uint8_t
>((colorShifted - brightness) / brightness);
202 const uint8_t brightness,
203 const uint16_t index)
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};
216 const uint16_t scaledColor = colorIn * brightness + brightness;
219 if constexpr (useColorDithering and useTemporalDithering)
221 const uint8_t allowedError = UINT8_MAX - (scaledColor & 0xFF);
222 errorIn += min<uint16_t>(allowedError, BLUE_NOISE_LUT[index % BLUE_NOISE_LUT.size()]);
226 const uint32_t fullColor = min<uint32_t>(scaledColor + errorIn, 0xFF00);
229 uint32_t finalColorRaw = fullColor >> 8;
230 const uint8_t finalError = fullColor & 0xFF;
233 if constexpr (useColorDithering and not useTemporalDithering)
235 finalColorRaw += (finalError > BLUE_NOISE_LUT[index % BLUE_NOISE_LUT.size()] ? 1 : 0);
238 const uint8_t finalColor = (finalColorRaw > 255) ? 255 :
static_cast<uint8_t
>(finalColorRaw);
239 return {finalColor, finalError};
251 const uint8_t brightness,
252 const uint16_t index,
256 if constexpr (not useTemporalDithering)
266 const auto& [green, greenError] =
268 const auto& [blue, blueError] =
276 void setPixelColor(uint16_t n, uint32_t c)
284 uint32_t getPixelColor(uint16_t n)
const {
return _colors[lmpd_constrain<uint16_t>(n, 0, LED_COUNT - 1)]; }
287 void signal_display() { hasSomeChanges =
true; }
297 nextWriteMask.set(n, shouldMask);
304 volatile bool hasSomeChanges;
307 volatile uint8_t brightness;
310 volatile bool isBlackout =
false;
313 volatile uint8_t brightnessAtShowTime;
316 volatile uint8_t shownCount;
319 etl::bitset<LED_COUNT> nextWriteMask;
322 std::array<uint32_t, useTemporalDithering ? LED_COUNT : 1> _colorErrors;
325 std::array<uint32_t, LED_COUNT> _colors;
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.