-
Notifications
You must be signed in to change notification settings - Fork 2
/
HolidayDuino06.ino
390 lines (347 loc) · 8.06 KB
/
HolidayDuino06.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
// HolidayDuino firmware
// compiled with FastSPI_LED2 RC4
// version for 16MHz or 20MHz using FastSPI_LED2
// also now 115200 baud (was 57600 in versions 01 to 03)
// and when 20MHz uses OptiBoot to get more usable flash
// when compiling, use board "Holiday Atmega328 20MHz" for 20MHz
// or "Holiday Atmega328 16MHz" for 16MHz (and larger bootloader)
// version number includes MHz so we use correct update
// ** can't easily compute MHZ from F_CPU and get it in string form :-(
#if F_CPU == 16000000L
#define CPUMHZ "16"
#elif F_CPU == 20000000L
#define CPUMHZ "20"
#else
#error "Unexpected F_CPU"
#endif
#define VERSION "HolidayDuino06-" CPUMHZ
#include <Wire.h>
// FastSPI_LED2 is seriously amazing!
// It handles 20MHz CPU and byte colour reordering
#include <FastSPI_LED2.h>
#define STRT 2 // INT 0
#define DBGI 3 // INT 1
#define DOUT 4
#define BUT1 5
#define BUT2 6
#define BUT3 7
#define ACKB 8
#define MAX_LEDS 250
CRGB leds[MAX_LEDS];
byte *ledsraw;
boolean active = false;
boolean echo = false;
boolean testmode = false;
boolean eeok = false;
char startbut = 0;
int num_leds = 50;
int cur_prog = 0;
int num_progs = 0;
int eepagesize = 32;
int reg[16];
int wdog = 0;
int argc = 0, argv[2] = {0, 0};
int led = 1, col = 0;
long next = 0;
long lastDBGI = -1000;
volatile byte pos;
volatile boolean got_frame;
void setup()
{
Serial.begin (115200);
// set up buttons with pullups
pinMode(BUT1, INPUT_PULLUP);
pinMode(BUT2, INPUT_PULLUP);
pinMode(BUT3, INPUT_PULLUP);
// set up acknowledge/busy pin
pinMode(ACKB, OUTPUT);
digitalWrite(ACKB, 1);
// set up SPI slave
pinMode(MISO, OUTPUT);
SPCR |= _BV(SPE);
// set up FastSPI output - WS2812 is GGRRBB
if (num_leds>0) FastLED.addLeds<WS2812, DOUT, GRB>(leds, num_leds);
ledsraw = (byte *)leds;
pos = 0; // skip status LED
got_frame = false;
// detect falling STRT to indicate start/end of frame
attachInterrupt(0, spiStartISR, CHANGE);
// detect falling DBGI to indicate iMX boot messages
attachInterrupt(1, dbgISR, FALLING);
// enable SPI slave interrupts
SPCR |= _BV(SPIE);
// capture button state at boot
if (digitalRead(BUT1)==0) startbut += 1;
if (digitalRead(BUT2)==0) startbut += 2;
if (digitalRead(BUT3)==0) startbut += 4;
Wire.begin(); // set up I2C as master
// check eeprom & overwrite defaults if valid
if (eeread(0)=='h' && eeread(1)==0x01)
{
eeok = true;
eepagesize = eeread(2) * 4;
num_leds = eeread(3);
num_progs = eeread(4);
cur_prog = eeread(5);
}
Serial.println(F(VERSION));
}
void spiStartISR() {
if (num_leds==0) return;
if (digitalRead(STRT)==0) // start of frame
{
if (!got_frame) // ignore if still processing last frame?
{
pos = 0; // reset frame position to start
digitalWrite(ACKB, 0); // indicate ack
}
// else // ???
}
else
{
if (pos>=num_leds*3 || testmode)
{
active = true;
next = millis() + wdog;
got_frame = true;
}
else // incomplete frame
{
pos = 0; // reset frame position to start
digitalWrite(ACKB, 1); // indicate error with immediate ack/busy change
}
}
}
void dbgISR() {
lastDBGI = millis();
}
ISR (SPI_STC_vect)
{
byte c = SPDR;
if (pos < num_leds*3)
{
ledsraw[pos] = c;
pos++;
}
}
void PrintHex8(byte data)
{
char tmp;
tmp = (data >> 4) | 48;
if (tmp > 57) tmp += 39;
Serial.write(tmp);
tmp = (data & 0x0F) | 48;
if (tmp > 57) tmp += 39;
Serial.write(tmp);
}
void eewrite(int addr, byte b)
{
Wire.beginTransmission(0x50);
Wire.write(addr>>8);
Wire.write(addr&0xff);
Wire.write(b);
Wire.endTransmission();
delay(5); // wait for write before read back
}
byte eeread(int addr)
{
Wire.beginTransmission(0x50);
Wire.write(addr>>8);
Wire.write(addr&0xff);
Wire.endTransmission();
Wire.requestFrom(0x50,1);
if (Wire.available())
return Wire.read();
else return 0;
}
//http://code.google.com/p/tinkerit/wiki/SecretVoltmeter
//only accurate to about 10% (bandgap can be 1.0V to 1.2V)
long readVcc()
{
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = (1100 * 1024L) / result; // Back-calculate AVcc in mV
return result;
}
void command(char c)
{
if (c=='?')
{
Serial.println(F(VERSION));
}
else if (c=='[' || c==']')
{
if (c=='[')
Wire.begin(0x08); // make I2C slave (can still act like a master)
else
Wire.begin(); // back to I2C master
Serial.println(c);
}
else if (c=='A')
{
pinMode(14 + (argv[0]&3), INPUT);
analogRead(argv[0]&3); // throw away
Serial.println(analogRead(argv[0]&3));
}
else if (c=='B')
{
Serial.println((digitalRead(BUT1)==0?1:0)|(digitalRead(BUT2)==0?2:0)|
(digitalRead(BUT3)==0?4:0)|(startbut<<4));
}
else if (c=='C')
{
if (argc==2) eewrite(argv[0]%eepagesize, argv[1]);
Serial.println(eeread(argv[0]%eepagesize));
}
else if (c=='D')
{
if (argc & eeok) eewrite(5, argv[0]>=num_progs?0:argv[0]);
Serial.println(eeread(5));
}
else if (c=='E')
{
if (argc) echo = argv[0]?true:false;
Serial.println(echo?1:0);
}
else if (c=='G')
{
pinMode(14 + (argv[0]&3), OUTPUT);
digitalWrite(14 + (argv[0]&3), LOW);
Serial.println(digitalRead(14 + (argv[0]&3)));
}
else if (c=='H')
{
pinMode(14 + (argv[0]&3), OUTPUT);
digitalWrite(14 + (argv[0]&3), HIGH);
Serial.println(digitalRead(14 + (argv[0]&3)));
}
else if (c=='I')
{
pinMode(14 + (argv[0]&3), INPUT);
Serial.println(digitalRead(14 + (argv[0]&3)));
}
else if (c=='L')
{
if (argc && eeok)
{
num_leds = (argv[0]>MAX_LEDS?MAX_LEDS:argv[0]);
eewrite(3, num_leds);
}
Serial.println(num_leds);
}
else if (c=='P')
{
if (argc) cur_prog = argv[0];
Serial.println(cur_prog);
}
else if (c=='R')
{
if (argc==2) reg[argv[0]&15] = argv[1];
Serial.println(reg[argv[0]&15]);
}
else if (c=='T')
{
if (argc) testmode = argv[0]?true:false;
Serial.println(testmode?1:0);
}
else if (c=='V')
{
Serial.println(readVcc());
}
else if (c=='W')
{
if (argc) wdog = argv[0];
Serial.println(wdog);
}
else if (c>=' ')
{
Serial.println('?');
}
}
void loop() {
if (Serial.available())
{
active = true;
next = millis() + wdog;
char c = Serial.read();
if (echo) Serial.print(c);
if (c>='0' && c<='9') {
if (argc==0) argc++;
argv[argc-1] = argv[argc-1] * 10 + c - '0';
}
else if (c==',' && argc<2)
{
if (argc==0) argc++;
argc++;
argv[argc-1] = 0;
}
else
{
command(c);
argc = 0;
argv[0] = 0;
}
}
if (!active && next <= millis())
{
memset(leds, 0, num_leds * 3);
if (lastDBGI + 1000 < millis())
leds[0].r = 63;
else
leds[0].b = 63;
if (digitalRead(BUT1)==0) leds[0].b += 63;
if (digitalRead(BUT2)==0) leds[0].b += 63;
if (digitalRead(BUT3)==0) leds[0].b += 63;
switch(col&3) {
case 0:
leds[led].r = 255;
break;
case 1:
leds[led].g = 255;
break;
case 2:
leds[led].b = 255;
break;
default:
leds[led].r = 255;
leds[led].g = 255;
leds[led].b = 255;
break;
}
if (num_leds>0) FastSPI_LED.show();
led++;
if (led>=num_leds) {
led = 1;
col++;
}
next = millis() + 50;
}
if (got_frame)
{
if (testmode)
{
// send data back via serial
for (int i=0; i<pos; i++) {
if (i>0) Serial.print(',');
PrintHex8(ledsraw[i]);
}
Serial.print("\r\n");
// and clear rest of buffer before display
for (; pos<num_leds*3; pos++)
ledsraw[pos] = 0;
}
//Serial.print(F("got frame!\r\n"));
if (num_leds>0) FastSPI_LED.show();
got_frame = false;
digitalWrite(ACKB, 1); // indicate complete with (delayed) ack/busy change
}
/* watchdog */
if (wdog>0 && active && next <= millis())
active = false;
}