Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
lamp_type.hpp
Go to the documentation of this file.
1
5#ifndef MODES_HARDWARE_LAMP_TYPE_HPP
6#define MODES_HARDWARE_LAMP_TYPE_HPP
7
8#include <cstdint>
9#include <cstring>
10
11#include <etl/bitset.h>
12
13#include "src/compile.h"
14#include "src/user/constants.h"
15
18
19#ifdef LMBD_LAMP_TYPE__INDEXABLE
21#endif
22
23#include "src/system/hal/time.h"
24
26
28
32
34
35#include "src/system/ext/math8.h"
36
39
42
43#include "src/modes/include/draw/helix_mask.hpp"
44
45namespace lampda::modes {
46
47struct XYTy
48{
49 uint16_t x;
50 uint16_t y;
51};
52
54static constexpr uint16_t to_strip(uint16_t, uint16_t);
56static constexpr XYTy strip_to_XY(uint16_t n);
57
59{
60 float x;
61 float y;
62 float z;
63};
64
66static constexpr HelixXYZTy strip_to_helix(int16_t n);
67
70static constexpr HelixXYZTy strip_to_helix_unconstraint(const int16_t n);
72static constexpr bool is_led_index_valid(const int16_t ledIndex);
74static constexpr bool is_lamp_coordinate_out_of_bounds(const float angle_rad, const float z);
75
79static constexpr uint16_t to_led_index(const float angle_rad, const float z);
80
84static constexpr int16_t to_led_index_no_bounds(const float angle_rad, const float z);
85
86} // namespace lampda::modes
87
90
95enum class LampTypes : uint8_t
96{
97 simple,
98 cct,
99 indexable,
100};
101
102#ifdef LMBD_LAMP_TYPE__SIMPLE
103static constexpr LampTypes _lampType = LampTypes::simple;
104#endif
105
106#ifdef LMBD_LAMP_TYPE__CCT
107static constexpr LampTypes _lampType = LampTypes::cct;
108#endif
109
110#ifdef LMBD_LAMP_TYPE__INDEXABLE
111static constexpr LampTypes _lampType = LampTypes::indexable;
112#endif
113
115static constexpr LampTypes lampType = _lampType;
116
119struct LampTy
120{
121 //
122 // state managed by manager
123 //
124
127 {
128 };
129
132
133 //
134 // private constructors
135 //
136
137#ifdef LMBD_LAMP_TYPE__INDEXABLE
138private:
139 static constexpr float _fwidth = stripXCoordinates;
140 static constexpr float _fheight = stripYCoordinates;
141 static constexpr uint16_t _width = floor(_fwidth);
142 static constexpr uint16_t _height = floor(_fheight);
143 static constexpr uint16_t _ledCount = LED_COUNT;
144 static constexpr uint16_t _nbBuffers = stripNbBuffers;
145 static constexpr float _lampBodyRadius_mm = lampda::lampBodyRadius_mm;
146 static constexpr float _ledStripWidth_mm = lampda::ledStripWidth_mm;
147 static constexpr float _ledStripLength_mm = lampda::ledStripLength_mm;
148 static constexpr float _ledByMeter = lampda::ledByMeter;
149 component::LedStrip& strip;
150
152 static constexpr uint16_t _safeBufSize = (_width + 1) * (_height + 1);
153
155 using BufferTy = std::array<uint32_t, _ledCount>;
156 // using BufferTy = std::array<uint32_t, _safeBufSize>;
157
158 LampTy() = delete;
159 LampTy(const LampTy&) = delete;
160 LampTy& operator=(const LampTy&) = delete;
161
162public:
164 LMBD_INLINE LampTy(component::LedStrip& strip) : config {}, strip {strip}, now {0}, tick {0}, raw_frame_count {0} {}
165#else
166private:
167 // (placeholder values to avoid bad fails on misuse)
168 static constexpr float _fwidth = 23.5451;
169 static constexpr float _fheight = 22.51;
170 static constexpr uint16_t _width = 23;
171 static constexpr uint16_t _height = 22;
172 static constexpr uint16_t _ledCount = 530;
173 static constexpr uint16_t _nbBuffers = 3;
174 static constexpr float _lampBodyRadius_mm = 25.0f;
175 static constexpr float _ledStripWidth_mm = 5.2f;
176 static constexpr float _ledStripLength_mm = 25.0f;
177 static constexpr float _ledByMeter = 160.0f;
178
180 static constexpr uint16_t _safeBufSize = (_width + 1) * (_height + 1);
181
183 using BufferTy = std::array<uint32_t, _ledCount>;
184 // using BufferTy = std::array<uint32_t, _safeBufSize>;
185
186 LampTy(const LampTy&) = delete;
187 LampTy& operator=(const LampTy&) = delete;
188
189 struct LedStrip
190 {
191 BufferTy _buffers[_nbBuffers];
192
193 void begin();
194 void show();
195 void show_now();
196 void signal_display();
197 void setBrightness(brightness_t);
198 uint8_t getBrightness();
199 void setBlackout(bool);
200 void setPixelColor(uint16_t, uint32_t);
201 uint32_t getPixelColor(uint16_t);
202 void mask_led_at_index(const size_t n, const bool shouldMask);
203
204 private:
205 COLOR _colors[_ledCount]; // !! slighly smaller than buffers in _buffers !!
206 };
207 LedStrip fakeStrip;
208 LedStrip& strip;
209
210public:
212 LMBD_INLINE LampTy() : config {}, fakeStrip {}, strip {fakeStrip}, now {0}, tick {0}, raw_frame_count {0} {}
213#endif
214
215 //
216 // private API
217 //
218
220 void LMBD_INLINE refresh_tick_value()
221 {
222 uint32_t* writeable_now = const_cast<uint32_t*>(&now);
223 *writeable_now = hal::time_ms();
224
225 uint32_t* writable_tick = const_cast<uint32_t*>(&tick);
226 *writable_tick = now / frameDurationMs;
227
228 uint32_t* writable_frame_count = const_cast<uint32_t*>(&raw_frame_count);
229 *writable_frame_count += 1; // monotonous
230 }
231
242 void LMBD_INLINE startup()
243 {
244 // set output voltage for leds that will not change voltage
245 if (stripInputMinVoltage_mV == stripInputMaxVoltage_mV)
246 component::outputPower::write_voltage(stripInputMaxVoltage_mV);
247
248 begin();
249 clear();
250 show_now();
253 }
254
263 void LMBD_INLINE begin()
264 {
265 if constexpr (flavor == LampTypes::indexable)
266 {
267 strip.begin();
268 }
269 else
270 {
271 // (this is optimized out, and is essentially no-op for other flavors)
272 assert(true);
273 }
274 }
275
284 void LMBD_INLINE show()
285 {
286 if constexpr (flavor == LampTypes::indexable)
287 {
288 strip.show();
289 }
290 else
291 {
292 // (this is optimized out, and is essentially no-op for other flavors)
293 assert(true);
294 }
295 }
296
305 void LMBD_INLINE show_now()
306 {
307 if constexpr (flavor == LampTypes::indexable)
308 {
309 strip.show_now();
310 }
311 else
312 {
313 // (this is optimized out, and is essentially no-op for other flavors)
314 assert(true);
315 }
316 }
317
326 void LMBD_INLINE signal_display()
327 {
328 if constexpr (flavor == LampTypes::indexable)
329 {
330 strip.signal_display();
331 }
332 else
333 {
334 // (this is optimized out, and is essentially no-op for other flavors)
335 assert(true);
336 }
337 }
338
339 static constexpr helixmask::Params helixMaskParameters = {};
340
341 //
342 // public constants
343 //
344
346 static constexpr LampTypes flavor = lampType;
347
349 static constexpr uint32_t frameDurationMs = MAIN_LOOP_UPDATE_PERIOD_MS;
350
351#ifndef LMBD_LAMP_TYPE__INDEXABLE_IS_HD
352 // (we have 12ms and 83.3fps values to be updated in this file)
353 static_assert(frameDurationMs == 12, "Update the documentation of .tick :)");
354#endif
355
357 static constexpr uint32_t brightnessDurationMs = frameDurationMs * 2;
358
363 static constexpr uint16_t ledCount = _ledCount;
364
376 static constexpr uint16_t maxWidth = _width;
377
379 static constexpr float maxWidthFloat = _fwidth;
380
382 static constexpr uint16_t maxOverflowWidth = maxWidth + 1;
383
397 static constexpr uint16_t maxHeight = _height;
398
400 static constexpr float maxHeightFloat = _fheight;
401
403 static constexpr uint16_t maxOverflowHeight = maxHeight + 1;
404
406 static constexpr float shiftPerTurn = _fwidth - floor(_fwidth - 0.0001);
407
409 static constexpr uint16_t shiftPeriod = 2;
410
412 static constexpr float _invShiftResidue = shiftPeriod * (_fwidth - floor(_fwidth - 0.0001));
413
415 static constexpr uint16_t shiftResidue = 100.0 / abs(100.0 * _invShiftResidue - 100.0);
416
418 static constexpr auto allResiduesY = details::computeResidues<maxOverflowHeight>(_fwidth);
419
421 static constexpr auto allDeltaResiduesY = details::computeDeltaResidues<maxOverflowHeight>(_fwidth);
422
424 static constexpr auto extraShiftResiduesY = details::computeExtraShiftResidues<maxOverflowHeight>(_fwidth);
425
427 static constexpr int extraShiftTotal = extraShiftResiduesY[maxOverflowHeight - 1];
428
430 static constexpr float lampBodyRadius_mm = _lampBodyRadius_mm;
431
433 static constexpr float ledStripWidth_mm = _ledStripWidth_mm;
435 static constexpr float ledStripLength_mm = _ledStripLength_mm;
436
438 static constexpr float ledByMeter = _ledByMeter;
439
441 static constexpr float ledSize_mm = 1000.0f / ledByMeter;
448
453 static constexpr uint8_t nbBuffers = _nbBuffers;
454
455 //
456 // public helpers
457 //
458
465 template<typename T> static constexpr LMBD_INLINE uint16_t toLedCount(T scale)
466 {
467 if constexpr (std::is_same_v<T, float>)
468 {
469 assert(scale <= 1.0 && "scale is 0-1");
470 return ledCount * scale;
471 }
472 else if constexpr (std::is_same_v<T, uint8_t>)
473 {
474 uint32_t scaled = ledCount * scale;
475 return static_cast<uint16_t>(scaled / 256);
476 }
477 else
478 {
479 static_assert(std::is_same_v<T, std::false_type>, "Invalid type, either float or uint8_t required!");
480 return 0;
481 }
482 }
483
484 //
485 // public API
486 //
487
496 void LMBD_INLINE clear()
497 {
498 if constexpr (flavor == LampTypes::indexable)
499 {
500 for (uint16_t i = 0; i < ledCount; ++i)
501 {
503 }
504 }
505 else
506 {
507 // (this is optimized out, and is essentially no-op for other flavors)
508 assert(true);
509 }
510 }
511
519 void LMBD_INLINE blip(const uint32_t duration)
520 {
521 if constexpr (flavor == LampTypes::indexable)
522 {
523 // disable strip temporarily
524 // should be reactivated by the manager
525 strip.setBlackout(true);
526 }
527 else
528 {
530 }
531 }
532
536 void LMBD_INLINE cancel_blip() const
537 {
538 end_blackout();
540 }
541
545 void LMBD_INLINE end_blackout() const
546 {
547 if constexpr (flavor == LampTypes::indexable)
548 {
549 strip.setBlackout(false);
550 }
551 }
552
556 bool LMBD_INLINE is_bliping() const { return component::outputPower::is_bliping(); }
557
561 void LMBD_INLINE set_steps_mask(uint8_t newValue)
562 {
563 // only available for indexable
564 if constexpr (flavor == LampTypes::indexable)
565 {
566 uint8_t* writeable_currentMaskStep = const_cast<uint8_t*>(&currentMaskStep);
567 if (newValue <= maxMaskStep)
568 {
569 *writeable_currentMaskStep = newValue;
570 }
571 }
572 }
573
577 void LMBD_INLINE cancel_step_mask()
578 {
579 // only available for indexable
580 if constexpr (flavor == LampTypes::indexable)
581 {
582 uint8_t* writeable_currentMaskStep = const_cast<uint8_t*>(&currentMaskStep);
583 *writeable_currentMaskStep = 0;
584 }
585 }
586
607 void LMBD_INLINE setBrightness(const brightness_t newBrightness,
608 const bool skipCallbacks = true,
609 const bool skipUpdateBrightness = false,
610 const bool updateSavedBrightess = false)
611 {
612 assert((skipCallbacks || !skipUpdateBrightness) && "implicit callback skip!");
613
614 // invalid call, fallback to enforcing
615 if (newBrightness < ::lampda::brightness::absoluteMinimumBrightness ||
616 newBrightness > ::lampda::brightness::absoluteMaximumBrightness)
617 {
619 return;
620 }
621
622 // force update if we are getting lower and lower
623 if (!skipUpdateBrightness or newBrightness > getMaxBrightness())
624 {
625 // store the new temporary
626 temporary_brightness = min<brightness_t>(newBrightness, getMaxBrightness());
627
629 if ((this->now - last) > brightnessDurationMs)
631 }
632 // if necessary, update the saved version
633 if (updateSavedBrightess)
634 {
635 saved_brightness = min<brightness_t>(static_cast<brightness_t>(temporary_brightness), getMaxBrightness());
636 }
637
638 // USE THE BRIGHTNESS VALUE AFTER THE UPDATE
639 // brightness can be limited by the system, so do not use the raw brightness
640 const auto trueNewBrightness = logic::brightness::get_brightness();
641
642 if constexpr (flavor == LampTypes::indexable)
643 {
644#ifndef LMBD_LAMP_TYPE__INDEXABLE
645 static constexpr uint8_t minimumAllowedBrightness = 5;
646#else
647 static constexpr uint8_t minimumAllowedBrightness = ::lampda::minimumAllowedBrightness_8;
648#endif
650 static curve_t brightnessCurve = ::lampda::common::curves::make_linear_curve(
651 curve_t::point_t {ledMasking_brightnessThreshold, minimumAllowedBrightness},
652 curve_t::point_t {::lampda::brightness::absoluteMaximumBrightness, 255});
653
654 bool applyStandardBrightnessUpdate = true;
655 // apply led masking below a threshold
656 if (trueNewBrightness < ledMasking_brightnessThreshold)
657 {
658 applyStandardBrightnessUpdate = false;
659 const uint8_t index = lmpd_map<uint8_t>(trueNewBrightness, 0, ledMasking_brightnessThreshold, maxMaskStep, 0);
660 if (index <= 0)
661 {
662 applyStandardBrightnessUpdate = true;
663 }
664 else
665 {
666 set_steps_mask(index);
667 strip.setBrightness(minimumAllowedBrightness);
668 }
669 }
670
671 if (applyStandardBrightnessUpdate)
672 {
673 // cancel the step masking
675 strip.setBrightness(brightnessCurve.sample(trueNewBrightness));
676 }
677 }
678
679 if constexpr (flavor == LampTypes::simple)
680 {
681 const auto trueMaxBrightness = logic::brightness::get_max_brightness();
682
683 // This is kind of a hack to detect that the user did this call
684 // using the known user call signature
685 const bool isUserCall = skipCallbacks and skipUpdateBrightness and not updateSavedBrightess;
686
687 // display an output blip on max brightness reached
688 if (isUserCall and trueNewBrightness >= trueMaxBrightness)
689 {
691 }
692
694 static curve_t brightnessCurve(
695 curve_t::point_t {::lampda::brightness::absoluteMinimumBrightness, stripInputMinVoltage_mV},
696 curve_t::point_t {::lampda::brightness::absoluteMaximumBrightness, stripInputMaxVoltage_mV},
697 1.0);
698
699 component::outputPower::write_voltage(round(brightnessCurve.sample(trueNewBrightness)));
700 }
701 }
702
705 {
707 }
708
715 {
716 last_system_brightness = systemBrightness;
717 saved_brightness = min<brightness_t>(static_cast<brightness_t>(last_system_brightness), getMaxBrightness());
719 }
720
729 {
730 const auto maxAllowedBrightness = getMaxBrightness();
732
733 const brightness_t followed_saved_brightness =
734 system_brightness >= last_system_brightness ?
735 saved_brightness + (system_brightness - last_system_brightness) :
736 min<brightness_t>(static_cast<brightness_t>(saved_brightness), system_brightness);
737 last_system_brightness = system_brightness;
738
739 const brightness_t new_saved_brightness = min<brightness_t>(followed_saved_brightness, maxAllowedBrightness);
740 const brightness_t new_temporary_brightness =
742 new_saved_brightness :
743 min<brightness_t>(static_cast<brightness_t>(temporary_brightness), maxAllowedBrightness);
744
745 const bool isChanged = new_saved_brightness != saved_brightness or new_temporary_brightness != temporary_brightness;
746 if (isChanged)
747 {
748 saved_brightness = new_saved_brightness;
749 temporary_brightness = new_temporary_brightness;
750
752 }
753 }
754
759
771 void LMBD_INLINE tempBrightness(const brightness_t brightness)
772 {
773 brightness_t current = getBrightness();
774 if (current != brightness)
775 setBrightness(brightness, true, false, false);
776 }
777
789 void LMBD_INLINE jumpBrightness(const brightness_t brightness) { setBrightness(brightness, true, false, true); }
790
797 brightness_t LMBD_INLINE getBrightness(const bool readPreviousBrightness = false)
798 {
799 if (readPreviousBrightness)
800 return saved_brightness;
801 else
803 }
804
806 brightness_t LMBD_INLINE getSavedBrightness() { return getBrightness(true); }
807
809 void LMBD_INLINE restoreBrightness()
810 {
811 const brightness_t current = getBrightness();
812 const brightness_t saved = getSavedBrightness();
813
814 if (current != saved)
815 setBrightness(saved, true, false, false);
816 }
817
822 void LMBD_INLINE restoreBrightnessNow()
823 {
825 {
828 }
829 }
830
839 void LMBD_INLINE fadeToBlackBy(uint8_t fadeBy)
840 {
841 if (fadeBy == 0)
842 return; // optimization - no scaling to apply
843
844 if constexpr (flavor == LampTypes::indexable)
845 {
846 for (uint16_t i = 0; i < ledCount; ++i)
847 {
848 const uint32_t c = getPixelColor(i);
849 setPixelColor(i, colors::fade(c, UINT8_MAX - fadeBy));
850 }
851 }
852 else
853 {
854 // scale fadeBy to the brightness range, and subtract it there (avoid a lossy round trip to 0-255)
855 constexpr uint32_t minBrightness = brightness::absoluteMinimumBrightness;
856 constexpr uint32_t brightnessRange = brightness::absoluteMaximumBrightness - minBrightness;
857 const uint32_t fadeAmount = (static_cast<uint32_t>(fadeBy) * brightnessRange) / UINT8_MAX;
858
859 const uint32_t current = getBrightness();
860 const uint32_t faded = (current <= minBrightness + fadeAmount) ? minBrightness : current - fadeAmount;
861 setBrightness(static_cast<brightness_t>(faded));
862 }
863 }
864
874 void LMBD_INLINE blur(uint8_t blurBy)
875 {
876 if (blurBy == 0)
877 return; // optimization: 0 means "don't blur"
878 uint8_t keep = 255 - blurBy;
879 uint8_t seep = blurBy >> 1;
880
881 uint32_t carryover = 0;
882 for (uint16_t i = 0; i < ledCount; i++)
883 {
884 uint32_t cur = getPixelColor(i);
885 uint32_t c = cur;
886 uint32_t part = colors::fade<false>(c, seep);
887 cur = colors::add<true>(colors::fade<false>(c, keep), carryover);
888 if (i > 0)
889 {
890 c = getPixelColor(i - 1);
891 setPixelColor(i - 1, colors::add<true>(c, part));
892 }
893 setPixelColor(i, cur);
894 carryover = part;
895 }
896 }
897
905 void LMBD_INLINE apply_low_brightness_mask()
906 {
907 // not enabled, skip
908 if (forceDisableLedDitherMask or currentMaskStep == 0 or currentMaskStep > maxMaskStep)
909 return;
910
911 if constexpr (flavor == LampTypes::indexable)
912 {
914 static constexpr helixmask::StepTable kTable = helixmask::make_step_table(kStrip, helixMaskParameters);
915
916 for (uint16_t I = 0; I < ledCount; ++I)
917 {
918 // mask disabled leds
919 if (not helixmask::led_on(kTable, I, currentMaskStep))
920 mask_led_at_index(I, true);
921 }
922 }
923 else
924 {
925 assert(false && "unsupported");
926 }
927 }
928
934 void LMBD_INLINE fill(uint32_t color, uint16_t start, uint16_t end)
935 {
936 if constexpr (flavor == LampTypes::indexable)
937 {
938 assert(start <= ledCount && "invalid start parameter");
939 assert(end <= ledCount && "invalid end parameter");
940
941 for (uint16_t I = start; I < end; ++I)
942 {
943 setPixelColor(I, color);
944 }
945 }
946 else
947 {
948 assert(false && "unsupported");
949 }
950 }
951
956 void LMBD_INLINE fill(uint32_t color) { fill(color, 0, ledCount); }
957
963 void LMBD_INLINE fill(const colors::PaletteTy& palette, uint16_t start, uint16_t end)
964 {
965 if constexpr (flavor == LampTypes::indexable)
966 {
967 assert(start <= ledCount && "invalid start parameter");
968 assert(end <= ledCount && "invalid end parameter");
969
970 for (uint16_t I = start; I < end; ++I)
971 {
972 // map index to [0; 255]
973 const auto color = colors::from_palette<false, uint8_t>(I / static_cast<float>(ledCount) * 255, palette);
974 setPixelColor(I, color);
975 }
976 }
977 else
978 {
979 assert(false && "unsupported");
980 }
981 }
982
986 void LMBD_INLINE fill(const colors::PaletteTy& palette) { fill(palette, 0, ledCount); }
987
993 void LMBD_INLINE mask_led_at_index(const size_t n, const bool shouldMask)
994 {
995 if constexpr (flavor == LampTypes::indexable)
996 {
997 strip.mask_led_at_index(n, shouldMask);
998 }
999 else
1000 {
1001 assert(false && "unsupported");
1002 }
1003 }
1004 void LMBD_INLINE mask_led_at_XY(uint16_t X, uint16_t Y, const bool shouldMask)
1005 {
1006 mask_led_at_index(to_strip(X, Y), shouldMask);
1007 }
1008
1014 void LMBD_INLINE setPixelColor(uint16_t n, uint32_t color)
1015 {
1016 if constexpr (flavor == LampTypes::indexable)
1017 {
1018 assert(n < ledCount && "invalid LED index");
1019 // apply color
1020 strip.setPixelColor(n, color);
1021 }
1022 else
1023 {
1024 assert(false && "unsupported");
1025 }
1026 }
1027
1030 uint32_t LMBD_INLINE getPixelColor(uint16_t n)
1031 {
1032 if constexpr (flavor == LampTypes::indexable)
1033 {
1034 assert(n < ledCount && "invalid LED index");
1035 return strip.getPixelColor(n);
1036 }
1037 else
1038 {
1039 assert(false && "unsupported");
1040 return 0;
1041 }
1042 }
1043
1048 void LMBD_INLINE setPixelColorXY(uint16_t X, uint16_t Y, uint32_t color) { setPixelColor(to_strip(X, Y), color); }
1049
1054 static uint16_t LMBD_INLINE fromXYtoStripIndex(uint16_t X, uint16_t Y) { return to_strip(X, Y); }
1055
1060 static XYTy LMBD_INLINE fromStripIndexToXY(uint16_t N) { return strip_to_XY(N); }
1061
1067 template<uint8_t bufIdx = 0> BufferTy& LMBD_INLINE getTempBuffer()
1068 {
1069 static_assert(bufIdx < nbBuffers, "bufIdx must be lower than nbBuffers");
1070 BufferTy& buffer = strip._buffers[bufIdx];
1071 return buffer;
1072 }
1073
1080 template<uint8_t dstBufIdx, uint8_t srcBufIdx> void LMBD_INLINE copyBuffer()
1081 {
1082 static_assert(srcBufIdx < nbBuffers, "srcBufIdx must be lower than nbBuffers");
1083 static_assert(dstBufIdx < nbBuffers, "dstBufIdx must be lower than nbBuffers");
1084
1085 strip._buffers[dstBufIdx] = strip._buffers[srcBufIdx];
1086 }
1087
1092 template<uint8_t bufIdx = 0, typename T> void LMBD_INLINE fillTempBuffer(const T& value)
1093 {
1094 getTempBuffer<bufIdx>().fill(value);
1095 }
1096
1101 template<uint8_t bufIdx = 0> void setColorsFromBuffer()
1102 {
1103 const BufferTy& buffer = getTempBuffer<bufIdx>();
1104
1105 for (uint16_t I = 0; I < ledCount; I++)
1106 {
1107 setPixelColor(I, buffer[I]);
1108 }
1109 }
1110
1115 template<uint8_t dstBufIdx = 0, uint8_t srcBufIdx = 1> void setColorsFromMixedBuffers(float phase)
1116 {
1117 const BufferTy& dstBuf = getTempBuffer<dstBufIdx>();
1118 const BufferTy& srcBuf = getTempBuffer<srcBufIdx>();
1119
1120 uint16_t start = 0;
1121 uint16_t end = srcBuf.size();
1122 for (uint16_t I = start; I < end; ++I)
1123 {
1124 COLOR src;
1125 COLOR dst;
1126 src.color = srcBuf[I];
1127 dst.color = dstBuf[I];
1128
1129 // use accessor
1130 setPixelColor(I, utils::get_gradient(src.color, dst.color, phase));
1131 }
1132 }
1133
1136 template<uint8_t bufIdx = 0> void setColorsFromBufferReversed(bool skipLastLine)
1137 {
1138 const BufferTy& buffer = getTempBuffer<bufIdx>();
1139
1140 uint16_t start = 0;
1141 uint16_t end = buffer.size();
1142
1143 if (skipLastLine)
1144 {
1145 end = maxWidth * maxHeight;
1146 }
1147
1148 for (uint16_t I = 0, J = start; I < end - start; ++I, ++J)
1149 {
1150 setPixelColor(J, buffer[end - start - I - 1]);
1151 }
1152 }
1153
1158 template<uint8_t bufIdx = 0, bool forceFullRead = false> void getColorsToBuffer()
1159 {
1160 BufferTy& buffer = getTempBuffer<bufIdx>();
1161
1162 for (uint16_t I = 0; I < ledCount; I++)
1163 {
1164 buffer[I] = getPixelColor(I);
1165 }
1166 }
1167
1172 {
1174 }
1175
1182
1188 volatile const uint32_t now;
1189
1197 volatile const uint32_t tick;
1198
1205 volatile const uint32_t raw_frame_count;
1206
1211 volatile const uint8_t currentMaskStep = 0;
1212 volatile bool forceDisableLedDitherMask = false;
1213 static constexpr uint8_t maxMaskStep = helixMaskParameters.max_level;
1215 ::lampda::brightness::absoluteMaximumBrightness *
1216 0.25f;
1217};
1218
1219} // namespace lampda::modes::hardware
1220
1221namespace lampda::modes {
1222
1224static constexpr uint16_t to_strip(uint16_t x, uint16_t y)
1225{
1226 // maxHeight is the last "full" row and maxOverflowHeight is truncated row
1227 // -> user max use to_strip(x, maxOverflowHeight) to set truncated row
1229 {
1231 }
1232
1233 // if row is longer (because next row gets a shift) use maxOverflowWidth
1234 if (hardware::LampTy::allDeltaResiduesY[y])
1235 {
1237 {
1239 }
1240
1241 // if row is aligned to XY maxWidth/maxHeight matrix, use maxWidth instead
1242 }
1243 else if (x >= hardware::LampTy::maxWidth)
1244 {
1246 }
1247
1248 // final result is capped by the "real" LED count of the strip
1249 uint16_t n = x + y * hardware::LampTy::maxWidth + hardware::LampTy::allResiduesY[y];
1252 return n;
1253}
1254
1256static constexpr XYTy strip_to_XY(uint16_t n)
1257{
1258 // (saturates N to ledCount)
1261
1262 uint16_t y = 0;
1263 while (y * hardware::LampTy::maxWidth + hardware::LampTy::allResiduesY[y] <= n)
1264 y += 1;
1265
1266 if (y == 0)
1267 return {0, 0};
1268 y -= 1;
1269
1270 uint16_t x = n - y * hardware::LampTy::maxWidth - hardware::LampTy::allResiduesY[y];
1271 return {x, y};
1272}
1273
1279static constexpr HelixXYZTy strip_to_helix(int16_t n)
1280{
1281 if (not is_led_index_valid(n))
1282 return HelixXYZTy {0.0, 0.0, 0.0};
1283
1285}
1286
1292static constexpr float to_helix_z(const int16_t n)
1293{
1295}
1296
1302static constexpr HelixXYZTy strip_to_helix_unconstraint(const int16_t n)
1303{
1306 to_helix_z(n)};
1307}
1308
1313static constexpr bool is_led_index_valid(const int16_t n) { return n >= 0 and n < hardware::LampTy::ledCount; }
1314
1321static constexpr bool is_lamp_coordinate_out_of_bounds(const float angle_rad, const float z_mm)
1322{
1323 return not is_led_index_valid(to_led_index_no_bounds(angle_rad, z_mm));
1324}
1325
1332static constexpr uint16_t to_led_index(const float angle_rad, const float z_mm)
1333{
1334 constexpr int32_t maxZCoordinate =
1336
1337 // snip Z per possible lines.
1338 // Clamp in floating point before the conversion: an out of range float converted to an integer is undefined (a
1339 // point above the lamp gives a negative row, a point far away overflows). The negated test also catches NaN.
1340 float zRow = floor(-z_mm / hardware::LampTy::ledStripWidth_mm);
1341 if (not(zRow >= 0.0f))
1342 zRow = 0.0f;
1343 if (zRow > maxZCoordinate)
1344 zRow = maxZCoordinate;
1345 const int32_t zIndex = static_cast<int32_t>(zRow);
1346
1347 // indexing around the led turn
1348 const float angularPosition = wrap_angle(angle_rad) / c_TWO_PI * hardware::LampTy::maxWidthFloat;
1349
1350 // convert to led index (approx)
1351 int16_t ledIndex = round(angularPosition + zIndex * hardware::LampTy::maxWidthFloat);
1352 if (ledIndex < 0)
1353 ledIndex = round(angularPosition + (zIndex + 1) * hardware::LampTy::maxWidthFloat);
1354 if (ledIndex >= hardware::LampTy::ledCount)
1355 ledIndex = round(angularPosition + (zIndex - 1) * hardware::LampTy::maxWidthFloat);
1356
1357 if (ledIndex < 0)
1358 return 0;
1359 if (ledIndex >= hardware::LampTy::ledCount)
1360 return hardware::LampTy::ledCount - 1;
1361 return ledIndex;
1362}
1363
1370static constexpr int16_t to_led_index_no_bounds(const float angle_rad, const float z_mm)
1371{
1372 // snip Z per possible lines
1373 const int16_t zIndex = floor(-z_mm / hardware::LampTy::ledStripWidth_mm);
1374 // indexing around the led turn
1375 const float angularPosition = wrap_angle(angle_rad) / c_TWO_PI * hardware::LampTy::maxWidthFloat;
1376
1377 // convert to led index (approx)
1378 return round(angularPosition + zIndex * hardware::LampTy::maxWidthFloat);
1379}
1380
1381} // namespace lampda::modes
1382
1383#endif
Define assertions helpers.
Handle the output LED strip brightness.
Given two points and an exponent, fit an exponential function.
Definition: curves.h:140
Given a set of points, will fit multiple linear segments to it.
Definition: curves.h:36
protected inheritence to avoid uncontroled hardware calls
Definition: strip.h:45
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
void setBlackout(const bool shouldBlackout)
While set, the next shows write black to all leds, whatever the brightness or masks....
Definition: strip.h:92
void begin()
Definition: strip.h:98
void show_now()
Show the current data, independant of changes.
Definition: strip.h:157
Manipulate color representations.
Compile definitions and sanity checks.
Contains shorthand macro definitions.
Define the lamps coordinates systems.
Define curves types, that can be sampled.
SoundStruct & get_sound_characteristics()
Compute and process sound data.
Definition: sound.cpp:233
void cancel_blip()
stop an ongoing blip
Definition: output_power.cpp:41
void write_voltage(const uint16_t voltage_mv)
Write a voltage to the output (will only write it in output mode)
Definition: output_power.cpp:19
bool is_bliping()
indicates that the gate is in a blip
Definition: output_power.cpp:43
void blip(const uint32_t timing)
short interruption of output voltage
Definition: output_power.cpp:39
uint32_t time_ms(void)
Returns the number of milliseconds since the Arduino board began running the current program....
brightness_t get_brightness()
Return the current brightness value.
Definition: brightness_handle.cpp:42
uint32_t when_last_update_brightness()
Get time in milliseconds when brightness was last updated.
Definition: brightness_handle.cpp:102
brightness_t get_max_user_brightness()
Return the maximum allowed brightness for the user modes.
Definition: brightness_handle.cpp:46
void update_brightness(const brightness_t newBrightness, const bool shouldSkipUserCallbacks)
update the internal brightness values
Definition: brightness_handle.cpp:73
brightness_t get_max_brightness()
Return the maximum allowed brightness.
Definition: brightness_handle.cpp:44
brightness_t get_scaled_saved_brightness()
Return the saved brightness level, with the user brightness scale applied. This is the level that the...
Definition: brightness_handle.cpp:71
static uint32_t fade(uint32_t inputColor, uint8_t fadeAmount)
fade the color toward black
Definition: utils.hpp:121
std::array< uint32_t, 16 > PaletteTy
Palette types.
Definition: palettes.hpp:18
@ Black
Definition: palettes.hpp:96
Provide interface to the physical hardware and other facilities.
Definition: coordinates.hpp:11
LampTypes
Enumeration to write code working with all lamp types.
Definition: lamp_type.hpp:96
@ indexable
Equivalent to LMBD_LAMP_TYPE__INDEXABLE.
@ simple
Equivalent to LMBD_LAMP_TYPE__SIMPLE.
@ cct
Equivalent to LMBD_LAMP_TYPE__CCT.
static constexpr LampTypes lampType
Currently implemented lamp type, defined at compile-time by the user.
Definition: lamp_type.hpp:115
Contains basic interface types to implement custom user modes.
Definition: control_fixed_modes.hpp:14
static constexpr uint16_t to_led_index(const float angle_rad, const float z)
convert a lamp coordinate to a led index
Definition: lamp_type.hpp:1332
static constexpr float to_helix_z(const int16_t n)
Convert a led index to an helix height coordinate.
Definition: lamp_type.hpp:1292
static constexpr uint16_t to_strip(uint16_t, uint16_t)
convert grid coordinates to strip index
Definition: lamp_type.hpp:1224
static constexpr XYTy strip_to_XY(uint16_t n)
convert strip index to grid coordinates
Definition: lamp_type.hpp:1256
static constexpr int16_t to_led_index_no_bounds(const float angle_rad, const float z)
convert a lamp coordinate to a led index, the result can be an index out of the lamp body
Definition: lamp_type.hpp:1370
static constexpr bool is_lamp_coordinate_out_of_bounds(const float angle_rad, const float z)
True if the given led index is out of bounds.
Definition: lamp_type.hpp:1321
static constexpr bool is_led_index_valid(const int16_t ledIndex)
True if the given strip index is a valid one.
Definition: lamp_type.hpp:1313
static constexpr HelixXYZTy strip_to_helix_unconstraint(const int16_t n)
convert strip index to 3D coordinates, without checks on led index. This allows to use 3D coordinates...
Definition: lamp_type.hpp:1302
static constexpr HelixXYZTy strip_to_helix(int16_t n)
convert strip index to 3D coordinates
Definition: lamp_type.hpp:1279
uint32_t get_gradient(const uint32_t colorStart, const uint32_t colorEnd, const float level)
Return the color gradient between colorStart to colorEnd.
Definition: utils.cpp:60
static constexpr float wrap_angle(const float angle_rad)
Wrap an angle in radians between 0 and 2*PI.
Definition: utils.h:115
static constexpr N abs(const N a)
absolute of a number
Definition: utils.h:46
static constexpr float c_TWO_PI
2*PI constant
Definition: constants.h:41
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:151
Interface for the physical components of the main power output (eg: LED strip output).
Define some useful color palettes, and tools to use them.
Interface for the physical components of the microphone.
Interface of the indexable strip object. Only used for RGB lamp type.
Handle the analysis of a sound sample. This structure handled the FastFourrier analysis of a sound sa...
Definition: sound.h:34
Definition: helix_mask.hpp:41
Definition: helix_mask.hpp:212
Definition: lamp_type.hpp:59
float x
x 3D mm coordinates
Definition: lamp_type.hpp:60
float y
y 3D mm coordinates
Definition: lamp_type.hpp:61
float z
z 3D mm coordinates
Definition: lamp_type.hpp:62
Definition: lamp_type.hpp:48
uint16_t y
y 2D led grid coordinates
Definition: lamp_type.hpp:50
uint16_t x
x 2D led grid coordinates
Definition: lamp_type.hpp:49
Store a display config.
Definition: lamp_type.hpp:127
Main interface between the user and the hardware of the lamp.
Definition: lamp_type.hpp:120
void LMBD_INLINE fill(uint32_t color, uint16_t start, uint16_t end)
(indexable) Fill lamp with target color, from start to end
Definition: lamp_type.hpp:934
void align_internal_to_system_brightness()
Special call to update the internal brightness values to the system brightness.
Definition: lamp_type.hpp:704
void setColorsFromMixedBuffers(float phase)
Mix two colors from a buffer.
Definition: lamp_type.hpp:1115
static XYTy LMBD_INLINE fromStripIndexToXY(uint16_t N)
(indexable) Given a LED index, return a corresponding X,Y value
Definition: lamp_type.hpp:1060
brightness_t LMBD_INLINE getMaxBrightness() const
Return the actual allowed maximum brightness.
Definition: lamp_type.hpp:758
static constexpr float ledByMeter
number of led per meters of strip used
Definition: lamp_type.hpp:438
void LMBD_INLINE restoreBrightness()
Reset brightness to internal saved brightness, skip callbacks.
Definition: lamp_type.hpp:809
void LMBD_INLINE blur(uint8_t blurBy)
Blur currently displayed content by blurBy units /!\ Only applied along the strip,...
Definition: lamp_type.hpp:874
static constexpr uint16_t maxHeight
(indexable) Height of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:397
void align_internal_to_system_brightness(const brightness_t systemBrightness)
Update the internal brightness values to a new system brightness.
Definition: lamp_type.hpp:714
static constexpr float lampHeight_mm
Computation of the lamp height.
Definition: lamp_type.hpp:447
void LMBD_INLINE setBrightness(const brightness_t newBrightness, const bool skipCallbacks=true, const bool skipUpdateBrightness=false, const bool updateSavedBrightess=false)
Set brightness of the lamp.
Definition: lamp_type.hpp:607
void LMBD_INLINE fill(const colors::PaletteTy &palette)
(indexable) Fill lamp with target palette
Definition: lamp_type.hpp:986
static constexpr float maxHeightFloat
Height as a precise floating point number, equal to stripYCoordinates.
Definition: lamp_type.hpp:400
volatile const uint8_t currentMaskStep
If true, only a proportion of the leds will be available to control If 1, all leds are available,...
Definition: lamp_type.hpp:1211
volatile brightness_t temporary_brightness
Define a localy consistant temporary brightness.
Definition: lamp_type.hpp:1179
void LMBD_INLINE copyBuffer()
Copy a source buffer at srcBufIdx to another buffer at dstBufIdx.
Definition: lamp_type.hpp:1080
static constexpr float lampBodyCircumpherence_mm
Computation of the lamp body circumpherence.
Definition: lamp_type.hpp:443
void LMBD_INLINE restoreBrightnessNow()
Same as restoreBrightness(), but ignores the brightness update rate limit.
Definition: lamp_type.hpp:822
static constexpr float maxWidthFloat
Width as a precise floating point number, equal to stripXCoordinates.
Definition: lamp_type.hpp:379
BufferTy &LMBD_INLINE getTempBuffer()
Get a reference to the bufIdx temporary buffer.
Definition: lamp_type.hpp:1067
static constexpr float ledStripWidth_mm
real size of the led strip in use
Definition: lamp_type.hpp:433
static uint16_t LMBD_INLINE fromXYtoStripIndex(uint16_t X, uint16_t Y)
(indexable) Given X,Y coordinate, return a corresponding LED index
Definition: lamp_type.hpp:1054
static constexpr float ledPerTurns
Computation of the led count per turns.
Definition: lamp_type.hpp:445
void LMBD_INLINE tempBrightness(const brightness_t brightness)
Temporarily set brightness, without affecting brightness navigation.
Definition: lamp_type.hpp:771
void setColorsFromBufferReversed(bool skipLastLine)
(indexable) Display bufIdx temporary buffer, but reversed
Definition: lamp_type.hpp:1136
void enforce_internal_brightness_limits()
Follow the system brightness and enforce its limits, if they changed. Will call setBrightness.
Definition: lamp_type.hpp:728
void LMBD_INLINE jumpBrightness(const brightness_t brightness)
Jump to brightness, affecting the next brightness navigation.
Definition: lamp_type.hpp:789
void LMBD_INLINE end_blackout() const
Stop the strip blackout of a blip (indexable only, no-op otherwise)
Definition: lamp_type.hpp:545
LampConfig config
store a display configuration
Definition: lamp_type.hpp:131
volatile const uint32_t tick
(physical) Tick number, ever-increasing every frameDurationMs
Definition: lamp_type.hpp:1197
static constexpr float ledSize_mm
Number of leds per one turn of led strip.
Definition: lamp_type.hpp:441
void LMBD_INLINE mask_led_at_index(const size_t n, const bool shouldMask)
Mark a led to be masked at the next show.
Definition: lamp_type.hpp:993
volatile const uint32_t now
(physical) The "now" on milliseconds, updated just before loop.
Definition: lamp_type.hpp:1188
void LMBD_INLINE setPixelColorXY(uint16_t X, uint16_t Y, uint32_t color)
(indexable) Set the X,Y-th LED color
Definition: lamp_type.hpp:1048
void LMBD_INLINE setPixelColor(uint16_t n, uint32_t color)
(indexable) Set the n-th LED color
Definition: lamp_type.hpp:1014
void LMBD_INLINE fillTempBuffer(const T &value)
Fill temporary buffer bufIdx with given value.
Definition: lamp_type.hpp:1092
void LMBD_INLINE clear()
Clear lamp to a clean state.
Definition: lamp_type.hpp:496
uint32_t LMBD_INLINE getPixelColor(uint16_t n)
(indexable) Get the n-th LED color
Definition: lamp_type.hpp:1030
static constexpr uint16_t maxOverflowHeight
Larger height, taken as the absolute maximum Y coordinate, overflowing.
Definition: lamp_type.hpp:403
void LMBD_INLINE blip(const uint32_t duration)
blip the power gate for set time
Definition: lamp_type.hpp:519
void setColorsFromBuffer()
(indexable) Display bufIdx temporary buffer as LED colors
Definition: lamp_type.hpp:1101
volatile brightness_t saved_brightness
Define a localy consistant saved brightness. Should be similar.
Definition: lamp_type.hpp:1177
bool LMBD_INLINE is_bliping() const
return true if the output is in a blip state
Definition: lamp_type.hpp:556
static constexpr float ledStripLength_mm
Copy the led strip lenght.
Definition: lamp_type.hpp:435
static constexpr float _invShiftResidue
(while shifting every shiftPeriod, we have a residue accumulating)
Definition: lamp_type.hpp:412
void LMBD_INLINE cancel_step_mask()
Diasble the step mask.
Definition: lamp_type.hpp:577
void LMBD_INLINE apply_low_brightness_mask()
Apply the optional low level brightnes dithering.
Definition: lamp_type.hpp:905
static constexpr LMBD_INLINE uint16_t toLedCount(T scale)
(indexable) Return a led count in 0-ledCount scaled by scale
Definition: lamp_type.hpp:465
component::microphone::SoundStruct &LMBD_INLINE get_sound_struct()
Return the an object containing sound analysis data.
Definition: lamp_type.hpp:1171
volatile bool forceDisableLedDitherMask
set to true to lock led mask brightness control
Definition: lamp_type.hpp:1212
static constexpr brightness_t ledMasking_brightnessThreshold
below this, the system will enable the led masking
Definition: lamp_type.hpp:1214
static constexpr float lampBodyRadius_mm
real radius of the lamp body
Definition: lamp_type.hpp:430
static constexpr LampTypes flavor
Which lamp flavor is currently used by the implementation?
Definition: lamp_type.hpp:346
static constexpr uint32_t frameDurationMs
Hardward try to call .loop() every frameDurationMs (12ms for 83.3fps)
Definition: lamp_type.hpp:349
void LMBD_INLINE set_steps_mask(uint8_t newValue)
Set the value of the half step mask, from 0 to maxMaskStep.
Definition: lamp_type.hpp:561
void LMBD_INLINE fill(const colors::PaletteTy &palette, uint16_t start, uint16_t end)
(indexable) Fill lamp with target palette, from start to end
Definition: lamp_type.hpp:963
static constexpr uint16_t maxWidth
(indexable) Width of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:376
brightness_t LMBD_INLINE getBrightness(const bool readPreviousBrightness=false)
Get brightness of the lamp.
Definition: lamp_type.hpp:797
void LMBD_INLINE fill(uint32_t color)
(indexable) Fill lamp with target color
Definition: lamp_type.hpp:956
static constexpr uint16_t maxOverflowWidth
Larger width, taken as the absolute maximum X coordinate on all rows.
Definition: lamp_type.hpp:382
void getColorsToBuffer()
(indexable) Copy all current LEDs color to bufIdx temp. buffer
Definition: lamp_type.hpp:1158
static constexpr uint16_t ledCount
(indexable) Count of indexable LEDs on the lamp
Definition: lamp_type.hpp:363
volatile brightness_t last_system_brightness
Value of the system brightness when the local saved brightness last followed it.
Definition: lamp_type.hpp:1181
brightness_t LMBD_INLINE getSavedBrightness()
Alias for getBrightness(true)
Definition: lamp_type.hpp:806
static constexpr uint8_t nbBuffers
(indexable) Number of color buffers available for direct access
Definition: lamp_type.hpp:453
volatile const uint32_t raw_frame_count
(physical) Raw frame count, incremented at an undefined rate
Definition: lamp_type.hpp:1205
static constexpr uint32_t brightnessDurationMs
Limit mode-requested brightness changes to one every few frames.
Definition: lamp_type.hpp:357
void LMBD_INLINE cancel_blip() const
Cancel the ongoing blip, if any.
Definition: lamp_type.hpp:536
void LMBD_INLINE fadeToBlackBy(uint8_t fadeBy)
Fade currently displayed content by fadeBy units.
Definition: lamp_type.hpp:839
Define the system hardware constants.
Convertion header to use the text out functions from C code.
Define all time related platform functions.
Use this to convert color to bytes.
Definition: utils.h:128
User defined constants, relative to specific lamp types.
Define useful functions.