-
Notifications
You must be signed in to change notification settings - Fork 14
/
StripInvaders.ino
389 lines (323 loc) · 9.48 KB
/
StripInvaders.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
//StripInvader (c) 2011 Michael Vogt <[email protected]> // http://pixelinvaders.ch
//Network/OSC Enabled Strip Controller
//Needed 3rd Party Library:
// -https://github.com/neophob/EthernetBonjour (Bonjour, original code by gkaindl.com)
// -https://github.com/neophob/ArdOSC (AndOSC, origianl code by recotana.com)
// -https://github.com/neophob/WS2801-Library (WS2801, original code by adafruit.com)
// OR
// -https://github.com/neophob/LPD8806 (LPD8806, original code by adafruit.com)
#include <SPI.h>
#include <Ethernet.h>
#include <ArdOSC.h>
#include <EthernetBonjour.h>
#include <EEPROM.h>
//*************************/
// OSC Stuff
#define OSC_MSG_SET_R "/knbb" //simple method to fix the fancy strip color order
#define OSC_MSG_SET_G "/knbg"
#define OSC_MSG_SET_B "/knbr"
#define OSC_MSG_SET_DELAY "/delay"
#define OSC_MSG_CHANGE_MODE "/mode"
#define OSC_MSG_CHANGE_MODE_DIRECT "/modd"
#define OSC_MSG_AUDIO "/audio"
#define OSC_MSG_CONFIG "/cfg"
#define OSC_MSG_SAVE "/sav"
#define OSC_MSG_UPDATE_PIXEL "/pxl"
//*************************/
// define strip hardware, use only ONE hardware type
#define USE_WS2801 1
//#define USE_LPD8806 1
#ifdef USE_WS2801
#include <WS2801.h>
#endif
#ifdef USE_LPD8806
#include <LPD8806.h>
#endif
//*************************/
// Defines
//use serail debug or not
//#define USE_SERIAL_DEBUG 1
//use DHCP server OR static IP. Using DHCP and static ip as fallback is not possible, too less space left on arduino ethernet
#define USE_DHCP 1
//uncomment it to enable audio
//#define USE_AUDIO_INPUT 1
//some common color defines
const uint32_t WHITE_COLOR = 0xffffff;
//*************************/
// Fader
const uint8_t FADER_STEPS = 25;
uint8_t clearColR, clearColG, clearColB;
uint8_t oldR, oldG, oldB;
uint8_t faderSteps;
//*************************/
// WS2801
//how many pixels, I use 32 pixels/m
#define NR_OF_PIXELS 160
//EEPROM offset
#define EEPROM_HEADER_1 0
#define EEPROM_HEADER_2 1
#define EEPROM_HEADER_3 2
#define EEPROM_POS_DATA 3
#define EEPROM_POS_CLK 4
#define EEPROM_POS_COUNT 5
#define EEPROM_MAGIC_BYTE 66
#define EEPROM_HEADER_10 10
#define EEPROM_HEADER_11 11
#define EEPROM_HEADER_12 12
#define EEPROM_POS_MODE 13
#define EEPROM_POS_DELAY 14
#define EEPROM_POS_R 15
#define EEPROM_POS_G 16
#define EEPROM_POS_B 17
//output pixels dni:3/2
uint8_t dataPin = 3;
uint8_t clockPin = 2;
//dummy init the pixel lib
#ifdef USE_WS2801
WS2801 strip = WS2801();
#endif
#ifdef USE_LPD8806
LPD8806 strip = LPD8806();
#endif
//*************************/
// Network settings
#ifndef USE_DHCP
byte myIp[] = { 192, 168, 1, 111 };
#endif
byte myMac[] = { 0x00, 0x00, 0xAF, 0xFE, 0xBE, 0x01 };
const uint16_t serverPort = 10000;
OSCServer oscServer;
//*************************/
// Misc
#define MAX_NR_OF_MODES 16
#define MAX_SLEEP_TIME 160.0f
const uint8_t ledPin = 9;
uint8_t oscR, oscG, oscB;
uint8_t mode, modeSave;
int frames=0;
const byte CONST_I = 'I';
const byte CONST_N = 'N';
const byte CONST_V = 'V';
//update strip after DELAY
uint8_t DELAY = 20;
//current delay value
uint8_t delayTodo = 0;
//*************************/
// Audio input
#ifdef USE_AUDIO_INPUT
boolean isAudioVolumeEnabled;
float audioVol;
uint16_t maxVal;
#endif
//reset the arduino
void(* resetFunc) (void) = 0; //declare reset function @ address 0
/******************************************************************************************
* SETUP
*****************************************************************************************/
void setup(){
//init
oscR = 255;
oscG = 255;
oscB = 255;
mode=0;
int cnt = NR_OF_PIXELS;
#ifdef USE_SERIAL_DEBUG
Serial.begin(115200);
#ifdef USE_WS2801
Serial.println("INV2801!");
#endif
#ifdef USE_LPD8806
Serial.println("INV8806!");
#endif
#endif
//check if data/clk port is stored in the eeprom. First check for header INV
if (checkEepromSignature()) {
//read data and clk pin from the eeprom
dataPin = EEPROM.read(EEPROM_POS_DATA);
clockPin = EEPROM.read(EEPROM_POS_CLK);
cnt = EEPROMReadInt(EEPROM_POS_COUNT);
//just add some sanity check here, prevent out of memory
if (cnt<1 || cnt>1024) {
cnt = NR_OF_PIXELS;
}
}
#ifdef USE_SERIAL_DEBUG
Serial.print("D:");
Serial.print(dataPin, DEC);
Serial.print(" C:");
Serial.print(clockPin, DEC);
Serial.print("//");
Serial.println(cnt, DEC);
#endif
#ifdef USE_WS2801
strip = WS2801(cnt, dataPin, clockPin);
#endif
#ifdef USE_LPD8806
strip = LPD8806(cnt, dataPin, clockPin);
#endif
//start strips
strip.begin();
//DHCP, hint: we cannot use DHCP and manual IP together, out of space!
#ifdef USE_DHCP
//start Ethernet library using dhcp
if (Ethernet.begin(myMac) == 0) {
#ifdef USE_SERIAL_DEBUG
Serial.println("Failed to configure Ethernet using DHCP");
#endif
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
#else
//Manual IP
Ethernet.begin(myMac, myIp);
#endif
#ifdef USE_SERIAL_DEBUG
Serial.print("IP:");////32818
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
#endif
oscServer.begin(serverPort);
//set callback function
oscServer.addCallback(OSC_MSG_SET_R, &oscCallbackR); //PARAMETER: 1, float value 0..1
oscServer.addCallback(OSC_MSG_SET_G, &oscCallbackG); //PARAMETER: 1, float value 0..1
oscServer.addCallback(OSC_MSG_SET_B, &oscCallbackB); //PARAMETER: 1, float value 0..1
oscServer.addCallback(OSC_MSG_SET_DELAY, &oscCallbackDelay); //PARAMETER: 1, float value 0..1
oscServer.addCallback(OSC_MSG_CHANGE_MODE, &oscCallbackChangeMode); //PARAMETER: None, just a trigger
oscServer.addCallback(OSC_MSG_CHANGE_MODE_DIRECT, &oscCallbackChangeModeDirect); //PARAMETER: None, just a trigger
oscServer.addCallback(OSC_MSG_CONFIG, &oscCallbackConfig);
oscServer.addCallback(OSC_MSG_SAVE, &oscCallbackSavePreset);
oscServer.addCallback(OSC_MSG_UPDATE_PIXEL, &oscCallbackPixel); //PARAMETER: 2, int offset, 4xlong
#ifdef USE_AUDIO_INPUT
Serial.println("AU");
oscServer.addCallback(OSC_MSG_AUDIO, &oscCallbackAudio); //PARAMETER: 1, int value 0..1
#endif
//init effect
initMode();
pinMode(ledPin, OUTPUT);
//we-are-ready indicator
synchronousBlink();
// Initialize the Bonjour/MDNS library. You can now reach or ping this
// Arduino via the host name "Invader.local", provided that your operating
// system is Bonjour-enabled (such as MacOS X).
EthernetBonjour.begin("Invader");
//load presets
restorePresetStateFromEeprom();
// Now let's register the service we're offering (a web service) via Bonjour!
// With the service registered, it will show up in a Bonjour-enabled web
// browser. As an example, if you are using Apple's Safari, you will now see
// the service under Bookmarks -> Bonjour (Provided that you have enabled
// Bonjour in the "Bookmarks" preferences in Safari).
uint16_t ret = EthernetBonjour.addServiceRecord("Invader._osc", serverPort, MDNSServiceUDP);
if (ret==0) {
//error, bonjour service failed
}
}
/*****************************************************************************************
* LOOP
*****************************************************************************************/
void loop(){
// This actually runs the Bonjour module. YOU HAVE TO CALL THIS PERIODICALLY,
// OR NOTHING WILL WORK! Preferably, call it once per loop().
EthernetBonjour.run();
if (oscServer.aviableCheck()>0){
//we need to call available check to update the osc server
}
//check if the effect should be updated or not
if (delayTodo>0) {
//delay not finished yet - do not modify the strip but read network messages
delayTodo--;
delay(1);
}
else {
//delay finished, update the strip content
delayTodo=DELAY;
#ifdef USE_AUDIO_INPUT
loopAudioSensor();
#endif
switch (mode) {
case 0: //color lines
loopLines();
break;
case 1: //solid color white
case 2: //solid color wheel fader
case 3: //solid color random fader
loopSolid();
break;
case 4:
loopRainbow(); //color wheel aka rainbow
break;
case 5: //knight rider, 1 mover
case 6: //knight rider, 4 movers
case 7: //knight rider, 8 movers
case 8: //knight rider, block mode
loopKnightRider();
break;
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
loopXor(); //fader
break;
case 15:
loopHsb(); //fader
break;
//internal mode, fade from one color to another
case 200:
faderLoop();
break;
}
strip.show();
frames++;
}
}
/*****************************************************************************************
* INIT MODE
*****************************************************************************************/
void initMode() {
switch (mode) {
case 0:
setupLines();
break;
case 1:
case 2:
case 3:
setupSolid(mode-1);
break;
case 4:
setupRainbow();
break;
case 5:
setupKnightRider(strip.numPixels()/10, 1, 0);
break;
case 6:
setupKnightRider(strip.numPixels()/10, 4, 0);
break;
case 7:
setupKnightRider(2, 8, 0);
break;
case 8:
setupKnightRider(1, 0, 1);
break;
//save some bytes here, setupXor from (0) till (5)
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
setupXor(mode-9);
break;
// case 15:
}
#ifdef USE_SERIAL_DEBUG
Serial.print("M:");
Serial.println(mode);
#endif
}