-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArduinoSer2FastLED.ino
437 lines (373 loc) · 10.7 KB
/
ArduinoSer2FastLED.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
/**
* Firmware to control a LED matrix display
* https://github.com/noahwilliamsson/lamatrix
*
* -- [email protected], 2018
*
*/
#ifdef TEENSYDUINO
#include <TimeLib.h>
#endif
#include "FastLED.h"
#define HOST_SHUTDOWN_PIN 8
#define LEFT_BUTTON_PIN 9
#define RIGHT_BUTTON_PIN 10
#define NUM_LEDS 256
#ifdef TEENSYDUINO
#define FastLED_Pin 6
#else
#define FastLED_Pin 22
#endif
static void put_pixel(int, int, int);
static void render_clock(int);
#ifdef TEENSYDUINO
static time_t getTeensy3Time();
#endif
/**
* Serial protocol
*/
enum {
FUNC_RESET = 0,
/* Initialize display with [pixels & 0xff, (pixels>>8) & 0xff] LEDs */
FUNC_INIT_DISPLAY = 'i',
/* Clear display: [dummy byte] */
FUNC_CLEAR_DISPLAY = 'c',
/* Update display: [dummy byte] */
FUNC_SHOW_DISPLAY = 's',
/* Put pixel at [pixel&0ff, (pixel >> 8) &0xff, R, G, B] */
FUNC_PUT_PIXEL = 'l',
/* Set time [t&0xff, (t >> 8) & 0xff, (t >> 16) & 0xff, (t >> 24) & 0xff] */
FUNC_SET_RTC = '@',
/* Automatically render time [enable/toggle byte] */
FUNC_AUTO_TIME = 't',
/* Suspend host for [seconds & 0xff, (seconds >> 8) & 0xff] */
FUNC_SUSPEND_HOST = 'S',
};
/* Computed with pixelfont.py */
static int font_width = 4;
static int font_height = 5;
static char font_alphabet[] = " %'-./0123456789:?acdefgiklmnoprstwxy";
static unsigned char font_data[] = "\x00\x00\x50\x24\x51\x66\x00\x00\x60\x00\x00\x00\x42\x24\x11\x57\x55\x27\x23\x72\x47\x17\x77\x64\x74\x55\x47\x74\x71\x74\x17\x57\x77\x44\x44\x57\x57\x77\x75\x74\x20\x20\x30\x24\x20\x52\x57\x25\x15\x25\x53\x55\x73\x31\x71\x17\x13\x71\x71\x75\x27\x22\x57\x35\x55\x11\x11\x57\x77\x55\x75\x77\x75\x55\x75\x57\x17\x71\x35\x55\x17\x47\x77\x22\x22\x55\x77\x55\x25\x55\x55\x27\x02";
/* Global states */
int state = 0;
int debug_serial = 0;
/* Debug state issues */
int last_states[8];
unsigned int last_state_counter = 0;
/* Non-zero when automatically rendering the current time */
int show_time = 1;
/* Non-zero while the host computer is turned off */
time_t reboot_at = 0;
/* Accumulator register for use between loop() calls */
unsigned int acc;
unsigned int color;
CRGB leds[NUM_LEDS];
static volatile int g_button_state;
static int button_down_t;
static void button_irq_left(void) {
int state = digitalRead(LEFT_BUTTON_PIN);
if(state == HIGH) {
/* Start counting when the circuit is broken */
button_down_t = millis();
return;
}
if(!button_down_t)
return;
int pressed_for_ms = millis() - button_down_t;
if(pressed_for_ms > 1500)
g_button_state = 4;
else if(pressed_for_ms > 500)
g_button_state = 2;
else if(pressed_for_ms > 100)
g_button_state = 1;
button_down_t = 0;
}
static void button_irq_right(void) {
int state = digitalRead(RIGHT_BUTTON_PIN);
if(state == HIGH) {
/* Start counting when the circuit is broken */
button_down_t = millis();
return;
}
if(!button_down_t)
return;
int pressed_for_ms = millis() - button_down_t;
if(pressed_for_ms > 1500)
g_button_state = 64;
else if(pressed_for_ms > 500)
g_button_state = 32;
else if(pressed_for_ms > 100)
g_button_state = 16;
button_down_t = 0;
}
void setup() {
Serial.begin(460800);
/* Initialize FastLED library */
FastLED.addLeds<NEOPIXEL, FastLED_Pin>(leds, NUM_LEDS);
/* Configure pin used to shutdown Raspberry Pi (connected to GPIO5 on the Pi) */
pinMode(HOST_SHUTDOWN_PIN, OUTPUT);
digitalWrite(HOST_SHUTDOWN_PIN, HIGH);
/* Configure pins for the buttons */
pinMode(LEFT_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(LEFT_BUTTON_PIN), button_irq_left, CHANGE);
pinMode(RIGHT_BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RIGHT_BUTTON_PIN), button_irq_right, CHANGE);
#ifdef TEENSYDUINO
/* Initialize time library */
setSyncProvider(getTeensy3Time);
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
}
else {
Serial.println("RTC has set the system time");
show_time = 1;
}
Serial.printf("%04d-%02d-%02dT%02d:%02d:%02dZ\n", year(), month(), day(), hour(), minute(), second());
#endif
}
void loop() {
#ifdef TEENSYDUINO
time_t now = getTeensy3Time();
#else
int now = 0;
#endif
int button_state = g_button_state;
if(button_state) {
g_button_state = 0;
if(button_state & 1)
Serial.println("LEFT_SHRT_PRESS");
else if(button_state & 2)
Serial.println("LEFT_LONG_PRESS");
else if(button_state & 4)
Serial.println("LEFT_HOLD_PRESS");
if(button_state & 16)
Serial.println("RGHT_SHRT_PRESS");
else if(button_state & 32)
Serial.println("RGHT_LONG_PRESS");
else if(button_state & 64)
Serial.println("RGHT_HOLD_PRESS");
}
if(reboot_at && now >= reboot_at) {
/* Restart host computer */
digitalWrite(HOST_SHUTDOWN_PIN, LOW);
delay(1);
digitalWrite(HOST_SHUTDOWN_PIN, HIGH);
reboot_at = 0;
}
if(show_time) {
/* Automatically render time */
if(show_time != now || button_state) {
render_clock(button_state);
show_time = now;
}
}
if (Serial.available() <= 0) return;
int val = Serial.read();
last_states[last_state_counter++ % (sizeof(last_states)/sizeof(last_states[0]))] = val;
switch(state) {
case FUNC_RESET:
/**
* Pyserial sometimes experience write timeouts so we
* use a string of zeroes to resynchronize the state.
*/
state = val;
break;
case FUNC_INIT_DISPLAY:
acc = val;
state++;
break;
case FUNC_INIT_DISPLAY+1:
acc |= val << 8;
FastLED.addLeds<NEOPIXEL, FastLED_Pin>(leds, acc);
/* fall through */
case FUNC_SET_RTC:
acc = val;
state++;
break;
case FUNC_SET_RTC+1:
acc |= val << 8;
state++;
break;
case FUNC_SET_RTC+2:
acc |= val << 16;
state++;
break;
case FUNC_SET_RTC+3:
acc |= val << 24;
#ifdef TEENSYDUINO
Teensy3Clock.set(acc); // set the RTC
setTime(acc);
Serial.printf("RTC synchronized: %04d-%02d-%02dT%02d:%02d:%02dZ\n", year(), month(), day(), hour(), minute(), second());
#endif
state = FUNC_RESET;
break;
case FUNC_CLEAR_DISPLAY:
for(int i = 0; i < NUM_LEDS; i++)
leds[i].setRGB(0,0,0);
/* fall through */
case FUNC_SHOW_DISPLAY:
FastLED.show();
state = FUNC_RESET;
break;
case FUNC_SUSPEND_HOST:
acc = val;
state++;
break;
case FUNC_SUSPEND_HOST+1:
acc |= val << 8;
/* TODO: Suspend host computer */
reboot_at = now + acc;
if(reboot_at >= 10) {
/* Automatically render time while host computer is offline */
show_time = 1;
Serial.printf("Shutting down host computer, reboot scheduled in %ds\n", reboot_at);
/* Initiate poweroff on Raspberry Pi */
digitalWrite(HOST_SHUTDOWN_PIN, LOW);
delay(1);
digitalWrite(HOST_SHUTDOWN_PIN, HIGH);
}
state = FUNC_RESET;
break;
case FUNC_AUTO_TIME:
if(val == '\r' || val == '\n')
show_time = !show_time; /* toggle */
else
show_time = val;
/* Clear display */
for(int i = 0; i < NUM_LEDS; i++)
leds[i].setRGB(0,0,0);
FastLED.show();
Serial.printf("Automatic rendering of current time: %d\n", show_time);
state = FUNC_RESET;
break;
case FUNC_PUT_PIXEL:
acc = val;
state++;
break;
case FUNC_PUT_PIXEL+1:
acc |= val << 8;
state++;
break;
case FUNC_PUT_PIXEL+2:
color = val;
state++;
break;
case FUNC_PUT_PIXEL+3:
color |= val << 8;
state++;
break;
case FUNC_PUT_PIXEL+4:
color |= val << 16;
leds[(acc % NUM_LEDS)].setRGB(color & 0xff, (color >> 8) & 0xff, (color >> 16) & 0xff);
state = FUNC_RESET;
break;
default:
Serial.printf("Unknown func %d with val %d, resetting\n", state, val);
for(unsigned int i = 0; i < sizeof(last_states)/sizeof(last_states[0]) && last_state_counter - i > 0; i++)
Serial.printf("Previous state %d: %d\n", i, last_states[(last_state_counter-i) % (sizeof(last_states)/sizeof(last_states[0]))]);
state = FUNC_RESET;
break;
}
}
/* Pretty much a port of LedMatrix.xy_to_phys() */
static void put_pixel(int x, int y, int lit) {
/**
* The LEDs are laid out in a long string going from north to south,
* one step to the east, and then south to north, before the cycle
* starts over
*/
int cycle = 16;
int nssn_block = x / 2;
int phys_addr = nssn_block * 16;
int brightness_scaler = 48; /* use less power */
if(x % 2)
phys_addr += cycle - 1 - y;
else
phys_addr += y;
lit &= 0xff;
lit /= brightness_scaler;
leds[phys_addr % NUM_LEDS].setRGB(lit, lit, lit);
}
#ifdef TEENSYDUINO
/* Wrapper function for Timelib's sync provider */
static time_t getTeensy3Time(void)
{
return Teensy3Clock.get();
}
#endif
/* Render time as reported by the RTC */
static int clock_state = 0x2;
static void render_clock(int button_state) {
char buf[10];
int x_off;
size_t len;
if(button_state) {
clock_state ^= 1 << (button_state-1);
for(int i = 0; i < NUM_LEDS; i++)
leds[i].setRGB(0,0,0);
}
#ifdef TEENSYDUINO
if((clock_state & 1) == 0) {
sprintf(buf, "%02d:%02d", hour(), minute());
if((clock_state & 2) && second() % 2)
buf[2] = ' ';
}
else {
sprintf(buf, "%02d.%02d.%02d", day(), month(), year() % 100);
}
#else
sprintf(buf, "00:00");
#endif
if((clock_state & 1) == 0)
x_off = 8 - clock_state;
else
x_off = 2;
len = strlen(buf);
for(size_t i = 0; i < len; i++) {
unsigned char digit = buf[i];
size_t offset;
/* Kludge to compress colons and dots to two columns */
if(digit == ':' || digit == '.' || digit == ' ' || (i && (buf[i-1] == ':' || buf[i-1] == '.' || buf[i-1] == ' ')))
x_off--;
for(offset = 0; offset < strlen(font_alphabet); offset++) {
if(font_alphabet[offset] == digit) break;
}
int font_byte = (offset * font_width * font_height) / 8;
int font_bit = (offset * font_width * font_height) % 8;
for(int y = 0; y < font_height; y++) {
for(int x = 0; x < font_width; x++) {
if(font_data[font_byte] & (1<<font_bit))
put_pixel(x_off+x, y, 255);
else
put_pixel(x_off+x, y, 0);
if(++font_bit == 8) {
font_byte++;
font_bit = 0;
}
}
}
x_off += font_width;
}
#ifdef TEENSYDUINO
/* Display seconds bar */
if(clock_state == 2) {
int height = 1 + second() / 12;
for(int y = 0; y < 5; y++) {
int color = 0;
if(y < height) color = 128;
if(y == height-1 && second() % 2) color = 0;
put_pixel(x_off+1, 4-y, color);
}
}
/* Display weekdays */
x_off = 2;
int today_to_i = (weekday() + 5) % 7;
for(int i = 0; i < 7; i++) {
int color = i == today_to_i? 255: 64;
put_pixel(x_off+4*i+0, 7, color);
put_pixel(x_off+4*i+1, 7, color);
put_pixel(x_off+4*i+2, 7, color);
}
#endif
FastLED.show();
}