-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dmat.cpp
executable file
·124 lines (100 loc) · 2.37 KB
/
Dmat.cpp
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
/*
Dmat.cpp
Display library for 16x16 led matrix
By Emil
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Dmat.h"
#include <FastLED.h>
#include <math.h>
Dmat::Dmat(int W, int H, CRGB* leds){
_H = H;
_W = W;
_leds = leds;
_mat = new int*[_W];
for(int i = 0; i < _W; ++i)
_mat[i] = new int[_H];
}
void Dmat::dot(int x, int y, CHSV color){
if (checkDims(x, y)){
_mat[x][y] = color;
}else{
//Serial.println("Error, point out of display");
}
}
void Dmat::square(int x, int y, int width, int height, CHSV color){
for (int i = x; i < (x + width); i++){
dot(i, y, color);
dot(i, y + height, color);
}
for (int j = y; j <= (y + height); j++){
dot(x, j, color);
dot(x + width, j, color);
}
}
void Dmat::sphere(float x, float y, int radius, int intensity, CHSV color){
float distanza = 0;
int elevation = 0;
for (int i = x - radius; i <= ceil(x) + radius; i++){
for (int j = y - radius; j <= ceil(y) + radius; j++){
distanza = sqrt(pow(x - i, 2) + pow(y - j, 2));
elevation = color.v - distanza*intensity;
if (elevation > 255){
elevation = 255;
}
if (elevation < 0){
elevation = 0;
}
dot(i, j, CHSV(color.h, color.s, elevation));
}
}
}
void Dmat::squareFill(int x, int y, int width, int height, CHSV color){
for (int i = x; i < (x + width); i++){
for (int j = y; j < (y + height); j++){
dot(i, j, color);
}
}
}
void Dmat::drawLetter(char letter, CHSV color){
int index = int(letter) - 97;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 5; j++){
if (_alphabet[index][j][i]){
dot(i, j, color);
}
}
}
}
CRGB* Dmat::getColor(int x, int y){
return &_mat[x][y];
}
int Dmat::getIdx(int x, int y){
int idx = 0;
if (y % 2 == 0){
idx = ((_H - 1) - x) + (y * _H);
}else{
idx = x + (y * _H);
}
return idx;
}
bool Dmat::checkDims(int x, int y){
if (((x < _W) && (y < _H)) && ((x >= 0) && (y >= 0))){
return true;
}else{
return false;
}
}
void Dmat::show(){
for (int i = 0; i < _W; i++){
for (int j = 0; j < _H; j++){
CHSV pixel = _mat[i][j];
_leds[getIdx(i, j)].setHSV(pixel.h, pixel.s, pixel.v);
}
}
FasLED.show();
}