-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDPOVpoi.ino
441 lines (401 loc) · 12.2 KB
/
SDPOVpoi.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
438
439
440
441
/* SDPovpoi by Ashley M. Kirchner, April 2014
* http://www.ka4designs.com
* email: [email protected]
* Copyright (c) 2015 KA4 Designs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define __DELAY_BACKWARD_COMPATIBLE__
#include <avr/io.h>
#include <util/delay.h>
#include "revisions.h";
// OneButton
#include <OneButton.h>
OneButton button(9, false);
volatile bool run = false;
volatile bool mode = true;
// FastLED
#include "FastLED.h"
#define NUM_LEDS 48
CRGB leds[NUM_LEDS + 1];
// For Rev. 1, uncomment this line
//#define DAT_PIN 11
// For Rev. 2, uncomment this line
#define DAT_PIN 6
#define CLK_PIN 12
#define FRAMES_PER_SECOND 60
// SdFat
#include <SdFat.h>
SdFile cntlFile;
SdFile myFile;
SdFat sd;
// File variables
#define CNTRL_FILE "order.ctl"
volatile uint32_t lastChange = 0;
volatile float imgRefresh = 0.0;
volatile float imgTimeout = 0.0;
volatile float displayPause = 0.0;
char fileData[100] = {};
char fileName[12] = {};
// Program structs and unions
struct imageHeader {
uint16_t imageCols;
};
union SD_buffer_t {
uint16_t receiveBuffer[1];
imageHeader hdr;
} SD_buffer;
// Include dynamic patterns
#include "patterns.h"
// Program variables
#define SECOND 1000
const uint8_t SdChipSelect = SS;
const uint8_t errorLED = 5;
const uint8_t STRIP_PWR = 8;
const uint8_t battPin = A0;
const uint8_t battReadings = 20;
volatile uint8_t myError = 0;
volatile uint8_t cntr = 0;
volatile uint8_t brightnessLevel = 1;
volatile uint8_t fireState = 0;
volatile int8_t fireStyle = 1;
volatile uint16_t battIdx = 0;
volatile uint16_t battTot = 0;
volatile uint16_t battTop = 0;
volatile uint16_t battAvg = 0;
volatile uint16_t battVal = 0;
volatile uint16_t pause = 100;
volatile uint16_t myRefresh = 0;
volatile uint16_t fileStatus = 0;
volatile uint32_t builtinLastRun = 0;
volatile uint32_t refreshLastRun = 0;
volatile uint32_t errorLastRun = 0;
volatile bool SDCARD = 1; // assume card is present and readable, can catch errors later
// Available error codes:
// 1 - Low battery
// 2 - Can't open file
// 3 - Unknown error reading file
void setup() {
// Interrupt button for brightness
attachInterrupt(4, intHandler, RISING);
// Status button
button.setClickTicks(200);
button.attachClick(singleClick); // On/Off
button.attachPress(longPress); // Built-in or bitmap on SD
button.attachDoubleClick(doubleClick); // Fire!
pinMode(STRIP_PWR, OUTPUT);
// Pull PWR_ON pin low to keep strips turned off
digitalWrite(STRIP_PWR, LOW);
pinMode(battPin, INPUT);
// Uncomment this for use with FastLED2.0-2.1 (NOT dithering) library
//LEDS.addLeds<LPD8806, DAT_PIN, CLK_PIN, RGB>(leds, NUM_LEDS);
// Uncomment this for use with FastLED2.1-dithering library
LEDS.addLeds<LPD8806, DAT_PIN, CLK_PIN, RGB>(leds, NUM_LEDS + 1).setDither(DISABLE_DITHER);
resetString(1);
//memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
//LEDS.show();
_delay_ms(5);
if (!batteryCheck()) {
myError = 1;
}
if (!sd.begin(SdChipSelect, SPI_FULL_SPEED)) {
// Can't read SD card - is it inserted?
SDCARD = 0;
mode = 0; // Force built-in mode
}
random16_set_seed(analogRead(A5));
// This eats up 24 bytes!
// Disable ADC by setting ADEN bit to zero
ADCSRA = ADCSRA & B01111111;
// Disable analog comparator by setting ACD bit to one
ACSR = B10000000;
// Disable digital buffers on ADC0, ADC1, ADC4, ADC5, ADC6, ADC7
DIDR0 = DIDR0 | B11110011;
// (DIDR2 Register)ADC8, ADC9, ADC10, ADC11, ADC12, ADC13 are all used as digital pins
}
static void resetString(uint8_t state) {
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
if (state) {
LEDS.show();
}
}
static uint8_t batteryCheck() {
// Battery Check - takes 2 seconds to take readings!!
for (uint8_t c = 0; c < battReadings; c++) {
battVal = analogRead(battPin);
battTot = battTot + battVal;
_delay_ms(100);
}
battAvg = battTot / battReadings;
if (battAvg < 569) {
return 0;
}
else {
// 5V = 1023 -> 204.60 per volt
// 4.2V = 859.32 -> round to 859 ! with 10K series resistor, this reading drops to 814 !
// 3.0V = 613.80 -> round to 614 ! with 10K series resistor, this reading drops to 569 ! <-- 3.0V safe cut-off
// 2.8V = 572.88 -> round to 573 ! with 10K series resistor, this reading drops to 528 !
battTop = map(battAvg, 569, 814, 0, 48);
// Set PWR_ON pin high, turn strips on to display power level
digitalWrite(STRIP_PWR, HIGH);
_delay_ms(5);
fill_rainbow(&(leds[0]), battTop, 0, 2);
LEDS.setBrightness(16);
LEDS.show();
_delay_ms(SECOND);
LEDS.setBrightness(64);
resetString(1);
// memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
// LEDS.show();
// Set PWR_ON pin low, turn strips off
//_delay_ms(5);
//digitalWrite(STRIP_PWR, LOW);
return 1;
}
}
static void brightness() {
brightnessLevel++;
if (brightnessLevel > 6) brightnessLevel = 1;
switch (brightnessLevel) {
case 1: LEDS.setBrightness(64); break; // 25% - default
case 2: LEDS.setBrightness(128); break; // 50%
case 3: LEDS.setBrightness(255); break; // 100%
case 4: LEDS.setBrightness(8); break; // 3%
case 5: LEDS.setBrightness(16); break; // 6%
case 6: LEDS.setBrightness(32); break; // 12%
}
}
static void displayPattern(uint8_t pattern) {
// patterns.h:MAX_PATTERNS must match the same number of patterns listed here
switch (pattern) {
case 1: HSVRainbowWheel(); break;
case 2: RedWhiteBlue(); break;
case 3: HSVEdges(); break;
case 4: flashers(); break;
case 5: HSVRainbowSpiral(); break;
case 6: flowerPower(); break;
case 7: randomSpokes(); break;
case 8: rainbowWheel(); break;
case 9: rainbowArrows(); break;
case 10: randomStripes(); break;
case 11: diamonds(); break;
case 12: wheelDivisions(); break;
}
}
static void getFileData() {
if (!cntlFile.isOpen()) {
if (!cntlFile.open(CNTRL_FILE, O_READ)) {
myError = 2;
return;
}
}
// Fetch filename from control file
fileStatus = cntlFile.fgets(fileData, sizeof(fileData));
if (fileStatus < 0) {
myError = 3;
return;
}
else if (fileStatus == 0) {
// End of file reached, rewind to beginning of file
cntlFile.seekSet(0);
// Read filename again
fileStatus = cntlFile.fgets(fileData, sizeof(fileData));
}
if (fileData[fileStatus - 1] == '\n') fileData[fileStatus - 1] = 0;
return;
}
void loop() {
button.tick();
if ((run || myError > 1) && !(digitalRead(STRIP_PWR))) {
_delay_ms(5);
digitalWrite(STRIP_PWR, HIGH);
} else if (((!run) && (!myError)) && digitalRead(STRIP_PWR)) {
//_delay_ms(5);
//digitalWrite(STRIP_PWR, LOW);
}
if (myError) {
// Make sure run is turned off!
run = false;
// Close control and bitmap data files if open
if (myFile.isOpen()) {
myFile.close();
}
if (cntlFile.isOpen()) {
cntlFile.close();
memset(fileData, 0, sizeof(fileData));
}
// Display error blink
LEDS.setBrightness(32);
if (millis() - errorLastRun > pause) {
if (!digitalRead(errorLED)) {
digitalWrite(errorLED, HIGH);
if (myError > 1) {
leds[0].r = 255;
LEDS.show();
}
pause = 100;
} else {
digitalWrite(errorLED, LOW);
if (myError > 1) {
resetString(1);
//memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
//LEDS.show();
}
if (myError > 1) {
cntr++;
if (cntr >= myError) {
cntr = 0;
pause = 750;
}
}
}
errorLastRun = millis();
}
}
if (run) {
if (fireState) {
fire(fireStyle);
} else if (!mode || !SDCARD) {
// Use build-in routines with fixed 5 seconds timeout
if (millis() - builtinLastRun > SECOND * 5) {
pattern++;
if (pattern > MAX_PATTERNS) pattern = 1;
builtinLastRun = millis();
resetString(0);
}
displayPattern(pattern);
} else {
// Use bitmap data on SD card
getFileData();
// Make sure we have a valid filename, not a comment or empty line
while ((fileData[0] == '#') || (fileData[0] == '\n') || (fileData[0] == NULL)) {
getFileData();
}
// Get tokens from control fileData
// [0] = fileName
// [1] = imgRefresh - PAUSE BETWEEN THE SAME IMAGE (secs)
// [2] = imgTimeout - TIME BEFORE CHANGING TO NEXT IMAGE (secs)
// [3] = displayPause - TIME TO PAUSE WHEN MOVING TO NEXT IMAGE (secs)
char *fileName = strtok(fileData, "|\t ");
imgRefresh = atof(strtok(NULL, "|\t "));
imgTimeout = atof(strtok(NULL, "|\t "));
displayPause = atof(strtok(NULL, "\r"));
if (imgRefresh > 0) {
myRefresh = (int)(imgRefresh * SECOND);
} else {
myRefresh = 0;
}
uint16_t myTimeout = (int)(imgTimeout * SECOND);
if (myTimeout < 100) {
// No sense in the image timeout being less than 100 ms
// Reset it to at least 1 second
myTimeout = SECOND;
}
uint16_t myDisplayPause = (int)(displayPause * SECOND);
if (!myFile.open(fileName, O_READ)) {
// Can't open or find file as listed in control file
myError = 2;
return;
} else {
// Read file header to get columns
myFile.read(SD_buffer.receiveBuffer, 2);
uint16_t imgColumns = SD_buffer.hdr.imageCols;
// Display image, one column at a time
lastChange = millis();
while(millis() - lastChange < (imgTimeout * SECOND)) {
if (millis() - refreshLastRun > myRefresh) {
for (uint16_t tmpCols = 0; tmpCols < imgColumns; tmpCols++) {
myFile.read((char*)leds, NUM_LEDS * 3);
LEDS.show();
button.tick();
if (!run || !mode) {
// STOP or MODE button press detected
// Close file, reset string, close control file, reset fileData array
myFile.close();
resetString(1);
cntlFile.close();
memset(fileData, 0, sizeof(fileData));
return;
}
_delay_us(10); // Really not needed but added here just to slow things down a tad
}
// Rewind back to the beginning of the file, AFTER the uint16_t (2 bytes) header
myFile.seekSet(2);
refreshLastRun = millis();
if (myRefresh > 0) {
resetString(1);
}
}
}
// Timeout reached! Reset string and close file
resetString(1);
myFile.close();
// Check for display pause before looping to next image
if (myDisplayPause > 0) {
_delay_ms(myDisplayPause);
}
}
}
} else {
pattern = 0;
builtinLastRun = 0;
refreshLastRun = 0;
if (!myError) {
if (digitalRead(errorLED)) {
digitalWrite(errorLED, LOW);
}
resetString(1);
}
}
}
static void intHandler() {
// Interrupt for brightness button
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// deBounce the button!
if (interrupt_time - last_interrupt_time > 200) {
brightness();
last_interrupt_time = interrupt_time;
}
}
static void singleClick() {
// Turn on or off
fireState = 0;
if (myError > 1) {
myError = 0;
run = 0;
} else if (myError != 1) {
run = !run;
} else {
// Battery too low, keep error status!
}
}
static void longPress() {
// Switch between built-in or SD card
fireState = 0;
mode = !mode;
if (!run) run = !run;
}
static void doubleClick() {
// Flames!
// Double clicking alternates between red or blue flames
fireStyle = -fireStyle;
fireState = 1;
if (!run) run = !run;
}