-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathespixelflut.ino
434 lines (371 loc) · 10.1 KB
/
espixelflut.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
#define ARTNET
#define PIXELFLUT
#define DEBUG
//#define DEBUG_VERBOSE
#include <ESP8266WiFi.h>
#ifdef PIXELFLUT
#include <WiFiUdp.h>
#define UDP_PORT 1234
#endif
#include <EEPROM.h>
// https://github.com/Makuna/NeoPixelBus
//#include <NeoPixelBus.h>
#include <NeoPixelBrightnessBus.h>
#ifdef ARTNET
// https://github.com/hideakitai/ArtNet
#include <Artnet.h>
#endif
#define LED_DEBUG_IP
#define PIN_ROTARY1 5
#define PIN_ROTARY2 4
#define PIN_PUSHBUTTON 0
#define PIN_LEDS 3
#define ESSID "ACKspaceWifi"
#define WPA_PASS "nope"
#define TIMEOUT 10 * 60 * 1000 // 10 minutes
// Debug logging
#ifdef DEBUG_VERBOSE
#define log( _str ) Serial.print( _str )
#else
#define log( _str ) void( _str )
#endif
// Create udp interface
#ifdef PIXELFLUT
WiFiUDP Udp;
char g_incomingPacket[ 14 ];
#endif
// 12V string uses RGB
NeoPixelBrightnessBus<NeoRgbFeature, NeoEsp8266Dma800KbpsMethod> strip( 250 ); // use rx0/gpio3
//5V separate neopixels: NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip( 250 ); // use rx0/gpio3
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip( 250 ); // tx1/gpio2
//NeoPixelBus<NeoGrbFeature, NeoEsp8266AsyncUart800KbpsMethod> strip( 250 );// tx1/gpio2
NeoGamma<NeoGammaTableMethod> colorGamma;
volatile bool g_bFalling_Edge = false;
uint8_t g_leds = 0;
unsigned long g_time = 0;
bool g_bBlink = false;
#ifdef ARTNET
ArtnetReceiver artnet;
uint32_t startUniverse = 1;
#define LEDS_PER_UNIVERSE 150 // 170 max floor(512/3)
void callbackA(uint8_t* data, uint16_t size)
{
#ifdef DEBUG_VERBOSE
Serial.printf("Art-Net packet u: %d, s: %d\n", startUniverse, size );
#endif
if ( size > (3*LEDS_PER_UNIVERSE) )
size = (3*LEDS_PER_UNIVERSE);
if ( size > (3 * g_leds) )
size = (3 * g_leds);
size_t i = 0;
for (size_t j = 0; j < size; j++)
{
#ifdef DEBUG_VERBOSE
Serial.printf("%d: %02X %02X %02X\n", j, data[i], data[i+1], data[i+2]);
#endif
strip.SetPixelColor( j, RgbColor( data[i++], data[i++], data[i++] ) );
}
strip.Show();
g_time = millis();
}
void callbackB(uint8_t* data, uint16_t size)
{
#ifdef DEBUG_VERBOSE
Serial.printf("Art-Net packet u: %d, s: %d\n", startUniverse, size );
#endif
if ( g_leds < LEDS_PER_UNIVERSE )
return;
if ( size + (3 * LEDS_PER_UNIVERSE ) > ( 3 * g_leds ))
size = 3 * (g_leds - LEDS_PER_UNIVERSE);
size_t i = 0;
for (size_t j = 0; j < size; j++)
strip.SetPixelColor( j + ( 3 * LEDS_PER_UNIVERSE), RgbColor( data[i++], data[i++], data[i++] ) );
strip.Show();
g_time = millis();
}
#endif
///////////////////////////////////////////////////////////////////////////////
// setup
///////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin( 115200 );
log( F( "initializing..\n" ) );
// Prepare pins
pinMode( PIN_PUSHBUTTON, INPUT_PULLUP );
pinMode( PIN_ROTARY1, INPUT_PULLUP );
pinMode( PIN_ROTARY2, INPUT_PULLUP );
initializeEEPROM();
initializeLeds();
initializeNetwork();
// Delay so the user can press the rotary encoder
delay( 2000 );
int nPos;
if ( !digitalRead( PIN_PUSHBUTTON ) )
{
log( F( "config mode" ) );
// Config
while ( !digitalRead( PIN_PUSHBUTTON ) )
{
ESP.wdtFeed();
delay( 10 );
}
g_leds = 0;
// enable interrupt for rotary encoder
attachInterrupt( digitalPinToInterrupt( PIN_ROTARY1 ), button_ISR, FALLING );
while ( digitalRead( PIN_PUSHBUTTON ) )
{
if ( g_bFalling_Edge )
{
//sei
//noInterrupts();
//delayMicros( 10000 );
if ( digitalRead( PIN_ROTARY2 ) )
{
++g_leds;
if ( g_leds > 249 )
g_leds = 0;
log( String( g_leds, DEC ) + "\n" );
blinkled( g_leds );
}
else
{
--g_leds;
// Note that unsigned wraps around
if ( g_leds > 249 )
g_leds = 249;
log( String( g_leds, DEC ) + "\n" );
blinkled( g_leds );
}
delay( 20 );
//cli
//interrupts();
g_bFalling_Edge = false;
}
if ( g_time + 250 < millis() )
{
g_bBlink = !g_bBlink;
g_time = millis();
blinkled( g_leds );
}
ESP.wdtFeed();
}
detachInterrupt( digitalPinToInterrupt( PIN_ROTARY1 ) );
// Get last written value (wear leveling)
nPos = getFirstAvailableEepromAddress();
if ( !nPos || EEPROM.read( nPos - 1 ) != g_leds )
{
log( F( "writing " ) );
log( String( g_leds, DEC ) );
log( F( " @ " ) );
log( String( nPos, DEC ) );
EEPROM.write( nPos, g_leds );
#ifdef ESP8266
// Commit after write
EEPROM.commit();
#endif
}
log( F( "done\n" ) );
}
else
{
// Get last written value (wear leveling)
nPos = getFirstAvailableEepromAddress();
if ( nPos > 0 )
g_leds = EEPROM.read( nPos - 1 );
else
g_leds = 200;
log( F( "led count: " ) );
log( String( g_leds, DEC ) );
log( F( "\n" ) );
}
#ifdef ARTNET
log( F( "Art-Net enabled\n" ));
#endif
#ifdef PIXELFLUT
log( F( "pixelflut enabled\n" ));
#endif
}
///////////////////////////////////////////////////////////////////////////////
// loop
///////////////////////////////////////////////////////////////////////////////
void loop()
{
// TODO: Upon button press, show the IP address using the LEDs
if ( millis() - g_time > TIMEOUT )
{
log( F( "Packet timeout: resetting colors\n" ) );
strip.ClearTo( RgbColor( 0, 0, 0 ) );
strip.Show();
g_time = millis();
}
#ifdef ARTNET
// check for artnet packet and handle callback
artnet.parse();
#endif
#ifdef PIXELFLUT
int packetSize = Udp.parsePacket();
if (packetSize)
{
#ifdef DEBUG_VERBOSE
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
#endif
// Read packet (and leave space for a null character)
// ugly hack #sorryjoshua
if ( packetSize < 13 )
packetSize = 13;
for ( byte packets = 0; packets < packetSize / 13; ++packets )
{
int len = Udp.read( g_incomingPacket, 13 );
if ( len > 0 )
{
// Stringify (null-terminate)
g_incomingPacket[len] = 0;
#ifdef DEBUG_VERBOSE
Serial.printf("UDP packet contents: %s\n", g_incomingPacket);
#endif
byte num;
uint32_t color;
RgbColor rgbColor;
color = parseCommand( g_incomingPacket, len, num );
if ( color != -1 )
{
#ifdef DEBUG_VERBOSE
Serial.print( "\nLed:" );
Serial.println( num, DEC );
Serial.print( "Color:" );
Serial.println( color, HEX );
#endif
rgbColor = colorGamma.Correct( RgbColor((color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff ) );
strip.SetPixelColor( num, rgbColor );
g_time = millis();
}
else
{
log( "failed to parse packet" );
}
}
} // for
strip.Show();
}
#endif
}
///////////////////////////////////////////////////////////////////////////////
// button_ISR
///////////////////////////////////////////////////////////////////////////////
void button_ISR()
{
g_bFalling_Edge = true;
}
bool initializeNetwork()
{
WiFi.begin( ESSID, WPA_PASS );
log( F( "Wifi." ) );
while ( WiFi.status() != WL_CONNECTED )
{
delay(500);
log( F( "." ) );
ESP.wdtFeed();
}
log( F( "Connected. IP: " ) );
#ifdef DEBUG
Serial.println( WiFi.localIP() );
#endif
#ifdef PIXELFLUT
// Start the UDP server
Udp.begin( UDP_PORT );
#endif
#ifdef ARTNET
// Artnet initialization
artnet.begin();
artnet.subscribe( startUniverse, callbackA );
artnet.subscribe( startUniverse + 1, callbackB );
#endif
return true;
}
bool initializeLeds()
{
log( F( "LEDs: " ) );
strip.Begin();
strip.Show(); // Initialize all pixels to 'off'
// Set brighness to 2/3 to make sure our cables and fuse hold
strip.SetBrightness( 177 );
log( F( "done\n" ) );
return true;
}
bool initializeEEPROM()
{
log( F( "EEPROM:\n" ) );
#ifdef ESP8266
#define E2END 511
// Assign flash memory for EEPROM emulation
EEPROM.begin( E2END + 1 );
#endif
#ifdef DEBUG_VERBOSE
// Show EEPROM memory, note that we can't use EEPROM.length() yet
for (int i=0; i <= E2END; ++i )
{
if ( EEPROM.read( i ) < 16 )
log( F( "0" ) );
log( String( EEPROM.read( i ), HEX ) );
if ( ( i % 16 ) == 15 )
log( F( "\n" ) );
}
#endif
log( F( "done\n" ) );
return true;
}
///////////////////////////////////////////////////////////////////////////////
// parseCommand
///////////////////////////////////////////////////////////////////////////////
uint32_t parseCommand( char* _command, byte _length, byte& _led )
{
// PX <index> <RRGGBB>
//echo -n "PX 123 FFFFFF" | nc -uw0 192.168.2.86 1234
// Verify proper length
if ( _length < 11 || _length > 13 )
{
log( F( "Command: wrong length\n" ) );
return -1;
}
// Verify that the command starts with "PX "
if ( _command[0] != 'P' || _command[1] != 'X' || _command[2] !=' ' )
{
log( F( "Command: invalid 'header'\n" ) );
return -1;
}
// Parse the number
char* pCommand;
_led = strtoul( _command + 3, &pCommand, 10 );
// Verify that it is followed by a space
if ( *pCommand != ' ' )
{
log( F( "Command: expected space after led index\n" ) );
return -1;
}
// Convert the next HEX characters into a number and return it
return strtoul( pCommand + 1, NULL, 16 );
}
///////////////////////////////////////////////////////////////////////////////
// getFirstAvailableEepromAddress
///////////////////////////////////////////////////////////////////////////////
int getFirstAvailableEepromAddress()
{
int nPos = 0;
while ( EEPROM.read( nPos ) != 255 && nPos <= E2END )
++nPos;
// TODO: verify off by one error
if ( nPos > E2END )
return -1;
return nPos;
}
///////////////////////////////////////////////////////////////////////////////
// blinkLed
///////////////////////////////////////////////////////////////////////////////
void blinkled( byte _nLed )
{
strip.ClearTo( RgbColor( 0, 0, 0 ) );
if ( g_bBlink )
strip.SetPixelColor( _nLed, RgbColor( 255, 255, 0 ) );
strip.Show();
}