-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeControl_StripNode.ino
324 lines (256 loc) · 6.7 KB
/
HomeControl_StripNode.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
//RFMInvader
//Serial/RFM12B Enabled LED Strip Controller
//Needed 3rd Party Library:
// - https://github.com/kroimon/Arduino-SerialCommand (Serial Command Library)
// - https://github.com/jwc/jeelib (JeeLib, RFM12B Library)
// - 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 <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"
//*************************/
// Config
//*************************/
// define strip hardware, use only ONE hardware type
#define USE_WS2801 1
//#define USE_LPD8806 1
//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 0
//uncomment it to enable audio
//#define USE_AUDIO_INPUT 1
//*************************/
// Other includes
//*************************/
//LED Hardware stuff
#ifdef USE_WS2801
#include <WS2801.h>
#endif
#ifdef USE_LPD8806
#include <LPD8806.h>
#endif
//Serial Command Library
#ifdef USE_SERIAL_DEBUG
#include <SerialCommand.h>
#endif
//*************************/
// Other Stuff
//*************************/
//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 31
//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 = 4;
uint8_t clockPin = 7;
//dummy init the pixel lib
#ifdef USE_WS2801
WS2801 strip = WS2801();
#endif
#ifdef USE_LPD8806
LPD8806 strip = LPD8806();
#endif
//init serial command lib
#ifdef USE_SERIAL_DEBUG
SerialCommand SCmd;
#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;
//*************************/
// 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;
//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;
Serial.begin(115200);
Serial.println("INV2801!");
//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;
}
}
Serial.print("DataPin: ");
Serial.print(dataPin, DEC);
Serial.print(" ClockPin: ");
Serial.print(clockPin, DEC);
Serial.print(" Count: ");
Serial.println(cnt, DEC);
strip = WS2801(cnt, dataPin, clockPin);
//start strips
strip.begin();
//init Serial Command Module
setupInSerial();
//init effect
initMode();
pinMode(ledPin, OUTPUT);
//we-are-ready indicator
synchronousBlink();
//load presets
restorePresetStateFromEeprom();
}
/*****************************************************************************************
* LOOP
*****************************************************************************************/
void loop(){
//check if serial data available
loopInSerial();
//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;
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("mode: ");
Serial.println(mode);
#endif
}