-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDMatrix.ino
More file actions
190 lines (169 loc) · 4.59 KB
/
Copy pathLEDMatrix.ino
File metadata and controls
190 lines (169 loc) · 4.59 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
#include <FastLED.h>
#define LED_PIN 13
#define NUM_LEDS 84
#define MAX_FRAMES 10
#define X 7
#define Y 12
CRGB leds[NUM_LEDS];
byte displayMode = 0;
byte effectMode = 0;
int nextFrameDelay = 100;
char animationBuffer[MAX_FRAMES][NUM_LEDS];
int animationFrameCount = 0;
bool animationIsPlaying = false;
int animationCurrentFrame = 0;
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
playBootAnimation();
}
void playBootAnimation() {
for (int i = 0; i < NUM_LEDS; i++) {
fadeToBlackBy(leds, NUM_LEDS, 40);
leds[i] = CRGB(0, 255, 0);
FastLED.show();
delay(15);
}
for (int i = 0; i < 20; i++) {
fadeToBlackBy(leds, NUM_LEDS, 50);
FastLED.show();
delay(20);
}
fill_solid(leds, NUM_LEDS, CRGB(0, 255, 0));
FastLED.show();
delay(500);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0));
FastLED.show();
delay(300);
}
void convertAnimationFrameBuffer(int frameIndex){
for (int i = 0; i<NUM_LEDS; i++){
switch (animationBuffer[frameIndex][i]){
case '0':
leds[i] = CRGB(0,0,0);
break;
case '1':
leds[i] = CRGB(255,0,0);
break;
case '2':
leds[i] = CRGB(0,255,0);
break;
case '3':
leds[i] = CRGB(0,0,255);
break;
case '4':
leds[i] = CRGB(255,255,0);
break;
case '5':
leds[i] = CRGB(255,0,255);
break;
case '6':
leds[i] = CRGB(0,255,255);
break;
case '7':
leds[i] = CRGB(255,255,255);
break;
}
}
}
int coordinatesToLedAddress(int x, int y){
x++;y++;
int address = X*y;
if ((y%2)==0){address -=x;}
else {address-=X-x+1;}
return address;
}
void generateNextEffectFrame(){
}
void parseSerialInput(){
String input = Serial.readStringUntil('\n');
char buf[120];
input.toCharArray(buf, 120);
char* cmd = strtok(buf, " ");
char* a = strtok(NULL, " ");
char* b = strtok(NULL, " ");
char* c = strtok(NULL, " ");
char* d = strtok(NULL, " ");
char* e = strtok(NULL, " ");
int ia = a ? atoi(a) : 0;
int ib = b ? atoi(b) : 0;
int ic = c ? atoi(c) : 0;
int id = d ? atoi(d) : 0;
int ie = e ? atoi(e) : 0;
if (!cmd) return;
switch (cmd[0]){
case 's':
switch (cmd[1]){
case 'm':
displayMode = ia;
break;
case 's':
switch (cmd[2]){
case 'h':
break;
case 'f':
break;
}
break;
case 'b':
FastLED.setBrightness(ia);
break;
}
break;
case 'f':
fill_solid(leds, NUM_LEDS, CRGB(ia,ib,ic));
break;
case 'o':{
if (ia >= 0 && ia < X && ib >= 0 && ib < Y) {
int address = coordinatesToLedAddress(ia,ib);
leds[address] = CRGB(ic,id,ie);
}
break;
}
case 'e':
switch (cmd[1]){
case 'm':
effectMode = ia;
break;
case 's':
nextFrameDelay = ia;
break;
}
break;
case 'a':
switch (cmd[1]){
case 's':
if (ia <= 10){
animationFrameCount = ia;
}
break;
case 'f':
strcpy(animationBuffer[ia], b);
break;
case 'w':
nextFrameDelay = ia;
break;
case 'p':
animationIsPlaying = !animationIsPlaying;
break;
}
break;
case 'd':
FastLED.show();
break;
}
}
void loop() {
if (Serial.available()) {
parseSerialInput();
}
if (animationIsPlaying && displayMode == 2){
convertAnimationFrameBuffer(animationCurrentFrame);
FastLED.show();
delay(nextFrameDelay);
animationCurrentFrame++;
if (animationCurrentFrame >= animationFrameCount) {
animationCurrentFrame = 0;
}
}
}