-
Notifications
You must be signed in to change notification settings - Fork 1
/
MadLed.ino
301 lines (247 loc) · 7.26 KB
/
MadLed.ino
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
#include <FastLED.h>
class LEDDevice {
public:
int numberOfLeds;
byte pin;
String name;
CRGB* leds;
void setLED(int led, int r, int g, int b)
{
CRGB ledcolor;
ledcolor.setRGB(r, g, b);
leds[led] = ledcolor;
}
CRGB getLED(int led)
{
return leds[led];
}
};
#define DP1 1
#define DP2 2
#define DP3 3
#define DP4 4
#define DP5 5
#define DP6 6
#define DP7 7
#define DP8 8
char rx_byte = 0;
String rx_str = "";
String tx_str = "";
bool showRequired = false;
LEDDevice ledDevices[8];
void setup() {
Serial.begin(19200);
Serial.println("MadLed by Mad Ninja / James Johnston 2020");
Serial.println("-----------------------------------------");
Serial.println("https://github.com/madninjaskillz/MadLed");
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void sprintln(String input)
{
tx_str = tx_str + input + "\r\n";
}
void sprint(String input)
{
tx_str = tx_str + input;
}
void processCommand(String input)
{
//sprintln(">"+input);
String cmd = getValue(rx_str, ':', 0);
if (cmd == "")
{
cmd = rx_str;
}
if (cmd == "ping")
{
sprintln("pong");
}
if (cmd == "ning")
{
sprintln("nong");
}
/*
if (cmd == "help") {
Serial.println("MadLed by Mad Ninja / James Johnston 2020");
Serial.println("-----------------------------------------");
Serial.println("https://github.com/madninjaskillz/MadLed");
Serial.println("add:{bank}:{datapin}:{ledCount}:[name]");
Serial.println("add a fan in bank [bank] on datapin [datapin] with [ledCount] leds called '[name]'");
Serial.println(" bank: bank/device number from 0 to 7");
Serial.println(" datapin: which arduino pin is the fan data line connected to (1 to 8)");
Serial.println(" ledCount: how many LEDs does the fan have?");
Serial.println(" name: Friendly name");
Serial.println("eg: add:0:3:21:Top Front");
Serial.println("");
Serial.println("set:[bank]:[led]:[Red]:[Green]:[Blue]");
Serial.println("set led [led] on device [bank] to R/G/B");
Serial.println(" bank: bank/device number from 0 to 7");
Serial.println(" led: led number");
Serial.println(" Red/Green/Blue: value from 0 to 255");
Serial.println("eg: set:0:15:255:0:255");
Serial.println(" ");
Serial.println("get:[bank]:[led]");
Serial.println("Get the rgb in format RRR:GGG:BBB of led [led] on device [bank]");
Serial.println(" bank: bank/device number from 0 to 7");
Serial.println(" led: led number");
Serial.println("eg: get:0:15");
}*/
if (cmd == "set")
{
String cmdBank = getValue(input, ':', 1);
String cmdLed = getValue(input, ':', 2);
String cmdR = getValue(input, ':', 3);
String cmdG = getValue(input, ':', 4);
String cmdB = getValue(input, ':', 5);
int intBank = cmdBank.toInt();
int intLed = cmdLed.toInt();
int intR = cmdR.toInt();
int intG = cmdG.toInt();
int intB = cmdB.toInt();
ledDevices[intBank].setLED(intLed, intR, intG, intB);
showRequired = true;
}
if (cmd == "bulk")
{
String cmdBank = getValue(input, ':', 1);
String cmdLed = getValue(input, ':', 2);
String cmdR = getValue(input, ':', 3);
String cmdG = getValue(input, ':', 4);
String cmdB = getValue(input, ':', 5);
String cmdLed1 = getValue(cmdLed, '-', 0);
String cmdLed2 = getValue(cmdLed, '-', 1);
int intBank = cmdBank.toInt();
int intLed1 = cmdLed1.toInt();
int intLed2 = cmdLed2.toInt();
int intR = cmdR.toInt();
int intG = cmdG.toInt();
int intB = cmdB.toInt();
for (int i = intLed1; i <= intLed2; i++)
{
ledDevices[intBank].setLED(i, intR, intG, intB);
}
showRequired = true;
}
if (cmd == "get")
{
String cmdBank = getValue(input, ':', 1);
String cmdLed = getValue(input, ':', 2);
int intBank = cmdBank.toInt();
int intLed = cmdLed.toInt();
CRGB cl = ledDevices[intBank].getLED(intLed);
sprint(cl.r + "");
sprint(":");
sprint(cl.g + "");
sprint(":");
sprintln(cl.b + "");
}
if (cmd == "add")
{
String cmdBank = getValue(input, ':', 1);
int intBank = cmdBank.toInt();
String cmdDataPin = getValue(input, ':', 2);
int intDataPin = cmdDataPin.toInt();
String cmdNoOfLed = getValue(input, ':', 3);
int intNoOfLed = cmdNoOfLed.toInt();
String cmdName = getValue(input, ':', 4);
ledDevices[intBank].name = cmdName;
ledDevices[intBank].numberOfLeds = intNoOfLed;
ledDevices[intBank].leds = new CRGB[intNoOfLed];
switch (intDataPin)
{
case 1:
FastLED.addLeds<NEOPIXEL, DP1>(ledDevices[intBank].leds, intNoOfLed);
break;
case 2:
FastLED.addLeds<NEOPIXEL, DP2>(ledDevices[intBank].leds, intNoOfLed);
break;
case 3:
FastLED.addLeds<NEOPIXEL, DP3>(ledDevices[intBank].leds, intNoOfLed);
break;
case 4:
FastLED.addLeds<NEOPIXEL, DP4>(ledDevices[intBank].leds, intNoOfLed);
break;
case 5:
FastLED.addLeds<NEOPIXEL, DP5>(ledDevices[intBank].leds, intNoOfLed);
break;
case 6:
FastLED.addLeds<NEOPIXEL, DP6>(ledDevices[intBank].leds, intNoOfLed);
break;
case 7:
FastLED.addLeds<NEOPIXEL, DP7>(ledDevices[intBank].leds, intNoOfLed);
break;
case 8:
FastLED.addLeds<NEOPIXEL, DP8>(ledDevices[intBank].leds, intNoOfLed);
break;
}
for (int f = 0; f < intNoOfLed; f++)
{
ledDevices[intBank].setLED(f, 255, 0, 0);
}
FastLED.show();
delay(500);
for (int f = 0; f < intNoOfLed; f++)
{
ledDevices[intBank].setLED(f, 0, 255, 0);
}
FastLED.show();
delay(500);
for (int f = 0; f < intNoOfLed; f++)
{
ledDevices[intBank].setLED(f, 0, 0, 255);
}
FastLED.show();
delay(500);
for (int f = 0; f < intNoOfLed; f++)
{
ledDevices[intBank].setLED(f, 0, 0, 0);
}
FastLED.show();
delay(500);
}
if (cmd == "devices")
{
for (int i = 0; i < 8; i++) {
sprint(i + "");
sprint(":");
sprint(ledDevices[i].numberOfLeds + "");
sprintln(":" + ledDevices[i].name);
}
}
}
int arrivingdatabyte = 0;
void loop()
{
if (Serial.available() > 0)
{
arrivingdatabyte = Serial.read( );
if (arrivingdatabyte == '\n' || arrivingdatabyte == '~') {
rx_str.trim();
processCommand(rx_str);
rx_str = "";
} else {
rx_str += (char)arrivingdatabyte;
}
}
if (tx_str.length() > 0) {
Serial.print(tx_str);
tx_str = "";
}
if (showRequired) {
FastLED.show();
showRequired = false;
}
}