/
illumi.ino
365 lines (304 loc) · 7.95 KB
/
illumi.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
/*
* Illumi Sketch (Arduino)
*
* Copyright 2012 - TBIdeas.com
* Distributed under the CC-BY-SA license.
* http://creativecommons.org/licenses/by-sa/3.0/
*
*/
/*
$$$
set uart baud 115200
set wlan ssid sarfata2
set wlan phrase PASSWORD
set ip address 0.0.0.0
set ip remote 80
set dns name io.sarfata.org
set ip proto 18
set com remote GET$/illumi/?
set option format 17
save
reboot
*/
#include <EEPROM.h>
// Edit your wifi settings in settings.h so that it is easy to pull/push
// on github without pushing those local settings.
#include "settings.h"
#include "Color.h"
#include "ColorLED.h"
// Define buffer sizes
#define BUFFER_SIZE 255
// If we have not received a command in the last 5 seconds, save the last command in the EEPROM
#define LASTCMD_WAIT_TIMER 5000
#define STARTUP_TIMER 2000
// The last command will take up at most 254 bytes.
// The first byte is used to tell the length so the next available byte is @ 0x100
#define EEPROM_LAST_CMD 0x0000
//#define ARDUINO_UNO
#ifdef ARDUINO_UNO
#define Serial1 Serial
#define DEBUG(msg) /* nothing */
#else
#define DEBUG(msg) Serial.write(msg)
#endif
unsigned long startTime;
unsigned long targetTime;
Color startColor;
Color currentColor;
Color targetColor;
#ifndef LED_SHUTDOWN
ColorLED led(RED, GREEN, BLUE, COMMON_ANODE);
#else
ColorLED led(RED, GREEN, BLUE, COMMON_ANODE, LED_SHUTDOWN);
#endif
void setup() {
int i;
led.setColor(currentColor);
#ifndef ARDUINO_UNO
// Arduino Uno
//Serial.setTimeout(1);
//Serial.begin(115200);
#endif
Serial1.setTimeout(10);
Serial1.begin(115200);
char *lastCommand = readLastCommand();
if (lastCommand != NULL) {
DEBUG("Reloading last command:");
DEBUG(lastCommand);
processCommand(lastCommand);
targetTime = millis() + STARTUP_TIMER;
}
// Use D2 as an instruction counter to measure the speed of transmission
pinMode(2, OUTPUT);
}
byte counter = 0;
char buffer[BUFFER_SIZE];
unsigned long lastCommandTime = 0;
char *lastCommandPtr;
void loop() {
updateLed();
processWifiInterface();
processDebugInterface();
}
void updateLed() {
if (targetTime != 0 && millis() < targetTime) {
currentColor = Color::interpolateColor(millis(), startTime, targetTime, startColor, targetColor);
}
else {
memcpy(¤tColor, &targetColor, sizeof(Color));
}
led.setColor(currentColor.red, currentColor.green, currentColor.blue);
}
#define REMOTE_TIMER 3000
unsigned long remoteTimer = millis();
void processWifiInterface()
{
if (Serial1.available()) {
int len = Serial1.readBytes(buffer, BUFFER_SIZE);
// Remove potential carriage return at the end of command.
if (buffer[len - 1] == '\n') {
len--;
}
// Apparently, those still exists too ...
if (buffer[len - 1] == '\r') {
len--;
}
buffer[len] = 0;
char *rgbCommand = strstr(buffer, "RGB");
if (rgbCommand != NULL) {
DEBUG("CMD ");
DEBUG(rgbCommand);
DEBUG('\n');
processCommand(rgbCommand);
digitalWrite(2, counter++ % 2 == 0 ? HIGH : LOW);
lastCommandTime = millis();
lastCommandPtr = rgbCommand;
}
else {
DEBUG("KO >");
DEBUG(buffer);
DEBUG('\n');
}
// reset the timer when the connection is closed
if (strstr(buffer, "*CLOS*")) {
remoteTimer = millis();
}
}
if (lastCommandTime != 0 && millis() > lastCommandTime + LASTCMD_WAIT_TIMER) {
DEBUG("Saving last command: ");
DEBUG(lastCommandPtr);
DEBUG('\n');
saveLastCommand(lastCommandPtr);
lastCommandTime = 0;
}
if (remoteTimer != 0 && millis() > remoteTimer + REMOTE_TIMER) {
remote_connect();
remoteTimer = 0;
}
}
#ifndef ARDUINO_UNO
void processDebugInterface()
{
char *last;
if (Serial.available()) {
char command = Serial.read();
switch (command) {
case '?':
Serial.println("Illumi / Woochi by TBideas\nUp and running!");
Serial.println(" x: Type and execute a command");
Serial.println(" w: Talk to Wifi module at 115200bps");
Serial.println(" W: Talk to Wifi module at 9600bps");
Serial.println(" o: Force connection to remote HTTP server");
Serial.println(" L: Get 'last command' saved in EEPROM");
Serial.println(" l: Time since last command");
Serial.println("");
break;
case 'L':
Serial.print("Last Command (EEPROM):\n");
last = readLastCommand();
if (last != NULL)
Serial.print(last);
else
Serial.print("NULL");
Serial.print('\n');
break;
case 'l':
if (lastCommandTime != 0) {
Serial.print("Last Command Time = ");
Serial.print(lastCommandTime);
Serial.print(" And millis() is: ");
Serial.print(millis());
Serial.print("\nLast Command buffer points to:");
Serial.print(lastCommandPtr);
Serial.print('\n');
}
else {
Serial.print("No last command.\n");
}
break;
//
case 'w':
wifly_configuration(115200);
break;
case 'W':
wifly_configuration(9600);
break;
case 'o':
remote_connect();
break;
case 'x':
readSerialCommand();
break;
default:
Serial.print("Unknown command: ");
Serial.print(command);
Serial.print("\n");
}
}
}
#else
void processDebugInterface() {}
#endif
void processCommand(char *command)
{
// The only command format supported right now is RGBXXXXXX\n
targetColor.red = hexPairsToInt(command + 3);
targetColor.green = hexPairsToInt(command + 5);
targetColor.blue = hexPairsToInt(command + 7);
startColor = currentColor;
startTime = millis();
// No animation for this command
targetTime = millis();
}
void saveLastCommand(char *cmd)
{
int i;
size_t len = strlen(cmd);
int address = EEPROM_LAST_CMD;
if (len <= 0xFE) { // max len is 0xFF including 1 byte for length
EEPROM.write(address++, (byte)len);
for (i = 0; i < len; i++)
{
EEPROM.write(address++, *cmd);
cmd++;
}
}
}
char *readLastCommand()
{
size_t len = EEPROM.read(EEPROM_LAST_CMD);
// If len is 0 there is nothing to read, and if len is 0xFF then the EEPROM
// has just never be written there.
if (len != 0 || len != 0xFF) {
int i;
for (i = 0; i < len; i++) {
buffer[i] = EEPROM.read(EEPROM_LAST_CMD + i + 1);
}
buffer[i] = 0;
return buffer;
}
else {
return NULL;
}
}
int hexPairsToInt(char *pair)
{
return (hexCharToInt(pair[0]) << 4) + hexCharToInt(pair[1]);
}
int hexCharToInt(char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0;
}
// Ask the wifi module to connect to a remote host
void remote_connect()
{
Serial1.print("$$$");
Serial1.flush();
delay(400);
Serial1.print("\r\nopen\r\n");
Serial1.flush();
}
void readSerialCommand()
{
Serial.println("Type a command and finish with \\n (Only command supported is RGBBXXXXXX");
int len = 0;
buffer[0] = 0;
while (len + 1 < BUFFER_SIZE) {
char c = Serial.read();
if (c > -1 && c != '\r') {
buffer[len++] = c;
Serial.print(c);
if (buffer[len - 1] == '\n')
break;
}
}
buffer[len] = 0;
Serial.print("Running command: ");
Serial.println(buffer);
processCommand(buffer);
}
void wifly_configuration(long speed)
{
Serial1.end();
Serial1.begin(speed);
Serial.print("You are now talking to the Wifly module at ");
Serial.print(speed);
Serial.println("bps.");
Serial.println("Use the RESET button to go back to normal.");
Serial.setTimeout(1);
Serial1.setTimeout(1);
int len;
while (1) {
if (Serial.available()) {
len = Serial.readBytes(buffer, BUFFER_SIZE - 1);
Serial1.write((uint8_t*)buffer, len);
}
if (Serial1.available()) {
len = Serial1.readBytes(buffer, BUFFER_SIZE - 1);
Serial.write((uint8_t*)buffer, len);
}
}
}