-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
210 lines (186 loc) · 5.98 KB
/
main.c
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
#include "ledmatrix.h"
const uint8_t row[ROWS] = { ROW1, ROW2, ROW3, ROW4, ROW5, ROW6 };
const uint8_t col[COLS] = { COL1, COL2, COL3, COL4, COL5, COL6 };
typedef struct {
uint8_t matrix[ROWS][COLS];
uint16_t frameDuration;
} frame;
typedef struct {
frame frames[12];
uint8_t framesNumber;
} animation;
uint8_t buff_matrix[ROWS][COLS] = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }
};
uint32_t prev_millis = 0;
char currentFrame = 0;
void drawMatrix();
void resetMatrix();
void activateColumn(uint8_t);
#pragma region animation frames
frame f0 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION };
frame f1 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION*2 };
frame f2 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION*2 };
frame f3 = { .matrix = {
{ 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION*2 };
frame f4 = { .matrix = {
{ 0, 1, 0, 1, 0, 0 },
{ 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION * 3 };
frame f5 = { .matrix = {
{ 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION * 2 };
frame f6 = { .matrix = {
{ 0, 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION };
frame f7 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION };
frame f8 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 0 },
{ 0, 1, 1, 1, 1, 1 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 0 } },
.frameDuration = FRAME_DURATION };
frame f9 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 } },
.frameDuration = FRAME_DURATION };
frame f10 = { .matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION };
frame f11 = { .matrix = {
{ 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = FRAME_DURATION };
#pragma endregion
frame frameProva = {
.matrix = {
{ 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1 } },
.frameDuration = 400,
};
frame frameProva2 = {
.matrix = {
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 } },
.frameDuration = 400,
};
animation anim = { .frames = { f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11 }, .framesNumber = 12 };
//animation anim = { .frames = { f1, frameProva2 }, .framesNumber = 1 };
void setup() {
for (uint8_t i = 0; i < ROWS; i++) {
pinMode(row[i], OUTPUT);
digitalWrite(row[i], LOW);
}
for (uint8_t i = 0; i < COLS; i++) {
pinMode(col[i], OUTPUT);
digitalWrite(col[i], HIGH);
}
}
void loop() {
if (millis() - prev_millis > anim.frames[currentFrame].frameDuration) {
prev_millis = millis();
currentFrame = currentFrame == anim.framesNumber - 1 ? 0 : currentFrame + 1;
}
drawMatrix();
}
void drawMatrix() {
//Draw one column at a time in PULSE intervals
for (int thisCol = 0; thisCol < COLS; thisCol++) {
resetMatrix();
// take the column pin (cathode) LOW:
activateColumn(col[thisCol]);
// draw the active rows in this column
for (int thisRow = 0; thisRow < ROWS; thisRow++) {
digitalWrite(row[thisRow], anim.frames[currentFrame].matrix[thisRow][thisCol]);
}
delay(PULSE);
}
}
void activateColumn(uint8_t column) {
//Turn off all the other columns while turning the desired one on(OFF=HIGH, ON=LOW)
for (int i = 0; i < COLS; i++) {
digitalWrite(col[i], !(column == col[i]));
}
}
void resetMatrix() {
for (uint8_t i = 0; i < COLS; i++)
digitalWrite(col[i], HIGH);
for (uint8_t i = 0; i < ROWS; i++)
digitalWrite(row[i], LOW);
}