-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-neopixel-server.ino
466 lines (418 loc) · 11.9 KB
/
esp32-neopixel-server.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
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
#include <stdint.h>
#include <WiFi.h>
#include <Preferences.h>
#include <Adafruit_NeoPixel.h>
//
// config parameter
//
#define WS2812_PINS 13 // 14, 15, ...
#define WS2812_DEFAULT_COUNT 300
#define DIGITAL_PINS // 13, 14, 15, ...
#define DIGITAL_DEFAULT_STATE LOW
#define SECONDS_UNTIL_FALLBACK 10
const uint32_t bootColors[] = {
0x000000,
0xFF0000,
0x00FF00,
0x0000FF,
0xFFFF00,
0xFF00FF,
0x00FFFF,
};
#define BOOTCOLORS_COUNT (sizeof(bootColors) / sizeof(*bootColors))
// end config
WiFiUDP server;
Adafruit_NeoPixel pixels(WS2812_DEFAULT_COUNT, 1, NEO_GRB + NEO_KHZ800);
const uint8_t ws2812Pins[] = {WS2812_PINS};
#define WS2812_PIN_COUNT (sizeof(ws2812Pins) / sizeof(*ws2812Pins))
const uint8_t digitalPins[] = {DIGITAL_PINS};
#define DIGITAL_PIN_COUNT (sizeof(digitalPins) / sizeof(*digitalPins))
_Static_assert(DIGITAL_PIN_COUNT <= 16, "Too many digital output pins! Maximum 16 allowed.");
Preferences preferences;
#define PREFERENCE_WIFI_MODE "wifi-mode" // 0 == AP, 1 == STA
#define PREFERENCE_WIFI_SSID "wifi-ssid"
#define PREFERENCE_WIFI_PSK "wifi-psk"
#define PREFERENCE_LED_COUNT "led-count"
#define PREFERENCE_FALLBACK_INDEX "fallback-index"
#define PREFERENCE_FALLBACK_ARGUMENT "fallback-arg"
typedef void (*fallbackAnimation)(uint8_t *arg, size_t argLen, bool firstTime);
fallbackAnimation selectedFallback = NULL;
uint8_t fallbackArgLen = 0;
uint8_t fallbackArgs[256];
void doNothingAnimation(uint8_t *, size_t, bool);
void constColorAnimation(uint8_t *, size_t, bool);
void rainbowAnimation(uint8_t *, size_t, bool);
void tailAnimation(uint8_t *, size_t, bool);
void sparcleAnimation(uint8_t *, size_t, bool);
void randomSparcleAnimation(uint8_t *, size_t, bool);
void clockAnimation(uint8_t *, size_t, bool);
void swapAnimation(uint8_t *, size_t, bool);
fallbackAnimation fallbackAnimations[] = {
doNothingAnimation,
constColorAnimation,
rainbowAnimation,
tailAnimation,
sparcleAnimation,
randomSparcleAnimation,
clockAnimation,
swapAnimation,
};
#define FALLBACK_ANIMATION_COUNT (sizeof(fallbackAnimations) / sizeof(fallbackAnimation))
void setup()
{
preferences.begin("ws2812server", false);
pixels.setPin(ws2812Pins[0]);
uint16_t pixelCount = preferences.getUShort(PREFERENCE_LED_COUNT, WS2812_DEFAULT_COUNT);
if (pixelCount != WS2812_DEFAULT_COUNT)
pixels.updateLength(pixelCount);
pixels.begin();
pixels.clear();
for (int i = 0; i < BOOTCOLORS_COUNT; i++)
pixels.setPixelColor(i, bootColors[i]);
pixels.show();
for (int i = 0; i < DIGITAL_PIN_COUNT; i++)
{
pinMode(digitalPins[i], OUTPUT);
digitalWrite(digitalPins[i], DIGITAL_DEFAULT_STATE);
}
uint8_t wifiMode = preferences.getUChar(PREFERENCE_WIFI_MODE, 0);
if (wifiMode == 1)
{
String ssid = preferences.getString(PREFERENCE_WIFI_SSID, "DefaultWiFi");
String password = preferences.getString(PREFERENCE_WIFI_PSK, "");
const char *pw;
if (password == "")
pw = NULL;
else
pw = password.c_str();
WiFi.begin(ssid.c_str(), pw);
for (int i = 0; i < 100; i++)
{
if (WiFi.status() == WL_CONNECTED)
break;
for (int j = 0; j < BOOTCOLORS_COUNT; j++)
pixels.setPixelColor(j, bootColors[(i + j) % BOOTCOLORS_COUNT]);
pixels.show();
delay(100);
}
}
if (wifiMode == 0 || WiFi.status() != WL_CONNECTED)
{
uint32_t mac = ESP.getEfuseMac() >> 24;
char apSsid[64];
sprintf(apSsid, "NeopixelServer-%06X", mac);
WiFi.mode(WIFI_AP);
WiFi.softAP(apSsid, NULL);
}
server.begin(1337);
uint8_t index = preferences.getUChar(PREFERENCE_FALLBACK_INDEX, 2);
fallbackArgLen = preferences.getBytes(PREFERENCE_FALLBACK_ARGUMENT, fallbackArgs, 256);
selectedFallback = fallbackAnimations[index];
selectedFallback(fallbackArgs, fallbackArgLen, true);
}
uint16_t readUint16BE()
{
return (server.read() << 8) | server.read();
}
uint32_t readColor()
{
uint8_t red = server.read();
uint8_t green = server.read();
uint8_t blue = server.read();
return Adafruit_NeoPixel::Color(red, green, blue);
}
String readString()
{
char buff[256];
uint8_t len = server.read();
for (int i = 0; i < len; i++)
buff[i] = server.read();
buff[len] = 0;
return String(buff);
}
void sendLine(const char *msg)
{
server.beginPacket(); // will answer to ip/port last received a packet from
server.print(msg);
server.write('\n');
server.endPacket();
}
void sendOk()
{
sendLine("ok");
}
void loop()
{
static uint32_t lastPacket = 0;
static bool inFallbackMode = true;
if (inFallbackMode)
selectedFallback(fallbackArgs, fallbackArgLen, false);
uint32_t now = millis();
if (!inFallbackMode && selectedFallback && now - lastPacket > SECONDS_UNTIL_FALLBACK * 1000)
{
inFallbackMode = true;
selectedFallback(fallbackArgs, fallbackArgLen, true);
}
server.parsePacket();
while (server.available() > 0)
{
lastPacket = now;
inFallbackMode = false;
uint8_t packetType = server.read();
uint32_t color;
uint16_t offset;
uint16_t length;
switch (packetType)
{
case 0x00: // update
pixels.show();
sendOk();
break;
case 0x01: // set pixel color
offset = readUint16BE();
color = readColor();
pixels.setPixelColor(offset, color);
break;
case 0x02: // fill pixel range
offset = readUint16BE();
length = readUint16BE();
color = readColor();
pixels.fill(color, offset, length);
break;
case 0x03: // set multiple
offset = readUint16BE();
length = readUint16BE();
for (uint16_t i = offset; i < offset + length; i++)
pixels.setPixelColor(i, readColor());
break;
case 0x10: // set LED count
length = readUint16BE();
pixels.updateLength(length);
preferences.putUShort(PREFERENCE_LED_COUNT, length);
sendOk();
break;
case 0x11: // set LED Pin
offset = server.read();
if (offset < 0 || offset >= WS2812_PIN_COUNT)
{
sendLine("error: pin index out of range");
break;
}
pixels.setPin(ws2812Pins[offset]);
sendOk();
break;
case 0x12: // set fallback animation
offset = server.read();
length = server.read();
if (offset >= 0 && offset < FALLBACK_ANIMATION_COUNT && length <= 256)
{
selectedFallback = fallbackAnimations[offset];
fallbackArgLen = length;
for (int i = 0; i < length; i++)
fallbackArgs[i] = server.read();
preferences.putUShort(PREFERENCE_FALLBACK_INDEX, offset);
preferences.putBytes(PREFERENCE_FALLBACK_ARGUMENT, fallbackArgs, length);
inFallbackMode = true;
selectedFallback(fallbackArgs, fallbackArgLen, true);
sendOk();
}
break;
case 0x20: // set wifi mode
preferences.putUChar(PREFERENCE_WIFI_MODE, server.read() ? 0x01 : 0x00);
sendOk();
break;
case 0x21: // set wifi ssid
preferences.putString(PREFERENCE_WIFI_SSID, readString());
sendOk();
break;
case 0x22: // set wifi password
preferences.putString(PREFERENCE_WIFI_PSK, readString());
sendOk();
break;
case 0x30: // set digital output
offset = readUint16BE();
for (int i = 0; i < DIGITAL_PIN_COUNT; i++)
digitalWrite(digitalPins[i], (offset & (1 << i)) ? HIGH : LOW);
sendOk();
break;
case 0xff: // reboot
sendLine("rebooting...");
ESP.restart();
break;
}
}
delay(5);
}
void doNothingAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
}
void constColorAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
if (firstTime)
{
uint32_t color = 0;
if (argLen == 3)
color = Adafruit_NeoPixel::Color(args[0], args[1], args[2]);
pixels.fill(color, 0, pixels.numPixels());
pixels.show();
}
}
void colorWheel(int firstPixel, int lastPixel, int stripeSize)
{
double colorsteps = 255 / (stripeSize / 3);
for (int idx = 0; idx < stripeSize / 3; idx++)
{
pixels.setPixelColor(idx, 255 - idx * colorsteps, idx * colorsteps, 0); //area red to green
pixels.setPixelColor(idx + stripeSize / 3, 0, 255 - idx * colorsteps, idx * colorsteps); //area green to blue
pixels.setPixelColor(idx + stripeSize * 2 / 3, idx * colorsteps, 0, 255 - idx * colorsteps); //area blue to red
}
}
void rainbowAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
if (firstTime)
{
uint16_t length = pixels.numPixels();
pixels.fill(0, 0, length - 1); //stripe reset
colorWheel(0, length - 1, length);
}
else
{
// first pixel gets color of the last pixel
pixels.setPixelColor(0, pixels.getPixelColor(pixels.numPixels() - 1));
for (uint16_t idx = pixels.numPixels(); idx > 0; idx--)
{
// every pixel gets the color of its predecessor
pixels.setPixelColor(idx, pixels.getPixelColor(idx - 1));
}
}
pixels.show();
}
void tailAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
static int position = 0;
if (argLen != 5)
return;
uint32_t color = Adafruit_NeoPixel::Color(args[0], args[1], args[2]);
uint16_t size = (args[3] << 8) | args[4];
if (position >= pixels.numPixels())
{ // reset at end of stripe
firstTime = true;
position = 0;
}
if (firstTime)
{
pixels.fill(0, 0, pixels.numPixels() - 1); //stripe reset
for (int i = 0; i < size; i++)
{ //initializing the stripe
pixels.setPixelColor(i, color);
}
}
else
{
pixels.setPixelColor(position, 0, 0, 0); //switchoff first led of the tail
pixels.setPixelColor(position + size, color); // switch on led behind tail
position++;
}
pixels.show();
}
void sparcleAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
if (argLen != 3)
return;
uint32_t color = Adafruit_NeoPixel::Color(args[0], args[1], args[2]);
int on = random(0, pixels.numPixels());
int off = random(0, pixels.numPixels());
pixels.setPixelColor(on, color); // defined color random position
pixels.setPixelColor(off, 0);
pixels.show();
}
void randomSparcleAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
int on = random(0, pixels.numPixels());
int red = random(0, 256);
int blue = random(0, 256);
int green = random(0, 256);
pixels.setPixelColor(on, red, blue, green); // random color + random position
int off = random(0, pixels.numPixels());
pixels.setPixelColor(off, 0);
pixels.show();
}
void clockAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
static int counter = 0;
static int position = 0;
if (firstTime)
{
position = pixels.numPixels() - 1;
}
if (counter >= pixels.numPixels())
{
counter = 0;
pixels.fill(0, 0, pixels.numPixels() - 1);
}
if (argLen == 5)
{
uint32_t color = Adafruit_NeoPixel::Color(args[0], args[1], args[2]);
uint32_t speed = args[3]; // leds per function call
uint32_t width = args[4]; //with of running LEDs
if (speed < 1)
{
speed = 1;
}
if (width < 1)
{
width = 1;
}
if (position >= counter)
{
if (position - width >= counter)
{
pixels.fill(color, position - width, width); // switch on new sets of LED
}
else
{
pixels.fill(color, counter, position - width - counter);
}
pixels.fill(0, position, position + width);//switch off old set of LEDs
position=position-speed;
}
else // increases the counter after one cycle
{
pixels.fill(color, counter, width + counter);
counter=counter+width;
position = pixels.numPixels() - 1;
}
}
pixels.show();
}
void swapAnimation(uint8_t *args, size_t argLen, bool firstTime)
{
if (argLen == 4)
{
uint32_t color = Adafruit_NeoPixel::Color(args[0], args[1], args[2]);
int middle = (pixels.numPixels() - 1) / 2; //middle of stripe
int counter = 0; // counts function calling
uint8_t wave = args[3]; // amount of "waves until reset"
int milestones = 1; // how many resetwaves were performed
int border = (((pixels.numPixels() - 1) / 2) / wave) * milestones; // border for next blinkiblinki
if (firstTime)
{
counter = 0;
milestones = 1;
}
if (counter >= border) // reset counter when it hits the border and increases the bordersize
{
pixels.fill(color, 0, pixels.numPixels() - 1); // reset stripe
counter = 0;
milestones++;
}
if (milestones > wave) // reset the milestones when they are more than the waves
{
milestones = 1;
}
pixels.setPixelColor(middle + counter, color); // colourrizes the pixels left and right from middle
pixels.setPixelColor(middle - counter, color);
counter++;
pixels.show();
}
}