-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixManager.h
More file actions
555 lines (508 loc) · 16.6 KB
/
MatrixManager.h
File metadata and controls
555 lines (508 loc) · 16.6 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#pragma once
#include <stdint.h>
#include <iostream>
#include <algorithm>
#include <cmath>
/**
* Controls the matrix and provides methods to set pixels.
*/
class MatrixManager
{
public:
MatrixManager(uint32_t* pixels, bool inverse = false)
{
this->pixels = pixels;
this->inverse = inverse;
}
/**
* Set a specific pixel to a specific color.
* @param x x-coordinate of the pixel
* @param y y-coordinate of the pixel
* @param color color of the pixel
* @param ignoreOutOfRange (optional) if true, the function won't complain if the coordinates are out of range
*/
void set(int x, int y, uint32_t color, bool ignoreOutOfRange = false)
{
this->set(x, y, color >> 16, color >> 8 & 0xFF, color & 0xFF, ignoreOutOfRange);
}
/**
* Turn off a specific pixel.
* @param x x-coordinate of the pixel
* @param y y-coordinate of the pixel
*/
void off(int x, int y)
{
this->set(x, y, 0, 0, 0);
}
/**
* Set a specific pixel to a specific color.
* @param x x-coordinate of the pixel
* @param y y-coordinate of the pixel
* @param r red value of the pixel
* @param g green value of the pixel
* @param b blue value of the pixel
* @param ignoreOutOfRange (optional) if true, the function won't complain if the coordinates are out of range
*/
void set(const int x, const int y, const int r, const int g, const int b, const bool ignoreOutOfRange = false)
{
if (x < 0 || x > 11 || y < 0 || y > 11)
{
if (!ignoreOutOfRange)
{
std::cout << "Out of range" << std::endl;
}
return;
}
int pixel = this->calculate_strip_pixel(x, y);
pixels[pixel] = (r << 16) | (g << 8) | b;
}
/**
* Set a specific pixel to a specific color with raw access to the pixel position.
* Most likely you don't need this function.
* @param n pixel position
* @param color color of the pixel
*/
void set_string(int n, uint32_t color)
{
this->set_string(n, color >> 16, color >> 8 & 0xFF, color & 0xFF);
}
/**
* Set a specific pixel to a specific color with raw access to the pixel position.
* Most likely you don't need this function.
* @param n pixel position
* @param r red value of the pixel
* @param g green value of the pixel
* @param b blue value of the pixel
*/
void set_string(int n, int r, int g, int b)
{
if (n > 143)
{
std::cout << "Out of range" << std::endl;
return;
}
int pixel = n;
pixels[pixel] = (r << 16) | (g << 8) | b;
}
/**
* Fill the complete matrix with a specific color.
* @param color color of the pixels
*/
void fill(uint32_t color)
{
this->fill(color >> 16, color >> 8 & 0xFF, color & 0xFF);
}
/**
* Fill the complete matrix with a specific color.
* @param r red value of the pixels
* @param g green value of the pixels
* @param b blue value of the pixels
*/
void fill(int r, int g, int b)
{
for (int i = 0; i < 144; i++)
{
pixels[i] = (r << 16) | (g << 8) | b;
}
}
/**
* Clear the complete matrix. By turning all the pixels off.
*/
void clear()
{
for (int i = 0; i < 144; i++)
{
pixels[i] = 0;
}
}
/**
* Draw a line from (x1,y1) to (x2,y2) with a specific color.
* @param x1 x-coordinate of the start point
* @param y1 y-coordinate of the start point
* @param x2 x-coordinate of the end point
* @param y2 y-coordinate of the end point
* @param color color of the line
*/
void line(int x1, int y1, int x2, int y2, uint32_t color)
{
int dx = abs(x2 - x1), sx = x1 < x2 ? 1 : -1;
int dy = -abs(y2 - y1), sy = y1 < y2 ? 1 : -1;
int err = dx + dy, e2; /* error value e_xy */
for (;;)
{
this->set(x1, y1, color);
if (x1 == x2 && y1 == y2)
break;
e2 = 2 * err;
if (e2 >= dy)
{
err += dy;
x1 += sx;
} /* e_xy+e_x > 0 */
if (e2 <= dx)
{
err += dx;
y1 += sy;
} /* e_xy+e_y < 0 */
}
}
/**
* Draw a line from (x1,y1) to (x2,y2) with a specific color.
* @param x1 x-coordinate of the start point
* @param y1 y-coordinate of the start point
* @param x2 x-coordinate of the end point
* @param y2 y-coordinate of the end point
* @param r red value of the line
* @param g green value of the line
* @param b blue value of the line
*/
void line(int x1, int y1, int x2, int y2, int r, int g, int b)
{
this->line(x1, y1, x2, y2, Color(r, g, b));
}
/**
* Draw a number at a specific position with a specific color.
* The origin of all numbers is bottom left.
* You most likely won't fit more than three digits on the screen.
* Utilize for example scrolling to display more digits.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param n number to be drawn
* @param color color of the number
* @param gap (optional) gap between the digits
*/
void number(int x, int y, unsigned int n, uint32_t color, int gap = 1)
{
int amount_of_digits = this->count_digits(n);
int needed_space = amount_of_digits * 3 + (amount_of_digits - 1) * gap;
if ((needed_space + x) > 12)
{
std::cout << "Out of range" << std::endl;
return;
}
if (n == 0)
{
digit(x, y, n, color);
}
int pos = needed_space - 3;
while (n > 0)
{
digit(x + pos, y, n % 10, color);
n /= 10;
pos -= 3 + gap;
}
}
/**
* Draw a number at a specific position with a specific color.
* The origin of all numbers is bottom left.
* You most likely won't fit more than three digits on the screen.
* Utilize for example scrolling to display more digits.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param n number to be drawn
* @param r red value of the number
* @param g green value of the number
* @param b blue value of the number
* @param gap (optional) gap between the digits
*/
void number(int x, int y, unsigned int n, int r, int g, int b, int gap = 1)
{
this->number(x, y, n, Color(r, g, b), gap);
}
/**
* Draw a digit at a specific position with a specific color.
* The origin of all digits is bottom left.
* You most likely won't fit more than three digits on the screen.
* Utilize for example scrolling to display more digits.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param n number to be drawn
* @param r red value of the number
* @param g green value of the number
* @param b blue value of the number
*/
void digit(int x, int y, int n, int r, int g, int b)
{
this->digit(x, y, n, Color(r, g, b));
}
/**
* Draw a digit at a specific position with a specific color.
* The origin of all digits is bottom left.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param n number to be drawn
* @param color color of the number
*/
void digit(const int x, const int y, const int n, uint32_t color)
{
switch (n)
{
case 0:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 3, color);
this->number_segment(x, y, 4, color);
this->number_segment(x, y, 5, color);
break;
case 1:
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
break;
case 2:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 3, color);
this->number_segment(x, y, 4, color);
this->number_segment(x, y, 6, color);
break;
case 3:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 3, color);
this->number_segment(x, y, 6, color);
break;
case 4:
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 5, color);
this->number_segment(x, y, 6, color);
break;
case 5:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 3, color);
this->number_segment(x, y, 5, color);
this->number_segment(x, y, 6, color);
break;
case 6:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 3, color);
this->number_segment(x, y, 4, color);
this->number_segment(x, y, 5, color);
this->number_segment(x, y, 6, color);
break;
case 7:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
break;
case 8:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 3, color);
this->number_segment(x, y, 4, color);
this->number_segment(x, y, 5, color);
this->number_segment(x, y, 6, color);
break;
case 9:
this->number_segment(x, y, 0, color);
this->number_segment(x, y, 1, color);
this->number_segment(x, y, 2, color);
this->number_segment(x, y, 5, color);
this->number_segment(x, y, 6, color);
break;
default:
std::cout << "Out of range" << std::endl;
break;
}
}
/**
* Draw a number segment at a specific position with a specific color.
* You probably want to use the @see digit function instead.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param s number segment to be drawn
* @param color color of the number segment
*/
void number_segment(int x, int y, int s, uint32_t color)
{
switch (s)
{
case 0:
set(x, y + 4, color);
set(x + 1, y + 4, color);
set(x + 2, y + 4, color);
break;
case 1:
set(x + 2, y + 4, color);
set(x + 2, y + 3, color);
set(x + 2, y + 2, color);
break;
case 2:
set(x + 2, y + 2, color);
set(x + 2, y + 1, color);
set(x + 2, y, color);
break;
case 3:
set(x, y, color);
set(x + 1, y, color);
set(x + 2, y, color);
break;
case 4:
set(x, y, color);
set(x, y + 1, color);
set(x, y + 2, color);
break;
case 5:
set(x, y + 2, color);
set(x, y + 3, color);
set(x, y + 4, color);
break;
case 6:
set(x, y + 2, color);
set(x + 1, y + 2, color);
set(x + 2, y + 2, color);
break;
default:
std::cout << "Out of range" << std::endl;
break;
}
}
/**
* Draw a rectangle at a specific position with a specific color.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param width width of the rectangle
* @param height height of the rectangle
* @param color color of the rectangle
* @param filled (optional) if true, the rectangle will be filled
*/
void rect(int x, int y, int width, int height, uint32_t color, bool filled = false)
{
width--;
height--;
if (filled)
{
for (int i = 0; i < width; i++)
{
this->line(x + i, y, x + i, y + height, color);
}
for (int i = 0; i < height; i++)
{
this->line(x, y + i, x + width, y + i, color);
}
}
this->line(x, y, x + width, y, color);
this->line(x, y + height, x + width, y + height, color);
this->line(x, y, x, y + height, color);
this->line(x + width, y, x + width, y + height, color);
}
/**
* Draw a rectangle at a specific position with a specific color.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param width width of the rectangle
* @param height height of the rectangle
* @param r red value of the rectangle
* @param g green value of the rectangle
* @param b blue value of the rectangle
* @param filled (optional) if true, the rectangle will be filled
*/
void rect(int x, int y, int width, int height, int r, int g, int b, bool filled = false)
{
this->rect(x, y, width, height, Color(r, g, b), filled);
}
/**
* Draw a circle at a specific position with a specific color.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param radius radius of the circle
* @param color color of the circle
* @param filled (optional) if true, the circle will be filled
* @param u (optional) number of steps for the circle (smaller numbers means higher fidelity)
*/
void circle(int x, int y, int radius, uint32_t color, bool filled = true, int u = 2)
{
for (int i = 0; i < 360; i += u)
{
if (i == 0)
continue;
this->set(floor(cos(i) * radius + x), floor(sin(i) * radius + y), color, true);
if (filled)
{
// TODO
}
}
}
/**
* Draw a circle at a specific position with a specific color.
* @param x x-coordinate of the position
* @param y y-coordinate of the position
* @param radius radius of the circle
* @param r red value of the circle
* @param g green value of the circle
* @param b blue value of the circle
* @param filled (optional) if true, the circle will be filled
* @param u (optional) number of steps for the circle (smaller numbers means higher fidelity)
*/
void circle(int x, int y, int radius, int r, int g, int b, bool filled = false, int u = 2)
{
this->circle(x, y, radius, Color(r, g, b), filled, u);
}
/**
* Create a color from red, green and blue values.
* Useful for most helper functions.
* @param r red value
* @param g green value
* @param b blue value
*/
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b)
{
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
/**
* Set the Ticks per Second for each application.
* This will adjust the frequency of the game_loop function.
* The draw calls won't be affected.
* @param tps Ticks per second
*/
void set_tps(float tps)
{
if (tps > 200)
{
tps = 200;
}
if (tps < 0)
{
tps = 0;
}
this->currentTPS = tps;
}
/**
* Get the ticks per second for
* the current animation
* @return Ticks per Second
*/
float get_current_tps()
{
return currentTPS;
}
private:
uint32_t* pixels;
bool inverse = false;
float currentTPS = 0;
int calculate_strip_pixel(int x, int y)
{
return x +(11-y)*12;
if (inverse)
{
x = 11 - x; //FOR MATRIX BUILD ON 11.01.2025
}
if (x < 0 || x > 11 || y < 0 || y > 11)
{
std::cout << "Out of range" << std::endl;
return -1;
}
int bottom_row = floor((x + 1) / 2) * 23 + floor(x / 2);
int height_correction = -((x % 2) * 2 - 1) * y;
return bottom_row + height_correction;
}
unsigned count_digits(unsigned i)
{
return i > 0 ? (int)log10((double)i) + 1 : 1;
}
};