-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightWand.ino
1945 lines (1812 loc) · 57.6 KB
/
LightWand.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Digital Light Wand + SD + LCD + Arduino MEGA - V MRR-3.0 (WS2812 RGB LED Light Strip)
by Michael Ross 2014
Based on original code by myself in 2010 then enhanced by Is0-Mick in 2012
The Digital Light Wand is for use in specialized Light Painting Photography
Applications.
This code is totally rewritten using code that IsO-Mick created made to specifically
support the WS2812 RGB LED strips running with an SD Card, an LCD Display, and the
Arduino Mega 2560 Microprocessor board.
The functionality that is included in this code is as follows:
Menu System
1 - File select
2 - Brightness
3 - Initial Delay
4 - Frame Delay
5 - Repeat Times (The number of times to repeat the current file playback)
6 - Repeat Delay (if you want a delay between repeated files)
This code supports direct reading of a 24bit Windows BMP from the SD card.
BMP images must be rotated 90 degrees clockwise and the width of the image should match the
number of pixels you have on your LED strip. The bottom of the tool will be the INPUT
end of the strips where the Arduino is connected and will be the left side of the input
BMP image.
Mick also added a Gamma Table from adafruit code which gives better conversion of 24 bit to
21 bit coloring.
Feb 2020: Extensive rewrites and added features by Martin Nohr
Switched over to SDFAT, reading is over twice as fast!
*/
// Library initialization
#include <FastLED.h> // Library for the WS2812 Neopixel Strip
#include <SDfat.h> // Library for the SD Card
#include <LiquidCrystal.h> // Library for the LCD Display
#include <SPI.h> // Library for the SPI Interface
#include <avr/eeprom.h>
//#include <timer.h> // this is from https://github.com/contrem/arduino-timer, note that the author has renamed the file
#include <arduino-timer.h> // the new name for <timer.h> when using the latest library
#include "LightWand.h"
// Gramma Correction (Defalt Gamma = 2.8)
const uint8_t PROGMEM gammaR[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5,
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9,
9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14,
15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
23, 24, 24, 25, 25, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33,
33, 34, 35, 36, 36, 37, 38, 39, 40, 40, 41, 42, 43, 44, 45, 46,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 80,
81, 83, 84, 85, 87, 88, 89, 91, 92, 94, 95, 97, 98, 99,101,102,
104,105,107,109,110,112,113,115,116,118,120,121,123,125,127,128,
130,132,134,135,137,139,141,143,145,146,148,150,152,154,156,158,
160,162,164,166,168,170,172,174,177,179,181,183,185,187,190,192,
194,196,199,201,203,206,208,210,213,215,218,220,223,225,227,230 };
const uint8_t PROGMEM gammaG[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
const uint8_t PROGMEM gammaB[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 8,
8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 12, 13,
13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19,
20, 20, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28,
29, 30, 30, 31, 32, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40,
40, 41, 42, 43, 44, 44, 45, 46, 47, 48, 49, 50, 51, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70,
71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 86, 88, 89,
90, 92, 93, 94, 96, 97, 98,100,101,103,104,106,107,109,110,112,
113,115,116,118,119,121,122,124,126,127,129,131,132,134,136,137,
139,141,143,144,146,148,150,152,153,155,157,159,161,163,165,167,
169,171,173,175,177,179,181,183,185,187,189,191,193,196,198,200 };
// white balance values, really only 8 bits, but menus need 16 for ints
struct {
uint16_t r;
uint16_t g;
uint16_t b;
} whiteBalance = { 255,255,255 };
#define DATA_PIN 31
#define NUM_LEDS 288
// functions
bool CheckCancel();
// Define the array of leds
CRGB leds[NUM_LEDS * 2];
SdFat SD;
// Pin assignments for the Arduino (Make changes to these if you use different Pins)
#define BACKLIGHT 10 // Pin used for the LCD Backlight
#define SDcsPin 53 // SD card CS pin
//int NPPin = 31; // Data Pin for the NeoPixel LED Strip
int AuxButton = 35; // Aux Select Button Pin
int g = 0; // Variable for the Green Value
int b = 0; // Variable for the Blue Value
int r = 0; // Variable for the Red Value
// menu strings
enum e_menuitem {
mFirstMenu,
mSelectFile = mFirstMenu,
mFrameHoldTime,
mStripBrightness,
mRepeatCount,
mRepeatDelay,
mInitDelay,
mChainFiles,
mGammaCorrection,
mStripLength,
mScaleHeight,
mBackLightBrightness,
mBackLightTimer,
mAutoLoadSettings,
mSavedSettings,
mTestPatterns,
mDeleteConfigFile,
mSaveConfigFile,
MAXMENU
};
const char* menuStrings[] = {
"#", // file, keep short to show folder name
"Frame Time",
"Brightness",
"Repeat Count",
"Repeat Delay",
"Init Delay",
"Chain Files",
"Gamma Correct",
"Strip Length",
"Scale Height",
"LCD Brightness",
"LCD Timeout",
"Autoload Sets",
"Saved Settings",
"Tests",
"Delete File CFG",
"Save File CFG",
};
// Initial Variable declarations and assignments (Make changes to these if you want to change defaults)
char signature[]{ "MLW" }; // set to make sure saved values are valid
int stripLength = 144; // Set the number of LEDs the LED Strip
int frameHold = 15; // default for the frame delay
int lastMenuItem = -1; // check to see if we need to redraw menu
int menuItem = mFirstMenu; // Variable for current main menu selection
int startDelay = 0; // Variable for delay between button press and start of light sequence, in seconds
int repeat = 0; // Variable to select auto repeat (until select button is pressed again)
int repeatDelay = 0; // Variable for delay between repeats
int repeatCount = 1; // Variable to keep track of number of repeats
int nStripBrightness = 10; // Variable and default for the Brightness of the strip
bool bGammaCorrection = true; // set to use the gamma table
bool bAutoLoadSettings = false; // set to automatically load saved settings
bool bScaleHeight = false; // scale the Y values to fit the number of pixels
bool bCancelRun = false; // set to cancel a running job
bool bChainFiles = false; // set to run all the files from current to the last one in the current folder
// Other program variable declarations, assignments, and initializations
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Init the LCD
// Variable assignments for the Keypad
int adc_key_val[5] = { 30, 170, 390, 600, 800 };
enum keyvals { KEYNONE = -1, KEYRIGHT = 0, KEYUP, KEYDOWN, KEYLEFT, KEYSELECT, NUM_KEYS };
int adc_key_in;
int key = -1;
int oldkey = -1;
bool bWaitForKeyNone = false; // set this after SELECT so holding the key won't try to cancel the run
String sCurrentLine0;
// SD Card Variables and assignments
#define OPEN_FOLDER_CHAR '\x7e'
#define OPEN_PARENT_FOLDER_CHAR '\x7f'
#define MAXFOLDERS 10
String folders[MAXFOLDERS];
int folderLevel = 0;
SdFile dataFile;
String CurrentFilename = "";
int CurrentFileIndex = 0;
int NumberOfFiles = 0;
String FileNames[200];
// keyboard speeds up when held down longer
#define KEYWAITPAUSE 250
int kbdWaitTime = KEYWAITPAUSE;
int kbdPause = 0;
// set to zero when no key down, and watch time when one is pressed
unsigned long startKeyDown = 0;
// built-in test patterns
enum e_tests {
mtDots = 0,
mtTwoDots,
mtRandomBars,
mtRandomColors,
mtCheckerBoard,
mtRandomRunningDot,
mtBarberPole,
mtTestCylon,
mtTwinkle,
mtBouncingBalls,
mtMeteor,
MAXTEST
};
// test functions, in same order as enums above
void RunningDot();
void OppositeRunningDots();
void RandomBars();
void RandomColors();
void CheckerBoard();
void RandomRunningDot();
void BarberPole();
void TestCylon();
void TestTwinkle();
void TestBouncingBalls();
void TestMeteor();
void (*testFunctions[MAXTEST])() = {
RunningDot,
OppositeRunningDots,
RandomBars,
RandomColors,
CheckerBoard,
RandomRunningDot,
BarberPole,
TestCylon,
TestTwinkle,
TestBouncingBalls,
TestMeteor
};
const char* testStrings[MAXTEST] = {
"Running Dot",
"Opposite Dots",
"Random Bars",
"Random Colors",
"Checker Board",
"Random Run Dot",
"Barber Pole",
"Cylon Eye",
"Twinkle",
"Bouncing Balls",
"Meteor"
};
// which one to use
int nTestNumber = 0;
// storage for special character
byte chZeroPattern[8];
int nMaxBackLight = 75; // maximum backlight to use in %
int nBackLightSeconds = 10; // how long to leave the backlight on before dimming
volatile bool bBackLightOn = false; // used by backlight timer to indicate that backlight is on
volatile bool bTurnOnBacklight = true; // set to turn the backlight on, safer than calling the BackLightControl code
struct saveValues {
void* val;
int size;
};
const saveValues saveValueList[] = {
{&signature, sizeof signature},
{&bAutoLoadSettings, sizeof bAutoLoadSettings},
{&nStripBrightness, sizeof nStripBrightness},
{&frameHold, sizeof frameHold},
{&startDelay, sizeof startDelay},
{&repeat, sizeof repeat},
{&repeatCount, sizeof repeatCount},
{&repeatDelay, sizeof repeatDelay},
{&bGammaCorrection, sizeof bGammaCorrection},
{&stripLength, sizeof stripLength},
{&nBackLightSeconds, sizeof nBackLightSeconds},
{&nMaxBackLight, sizeof nMaxBackLight},
{&CurrentFileIndex,sizeof CurrentFileIndex},
{&nTestNumber,sizeof nTestNumber},
{&bScaleHeight,sizeof bScaleHeight},
{&bChainFiles,sizeof bChainFiles},
};
// timers to run things
auto EventTimers = timer_create_default();
// set this to the delay time while we get the next frame
bool bStripWaiting = false;
// this gets called every second/TIMERSTEPS
#define TIMERSTEPS 10
bool BackLightControl(void*)
{
static int light;
static int fade;
static int timer;
// don't do anything while writing the file out
if (bStripWaiting) {
return;
}
// change % to 0-255
int abslight = 255 * nMaxBackLight / 100;
if (bTurnOnBacklight) {
timer = nBackLightSeconds * TIMERSTEPS;
bBackLightOn = true;
bTurnOnBacklight = false;
}
if (timer > 1) {
light = abslight;
}
else if (timer == 1) {
// start the fade timer
fade = abslight / TIMERSTEPS;
}
if (bBackLightOn)
analogWrite(BACKLIGHT, light);
if (timer > 0)
--timer;
if (fade) {
light -= fade;
if (light < 0) {
light = 0;
bBackLightOn = false;
analogWrite(BACKLIGHT, light);
}
}
return true; // repeat true
}
// counts the delay for the frame hold time
bool StripDelay(void*)
{
bTurnOnBacklight = true;
bStripWaiting = false;
return false;
}
// Setup loop to get everything ready. This is only run once at power on or reset
void setup() {
Serial.begin(115200);
delay(50);
Serial.println("Starting setup");
pinMode(AuxButton, INPUT_PULLUP);
folders[folderLevel = 0] = String("/");
setupLEDs();
setupLCDdisplay();
setupSDcard();
// turn on the keyboard reader
digitalWrite(LED_BUILTIN, HIGH);
SaveSettings(false, true);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, stripLength);
FastLED.setTemperature(CRGB(whiteBalance.r, whiteBalance.g, whiteBalance.b));
FastLED.setBrightness(map(nStripBrightness, 0, 100, 0, 255));
// Turn the LED on, then pause
leds[0] = leds[1] = CRGB::Red;
leds[4] = leds[5] = CRGB::Green;
leds[8] = leds[9] = CRGB::Blue;
leds[12] = leds[13] = CRGB::White;
for (int ix = 0; ix < 255; ix += 5) {
FastLED.setBrightness(ix);
FastLED.show();
}
for (int ix = 255; ix >= 0; ix -= 5) {
FastLED.setBrightness(ix);
FastLED.show();
}
// Now turn the LED off
FastLED.clear(true);
// run a white dot up the display and back
FastLED.setBrightness(map(nStripBrightness, 0, 100, 0, 255));
for (int ix = 0; ix < stripLength; ++ix) {
leds[ix] = CRGB(255, 255, 255);
if (ix)
leds[ix - 1] = CRGB::Black;
FastLED.show();
delay(1);
}
for (int ix = stripLength - 1; ix >= 0; --ix) {
leds[ix] = CRGB(255, 255, 255);
if (ix)
leds[ix + 1] = CRGB::Black;
FastLED.show();
delay(1);
}
FastLED.clear(true);
EventTimers.every(1000 / TIMERSTEPS, BackLightControl);
Serial.println("Finishing setup");
}
// create a character by filling blocks to indicate how far down the menu we are
void CreateMenuCharacter()
{
memset(chZeroPattern, 0, sizeof chZeroPattern);
for (int menu = 0; menu <= menuItem; ++menu) {
chZeroPattern[menu % 7] |= (1 << (4 - menu / 7));
}
lcd.createChar(0, chZeroPattern);
}
// The Main Loop for the program starts here...
// This will loop endlessly looking for a key press to perform a function
void loop() {
EventTimers.tick();
if (bBackLightOn && menuItem != lastMenuItem) {
CreateMenuCharacter();
lcd.clear();
lcd.setCursor(0, 0);
lcd.write((byte)0);
lcd.print(menuStrings[menuItem]);
if (menuItem == mSelectFile) {
lcd.print(String(CurrentFileIndex + 1) + "/" + String(NumberOfFiles) + " " + folders[folderLevel]);
}
if (menuItem == mTestPatterns) {
lcd.print(" " + String(nTestNumber + 1) + "/" + String(MAXTEST));
}
lcd.setCursor(0, 1);
switch (menuItem) {
case mSelectFile:
DisplayCurrentFilename();
break;
case mChainFiles:
lcd.print(bChainFiles ? "ON" : "OFF");
break;
case mStripBrightness:
lcd.print(nStripBrightness);
lcd.print(" % ");
break;
case mInitDelay:
lcd.print(String(startDelay) + " Seconds");
break;
case mFrameHoldTime:
lcd.print(String(frameHold) + " mSec");
break;
case mRepeatCount:
lcd.print(repeatCount);
break;
case mRepeatDelay:
lcd.print(String(repeatDelay) + " mSec");
break;
case mTestPatterns:
lcd.print(testStrings[nTestNumber]);
break;
case mBackLightBrightness:
lcd.print(String(nMaxBackLight) + " %");
break;
case mBackLightTimer:
lcd.print(String(nBackLightSeconds) + " Seconds");
break;
case mSavedSettings:
lcd.print("<=Load >=Save");
break;
case mGammaCorrection:
lcd.print(bGammaCorrection ? "ON" : "OFF");
break;
case mStripLength:
lcd.print(stripLength);
lcd.print(" pixels");
break;
case mScaleHeight:
lcd.print(bScaleHeight ? "ON" : "OFF");
break;
case mAutoLoadSettings:
lcd.print(bAutoLoadSettings ? "ON" : "OFF");
break;
case mDeleteConfigFile:
lcd.print("SELECT to Delete");
break;
case mSaveConfigFile:
lcd.print("<=Load >=Save");
}
lastMenuItem = menuItem;
}
bool oldBackLightOn = bBackLightOn;
int keypress = ReadKeypad();
if (keypress != KEYNONE) {
if (!oldBackLightOn) {
// just eat the key if the light was off
// wait for release
while (ReadKeypad() != -1)
;
return;
}
}
switch (keypress) {
case KEYSELECT:
HandleKeySelect();
lastMenuItem = -1; // show the menu again
break;
case KEYUP:
menuItem = menuItem > mFirstMenu ? menuItem - 1 : MAXMENU - 1;
break;
case KEYDOWN:
menuItem = menuItem < MAXMENU - 1 ? menuItem + 1 : mFirstMenu;
break;
case KEYLEFT:
HandleKeyLeft();
// redraw
lastMenuItem = -1;
break;
case KEYRIGHT:
HandleKeyRight();
// redraw
lastMenuItem = -1;
break;
case KEYNONE:
// no key is pressed, reset the timer
startKeyDown = 0;
// and the keypause
kbdWaitTime = KEYWAITPAUSE;
break;
}
// wait a bit between keypresses
if (keypress != KEYNONE) {
// a key is down
// remember the time it was pressed
if (startKeyDown == 0) {
startKeyDown = millis();
}
// calcualate how long to wait
unsigned long now = millis();
if (now > startKeyDown + 4000)
kbdWaitTime = KEYWAITPAUSE / 5;
if (now > startKeyDown + 6000)
kbdWaitTime = KEYWAITPAUSE / 10;
if (now > startKeyDown + 8000)
kbdWaitTime = KEYWAITPAUSE / 20;
// do the prescribed wait
delay(kbdWaitTime);
}
}
// always run the current file unless the test menu is slected
void HandleKeySelect()
{
if (menuItem == mDeleteConfigFile) {
WriteOrDeleteConfigFile(CurrentFilename, true);
lcd.setCursor(0, 1);
lcd.print("Deleted ");
delay(1000);
return;
}
// make sure we wait before accepting this key again
bWaitForKeyNone = true;
int chainNumber = FileCountOnly() - CurrentFileIndex;
bool isFolder = ProcessFileOrTest(bChainFiles ? chainNumber : 0);
isFolder |= FileNames[CurrentFileIndex][0] == OPEN_FOLDER_CHAR
|| FileNames[CurrentFileIndex][0] == OPEN_PARENT_FOLDER_CHAR;
// check if file chaining is on
if (!isFolder && bChainFiles) {
// save our settings and process files to the end of the list
int savedFileIndex = CurrentFileIndex;
while (CurrentFileIndex < NumberOfFiles - 1) {
--chainNumber;
++CurrentFileIndex;
// stop on folder
if (FileNames[CurrentFileIndex][0] == OPEN_FOLDER_CHAR
|| FileNames[CurrentFileIndex][0] == OPEN_PARENT_FOLDER_CHAR)
break;
DisplayCurrentFilename();
ProcessFileOrTest(chainNumber);
}
CurrentFileIndex = savedFileIndex;
}
// wait for release
while (ReadKeypad() != KEYNONE)
delay(10);
}
void HandleKeyRight()
{
// redid this as if/else if because switch was crashing
if (menuItem == mSelectFile) {
if (CurrentFileIndex < NumberOfFiles - 1) {
CurrentFileIndex++;
}
else {
CurrentFileIndex = 0; // On the last file so wrap round to the first file
}
DisplayCurrentFilename();
}
else if (menuItem == mChainFiles) {
bChainFiles = !bChainFiles;
}
else if (menuItem == mStripBrightness) {
if (nStripBrightness < 100) {
++nStripBrightness;
FastLED.setBrightness(map(nStripBrightness, 0, 100, 0, 255));
}
}
else if (menuItem == mInitDelay) {
++startDelay;
}
else if (menuItem == mFrameHoldTime) {
++frameHold;
}
else if (menuItem == mRepeatCount) {
repeatCount += 1;
}
else if (menuItem == mRepeatDelay) {
repeatDelay += 100;
}
else if (menuItem == mTestPatterns) {
++nTestNumber;
if (nTestNumber >= MAXTEST)
nTestNumber = 0;
}
else if (menuItem == mBackLightBrightness) {
if (nMaxBackLight < 100)
++nMaxBackLight;
}
else if (menuItem == mBackLightTimer) {
++nBackLightSeconds;
}
else if (menuItem == mSavedSettings) {
SaveSettings(true, false);
}
else if (menuItem == mGammaCorrection) {
bGammaCorrection = !bGammaCorrection;
}
else if (menuItem == mStripLength) {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, ++stripLength);
}
else if (menuItem == mScaleHeight) {
bScaleHeight = !bScaleHeight;
}
else if (menuItem == mAutoLoadSettings) {
bAutoLoadSettings = !bAutoLoadSettings;
}
else if (menuItem == mSaveConfigFile) {
if (WriteOrDeleteConfigFile(CurrentFilename, false)) {
lcd.setCursor(0, 0);
lcd.print("Saved CFG ");
delay(1000);
}
}
}
void HandleKeyLeft()
{
if (menuItem == mSelectFile) {
if (CurrentFileIndex > 0) {
CurrentFileIndex--;
}
else {
CurrentFileIndex = NumberOfFiles - 1; // On the last file so wrap round to the first file
}
DisplayCurrentFilename();
}
else if (menuItem == mChainFiles) {
bChainFiles = !bChainFiles;
}
else if (menuItem == mStripBrightness) {
if (nStripBrightness > 1) {
--nStripBrightness;
FastLED.setBrightness(map(nStripBrightness, 0, 100, 0, 255));
}
}
else if (menuItem == mInitDelay) {
if (startDelay > 0) {
--startDelay;
}
}
else if (menuItem == mFrameHoldTime) {
--frameHold;
if (frameHold < 15) {
frameHold = 15;
}
}
else if (menuItem == mRepeatCount) {
if (repeatCount > 1) {
repeatCount -= 1;
}
}
else if (menuItem == mRepeatDelay) {
if (repeatDelay > 0) {
repeatDelay -= 100;
}
}
else if (menuItem == mTestPatterns) {
--nTestNumber;
if (nTestNumber < 0)
nTestNumber = MAXTEST - 1;
}
else if (menuItem == mBackLightBrightness) {
if (nMaxBackLight > 5)
--nMaxBackLight;
}
else if (menuItem == mBackLightTimer) {
if (nBackLightSeconds > 1)
--nBackLightSeconds;
}
else if (menuItem == mSavedSettings) {
// load the settings
SaveSettings(false, false);
}
else if (menuItem == mGammaCorrection) {
bGammaCorrection = !bGammaCorrection;
}
else if (menuItem == mStripLength) {
if (stripLength > 1)
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, --stripLength);
}
else if (menuItem == mScaleHeight) {
bScaleHeight = !bScaleHeight;
}
else if (menuItem == mAutoLoadSettings) {
bAutoLoadSettings = !bAutoLoadSettings;
}
else if (menuItem == mSaveConfigFile) {
if (WriteOrDeleteConfigFile(CurrentFilename, false)) {
lcd.setCursor(0, 0);
lcd.print("Loaded LWC");
delay(1000);
}
}
}
// count the actual files
int FileCountOnly()
{
int count = 0;
// ignore folders, at the end
char start = FileNames[0][0];
while (start != OPEN_FOLDER_CHAR && start != OPEN_PARENT_FOLDER_CHAR) {
++count;
start = FileNames[count][0];
}
return count;
}
// returns true if the folder was changed
bool ProcessFileOrTest(int chainnumber)
{
bool bFolderChanged = false;
char line[17];
lcd.clear();
lcd.setCursor(0, 0);
if (startDelay) {
for (int seconds = startDelay; seconds; --seconds) {
lcd.setCursor(0, 0);
sprintf(line, "Wait: %d", seconds);
lcd.print(line);
delay(1000);
}
}
for (int counter = repeatCount; counter > 0; counter--) {
lcd.clear();
lcd.setCursor(0, 1);
if (menuItem == mTestPatterns) {
lcd.print(testStrings[nTestNumber]);
}
else {
DisplayCurrentFilename();
}
if (chainnumber) {
lcd.setCursor(13, 1);
char line[10];
sprintf(line, "%2d", chainnumber);
lcd.print(line);
}
lcd.setCursor(0, 0);
// only display if a file
char first = CurrentFilename[0];
if (first != OPEN_FOLDER_CHAR && first != OPEN_PARENT_FOLDER_CHAR) {
sprintf(line, "Count %d", counter);
lcd.print(line);
// save this for restoring if cancel is cancelled
sCurrentLine0 = line;
}
if (menuItem == mTestPatterns) {
// run the test
(*testFunctions[nTestNumber])();
}
else {
// first see if a folder
if (first == OPEN_FOLDER_CHAR) {
if (folderLevel < MAXFOLDERS - 1) {
folders[++folderLevel] = CurrentFilename.substring(1);
GetFileNamesFromSD(folders[folderLevel]);
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MAX " + MAXFOLDERS);
lcd.setCursor(0, 1);
lcd.print("FOLDERS");
}
// stop if folder
bFolderChanged = true;
break;
}
else if (first == OPEN_PARENT_FOLDER_CHAR) {
// go back a level
if (folderLevel > 0) {
GetFileNamesFromSD(folders[--folderLevel]);
}
// stop if folder
bFolderChanged = true;
break;
}
//CurrentFilename = FileNames[CurrentFileIndex];
// output the file
SendFile(CurrentFilename);
}
if (bCancelRun) {
bCancelRun = false;
break;
}
if (counter > 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Repeat delay...");
if (repeatDelay) {
FastLED.clear(true);
delay(repeatDelay);
}
}
}
FastLED.clear(true);
}
// save or restore all the settings that are relevant
// this is used when reading the LWC associated with a file
bool SettingsSaveRestore(bool save)
{
static void* memptr = NULL;
if (save) {
// get some memory and save the values
if (memptr)
free(memptr);
memptr = malloc(sizeof saveValueList);
if (!memptr)
return false;
}
void* blockptr = memptr;
for (int ix = 0; ix < (sizeof saveValueList / sizeof * saveValueList); ++ix) {
if (save) {
memcpy(blockptr, saveValueList[ix].val, saveValueList[ix].size);
}
else {
memcpy(saveValueList[ix].val, blockptr, saveValueList[ix].size);
}
blockptr = (void*)((byte*)blockptr + saveValueList[ix].size);
}
if (!save) {
// if it was saved, restore it and free the memory
if (memptr) {
free(memptr);
memptr = NULL;
}
}
return true;
}
// save some settings in the eeprom
// if autoload is true, check the first flag, and load the rest if it is true
void SaveSettings(bool save, bool autoload)
{
void* blockpointer = (void*)NULL;
for (int ix = 0; ix < (sizeof saveValueList / sizeof * saveValueList); ++ix) {
if (save) {
eeprom_write_block(saveValueList[ix].val, blockpointer, saveValueList[ix].size);
}
else { // load
// check signature
char svalue[sizeof signature];
eeprom_read_block(svalue, (void*)NULL, sizeof svalue);
if (strncmp(svalue, signature, sizeof signature)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("bad signature");
lcd.setCursor(0, 1);
lcd.print(svalue);
delay(1000);
return;
}
eeprom_read_block(saveValueList[ix].val, blockpointer, saveValueList[ix].size);
// if autoload, exit if the save value is not true
if (autoload && ix == 1) {
if (!bAutoLoadSettings) {
return;
}
}
}
blockpointer = (void*)((byte*)blockpointer + saveValueList[ix].size);
}
if (!save) {
int savedFileIndex = CurrentFileIndex;
// we don't know the folder path, so just reset the folder level
folderLevel = 0;
setupSDcard();
CurrentFileIndex = savedFileIndex;
// make sure file index isn't too big
if (CurrentFileIndex >= NumberOfFiles) {
CurrentFileIndex = 0;
}
CurrentFilename = FileNames[CurrentFileIndex];
// check test number also
if (nTestNumber >= MAXTEST) {
nTestNumber = 0;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(save ? "Settings Saved" : "Settings Loaded");
delay(1000);
}
void setupLEDs() {
FastLED.clear(true);
}
void setupLCDdisplay() {
lcd.begin(16, 2);
lcd.print("LightWand V5.2");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void setupSDcard() {
pinMode(SDcsPin, OUTPUT);
while (!SD.begin(SDcsPin)) {
bBackLightOn = true;
lcd.print("SD init failed! ");
delay(1000);
lcd.clear();
delay(500);
}
lcd.clear();
lcd.print("SD init done ");
delay(1000);
folders[folderLevel = 0] = String("/");
lcd.clear();
lcd.print("Reading SD... ");
delay(500);
GetFileNamesFromSD(folders[folderLevel]);
CurrentFilename = FileNames[0];
DisplayCurrentFilename();
}
// read the keys
int ReadKeypad() {
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
}
}
if (key != -1) {
// turn the light on
bTurnOnBacklight = true;
}
return key;
}
// Convert ADC value to key number
int get_key(unsigned int input) {
if (digitalRead(AuxButton) == LOW) {
return KEYSELECT;
}
int k;