-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_clock.cpp
More file actions
365 lines (327 loc) · 10.4 KB
/
segment_clock.cpp
File metadata and controls
365 lines (327 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "segment_clock.h"
/**
* @brief Construct a new Segment Clock:: Segment Clock object
*
* @param ledstrip pointer to the LEDStrip object
* @param logger pointer to the UDPLogger object
*/
SegmentClock::SegmentClock(LEDStrip *ledstrip, UDPLogger *logger)
{
this->ledstrip = ledstrip;
this->logger = logger;
calcBackgroundIds();
speeds = new float[LED_COUNT];
phases = new float[LED_COUNT];
for (int i = 0; i < LED_COUNT; i++)
{
speeds[i] = 0.5 + random(100) / 100.0; // Speeds between 0.5 and 1.5
phases[i] = random(0, 628) / 100.0; // Phase offset between 0 and 2π
}
}
/**
* @brief Show the time on the clock
*
* @param hour hour to show
* @param minute minute to show
*/
void SegmentClock::setTime(uint8_t hour, uint8_t minute)
{
clearTempBackground();
uint32_t scaled_color_time = scaleColor(color_time, brightness_time);
// set time
if(hour < 10)
{
setSegment(ids_of_first_segment, NO_DIGIT, scaled_color_time);
}
else
{
setSegment(ids_of_first_segment, hour/10, scaled_color_time);
}
setSegment(ids_of_second_segment, hour%10, scaled_color_time);
setSegment(ids_of_third_segment, minute/10, scaled_color_time);
setSegment(ids_of_fourth_segment, minute%10, scaled_color_time);
// set points
ledstrip->setPixel(ids_of_points[0], scaled_color_time);
ledstrip->setPixel(ids_of_points[1], scaled_color_time);
}
/**
* @brief Set the color of the time
*/
void SegmentClock::setTimeColor(uint32_t color)
{
this->color_time = color;
}
/**
* @brief Set the brightness of the time
*/
void SegmentClock::setTimeBrightness(uint8_t brightness)
{
this->brightness_time = brightness;
}
/**
* @brief Set the background color of the clock
*/
void SegmentClock::setBackgroundColor(uint32_t color)
{
this->color_background = color;
}
/**
* @brief Set the brightness of the background of the clock
*/
void SegmentClock::setBackgroundBrightness(uint8_t brightness)
{
this->brightness_background = brightness;
}
/**
* @brief Set the speed of the background animation
*
* @param speed speed of the background animation
*/
void SegmentClock::setBackgroundAnimationSpeed(float speed)
{
this->animation_speed = speed;
}
/**
* @brief Get the brightness of the time
*/
uint8_t SegmentClock::getBrightnessTime()
{
return brightness_time;
}
/**
* @brief Get the brightness of the background
*/
uint8_t SegmentClock::getBrightnessBackground()
{
return brightness_background;
}
/**
* @brief Get the speed of the background animation
*/
float SegmentClock::getBackgroundAnimationSpeed()
{
return animation_speed;
}
/**
* @brief Randomize the background of the clock
*/
void SegmentClock::randomizeBackground()
{
background_time += 0.02 * animation_speed;
for (int i = 0; i < num_background_leds; i++)
{
// Smooth brightness using sine wave animation
float brightnessFactor = (sin(background_time * speeds[ids_of_background[i]] + phases[ids_of_background[i]]) + 1.0) / 2.0;
uint8_t brightness = brightnessFactor * brightness_background;
uint32_t color = scaleColor(color_background, brightness);
ledstrip->setPixel(ids_of_background[i], color);
}
for (int i = 0; i < num_temp_background_leds; i++)
{
// Smooth brightness using sine wave animation
float brightnessFactor = (sin(background_time * speeds[ids_of_background_temp[i]] + phases[ids_of_background_temp[i]]) + 1.0) / 2.0;
uint8_t brightness = brightnessFactor * brightness_background;
uint32_t color = scaleColor(color_background, brightness);
ledstrip->setPixel(ids_of_background_temp[i], color);
}
}
/**
* @brief Calculate the ids of the background LEDs
*/
void SegmentClock::calcBackgroundIds()
{
int id = 0;
for(int led = 0; led < LED_COUNT; led++)
{
bool is_background = true;
for(int k = 0; k < 7; k++)
{
if(ids_of_first_segment[k] == led){
is_background = false;
break;
}
if(ids_of_second_segment[k] == led){
is_background = false;
break;
}
if(ids_of_third_segment[k] == led){
is_background = false;
break;
}
if(ids_of_fourth_segment[k] == led){
is_background = false;
break;
}
}
if(ids_of_points[0] == led){
is_background = false;
}
if(ids_of_points[1] == led){
is_background = false;
}
if(is_background){
ids_of_background[id] = led;
id++;
}
}
}
/**
* @brief Draw a digit to a segment of the clock
*
* @param segment_ids ids of the segment leds
* @param digit digit to show
* @param color_time color of the segment
* @param color_background color of the background
*/
void SegmentClock::setSegment(const uint8_t *segment_ids, uint8_t digit, uint32_t color_time)
{
switch (digit)
{
case 0:
ledstrip->setPixel(segment_ids[0], color_time);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
ledstrip->setPixel(segment_ids[3], color_time);
ledstrip->setPixel(segment_ids[4], color_time);
ledstrip->setPixel(segment_ids[5], color_time);
addTempBackground(segment_ids[6]);
break;
case 1:
addTempBackground(segment_ids[0]);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
addTempBackground(segment_ids[3]);
addTempBackground(segment_ids[4]);
addTempBackground(segment_ids[5]);
addTempBackground(segment_ids[6]);
break;
case 2:
ledstrip->setPixel(segment_ids[0], color_time);
ledstrip->setPixel(segment_ids[1], color_time);
addTempBackground(segment_ids[2]);
ledstrip->setPixel(segment_ids[3], color_time);
ledstrip->setPixel(segment_ids[4], color_time);
addTempBackground(segment_ids[5]);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case 3:
ledstrip->setPixel(segment_ids[0], color_time);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
ledstrip->setPixel(segment_ids[3], color_time);
addTempBackground(segment_ids[4]);
addTempBackground(segment_ids[5]);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case 4:
addTempBackground(segment_ids[0]);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
addTempBackground(segment_ids[3]);
addTempBackground(segment_ids[4]);
ledstrip->setPixel(segment_ids[5], color_time);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case 5:
ledstrip->setPixel(segment_ids[0], color_time);
addTempBackground(segment_ids[1]);
ledstrip->setPixel(segment_ids[2], color_time);
ledstrip->setPixel(segment_ids[3], color_time);
addTempBackground(segment_ids[4]);
ledstrip->setPixel(segment_ids[5], color_time);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case 6:
ledstrip->setPixel(segment_ids[0], color_time);
addTempBackground(segment_ids[1]);
ledstrip->setPixel(segment_ids[2], color_time);
ledstrip->setPixel(segment_ids[3], color_time);
ledstrip->setPixel(segment_ids[4], color_time);
ledstrip->setPixel(segment_ids[5], color_time);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case 7:
ledstrip->setPixel(segment_ids[0], color_time);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
addTempBackground(segment_ids[3]);
addTempBackground(segment_ids[4]);
addTempBackground(segment_ids[5]);
addTempBackground(segment_ids[6]);
break;
case 8:
ledstrip->setPixel(segment_ids[0], color_time);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
ledstrip->setPixel(segment_ids[3], color_time);
ledstrip->setPixel(segment_ids[4], color_time);
ledstrip->setPixel(segment_ids[5], color_time);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case 9:
ledstrip->setPixel(segment_ids[0], color_time);
ledstrip->setPixel(segment_ids[1], color_time);
ledstrip->setPixel(segment_ids[2], color_time);
ledstrip->setPixel(segment_ids[3], color_time);
addTempBackground(segment_ids[4]);
ledstrip->setPixel(segment_ids[5], color_time);
ledstrip->setPixel(segment_ids[6], color_time);
break;
case NO_DIGIT:
addTempBackground(segment_ids[0]);
addTempBackground(segment_ids[1]);
addTempBackground(segment_ids[2]);
addTempBackground(segment_ids[3]);
addTempBackground(segment_ids[4]);
addTempBackground(segment_ids[5]);
addTempBackground(segment_ids[6]);
break;
default:
break;
}
}
/**
* @brief Set the background pixels of the clock
*/
void SegmentClock::setBackground(uint32_t color_background)
{
for(int i = 0; i < LED_COUNT - 30; i++)
{
ledstrip->setPixel(ids_of_background[i], color_background);
}
}
/**
* @brief Scale a color with a brightness value
*
* @param color color to scale
* @param brightness brightness value
*
* @return scaled color
*/
uint32_t SegmentClock::scaleColor(uint32_t color, uint8_t brightness)
{
uint8_t red = color >> 16 & 0xff;
uint8_t green = color >> 8 & 0xff;
uint8_t blue = color & 0xff;
red = red * brightness / 255;
green = green * brightness / 255;
blue = blue * brightness / 255;
return LEDStrip::Color24bit(red, green, blue);
}
/**
* @brief Clear the the array of temporary background pixels
*/
void SegmentClock::clearTempBackground()
{
num_temp_background_leds = 0;
}
/**
* @brief Add a temporary background pixel to the array
*
* @param id id of the pixel
*/
void SegmentClock::addTempBackground(uint8_t id)
{
ids_of_background_temp[num_temp_background_leds] = id;
num_temp_background_leds++;
}