forked from happyhappysundays/SparkBox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUI.ino
1750 lines (1624 loc) · 60.3 KB
/
UI.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
extern ESPxWebFlMgr filemgr; // Filemanager instance
// Overlay static graphics ============================================================
void screenOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) {
uint8_t conn_icons = inWifi ? 1 : 2;
uint8_t visibleLeft = (CONN_ICON_WIDTH+1)*conn_icons; // calculate the place to show compact scrolling name at the top line
uint8_t visibleW = display->width() - BATT_WIDTH - visibleLeft - 1;
if (isTimeout) {
readBattery(); // Read analog voltage and average it
isTimeout = false;
}
if ( curMode==MODE_PRESETS || curMode==MODE_BYPASS) {
display->setColor(BLACK);
display->fillRect(visibleLeft, 0, display->width()-BATT_WIDTH-visibleLeft, STATUS_HEIGHT);
fxIcons();
}
display->setColor(BLACK);
display->fillRect(0, 0, visibleLeft, STATUS_HEIGHT);
display->fillRect(display->width()-BATT_WIDTH-1, 0, BATT_WIDTH+1, STATUS_HEIGHT);
mainIcons();
/*
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setColor(INVERSE);
display->setFont(MEDIUM_FONT);
display->drawString(0, 17, (String)loopTime);
*/
}
// frSomething functions are frame drawing of the UI
// PRESETS MODE =======================================================================
void frPresets(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
static uint8_t scrollStep = 2; // speed of horiz scrolling tone names
static ulong scrollCounter;
display->setColor(WHITE);
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(HUGE_FONT);
int numW = display->getStringWidth((String)(display_preset_num + 1))+5;
display->setFont(BIG_FONT);
int nameW = display->getStringWidth(presets[CUR_EDITING].Name)+5;
if (numW+nameW <= display->width()) {
scroller = ( display->width() - numW ) / 2;
display->setTextAlignment(TEXT_ALIGN_CENTER);
} else {
display->setTextAlignment(TEXT_ALIGN_LEFT);
if ( millis() > scrollCounter ) {
scroller = scroller - scrollStep;
if (scroller <= 0) {
scroller = scroller + nameW;
}
scrollCounter = millis() + 20;
}
display->setFont(BIG_FONT); // Draw a preset name
display->drawString( x + scroller - nameW , y + STATUS_HEIGHT, presets[CUR_EDITING].Name);
}
display->setFont(BIG_FONT); // Draw a preset name
display->drawString( x + scroller , y + STATUS_HEIGHT, presets[CUR_EDITING].Name);
display->setColor(BLACK);
display->fillRect(display->width()-numW+x+2, STATUS_HEIGHT+y, numW-2, display->height()-STATUS_HEIGHT);
display->setFont(HUGE_FONT); // Gonna draw a preset number
display->setColor(WHITE);
display->setTextAlignment(TEXT_ALIGN_RIGHT);
display->drawString( display->width()+x, STATUS_HEIGHT-7+y, (String)(display_preset_num + 1) ); // +1 for humans
display->setFont(BIG_FONT); // Draw a bank num
display->setTextAlignment(TEXT_ALIGN_LEFT);
static String bankN;
bankN = localBankNum>0 ? (String)(localBankNum):"HW";
display->drawString( x-2, display->height()-23+y, "bnk: " + bankN ); // No +1, cause humans can see these folders in a raw manner
}
// EFFECTS MODE =======================================================================
void frEffects(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
static uint8_t scrollStep = 1; // speed of horiz scrolling tone names
static ulong scrollCounter;
uint8_t conn_icons = inWifi ? 1 : 2;
int visibleLeft = (CONN_ICON_WIDTH+1)*conn_icons; // calculate the place to show compact scrolling name at the top line
int visibleW = display->width() - BATT_WIDTH - visibleLeft - 1;
display->setColor(WHITE);
fxHugeIcons(x,y); // Big FX icons
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(SMALL_FONT);
int numW = display->getStringWidth((String)(display_preset_num + 1))+5; // Width of the 1st string representing the preset number
int nameW = display->getStringWidth(presets[CUR_EDITING].Name)+5; // Width of the 2nd string representing the name of the preset
if (numW+nameW <= visibleW) {
scroller = ( visibleW - numW - nameW ) / 2;
} else {
if ( millis() > scrollCounter ) {
scroller = scroller - scrollStep;
if (scroller <= 0) {
scroller = nameW + numW;
}
scrollCounter = millis() + 20;
}
display->drawString( visibleLeft + x + scroller - numW - nameW, y, (String)(display_preset_num + 1) ); // +1 for humans
display->drawString( visibleLeft + x + scroller - nameW, y, presets[CUR_EDITING].Name);
}
display->drawString( visibleLeft + x + scroller, y, (String)(display_preset_num + 1) ); // +1 for humans
display->drawString( visibleLeft + x + scroller + numW, y, presets[CUR_EDITING].Name);
}
// BANK SELECT MODE =======================================================================
void frBanks(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
static String bankN;
bankN = pendingBankNum>0 ? (String)(pendingBankNum):"HW";
static int scrollStep = -2; // speed of horiz scrolling tone names
static ulong scrollCounter;
display->setColor(WHITE);
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->setFont(HUGE_FONT);
int numW = display->getStringWidth(bankN);
display->setFont(BIG_FONT);
int nameW = display->getStringWidth(bankConfig[pendingBankNum].bank_name)+5;
if (numW+nameW <= display->width()) {
scroller = numW + ( display->width() - numW ) / 2;
display->setTextAlignment(TEXT_ALIGN_CENTER);
} else {
display->setTextAlignment(TEXT_ALIGN_LEFT);
if ( millis() > scrollCounter ) {
scroller = scroller + scrollStep;
if (scroller <= 0) {
scroller = scroller + nameW;
}
scrollCounter = millis() + 20;
}
display->setFont(BIG_FONT);
display->drawString( x + scroller - nameW + numW, y + display->height()/2 - 6, bankConfig[pendingBankNum].bank_name);
}
display->setFont(BIG_FONT);
display->drawString( x + scroller + numW, y + display->height()/2 - 6, bankConfig[pendingBankNum].bank_name);
display->setColor(BLACK);
display->fillRect(x, STATUS_HEIGHT+y, numW, display->height()-STATUS_HEIGHT);
display->setFont(HUGE_FONT);
display->setColor(WHITE);
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->drawString( x, STATUS_HEIGHT-11+y, bankN ); // No +1 cause folder names are also accessable via web so just made them 1-based
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->setFont(SMALL_FONT);
display->drawString(display->width()/2 + x, y, "Bank select");
hintIcons(x,y);
}
// BANK CONFIG MODE =======================================================================
void frConfig(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
hintIcons(x,y);
}
// BYPASS MODE =======================================================================
void frBypass(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
display->setColor(WHITE);
display->setFont(BIG_FONT);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->drawString(display->width()/2 + x, 20 + y, "BYPASS" );
hintIcons(x,y);
}
// MESSAGE MODE =======================================================================
void frMessage(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
// splash screen with a text message
int h = 0;
int h1 = 0;
display->setColor(WHITE);
display->setFont(SMALL_FONT);
display->setTextAlignment(TEXT_ALIGN_CENTER);
if (inWifi) {
display->drawString(CONN_ICON_WIDTH + (display->width()-BATT_WIDTH-CONN_ICON_WIDTH) /2 + x, y, msgCaption);
} else {
display->drawString(display->width()/2 + x, y, msgCaption);
}
if (msgText1.length()>0) {
display->setFont(MEDIUM_FONT);
h1 = 12;
if(display->getStringWidth(msgText1)>display->width()) {
display->setFont(SMALL_FONT);
h1 = 6;
}
} else {
h1 = 0;
}
uint8_t freeH = (oled.height() - STATUS_HEIGHT)/2 - h1 - h1;
display->drawString((display->width())/2 + x, STATUS_HEIGHT + (oled.height()-STATUS_HEIGHT)/2 + freeH/2 + y, msgText1 );
if (msgText.length()>0) {
display->setFont(MEDIUM_FONT);
h = 12;
if(display->getStringWidth(msgText)>display->width()) {
display->setFont(SMALL_FONT);
h = 6;
}
} else {
h = 0;
}
freeH = (oled.height() - STATUS_HEIGHT)/2 - h - h1;
display->drawString((display->width())/2 + x, STATUS_HEIGHT + freeH/2 + y , msgText );
}
// FX LEVEL MODE ======================================================================
void frLevel(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
// indicates parameter change
display->setColor(WHITE);
display->setFont(SMALL_FONT);
display->setTextAlignment(TEXT_ALIGN_CENTER);
display->drawString(display->width()/2 + x, y, fxCaption);
display->setFont(HUGE_FONT);
display->setTextAlignment(TEXT_ALIGN_CENTER);
sprintf(str,"%3.1f",(float)(level)/10);
if(display->getStringWidth(str)>display->width()) {
display->setFont(BIG_FONT);
}
display->drawString((display->width())/2 + x, STATUS_HEIGHT- 11 + y , (String)(str) );
hintIcons(x,y);
}
// TUNER MODE =======================================================================
void frTuner(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) {
const String note_names[] = {"...","C","C#","D","D#","E","F","F#","G","G#","A","A#","B","..."};
//String note_names[] = {"...","Do","Do#","Re","Re#","Mi","Fa","Fa#","Sol","Sol#","La","La#","Si","..."};
int16_t val_deg = 0;
int16_t meter_x = 0;
int16_t meter_y = 0;
int16_t hub_x = 0;
int16_t hub_y = 0;
float test_val;
// Show tuner screen when requested by Spark
display->setColor(WHITE);
// Default display - draw meter bitmap and label
display->drawXbm(0+x, Y5+y, tuner_width, tuner_height, tuner_bits);
//display->setTextAlignment(TEXT_ALIGN_LEFT);
//display->drawString(0,0,"Tuner");
test_val = msg.val;
//test_val = (millis() % 2000)/2000.0;
display->setFont(MEDIUM_FONT);
display->setTextAlignment(TEXT_ALIGN_CENTER);
if (test_val != -1.0) {
// Show note names
display->drawString(display->width()/2+x,note_y+y,note_names[constrain(msg.param1+1,0,13)]); // The first and the last note_names[] are '...' such we name everything outside the bounds
display->drawString(display->width()/2+x-1,note_y+y,note_names[constrain(msg.param1+1,0,13)]); // Fake bold
// If something to show
// Work out start and end-points of meter needle
val_deg = constrain(int16_t(test_val * 180.0), 0, 180); // Span tuner's 0-1.0, to 0-180 degrees
meter_x = (tuner_width/2) - (tuner_width/2)*sin(radians(90-val_deg));
meter_y = (display->height()) - (tuner_height)*cos(radians(90-val_deg))+ (display->height()-tuner_height*tuner_share/4);
hub_x = (tuner_width/2) - (tuner_width/2/4)*sin(radians(90-val_deg));
hub_y = (display->height()) - (display->height()/4)*cos(radians(90-val_deg)) ;
display->drawLine(meter_x+x, meter_y+y, hub_x+x, hub_y+y); // Draw line from hub to meter edge
if (meter_x >= hub_x-1 && meter_x <= hub_x+1) {
// Fine tune signaling
display->setColor(INVERSE);
display->fillRect(0,0,display->width(),STATUS_HEIGHT);
}
} else {
// Nothing to show
display->drawString(display->width()/2+x,note_y+y,"..."); // Not detected
}
}
// Draw connection status icons and the battery icon
void mainIcons() {
if (!inWifi){
// Spark Amp BT connection icon
drawStatusIcon(s_bt_bits, s_bt_width, STATUS_HEIGHT, 0, 0, CONN_ICON_WIDTH, STATUS_HEIGHT, spark_state==SPARK_SYNCED);
// App connection icon
drawStatusIcon(a_bt_bits, a_bt_width, STATUS_HEIGHT, CONN_ICON_WIDTH+1, 0, CONN_ICON_WIDTH, STATUS_HEIGHT, conn_status[APP]);
} else {
// WiFi connection icon
drawStatusIcon(wifi_bits, wifi_width, STATUS_HEIGHT, 0, 0, CONN_ICON_WIDTH, STATUS_HEIGHT, wifi_connected);
}
// Battery icon
drawBatteryH(oled.width()-BATT_WIDTH, 0, BATT_WIDTH, STATUS_HEIGHT, batteryPercent(vbat_result), batteryCharging());
}
// Draw fx on/off icons (for the status line)
void fxIcons() {
uint8_t conn_icons = inWifi ? 1 : 2;
// Drive icon
drawStatusIcon(dr_bits, dr_width, STATUS_HEIGHT, conn_icons*(CONN_ICON_WIDTH+1)+1, 0, FX_ICON_WIDTH, STATUS_HEIGHT, presets[CUR_EDITING].effects[FX_DRIVE].OnOff);
// Mod icon
drawStatusIcon(md_bits, md_width, STATUS_HEIGHT, conn_icons*(CONN_ICON_WIDTH+1)+1+FX_ICON_WIDTH+1, 0, FX_ICON_WIDTH, STATUS_HEIGHT, presets[CUR_EDITING].effects[FX_MOD].OnOff);
// Delay icon
drawStatusIcon(dy_bits, dy_width, STATUS_HEIGHT, conn_icons*(CONN_ICON_WIDTH+1)+1+(FX_ICON_WIDTH+1)*2, 0, FX_ICON_WIDTH, STATUS_HEIGHT, presets[CUR_EDITING].effects[FX_DELAY].OnOff);
// Reverb icon
drawStatusIcon(rv_bits, rv_width, STATUS_HEIGHT, conn_icons*(CONN_ICON_WIDTH+1)+1+(FX_ICON_WIDTH+1)*3, 0, FX_ICON_WIDTH, STATUS_HEIGHT, presets[CUR_EDITING].effects[FX_REVERB].OnOff);
}
// Draw the big on/off icons for the EFFECTS mode
void fxHugeIcons(int x, int y) {
// Drive icon
drawTextIcon("Dr", x+0, y+18, 30, 32, presets[CUR_EDITING].effects[FX_DRIVE].OnOff, MEDIUM_FONT);
// Mod icon
drawTextIcon("Md", x+32, y+18, 30, 32, presets[CUR_EDITING].effects[FX_MOD].OnOff, MEDIUM_FONT);
// Delay icon
drawTextIcon("Dy", x+64, y+18, 30, 32, presets[CUR_EDITING].effects[FX_DELAY].OnOff, MEDIUM_FONT);
// Reverb icon
drawTextIcon("Rv", x+96, y+18, 30, 32, presets[CUR_EDITING].effects[FX_REVERB].OnOff, MEDIUM_FONT);
}
void hintIcons(int x, int y) {
int i, k;
const int iconH = 8, gap = 2;
for (i = 0; i < NUM_SWITCHES; ++i) {
if (strcmp(hints[curMode][i] , "")==0) {break;};
}
if (i>0) {
int iconW = ( oled.width() - (gap*(i-1)) )/ i;
for (k = 0; k < i; ++k) {
drawTextIcon(hints[curMode][k], k*(iconW+gap)+x, oled.height()-iconH+y, (iconW), iconH, true, SMALL_FONT);
}
}
}
// Print out the requested preset data
void dump_preset(SparkPreset preset) {
int i,j;
DEB(preset.curr_preset); DEB(" ");
DEB(preset.preset_num); DEB(" ");
DEB(preset.Name); DEB(" ");
DEBUG(preset.Description);
for (j=0; j<7; j++) {
DEB(" ");
DEB(preset.effects[j].EffectName); DEB(" ");
if (preset.effects[j].OnOff == true) DEB(" On "); else DEB (" Off ");
for (i = 0; i < preset.effects[j].NumParameters; i++) {
DEB(preset.effects[j].Parameters[i]); DEB(" ");
}
DEBUG();
}
}
// cycle through knobs (like they are on the Spark Amp)
void changeKnobFx(int changeDirection=1) {
curKnob = curKnob + changeDirection;
if (curKnob>=knobs_number) curKnob=0;
if (curKnob<0) curKnob=knobs_number-1;
curFx = knobs_order[curKnob].fxSlot;
curParam = knobs_order[curKnob].fxNumber;
fxCaption = spark_knobs[curFx][curParam];
level = presets[CUR_EDITING].effects[curFx].Parameters[curParam] * MAX_LEVEL;
timeToGoBack = millis() + actual_timeout;
DEBUG(curKnob);
}
// Pushbutton handling
void doPushButtons(void)
{
static unsigned long buttonTimer[NUM_SWITCHES]; // stores the time that the button was pressed (relative to boot time)
static unsigned long buttonPressDuration[NUM_SWITCHES]; // stores the duration (in milliseconds) that the button was pressed/held down for
static unsigned long autoFireTimer = 0;
static bool buttonActive[NUM_SWITCHES]; // indicates if the button is active/pressed
static bool longPressActive[NUM_SWITCHES]; // indicates if the button has been long-pressed
static bool buttonClick[NUM_SWITCHES]; // indicates if the button has been clicked
static bool longPressFired = false; // indicates if the long-press event has fired
bool AnylongPressActive = false; // OR of any longPressActive states
bool AllPressActive = true; // AND of any longPressActive states
uint8_t ClickFlags = 0; // Write buttons states to one binary mask variable
uint8_t LongPressFlags = 0; // Write buttons states to one binary mask variable
static uint8_t zeroCounter = 0;
static uint8_t oldActiveFlags = 0;
static uint8_t maxFlags = 0;
ActiveFlags = 0;
// Debounce and long press code
for (int i = 0; i < NUM_SWITCHES; i++) {
// If the button pin reads ON, the button is pressed
if (digitalRead(switchPins[i]) == logicON)
{
// If button was previously off, mark the button as active, and reset the timer
if (buttonActive[i] == false){
buttonActive[i] = true;
buttonTimer[i] = millis();
}
// Calculate the button press duration by subtracting the button time from the local time
buttonPressDuration[i] = millis() - buttonTimer[i];
// Mark the button as long-pressed if the button press duration exceeds the long press threshold
// and is not already flagged as such
if ((buttonPressDuration[i] > longPressThreshold) && (longPressActive[i] == false)) {
longPressActive[i] = true;
longPressFired = false;
}
}
// The button either hasn't been pressed, or has just been released
else { // The button state is LOW
// Reset switch register here so that switch is not repeated
buttonClick[i] = false;
// If the button was marked as active, it was recently pressed
if (buttonActive[i] == true){
// Reset the long press active state
if (longPressActive[i] == true){
longPressActive[i] = false;
longPressFired = true;
}
// Long press wasn't active. We either need to debounce/reject the press or register a normal tap
else
{
// if the button press duration exceeds our bounce threshold, then we register a tap
if (buttonPressDuration[i] > debounceThreshold){
buttonClick[i] = true;
DEBUG("Tap " + (String)(1<<i));
onTap(1<<i);
}
}
// Reset the button active status
buttonActive[i] = false;
}
} // The button either hasn't been pressed, or has just been released
LongPressFlags += (static_cast <uint8_t> (longPressActive[i])) << i;
ActiveFlags += (static_cast <uint8_t> (buttonActive[i])) << i;
ClickFlags += (static_cast <uint8_t> (buttonClick[i])) << i;
} // Debounce and long press code loop
// OR all the long press flags so any of the four main footswitches can switch modes
AnylongPressActive = (LongPressFlags > 0) ;
AllPressActive = (LongPressFlags == ((1 << NUM_SWITCHES)-1)) ;
if (oldActiveFlags == 0 && ActiveFlags == 0) {
zeroCounter++; // Idle counter to drop maxFlags when there's actually no activity
if (zeroCounter>10) {maxFlags = 0;}
}
if (oldActiveFlags != ActiveFlags) {
// DEBUG(ActiveFlags);
oldActiveFlags = ActiveFlags;
maxFlags = max(maxFlags, ActiveFlags);
}
if (LongPressFlags >0 && LongPressFlags==ActiveFlags && !longPressFired){
DEBUG("Long press " + (String)(LongPressFlags));
longPressFired = true; // In case when the next function is async and time consuming,
// let's flush it here not to call the function twice or more in a row
onLongPress(LongPressFlags); // function to execute on Long Press event
} else if (LongPressFlags >0 && LongPressFlags==ActiveFlags && autoFireEnabled) { //Autofire
if (autoFireTimer < millis()-autoFireDelay) {
DEBUG("AutoFire " + (String)(LongPressFlags));
autoFireTimer = millis();
onAutoClick(LongPressFlags); // function to execute on Long Press event
}
}
if (ClickFlags > 0 && ActiveFlags ==0){
DEBUG("Click " + (String)(maxFlags));
onClick(maxFlags); // This will give you multi-button clicks
// onClick(clickFlags); // This will give only single button at a time to be clicked
}
}
// buttonMask is binary mask that has 1 in Nth position, if Nth button is active,
// say 0b00000100 (decimal 4) means that your 3rd button fired this event, multiple buttons allowed
void onClick(uint8_t buttonMask) {
// In Preset mode, use the four buttons to select the four HW presets
uint8_t buttonId;
if (isTunerMode) {
// bail out
tunerOff();
} else if (curMode == MODE_BYPASS) {
// bail out
bypassOff();
} else if (curMode == MODE_PRESETS) {
// Mode PRESETS
switch (buttonMask) {
case 1: // button 1
case 2: // button 2
case 4: // button 3
case 8: // button 4
case 16:// ...
case 32:// ...
case 64:// any single button click
case 128:// ...
buttonId = log(buttonMask)/log(2);
display_preset_num = buttonId;
change_hardware_preset(display_preset_num);
break;
default:
//no action yet
break;
}
} else if (curMode == MODE_EFFECTS) {
// Mode EFFECTS
for(int i = 0; i<NUM_SWITCHES; ++i) {
if(bitRead(buttonMask,i)==1){
SWITCHES[i].fxOnOff = !SWITCHES[i].fxOnOff;
change_generic_onoff(SWITCHES[i].fxSlotNumber, SWITCHES[i].fxOnOff);
setting_modified = true;
}
}
} else if (curMode == MODE_LEVEL && (buttonMask==2 || buttonMask==8)) {
// Effect level adjustment with buttons 2 and 4
timeToGoBack = millis() + actual_timeout; // Prolongue the Mode as we are not idle
curFx = knobs_order[curKnob].fxSlot;
curParam = knobs_order[curKnob].fxNumber;
fxCaption = spark_knobs[curFx][curParam];
level = presets[CUR_EDITING].effects[curFx].Parameters[curParam] * MAX_LEVEL;
DEBUG(level);
if (buttonMask==8) {
level=level+1;
if (level>MAX_LEVEL) {level = MAX_LEVEL;}
} else {
level=level-1;
if (level<0) {level=0;}
}
float newVal = (float)level/(float)MAX_LEVEL + 0.005;
change_generic_param(curFx, curParam, newVal);
presets[CUR_EDITING].effects[curFx].Parameters[curParam] = newVal;
} else if (curMode == MODE_LEVEL && buttonMask == 1) {
changeKnobFx();
} else if (curMode == MODE_BANKS && buttonMask == 2) {
pendingBankNum--;
pendingBankNum = constrain(pendingBankNum, 0, NUM_BANKS);
} else if (curMode == MODE_BANKS && buttonMask == 8) {
pendingBankNum++;
pendingBankNum = constrain(pendingBankNum, 0, NUM_BANKS);
}
}
// buttonMask is binary mask that has 1 in Nth position, if Nth button is active,
// say 0b00000110 (decimal 6) means that your 2nd and 3rd button were pressed
void onLongPress(uint8_t buttonMask) {
if (inWifi) {
if (buttonMask == 6) {
esp_restart(); // A way to restart w/o power-cycling
}
} else {
if (buttonMask == 6) {
ESP_off(); // A way to make it sleep
}
switch (curMode) {
case MODE_LEVEL:
case MODE_BANKS:
autoFireEnabled = true;
break;
case MODE_PRESETS:
if (buttonMask == 2 || buttonMask == 8) {
autoFireEnabled = true;
pendingBankNum = localBankNum;
tempFrame(MODE_BANKS, curMode, FRAME_TIMEOUT); // Begin surfing thru banks
} else {
autoFireEnabled = false;
}
break;
default:
autoFireEnabled = false;
break;
}
if (isTunerMode) {
// bail out on any long press
tunerOff();
} else if (curMode == MODE_BYPASS) {
// bail out on any long press
bypassOff();
} else {
switch (buttonMask) {
case 1: // button 1
if (curMode < CYCLE_MODES) {
// Change current mode in cycle
cycleModes();
break;
}
if (curMode == MODE_BANKS) {
// Receive presets from the Spark Amp and save them to the bank
break;
}
break;
case 3: // buttons 1 an 2
toggleTuner();
break;
case 4: // button 3
if (!tempUI) {
curFx = knobs_order[curKnob].fxSlot;
curParam = knobs_order[curKnob].fxNumber;
fxCaption = spark_knobs[curFx][curParam];
level = presets[CUR_EDITING].effects[curFx].Parameters[curParam] * MAX_LEVEL;
tempFrame(MODE_LEVEL, curMode, FRAME_TIMEOUT); // Master level adj
} else {
tempUI=false;
change_custom_preset(&presets[CUR_EDITING], display_preset_num);
showMessage("DONE!", "CHANGES", "SAVED TO AMP", 1000);
}
break;
case 12: // buttons 3 an 4
toggleBypass();
break;
case 8:
esp_restart();
break;
default:
//no action yet
break;
}
}
}
}
// Repeating event autogenerated (if AutoClickEnabled == true) after a long press the interval is in the defines
void onAutoClick(uint8_t buttonMask) {
if ((curMode == MODE_BANKS || curMode == MODE_LEVEL) && (buttonMask==2 || buttonMask==8)) {
onClick(buttonMask); // inc/dec with buttons 2 and 4
}
}
// The event is generated right after debouncing
void onTap(uint8_t buttonMask) {
//DEBUG("TAP " + (String)(buttonMask));
//timeToGoBack = millis() + actual_timeout;
}
// Cycle through the first {CYCLE_MODES} modes in the list
void cycleModes() {
uint8_t iCurMode;
if (isTunerMode) {
tunerOff();
} else if (curMode == MODE_BYPASS) {
bypassOff();
} else {
returnToMainUI();
iCurMode = static_cast <uint8_t> (curMode);
iCurMode++;
if (iCurMode >= CYCLE_MODES) {iCurMode = 0;}
curMode = static_cast <eMode_t> (iCurMode);
mainMode = curMode;
updateFxStatuses();
DEBUG("Mode: " + (String)(curMode));
}
}
// Refresh UI ============================================================================
void refreshUI(void) {
// If some button is active, ploceed with a temp frame
if (ActiveFlags > 0) {
timeToGoBack = millis() + actual_timeout;
}
// maybe it's time to return from a temp UI
if ((millis() > timeToGoBack) && tempUI) {
returnToMainUI();
}
// Flip GUI flash bool
if (isTimeout) {
flash_GUI = !flash_GUI;
}
if (isTunerMode) { // If Spark reports that we are in tuner mode
if (curMode!=MODE_TUNER) { // We have to switch the pedal to tuner also
returnMode = mainMode;
curMode = MODE_TUNER;
}
} else {
if (curMode == MODE_TUNER) { //as this is async operation, we have to sync the pedal again
curMode = returnMode;
}
}
if (oldMode!=curMode) {
switch (oldMode) {
case MODE_PRESETS:
break;
case MODE_EFFECTS:
break;
case MODE_LEVEL:
break;
default:
break;
}
setTransition();
ui.transitionToFrame(curMode);
oldMode = curMode;
DEBUG("**UI** Switch to mode: " + (String)(curMode));
}
updateFxStatuses();
/*
// if a change has been made or the timer timed out and fully synched...
if ((isOLEDUpdate || isTimeout) && (spark_state == SPARK_SYNCED)){
isOLEDUpdate = false;
}
*/
if (!connected_sp && !inWifi) {
// Show reconnection message
oled.clear();
oled.setFont(MEDIUM_FONT);
oled.setTextAlignment(TEXT_ALIGN_CENTER);
oled.drawString(X1, Y3, "Reconnecting");
oled.setFont(MEDIUM_FONT);
oled.setTextAlignment(TEXT_ALIGN_CENTER);
oled.drawString(X1, Y4, "Please wait");
mainIcons();
oled.display();
delay(10);
#ifndef NOSLEEP
if (millis() > time_to_sleep) {
ESP_off();
esp_restart(); // if it sleeps deep, then we never get here, but if light, then we need to restart the unit after it wakes up
}
#endif
} else {
time_to_sleep = millis() + (BT_MAX_ATTEMPTS * MILLIS_PER_ATTEMPT);
}
}
// Depending on the old and the new frames, this function sets the appropriate transition direction
void setTransition() {
AnimationDirection dir = SLIDE_LEFT;
if (oldMode >= CYCLE_MODES && curMode < CYCLE_MODES) {dir = SLIDE_UP;}
else if (oldMode < CYCLE_MODES && curMode >= CYCLE_MODES) {dir = SLIDE_UP;}
else if (oldMode < CYCLE_MODES && curMode < CYCLE_MODES) {
if (oldMode < curMode) {
dir = SLIDE_LEFT;
} else {
dir = SLIDE_RIGHT;
}
}
else if (oldMode >= CYCLE_MODES && curMode >= CYCLE_MODES) {dir = SLIDE_LEFT;}
ui.setFrameAnimation(dir);
}
// Draw an active or inactive icon with a given XBMP in the middle
void drawStatusIcon(const uint8_t* xbmVar, int xbmW, int xbmH, int x, int y, int w, int h, bool active) {
// draw active or inactive icon placeholder
oled.setColor(WHITE);
if (active) {
oled.fillRect(x, y, w, h);
} else {
oled.setColor(INVERSE); // Rounded inactive icon borders or comment this line out for simple corners
oled.drawRect(x, y, w, h); // Comment this line out if you want inactive icons without borders
}
// draw letters and signs within
oled.setColor(INVERSE);
oled.drawXbm(x+(w-xbmW)/2, y+(h-xbmH)/2, xbmW, xbmH, xbmVar);
}
// Draw an active or inactive icon tith a given text in the middle
void drawTextIcon(const String &text, int x, int y, int w, int h, bool active, const uint8_t* font) {
int16_t yOffset;
oled.setFont(font);
int testW = oled.getStringWidth("W") * 0.5 +3;
yOffset = -testW;
oled.setTextAlignment(TEXT_ALIGN_CENTER);
// draw letters and signs within
oled.setColor(WHITE);
oled.drawString(x+w/2, y+h/2+yOffset, text);
if (yOffset<-8) {
oled.drawString(x+w/2-1, y+h/2+yOffset, text); // faux bold
}
// draw active or inactive icon placeholder
oled.setColor(INVERSE);
if (active) {
oled.fillRect(x, y, w, h);
} else {
oled.setColor(INVERSE); // INVERSE = rounded inactive icon borders, WHITE = corners
oled.drawRect(x, y, w, h); // Comment this line out if you want inactive icons without borders
}
}
// Draw a horizontal battery icon with a bar representing charge percentage and a lightning sign if charging is true
void drawBatteryH(int x, int y, int w, int h, int chg_percent, bool charging) {
//draw gauge
oled.setColor(WHITE);
oled.fillRect(x+2, y+2, constrain(map(chg_percent, 0, 100, 0, w-4), 0, (w*0.9)-3), h-4); // not to draw on the cap we use constrain
oled.fillRect(x+2, y+2+(h/4)-1, map(chg_percent, 0, 100, 0, w-4), h/2-2); // narrow gauge can draw on the cap
oled.setColor(INVERSE);
if (charging) {
chg_percent = 100; // overwrite because we don't really measure the process of charging
oled.drawXbm(x+((w-chrg_width)/2), y, chrg_width, chrg_height, chrg_bits);
}
//draw contour
oled.setColor(WHITE);
oled.drawLine(x, y, x+(w*0.9), y);
oled.drawLine(x+(w*0.9), y, x+(w*0.9), y+(h/4)-1);
oled.drawLine(x+(w*0.9), y+(h/4)-1, x+w-1, y+(h/4)-1);
oled.drawLine(x+w-1, y+(h/4)-1, x+w-1, y+(h-h/4));
oled.drawLine(x+(w*0.9), y+(h-h/4), x+w-1, y+(h-h/4));
oled.drawLine(x+(w*0.9), y+(h-h/4), x+(w*0.9), y+h-1);
oled.drawLine(x, y+h-1, x+(w*0.9), y+h-1);
oled.drawLine(x, y, x, y+h-1);
}
// a simple script to show a text string during a given time
void textAnimation(const String &s, ulong msDelay, int yShift=0, bool show=true) {
oled.clear();
oled.drawString(oled.width()/2, oled.height()/2-6 + yShift, s);
if (show) {
oled.display();
delay(msDelay);
}
}
// Read the ADC, average the result and set globals: vbat_result, chrg_result
void readBattery(){
static int vbat_ring_count = 0;
static int vbat_ring_sum = 0;
vbat_result = analogRead(VBAT_AIN); // Read battery voltage
//DEBUG(vbat_result);
// To speed up the display when a battery is connected from scratch
// ignore/fudge any readings below the lower threshold
if (vbat_result < BATTERY_FUDGE * ADC_COEFF) {
vbat_result = BATTERY_FUDGE * ADC_COEFF;
}
// While collecting data
if (vbat_ring_count < VBAT_NUM) {
vbat_ring_sum += vbat_result;
vbat_ring_count++;
vbat_result = vbat_ring_sum / vbat_ring_count;
}
// Once enough is gathered, do a decimating average
else {
vbat_ring_sum = (VBAT_NUM-1) * vbat_ring_sum / VBAT_NUM + vbat_result;
vbat_result = vbat_ring_sum / VBAT_NUM;
#ifndef BATT_CHECK_0
// Low-battery go to sleep to save the LiPo's life
if ((vbat_result < BATTERY_OFF * ADC_COEFF) && (batteryCharging()<=0)) {
oled.clear();
oled.setFont(MEDIUM_FONT);
oled.setColor(WHITE);
textAnimation("LOW BATTERY", 5000);
ESP_off();
}
#endif
}
#ifdef BATT_CHECK_2
chrg_result = analogRead(CHRG_AIN); // Check state of /CHRG output
#else
chrg_result = 0;
#endif
//DEBUG(vbat_result/ADC_COEFF);
}
// Calc the battery percentage basing on the voltage and the LiPo discharging curve
int batteryPercent(int vbat_value) {
// Partly linear discharge function approximation
uint8_t percentage = 0;
if ( vbat_value >= BATTERY_60*ADC_COEFF ) { percentage = map(vbat_value, BATTERY_60*ADC_COEFF, BATTERY_100*ADC_COEFF, 60, 100); }
else if ( vbat_value >= BATTERY_6*ADC_COEFF ) { percentage = map(vbat_value, BATTERY_6*ADC_COEFF, BATTERY_60*ADC_COEFF, 6, 60); }
else { percentage = map(vbat_value, BATTERY_0*ADC_COEFF, BATTERY_6*ADC_COEFF, 0, 6); }
percentage = map(percentage, GAUGE_0, GAUGE_100, 0, 100);
return constrain(percentage, 0, 100);
}
// Charging or not? Returns -1 if unsupported, 0 if not charging and 1 if charging
int batteryCharging() {
#ifdef BATT_CHECK_0
return -1; //unsupported
#endif
// For level-based charge detection (not very reliable)
#ifdef BATT_CHECK_1
if (vbat_result >= BATTERY_CHRG*ADC_COEFF) {
return 1;
} else {
return 0;
}
#endif
// If advanced charge detection available, and charge detected
#ifdef BATT_CHECK_2
if (chrg_result < CHRG_LOW*ADC_COEFF) {
return 1;
} else {
return 0;
}
#endif
return -1; // default value
}
// Stand-by mode with some fun
void ESP_off(){
uint64_t bit_mask=0;
String wake_buttons = "";
int deep_sleep_pins;
int k;
deep_sleep_pins = Check_RTC(); // Find out if we have RTC pins assigned to buttons allowing deep sleep, otherwise we use light sleep
// CRT-off effect =) or something
String s = "_________________";
// Debug
DEBUG("deep_sleep_pins = " + (String)(deep_sleep_pins));
DEBUG("RTC_present = " + (String)(RTC_present));
if (deep_sleep_pins > 0){
oled.clear();
oled.display();
oled.setFont(MEDIUM_FONT);
oled.setTextAlignment(TEXT_ALIGN_CENTER);
// Only GPIOs which have RTC functionality can be used for deep sleep: 0,2,4,12-15,25-27,32-39
// Future radio shutdown support here:
// esp_bluedroid_disable(); //gracefully shutdoun BT (and WiFi)
// esp_bt_controller_disable();
// esp_wifi_stop();
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
#ifdef ACTIVE_HIGH
for (i = 0; i < NUM_SWITCHES; i++) {
if (sw_RTC[i]) {
gpio_pullup_dis(static_cast <gpio_num_t> (switchPins[i]));
gpio_pulldown_en(static_cast <gpio_num_t> (switchPins[i]));
bit_mask += 1<<switchPins[i];
wake_buttons += (String)(i+1) + ",";
k = i+1;
}
}
if (deep_sleep_pins == NUM_SWITCHES) {
wake_buttons = "Any button wakes";
} else {
if (deep_sleep_pins == 1) {
wake_buttons = "Button " + (String)(k) + " wakes";
} else {
wake_buttons = "Buttons " + wake_buttons.substring(0, wake_buttons.length()-1) + " wake";
}
}
#else
gpio_pulldown_dis(static_cast <gpio_num_t> (switchPins[RTC_1st]));
gpio_pullup_en(static_cast <gpio_num_t> (switchPins[RTC_1st]));
bit_mask += 1<<switchPins[RTC_1st];
wake_buttons = "Button " + (String)(RTC_1st+1) + " wakes";
#endif
oled.setFont(MEDIUM_FONT);
textAnimation(wake_buttons,3000);
textAnimation("Deep sleep",1000);
for (int i=0; i<8; i++) {
s = s.substring(i);
textAnimation(s,70,-8);
}
textAnimation(".",200,-5);
textAnimation("*",100,3);
textAnimation("x",100,-3);
textAnimation("X",100,-1);
textAnimation("x",100,-3);
textAnimation(".",100,-5);
DEBUG("Deep sleep");
oled.displayOff(); // turn it off, otherwise oled remains active
#ifdef ACTIVE_HIGH
esp_sleep_enable_ext1_wakeup( bit_mask, ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on RTC enabled GPIOs
#else
// if you use LOW as an active state, only one button can be used for wake up source
esp_sleep_enable_ext1_wakeup( bit_mask, ESP_EXT1_WAKEUP_ALL_LOW );
#endif
esp_deep_sleep_start();
}
else { // if we don't have buttons on RTC GPIOs
oled.setFont(MEDIUM_FONT);
textAnimation("Button 1 wakes",3000);
textAnimation("Sleep",1000);
for (int i=0; i<8; i++) {
s = s.substring(i);
textAnimation(s,70,-8);
}
textAnimation(".",200,-5);
textAnimation("*",100,3);
textAnimation("x",100,-3);
textAnimation("X",100,-1);
textAnimation("x",100,-3);
textAnimation(".",100,-5);
DEBUG("Light sleep");
oled.displayOff(); // turn it off, otherwise oled remains active
#ifdef ACTIVE_HIGH
gpio_wakeup_enable(static_cast <gpio_num_t> (switchPins[0]), GPIO_INTR_HIGH_LEVEL );
#else
gpio_wakeup_enable(static_cast <gpio_num_t> (switchPins[0]), GPIO_INTR_LOW_LEVEL );
#endif
esp_sleep_enable_gpio_wakeup();
esp_light_sleep_start();
esp_restart();
}
};
// Respawn
void ESP_on () {
uint8_t GPIO;
esp_sleep_wakeup_cause_t wakeup_cause;
wakeup_cause = esp_sleep_get_wakeup_cause();
switch(wakeup_cause)
{
case ESP_SLEEP_WAKEUP_EXT0 : DEBUG("Wakeup caused by ext0 signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 :
DEBUG("Wakeup caused by ext1 signal using RTC_CNTL");
GPIO = log(esp_sleep_get_ext1_wakeup_status() )/log(2);
DEBUG("Waken up by GPIO_" + (String)(GPIO));
break;
case ESP_SLEEP_WAKEUP_TIMER : DEBUG("Wakeup caused by timer"); break;