-
Notifications
You must be signed in to change notification settings - Fork 1
/
LedImagePainter.ino
3522 lines (3357 loc) · 92.8 KB
/
LedImagePainter.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
/*
Name: LedImagePainter.ino
Created: 8/5/2020 9:50:12 PM
Author: Martin
*/
/*
Martin Nohr ESP32 LED Image Painter
*/
/*
BLE code based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
*/
#include "LedImagePainter.h"
RTC_DATA_ATTR int nBootCount = 0;
// some forward references that Arduino IDE needs
int IRAM_ATTR readByte(bool clear);
void IRAM_ATTR ReadAndDisplayFile(bool doingFirstHalf);
uint16_t IRAM_ATTR readInt();
uint32_t IRAM_ATTR readLong();
void IRAM_ATTR FileSeekBuf(uint32_t place);
//static const char* TAG = "lightwand";
//esp_timer_cb_t oneshot_timer_callback(void* arg)
void IRAM_ATTR oneshot_LED_timer_callback(void* arg)
{
bStripWaiting = false;
//int64_t time_since_boot = esp_timer_get_time();
//Serial.println("in isr");
//ESP_LOGI(TAG, "One-shot timer called, time since boot: %lld us", time_since_boot);
}
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
BLEDeviceConnected = true;
//OLED->clear();
//WriteMessage("BLE connected");
//OLED->clear();
//DisplayCurrentFile();
//Serial.println("BLE connected");
};
void onDisconnect(BLEServer* pServer) {
BLEDeviceConnected = false;
//OLED->clear();
//WriteMessage("BLE disconnected");
//OLED->clear();
//DisplayCurrentFile();
//Serial.println("BLE disconnected");
}
};
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic* pCharacteristic) {
BLEUUID uuid = pCharacteristic->getUUID();
//Serial.println("UUID:" + String(uuid.toString().c_str()));
std::string value = pCharacteristic->getValue();
//String stmp = value.c_str();
if (value.length() > 0) {
//Serial.println("*********");
//Serial.print("New value: ");
//for (int i = 0; i < value.length(); i++) {
// Serial.print(value[i]);
//}
//Serial.println();
//Serial.println("*********");
// see if this a UUID we can change
if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_RUN))) {
sBLECommand = value;
}
else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_WANDSETTINGS))) {
//Serial.println(value.c_str());
ARDUINOJSON_NAMESPACE::StaticJsonDocument<200> doc;
const char* json = value.c_str();
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
//Serial.print("deserializeJson() failed: ");
//Serial.println(error.c_str());
return;
}
JsonObject object = doc.as<JsonObject>();
// see if brightness is there
JsonVariant jv = doc.getMember("bright");
if (!jv.isNull()) {
nStripBrightness = jv.as<int>();
}
// see if repeat
jv = doc.getMember("repeatcount");
if (!jv.isNull()) {
repeatCount = jv.as<int>();
}
// see if repeat delay
jv = doc.getMember("repeatdelay");
if (!jv.isNull()) {
repeatDelay = jv.as<int>();
}
// change the current file
jv = doc.getMember("current");
if (!jv.isNull()) {
String fn = jv.as<char*>();
// lets search for the file
int ix = LookUpFile(fn);
if (ix != -1) {
CurrentFileIndex = ix;
DisplayCurrentFile();
}
}
// change framehold
jv = doc.getMember("framehold");
if (!jv.isNull()) {
nFrameHold = jv.as<int>();
}
// change builtin setting
jv = doc.getMember("builtin");
if (!jv.isNull()) {
String bist = jv.as<char*>();
bist.toUpperCase();
bool bi = bist[0] == 'T';
if (bi != bShowBuiltInTests) {
ToggleFilesBuiltin(NULL);
//Serial.println("builtin:" + String(bShowBuiltInTests ? "true" : "false"));
CurrentFileIndex = 0;
OLED->clear();
DisplayCurrentFile();
}
}
}
//else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_LEDBRIGHT))) {
// nStripBrightness = stmp.toInt();
// nStripBrightness = constrain(nStripBrightness, 1, 100);
//}
//else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_BUILTIN))) {
// stmp.toUpperCase();
// bool newval = stmp.equals("TRUE") ? true : false;
// //Serial.println("newval:" + String(newval ? "true" : "false"));
// if (newval != bShowBuiltInTests) {
// //Serial.println("builtin:" + String(bShowBuiltInTests ? "true" : "false"));
// ToggleFilesBuiltin(NULL);
// //Serial.println("builtin:" + String(bShowBuiltInTests ? "true" : "false"));
// OLED->clear();
// DisplayCurrentFile();
// }
//}
//else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_2LEDSTRIPS))) {
// stmp.toUpperCase();
// bSecondStrip = stmp.compareTo("TRUE") == 0 ? true : false;
//}
else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_FILEINFO))) {
// change the current file here, maybe we search for it?
}
//else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_FRAMETIME))) {
// frameHold = stmp.toInt();
//}
//else if (uuid.equals(BLEUUID(CHARACTERISTIC_UUID_STARTDELAY))) {
// startDelay = stmp.toInt();
//}
UpdateBLE(false);
}
}
};
void EnableBLE()
{
BLEDevice::init("MN LED Image Painter");
BLEServer* pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService* pServiceDevInfo = pServer->createService(BLEUUID((uint16_t)0x180a));
BLECharacteristic* pCharacteristicDevInfo = pServiceDevInfo->createCharacteristic(
BLEUUID((uint16_t)0x2a29),
BLECharacteristic::PROPERTY_READ
);
pCharacteristicDevInfo->setValue("NOHR PHOTO");
pCharacteristicDevInfo = pServiceDevInfo->createCharacteristic(
BLEUUID((uint16_t)0x2a24), // model
BLECharacteristic::PROPERTY_READ
);
pCharacteristicDevInfo->setValue("LED Image Painter Small Display");
pCharacteristicDevInfo = pServiceDevInfo->createCharacteristic(
BLEUUID((uint16_t)0x2a28), // software version
BLECharacteristic::PROPERTY_READ
);
pCharacteristicDevInfo->setValue("1.1");
pCharacteristicDevInfo = pServiceDevInfo->createCharacteristic(
BLEUUID((uint16_t)0x2a27), // hardware version
BLECharacteristic::PROPERTY_READ
);
pCharacteristicDevInfo->setValue("1.0");
BLEService* pService = pServer->createService(SERVICE_UUID);
// filepath
pCharacteristicFileInfo = pService->createCharacteristic(
CHARACTERISTIC_UUID_FILEINFO,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
// Create a BLE Descriptor
//pCharacteristicFilename->addDescriptor(new BLE2902());
// Wand settins
pCharacteristicWandSettings = pService->createCharacteristic(
CHARACTERISTIC_UUID_WANDSETTINGS,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
// run command
pCharacteristicRun = pService->createCharacteristic(
CHARACTERISTIC_UUID_RUN,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
// all the filenames
pCharacteristicFileList = pService->createCharacteristic(
CHARACTERISTIC_UUID_FILELIST,
BLECharacteristic::PROPERTY_READ
);
// add anybody that can be changed or can call us with something to do
MyCharacteristicCallbacks* pCallBacks = new MyCharacteristicCallbacks();
pCharacteristicRun->setCallbacks(pCallBacks);
pCharacteristicWandSettings->setCallbacks(pCallBacks);
pCharacteristicFileInfo->setCallbacks(pCallBacks);
UpdateBLE(false);
pService->start();
pServiceDevInfo->start();
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->addServiceUUID(BLEUUID((uint16_t)0x180a));
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
}
void setup()
{
Serial.begin(115200);
delay(100);
//Serial.println("boot: " + String(nBootCount));
CRotaryDialButton::getInstance()->begin(BTN_A, BTN_B, BTN_PUSH);
setupSDcard();
gpio_set_direction((gpio_num_t)LED, GPIO_MODE_OUTPUT);
digitalWrite(LED, HIGH);
gpio_set_direction(FRAMEBUTTON, GPIO_MODE_INPUT);
gpio_set_pull_mode(FRAMEBUTTON, GPIO_PULLUP_ONLY);
oneshot_LED_timer_args = {
oneshot_LED_timer_callback,
/* argument specified here will be passed to timer callback function */
(void*)0,
ESP_TIMER_TASK,
"one-shotLED"
};
esp_timer_create(&oneshot_LED_timer_args, &oneshot_LED_timer);
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Enable*/, true /*Serial Enable*/);
delay(100);
digitalWrite(LED, LOW);
int width = OLED->width();
int height = OLED->height();
OLED->clear();
if (nBootCount == 0) {
OLED->drawRect(0, 0, width - 1, height - 1);
OLED->setFont(ArialMT_Plain_24);
OLED->drawString(2, 2, "LEDPainter");
OLED->setFont(ArialMT_Plain_16);
OLED->drawString(4, 30, "Version 2.17");
OLED->setFont(ArialMT_Plain_10);
OLED->drawString(4, 48, __DATE__);
OLED->display();
}
//OLED->setFont(ArialMT_Plain_10);
charHeight = 13;
EEPROM.begin(1024);
// this will fix the signature if necessary
if (SaveSettings(false, true)) {
// get the autoload flag
SaveSettings(false, false, true);
}
// load the saved settings if flag is true and the button isn't pushed
if ((nBootCount == 0) && bAutoLoadSettings && gpio_get_level(BTN_PUSH)) {
// read all the settings
SaveSettings(false);
}
menuPtr = new MenuInfo;
MenuStack.push(menuPtr);
MenuStack.peek()->menu = MainMenu;
MenuStack.peek()->index = 0;
MenuStack.peek()->offset = 0;
//const int ledPin = 16; // 16 corresponds to GPIO16
//// configure LED PWM functionalitites
//ledcSetup(ledChannel, freq, resolution);
//// attach the channel to the GPIO to be controlled
//ledcAttachPin(WandPin, ledChannel);
//ledcWrite(ledChannel, 200);
FastLED.addLeds<NEOPIXEL, DATA_PIN1>(leds, 0, NUM_LEDS);
//FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds, 0, NUM_LEDS); // to test parallel second strip
//if (bSecondStrip)
// create the second led controller
FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds, NUM_LEDS, NUM_LEDS);
//FastLED.setTemperature(whiteBalance);
FastLED.setTemperature(CRGB(whiteBalance.r, whiteBalance.g, whiteBalance.b));
FastLED.setBrightness(nStripBrightness);
if (nBootCount == 0) {
//bool oldSecond = bSecondStrip;
//bSecondStrip = true;
RainbowPulse();
//bSecondStrip = oldSecond;
//// Turn the LED on, then pause
//SetPixel(0, CRGB::Red);
//SetPixel(1, CRGB::Red);
//SetPixel(4, CRGB::Green);
//SetPixel(5, CRGB::Green);
//SetPixel(8, CRGB::Blue);
//SetPixel(9, CRGB::Blue);
//SetPixel(12, CRGB::White);
//SetPixel(13, CRGB::White);
//SetPixel(NUM_LEDS - 0, CRGB::Red);
//SetPixel(NUM_LEDS - 1, CRGB::Red);
//SetPixel(NUM_LEDS - 4, CRGB::Green);
//SetPixel(NUM_LEDS - 5, CRGB::Green);
//SetPixel(NUM_LEDS - 8, CRGB::Blue);
//SetPixel(NUM_LEDS - 9, CRGB::Blue);
//SetPixel(NUM_LEDS - 12, CRGB::White);
//SetPixel(NUM_LEDS - 13, CRGB::White);
//SetPixel(0 + NUM_LEDS, CRGB::Red);
//SetPixel(1 + NUM_LEDS, CRGB::Red);
//SetPixel(4 + NUM_LEDS, CRGB::Green);
//SetPixel(5 + NUM_LEDS, CRGB::Green);
//SetPixel(8 + NUM_LEDS, CRGB::Blue);
//SetPixel(9 + NUM_LEDS, CRGB::Blue);
//SetPixel(12 + NUM_LEDS, CRGB::White);
//SetPixel(13 + NUM_LEDS, CRGB::White);
//for (int ix = 0; ix < 255; ix += 5) {
// FastLED.setBrightness(ix);
// FastLED.show();
// delayMicroseconds(50);
//}
//for (int ix = 255; ix >= 0; ix -= 5) {
// FastLED.setBrightness(ix);
// FastLED.show();
// delayMicroseconds(50);
//}
//delayMicroseconds(50);
//FastLED.clear(true);
//delayMicroseconds(50);
//FastLED.setBrightness(nStripBrightness);
//delay(50);
//// Now turn the LED off
//FastLED.clear(true);
//delayMicroseconds(50);
//// run a white dot up the display and back
//for (int ix = 0; ix < STRIPLENGTH; ++ix) {
// SetPixel(ix, CRGB::White);
// if (ix)
// SetPixel(ix - 1, CRGB::Black);
// FastLED.show();
// delayMicroseconds(50);
//}
//for (int ix = STRIPLENGTH - 1; ix >= 0; --ix) {
// SetPixel(ix, CRGB::White);
// if (ix)
// SetPixel(ix + 1, CRGB::Black);
// FastLED.show();
// delayMicroseconds(50);
//}
}
FastLED.clear(true);
delay(100);
OLED->clear();
if (bEnableBLE) {
EnableBLE();
}
// wait for button release
while (!digitalRead(BTN_PUSH))
;
delay(30); // debounce
while (!digitalRead(BTN_PUSH))
;
// clear the button buffer
CRotaryDialButton::getInstance()->clear();
if (!bSdCardValid) {
DisplayCurrentFile();
delay(2000);
ToggleFilesBuiltin(NULL);
}
DisplayCurrentFile();
/*
analogSetCycles(8); // Set number of cycles per sample, default is 8 and provides an optimal result, range is 1 - 255
analogSetSamples(1); // Set number of samples in the range, default is 1, it has an effect on sensitivity has been multiplied
analogSetClockDiv(1); // Set the divider for the ADC clock, default is 1, range is 1 - 255
analogSetAttenuation(ADC_11db); // Sets the input attenuation for ALL ADC inputs, default is ADC_11db, range is ADC_0db, ADC_2_5db, ADC_6db, ADC_11db
//analogSetPinAttenuation(36, ADC_11db); // Sets the input attenuation, default is ADC_11db, range is ADC_0db, ADC_2_5db, ADC_6db, ADC_11db
analogSetPinAttenuation(37, ADC_11db);
// ADC_0db provides no attenuation so IN/OUT = 1 / 1 an input of 3 volts remains at 3 volts before ADC measurement
// ADC_2_5db provides an attenuation so that IN/OUT = 1 / 1.34 an input of 3 volts is reduced to 2.238 volts before ADC measurement
// ADC_6db provides an attenuation so that IN/OUT = 1 / 2 an input of 3 volts is reduced to 1.500 volts before ADC measurement
// ADC_11db provides an attenuation so that IN/OUT = 1 / 3.6 an input of 3 volts is reduced to 0.833 volts before ADC measurement
// adcAttachPin(VP); // Attach a pin to ADC (also clears any other analog mode that could be on), returns TRUE/FALSE result
// adcStart(VP); // Starts an ADC conversion on attached pin's bus
// adcBusy(VP); // Check if conversion on the pin's ADC bus is currently running, returns TRUE/FALSE result
// adcEnd(VP);
//adcAttachPin(36);
adcAttachPin(37);
*/
}
void loop()
{
static bool didsomething = false;
bool lastStrip = bSecondStrip;
bool bLastEnableBLE = bEnableBLE;
//int lastDisplayBrightness = displayBrightness;
didsomething = bSettingsMode ? HandleMenus() : HandleRunMode();
// special handling for things that might have changed
//if (lastDisplayBrightness != displayBrightness) {
// OLED->setBrightness(displayBrightness);
//}
//if (lastStrip != bSecondStrip) {
//******** this crashes
//if (bSecondStrip)
// FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds, NUM_LEDS, NUM_LEDS);
//else
// FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds, NUM_LEDS, 0);
//}
// wait for no keys
if (didsomething) {
//Serial.println("calling wait for none");
UpdateBLE(false);
didsomething = false;
// see if BLE enabled
if (bEnableBLE != bLastEnableBLE) {
if (bEnableBLE) {
EnableBLE();
}
else {
// shutdown BLE
BLEDeviceConnected = false;
// TODO: is there anything else we need to do here?
}
}
delay(1);
}
// disconnecting
if (!BLEDeviceConnected && oldBLEDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
BLEDevice::startAdvertising();
//Serial.println("start advertising");
oldBLEDeviceConnected = BLEDeviceConnected;
}
// connecting
if (BLEDeviceConnected && !oldBLEDeviceConnected) {
// do stuff here on connecting
oldBLEDeviceConnected = BLEDeviceConnected;
UpdateBLE(false);
}
}
void UpdateBLE(bool bProgressOnly)
{
// update the settings, except for progress percent which is done in ShowProgress.
if (BLEDeviceConnected) {
String js;
// running information and commands
DynamicJsonDocument runinfo(256);
runinfo["running"] = bIsRunning;
runinfo["progress"] = nProgress;
runinfo["repeatsleft"] = nRepeatsLeft;
js = "";
serializeJson(runinfo, js);
pCharacteristicRun->setValue(js.c_str());
if (!bProgressOnly) {
// file information
DynamicJsonDocument filesdoc(1024);
filesdoc["builtin"] = bShowBuiltInTests;
filesdoc["path"] = currentFolder.c_str();
filesdoc["current"] = FileNames[CurrentFileIndex].c_str();
js = "";
serializeJson(filesdoc, js);
pCharacteristicFileInfo->setValue(js.c_str());
// settings
DynamicJsonDocument wsdoc(1024);
wsdoc["secondstrip"] = bSecondStrip;
wsdoc["bright"] = nStripBrightness;
wsdoc["framehold"] = nFrameHold;
wsdoc["framebutton"] = nFramePulseCount;
wsdoc["startdelay"] = startDelay;
wsdoc["repeatdelay"] = repeatDelay;
wsdoc["repeatcount"] = repeatCount;
wsdoc["gamma"] = bGammaCorrection;
wsdoc["reverse"] = bReverseImage;
wsdoc["upsidedown"] = bUpsideDown;
wsdoc["mirror"] = bMirrorPlayImage;
wsdoc["chain"] = bChainFiles;
wsdoc["chainrepeats"] = nChainRepeats;
wsdoc["scaley"] = bScaleHeight;
js = "";
serializeJson(wsdoc, js);
pCharacteristicWandSettings->setValue(js.c_str());
// file list
DynamicJsonDocument filelist(512);
filelist["builtin"] = bShowBuiltInTests;
filelist["path"] = currentFolder.c_str();
filelist["count"] = FileNames.size();
filelist["ix"] = CurrentFileIndex;
JsonArray data = filelist.createNestedArray("files");
for (auto st : FileNames) {
data.add(st.c_str());
}
js = "";
serializeJson(filelist, js);
pCharacteristicFileList->setValue(js.c_str());
}
}
}
bool RunMenus(int button)
{
// save this so we can see if we need to save a new changed value
bool lastAutoLoadFlag = bAutoLoadSettings;
// see if we got a menu match
bool gotmatch = false;
int menuix = 0;
MenuInfo* oldMenu;
bool bExit = false;
for (int ix = 0; !gotmatch && MenuStack.peek()->menu[ix].op != eTerminate; ++ix) {
// see if this is one is valid
if (!MenuStack.peek()->menu[ix].valid) {
continue;
}
//Serial.println("menu button: " + String(button));
if (button == BTN_SELECT && menuix == MenuStack.peek()->index) {
//Serial.println("got match " + String(menuix) + " " + String(MenuStack.peek()->index));
gotmatch = true;
//Serial.println("clicked on menu");
// got one, service it
switch (MenuStack.peek()->menu[ix].op) {
case eText:
case eTextInt:
case eTextCurrentFile:
case eBool:
bMenuChanged = true;
if (MenuStack.peek()->menu[ix].function) {
(*MenuStack.peek()->menu[ix].function)(&MenuStack.peek()->menu[ix]);
}
break;
case eList:
bMenuChanged = true;
if (MenuStack.peek()->menu[ix].function) {
(*MenuStack.peek()->menu[ix].function)(&MenuStack.peek()->menu[ix]);
}
bExit = true;
// if there is a value, set the min value in it
if (MenuStack.peek()->menu[ix].value) {
*(int*)MenuStack.peek()->menu[ix].value = MenuStack.peek()->menu[ix].min;
}
break;
case eMenu:
if (MenuStack.peek()->menu) {
oldMenu = MenuStack.peek();
MenuStack.push(new MenuInfo);
MenuStack.peek()->menu = oldMenu->menu[ix].menu;
bMenuChanged = true;
MenuStack.peek()->index = 0;
MenuStack.peek()->offset = 0;
//Serial.println("change menu");
// check if the new menu is an eList and if it has a value, if it does, set the index to it
if (MenuStack.peek()->menu->op == eList && MenuStack.peek()->menu->value) {
int ix = *(int*)MenuStack.peek()->menu->value;
MenuStack.peek()->index = ix;
// adjust offset if necessary
if (ix > 4) {
MenuStack.peek()->offset = ix - 4;
}
}
}
break;
case eBuiltinOptions: // find it in builtins
if (BuiltInFiles[CurrentFileIndex].menu != NULL) {
MenuStack.peek()->index = MenuStack.peek()->index;
MenuStack.push(new MenuInfo);
MenuStack.peek()->menu = BuiltInFiles[CurrentFileIndex].menu;
MenuStack.peek()->index = 0;
MenuStack.peek()->offset = 0;
}
else {
WriteMessage("No settings available for:\n" + String(BuiltInFiles[CurrentFileIndex].text));
}
bMenuChanged = true;
break;
case eExit: // go back a level
bExit = true;
break;
case eReboot:
WriteMessage("Rebooting in 2 seconds\nHold button for factory reset", false, 2000);
ESP.restart();
break;
}
}
++menuix;
}
// if no match, and we are in a submenu, go back one level, or if bExit is set
if (bExit || (!bMenuChanged && MenuStack.count() > 1)) {
bMenuChanged = true;
menuPtr = MenuStack.pop();
delete menuPtr;
}
// see if the autoload flag changed
if (bAutoLoadSettings != lastAutoLoadFlag) {
// the flag is now true, so we should save the current settings
SaveSettings(true, false, true);
}
}
// display the menu
// if MenuStack.peek()->index is > 5, then shift the lines up by enough to display them
// remember that we only have room for 5 lines
void ShowMenu(struct MenuItem* menu)
{
MenuStack.peek()->menucount = 0;
int y = 0;
int x = 0;
char line[100];
bool skip = false;
// loop through the menu
for (; menu->op != eTerminate; ++menu) {
menu->valid = false;
switch (menu->op) {
case eIfEqual:
// skip the next one if match, only booleans are handled so far
skip = *(bool*)menu->value != (menu->min ? true : false);
//Serial.println("ifequal test: skip: " + String(skip));
break;
case eElse:
skip = !skip;
break;
case eEndif:
skip = false;
break;
}
if (skip) {
menu->valid = false;
continue;
}
char line[100], xtraline[100];
// only displayable menu items should be in this switch
line[0] = '\0';
int val;
bool exists;
switch (menu->op) {
case eTextInt:
case eText:
case eTextCurrentFile:
menu->valid = true;
if (menu->value) {
val = *(int*)menu->value;
if (menu->op == eText)
sprintf(line, menu->text, val);
else if (menu->op == eTextInt) {
if (menu->decimals == 0) {
sprintf(line, menu->text, val);
}
else {
sprintf(line, menu->text, val / 10, val % 10);
}
}
//Serial.println("menu text1: " + String(line));
}
else {
if (menu->op == eTextCurrentFile) {
sprintf(line, menu->text, MakeIPCFilename(FileNames[CurrentFileIndex], false).c_str());
//Serial.println("menu text2: " + String(line));
}
else {
strcpy(line, menu->text);
//Serial.println("menu text3: " + String(line));
}
}
// next line
++y;
break;
case eList:
menu->valid = true;
// the list of macro files
// min holds the macro number
val = menu->min;
// see if the macro is there and append the text
exists = SD.exists("/" + String(val) + ".ipc");
sprintf(line, menu->text, val, exists ? menu->on : menu->off);
// next line
++y;
break;
case eBool:
menu->valid = true;
if (menu->value) {
// clean extra bits, just in case
bool* pb = (bool*)menu->value;
//*pb &= 1;
sprintf(line, menu->text, *pb ? menu->on : menu->off);
//Serial.println("bool line: " + String(line));
}
else {
strcpy(line, menu->text);
}
// increment displayable lines
++y;
break;
case eBuiltinOptions:
// for builtins only show if available
if (BuiltInFiles[CurrentFileIndex].menu != NULL) {
menu->valid = true;
sprintf(line, menu->text, BuiltInFiles[CurrentFileIndex].text);
++y;
}
break;
case eMenu:
case eExit:
case eReboot:
menu->valid = true;
if (menu->value) {
sprintf(xtraline, menu->text, *(int*)menu->value);
}
else {
strcpy(xtraline, menu->text);
}
if (menu->op == eExit)
sprintf(line, "%s%s", "-", xtraline);
else
sprintf(line, "%s%s", (menu->op == eReboot) ? "" : "+", xtraline);
++y;
//Serial.println("menu text4: " + String(line));
break;
}
if (strlen(line) && y >= MenuStack.peek()->offset) {
DisplayMenuLine(y - 1, y - 1 - MenuStack.peek()->offset, line);
}
}
//Serial.println("menu: " + String(offsetLines) + ":" + String(y) + " active: " + String(MenuStack.peek()->index));
MenuStack.peek()->menucount = y;
// blank the rest of the lines
for (int ix = y; ix < 5; ++ix) {
DisplayLine(ix, "");
}
// show line if menu has been scrolled
if (MenuStack.peek()->offset > 0)
OLED->drawLine(0, 0, 5, 0);
// show bottom line if last line is showing
if (MenuStack.peek()->offset + 4 < MenuStack.peek()->menucount - 1)
OLED->drawLine(0, OLED->getHeight() - 1, 5, OLED->getHeight() - 1);
OLED->display();
}
// switch between SD and built-ins
void ToggleFilesBuiltin(MenuItem* menu)
{
// clear filenames list
FileNames.clear();
bool lastval = bShowBuiltInTests;
int oldIndex = CurrentFileIndex;
String oldFolder = currentFolder;
if (menu != NULL) {
ToggleBool(menu);
}
else {
bShowBuiltInTests = !bShowBuiltInTests;
}
if (lastval != bShowBuiltInTests) {
if (bShowBuiltInTests) {
CurrentFileIndex = 0;
for (int ix = 0; ix < sizeof(BuiltInFiles) / sizeof(*BuiltInFiles); ++ix) {
// add each one
FileNames.push_back(String(BuiltInFiles[ix].text));
}
currentFolder = "";
}
else {
// read the SD
currentFolder = lastFolder;
GetFileNamesFromSD(currentFolder);
}
}
// restore indexes
CurrentFileIndex = lastFileIndex;
lastFileIndex = oldIndex;
currentFolder = lastFolder;
lastFolder = oldFolder;
}
// toggle a boolean value
void ToggleBool(MenuItem* menu)
{
bool* pb = (bool*)menu->value;
*pb = !*pb;
if (menu->change != NULL) {
(*menu->change)(menu, -1);
}
//Serial.println("autoload: " + String(bAutoLoadSettings));
//Serial.println("fixed time: " + String(bFixedTime));
}
// get integer values
void GetIntegerValue(MenuItem* menu)
{
// -1 means to reset to original
int stepSize = 1;
int originalValue = *(int*)menu->value;
//Serial.println("int: " + String(menu->text) + String(*(int*)menu->value));
char line[50];
CRotaryDialButton::Button button = BTN_NONE;
bool done = false;
OLED->clear();
DisplayLine(1, "Range: " + String(menu->min) + " to " + String(menu->max));
DisplayLine(3, "Long Press to Accept");
int oldVal = *(int*)menu->value;
if (menu->change != NULL) {
(*menu->change)(menu, 1);
}
do {
//Serial.println("button: " + String(button));
switch (button) {
case BTN_LEFT:
if (stepSize != -1)
*(int*)menu->value -= stepSize;
break;
case BTN_RIGHT:
if (stepSize != -1)
*(int*)menu->value += stepSize;
break;
case BTN_SELECT:
if (stepSize == -1) {
stepSize = 1;
}
else {
stepSize *= 10;
}
if (stepSize > (menu->max / 10)) {
stepSize = -1;
}
break;
case BTN_LONG:
if (stepSize == -1) {
*(int*)menu->value = originalValue;
stepSize = 1;
}
else {
done = true;
}
break;
}
// make sure within limits
*(int*)menu->value = constrain(*(int*)menu->value, menu->min, menu->max);
// show slider bar
OLEDDISPLAY_COLOR oldColor = OLED->getColor();
OLED->setColor(OLEDDISPLAY_COLOR::BLACK);
OLED->fillRect(0, 30, OLED->width() - 1, 6);
OLED->setColor(oldColor);
OLED->drawProgressBar(0, 30, OLED->width() - 1, 6, map(*(int*)menu->value, menu->min, menu->max, 0, 100));
if (menu->decimals == 0) {
sprintf(line, menu->text, *(int*)menu->value);
}
else {
sprintf(line, menu->text, *(int*)menu->value / 10, *(int*)menu->value % 10);
}
DisplayLine(0, line);
DisplayLine(4, stepSize == -1 ? "Reset: long press (Click +)" : "step: " + String(stepSize) + " (Click +)");
if (menu->change != NULL && oldVal != *(int*)menu->value) {
(*menu->change)(menu, 0);
oldVal = *(int*)menu->value;
}
while (!done && (button = ReadButton()) == BTN_NONE) {
delay(1);
}
} while (!done);
if (menu->change != NULL) {
(*menu->change)(menu, -1);
}
}
void UpdateStripBrightness(MenuItem* menu, int flag)
{
switch (flag) {
case 1: // first time
for (int ix = 0; ix < 64; ++ix) {
SetPixel(ix, CRGB::White);
}
FastLED.show();
break;
case 0: // every change
FastLED.setBrightness(*(int*)menu->value);
FastLED.show();
break;
case -1: // last time
FastLED.clear(true);
break;
}
}
void UpdateStripWhiteBalanceR(MenuItem* menu, int flag)
{
switch (flag) {
case 1: // first time
for (int ix = 0; ix < 64; ++ix) {
SetPixel(ix, CRGB::White);
}
FastLED.show();
break;
case 0: // every change
FastLED.setTemperature(CRGB(*(int*)menu->value, whiteBalance.g, whiteBalance.b));
FastLED.show();
break;
case -1: // last time
FastLED.clear(true);
break;
}
}
void UpdateStripWhiteBalanceG(MenuItem* menu, int flag)
{
switch (flag) {
case 1: // first time
for (int ix = 0; ix < 64; ++ix) {
SetPixel(ix, CRGB::White);
}
FastLED.show();
break;
case 0: // every change
FastLED.setTemperature(CRGB(whiteBalance.r, *(int*)menu->value, whiteBalance.b));
FastLED.show();
break;
case -1: // last time
FastLED.clear(true);
break;
}
}
void UpdateStripWhiteBalanceB(MenuItem* menu, int flag)
{
switch (flag) {
case 1: // first time
for (int ix = 0; ix < 64; ++ix) {
SetPixel(ix, CRGB::White);
}
FastLED.show();
break;
case 0: // every change
FastLED.setTemperature(CRGB(whiteBalance.r, whiteBalance.g, *(int*)menu->value));
FastLED.show();
break;
case -1: // last time
FastLED.clear(true);
break;
}
}
void UpdateOledBrightness(MenuItem* menu, int flag)
{
OLED->setBrightness(map(*(int*)menu->value, 0, 100, 0, 255));
bDisplayInvert ? OLED->invertDisplay() : OLED->normalDisplay();
}
void UpdateOledInvert(MenuItem* menu, int flag)
{
*(bool*)menu->value ? OLED->invertDisplay() : OLED->normalDisplay();
}
// handle the menus
bool HandleMenus()
{
if (bMenuChanged) {
ShowMenu(MenuStack.peek()->menu);
bMenuChanged = false;
}
bool didsomething = true;
CRotaryDialButton::Button button = ReadButton();
int lastOffset = MenuStack.peek()->offset;
int lastMenu = MenuStack.peek()->index;
int lastMenuCount = MenuStack.peek()->menucount;
bool lastRecording = bRecordingMacro;
switch (button) {
case BTN_SELECT:
RunMenus(button);
bMenuChanged = true;
break;
case BTN_RIGHT:
if (bAllowMenuWrap || MenuStack.peek()->index < MenuStack.peek()->menucount - 1) {
++MenuStack.peek()->index;