Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
overlay.hpp
Go to the documentation of this file.
1
5#ifndef MODES_DRAW_OVERLAY_HPP
6#define MODES_DRAW_OVERLAY_HPP
7
8namespace lampda::modes::draw {
10namespace overlay {
11
13enum class ElementType : uint8_t
14{
15 NONE = 0,
16 RAMP,
17 DOT,
18};
19
20namespace __private {
21
23struct Color
24{
25 uint8_t red;
26 uint8_t green;
27 uint8_t blue;
28 uint32_t get_color() const { return (red << 16) | (green << 8) | blue; }
29};
30
35{
36 ElementType type = ElementType::NONE;
37 uint8_t progress;
38
39 uint16_t coordinateX;
40 uint16_t coordinateY;
41
43
44 int32_t timeout_ms = -1;
45};
46
47} // namespace __private
48
49template<int UIElementSize = 16, uint8_t freezeSize = 5, uint8_t nbBlackLines = 2> class Manager
50{
51public:
52 Manager() : activeUiElements(0) {}
53
54 Manager(const Manager&) = delete;
55 Manager& operator=(Manager const&) = delete;
56
60 void display_update(auto& ctx)
61 {
62 assert(activeUiElements < UIElementSize);
63
64 if (activeUiElements == 0)
65 return;
66
67 // clear overlay area first
68 for (uint8_t i = 0; i < ctx.lamp.maxWidth * nbBlackLines; ++i)
69 {
70 // mask leds in this area
71 ctx.lamp.mask_led_at_index(i, true);
72 ctx.lamp.setPixelColor(i, 0);
73 }
74
75 // keep track of the last active element index
76 size_t lastActiveElementIndex = 0;
77 const auto timeNow = ctx.lamp.now;
78 const size_t activeUiElementsToDisplay = activeUiElements;
79 // display all elements
80 for (uint8_t i = 0; i < activeUiElementsToDisplay; ++i)
81 {
82 const auto elementTimeout = elements[i].timeout_ms;
83 // auto deletion of element
84 if (elementTimeout > 0 and timeNow > elementTimeout)
85 {
86 // remove one active element
87 if (activeUiElements > 0)
88 --activeUiElements;
89 }
90 else
91 {
92 drawElement(elements[i], ctx);
93
94 // if an element before this was delete, shift this element index
95 if (i != lastActiveElementIndex)
96 {
97 // shift the saved elements
98 elements[lastActiveElementIndex] = elements[i];
99 }
100 ++lastActiveElementIndex;
101 }
102 }
103 }
104
106 void clear() { activeUiElements = 0; }
107
109 uint8_t get_element_count(const ElementType type) const
110 {
111 uint8_t cnt = 0;
112 for (uint8_t i = 0; i < activeUiElements; ++i)
113 {
114 // find elements of a type
115 if (elements[i].type == type)
116 {
117 cnt++;
118 }
119 }
120 return cnt;
121 }
122
133 bool add_ui_element(const auto& ctx,
134 const ElementType type,
135 const colors::PaletteTy& palette,
136 const uint16_t coordinateX = 0,
137 const uint16_t coordinateY = 0,
138 const uint8_t progress = 0,
139 const uint32_t activityDelay_ms = 50)
140 {
141 if (activeUiElements >= UIElementSize - 1)
142 return false;
143
144 // set the UI element
145 __private::UIElement& element = elements[activeUiElements];
146 element.type = type;
147 element.progress = progress;
148 element.coordinateX = coordinateX;
149 element.coordinateY = coordinateY;
150 element.color = palette;
151 // set optionnal auto destroy
152 if (activityDelay_ms > 0)
153 element.timeout_ms = ctx.lamp.now + activityDelay_ms;
154
155 // increase element count
156 activeUiElements++;
157 return true;
158 }
159
167 template<bool shouldUpdateTimeout = true>
168 bool update_type_progress(const auto& ctx, const ElementType type, uint16_t desiredIndex, const uint8_t progress)
169 {
170 size_t index;
171 if (get_N_of_type(type, desiredIndex, index))
172 {
173 elements[index].progress = progress;
174
175 // if requested, update timeout
176 const uint32_t newTimeout = ctx.lamp.now + freezeSize * ctx.lamp.frameDurationMs;
177 if (shouldUpdateTimeout and elements[index].timeout_ms <= newTimeout)
178 {
179 // add two frames of breathing room
180 elements[index].timeout_ms = newTimeout;
181 }
182 return true;
183 }
184 return false;
185 }
186
194 template<bool shouldUpdateTimeout = true> bool update_type_color(const auto& ctx,
195 const ElementType type,
196 uint16_t desiredIndex,
197 const colors::PaletteTy& palette)
198 {
199 size_t index;
200 if (get_N_of_type(type, desiredIndex, index))
201 {
202 elements[index].color = palette;
203
204 // if requested, update timeout
205 const uint32_t newTimeout = ctx.lamp.now + freezeSize * ctx.lamp.frameDurationMs;
206 if (shouldUpdateTimeout and elements[index].timeout_ms <= newTimeout)
207 {
208 // add two frames of breathing room
209 elements[index].timeout_ms = newTimeout;
210 }
211 return true;
212 }
213 return false;
214 }
215
216 bool update_type_timeout(const auto& ctx, const ElementType type, uint16_t desiredIndex, const uint32_t timeout_ms)
217 {
218 size_t index;
219 if (get_N_of_type(type, desiredIndex, index))
220 {
221 // add two frames of breathing room
222 elements[index].timeout_ms = ctx.lamp.now + timeout_ms;
223 return true;
224 }
225 return false;
226 }
227
236 template<bool shouldUpdateTimeout = true> bool update_type(const auto& ctx,
237 const ElementType type,
238 uint16_t desiredIndex,
239 const uint8_t progress,
240 const colors::PaletteTy& palette)
241 {
242 size_t index;
243 if (get_N_of_type(type, desiredIndex, index))
244 {
245 elements[index].progress = progress;
246 elements[index].color = palette;
247
248 // if requested, update timeout
249 const uint32_t newTimeout = ctx.lamp.now + freezeSize * ctx.lamp.frameDurationMs;
250 if (shouldUpdateTimeout and elements[index].timeout_ms <= newTimeout)
251 {
252 // add two frames of breathing room
253 elements[index].timeout_ms = newTimeout;
254 }
255 return true;
256 }
257 return false;
258 }
259
260protected:
262 bool get_N_of_type(const ElementType type, uint16_t desiredIndex, size_t& elementId) const
263 {
264 // display all elements
265 for (uint8_t i = 0; i < activeUiElements; ++i)
266 {
267 // find elements of a type
268 if (elements[i].type == type)
269 {
270 // check that we reached the desired element
271 if (desiredIndex <= 0)
272 {
273 elementId = i;
274 return true;
275 }
276 // or decrement the element count
277 desiredIndex -= 1;
278 }
279 }
280 return false;
281 }
282
283 void draw_pixel_xy_wrapper(auto& ctx, uint16_t X, uint16_t Y, uint32_t color) const
284 {
285 // always unmask overlay leds
286 ctx.lamp.mask_led_at_XY(X, Y, false);
287 // and set color below
288 ctx.lamp.setPixelColorXY(X, Y, color);
289 }
290
291 void drawElement(const __private::UIElement& e, auto& ctx) const
292 {
293 switch (e.type)
294 {
296 {
297 display_ramp(ctx, e.progress, e.color, e.coordinateY);
298 break;
299 }
300 case ElementType::DOT:
301 {
302 display_dot(ctx, e.progress, e.color, e.coordinateX, e.coordinateY);
303 break;
304 }
305 case ElementType::NONE:
306 {
307 break;
308 }
309
310 // NO DEFAULT: so we have errors on compile
311 }
312 }
313
315 void display_ramp(auto& ctx,
316 const uint8_t progress,
317 const colors::PaletteTy& palette,
318 const uint16_t startCoordinateY) const
319 {
320 const uint16_t rampScale = (progress / 255.0) * ctx.lamp.maxWidth;
321 for (uint16_t i = 0; i < rampScale; ++i)
322 {
323 const auto color = modes::colors::from_palette<false>(lmpd_map<uint8_t>(i, 0, rampScale, 0, 255), palette);
324 draw_pixel_xy_wrapper(ctx, i, startCoordinateY, color);
325 }
326 }
327
329 void display_dot(auto& ctx,
330 const uint8_t progress,
331 const colors::PaletteTy& palette,
332 const uint16_t coordinateX,
333 const uint16_t coordinateY) const
334 {
335 const auto color = modes::colors::from_palette<false>(progress, palette);
336 draw_pixel_xy_wrapper(ctx, coordinateX, coordinateY, color);
337 }
338
339private:
341 std::array<__private::UIElement, UIElementSize> elements;
343 size_t activeUiElements = 0;
344};
345
346} // namespace overlay
347} // namespace lampda::modes::draw
348
349#endif
Definition: overlay.hpp:50
void display_dot(auto &ctx, const uint8_t progress, const colors::PaletteTy &palette, const uint16_t coordinateX, const uint16_t coordinateY) const
Display a single colored pixel.
Definition: overlay.hpp:329
bool add_ui_element(const auto &ctx, const ElementType type, const colors::PaletteTy &palette, const uint16_t coordinateX=0, const uint16_t coordinateY=0, const uint8_t progress=0, const uint32_t activityDelay_ms=50)
Add a new UI element to the overlay.
Definition: overlay.hpp:133
void display_ramp(auto &ctx, const uint8_t progress, const colors::PaletteTy &palette, const uint16_t startCoordinateY) const
Display a colored ramp.
Definition: overlay.hpp:315
uint8_t get_element_count(const ElementType type) const
Return the count of elements of a target type.
Definition: overlay.hpp:109
bool get_N_of_type(const ElementType type, uint16_t desiredIndex, size_t &elementId) const
Find and return the Nth element of a given type.
Definition: overlay.hpp:262
void clear()
Clear all UI elements.
Definition: overlay.hpp:106
bool update_type_color(const auto &ctx, const ElementType type, uint16_t desiredIndex, const colors::PaletteTy &palette)
Update the target UI element color palette.
Definition: overlay.hpp:194
bool update_type(const auto &ctx, const ElementType type, uint16_t desiredIndex, const uint8_t progress, const colors::PaletteTy &palette)
Update the target UI element.
Definition: overlay.hpp:236
bool update_type_progress(const auto &ctx, const ElementType type, uint16_t desiredIndex, const uint8_t progress)
Update the target UI element progress.
Definition: overlay.hpp:168
void display_update(auto &ctx)
Display and update the overlay.
Definition: overlay.hpp:60
std::array< uint32_t, 16 > PaletteTy
Palette types.
Definition: palettes.hpp:18
ElementType
< Define an UI element type
Definition: overlay.hpp:14
< define a display color
Definition: overlay.hpp:24
Define an UI element.
Definition: overlay.hpp:35
uint16_t coordinateY
start Y coordinates
Definition: overlay.hpp:40
ElementType type
type of UI elements
Definition: overlay.hpp:36
colors::PaletteTy color
color palette to display
Definition: overlay.hpp:42
uint16_t coordinateX
start X coordinates
Definition: overlay.hpp:39
int32_t timeout_ms
If set to a positive number, this element will delete itself at the set time.
Definition: overlay.hpp:44
uint8_t progress
0-255 animation progress
Definition: overlay.hpp:37