/
openvfd_firmware.ino
2708 lines (2361 loc) · 98.7 KB
/
openvfd_firmware.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
/*MIT License
Copyright (c) 2019 Frank F. Zheng, Date: 01/05/2019
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
// --------- Libraries Used ---------
#include <avr/pgmspace.h>
#include <RTClib.h> // RTC Clock Library
#include <Wire.h> // RTC Clock Communication Library (Wire)
#include <digitalWriteFast.h> // Clock Cycle Optimized Output
#include <EEPROM.h> // EEPROM Access
#include <SoftwareSerial.h> // Bluetooth Serial Communication
// Remove comment to enable temperature sensor reading at STEM pin (DS18B20)
// #define TEMPERATURE_ENABLE
#ifdef TEMPERATURE_ENABLE
#include <OneWire.h> // One Wire DS18B20 Temperature Sensor Access - FLUORESCENCE: No TEMP
#endif
// Running firmware for the first time? Remove comment to configure OpenVFD for the first time (EEPROM reset)!
// #define FIRSTCONFIG
// Hardware Version Control
// #define HW_VERSION_22
// --------- Pin Mapping Defines ---------
// Pin Name | A | ATMEGA Mapping | Comment, Schematics Signal Name
// ------------------------------------------------------------------------------------------
#define CLOCK_PIN 2 // ATMEGA: 4 74HC595 SPI Clock Pin, SCK
#define LATCH_PIN 3 // ATMEGA: 5 74HC595 SPI Latch Pin, RCK
#define DATA_PIN 4 // ATMEGA: 6 74HC595 SPI Data Pin, SER
#define B_F1_PIN 5 // ATMEGA: 11 F1 Button
#define B_F2_PIN 6 // ATMEGA: 12 F2 Button
#define B_F3_PIN 7 // ATMEGA: 13 F3 Button
#define B_F4_PIN 8 // ATMEGA: 14 F4 Button
#define LED_PIN 13 // ATMEGA: 19 LED Pin, LEDPIN
#define MIC_PIN A0 // ATMEGA: 23 Microphone Input Pin, MIN
#ifdef TEMPERATURE_ENABLE
#define STEM_PIN A1 // ATMEGA: 24 Temperature Sensor Input Pin, STEM
#endif
#define B_GROUND 9 // Bluetooth Ground Pin
#define B_TXD 10 // Bluetooth TXD Pin
#define B_RXD 11 // Bluetooth RXD Pin
// ------------------------------------------------------------------------------------------
#define SHORTPRESS 1 // Short press is 1
#define LONGPRESS 2 // Long press is 2
#define NUM_RGB 6 // 6 LEDs for OpenVFD
#define NUM_BYTES (NUM_RGB * 3) // 3 * 6 = 18 bytes
const uint8_t NUM_DIGITS_V = NUM_RGB;
#define PORT (PORTB) // Digital pin's port
#define PORT_PIN (PORTB5) // Digital pin's bit position
#define NUM_BITS (8) // Const 8
// Used for LED crossfade phase value
#define PI85 0.0369599135716446263348546280
// Boolean values
#define FALSE 0
#define TRUE 1
// --------- Firmware Information ---------
// FIRMWARE VERSION STRING
// Version 2.3 fluorescence, Date: 01/05/2019
char fwString[7] = {'v', '2', '.', '3', 'f', ' ', ' '};
/* Changelog
* 2.3f (01/05/2019):
* - New mode: Stopwatch! Use F2 to start/reset stopwatch, F3 to pause/resume!
* - New settings for spectrum fade: Saturation & brightness control
* - New settings for cross fade and chase fade: brightness control
* - Significantly faster color wheel algorithm using fixed point arithmetic HSL transform enables adjustment of saturation and lightness besides color (hue)
* - (Finally) fixed a pesk bugs that caused night shift to turn on/off incorrectly or at unexpected time
* - Hardware Version 2.2b (experimental) support
*
* 2.21rf (04/21/2018):
* - Fixed an issue in 12h mode
*
* 2.2rf (03/21/2018):
* - Clock can now be fully controlled via Bluetooth!
* - Significant performance and code efficiency improvements
* - Dynamic memory optimization, moving less frequently used constant variables into PROGMEM
* - Night shift is now available. The clock will enter low brightness when time is reached. Time setting using serial command (app or myOpenVFD)
* - 12h/24h, international date format and leading zero now configurable using serial command
* - Resolved an issue that would cause significant lag when flip dot mode is active in night shift
* - First firmware config can now be enabled in code to load default settings correctly
*
* 2.2f (02/25/2018):
* - Serial Bluetooth enables Bluetooth communication between clock and HM-10 module
* -> Serial protocol is the same
* - Minor enhancements on color distance for USB communication
*
* 2.1f (01/11/2018):
* - Removed manual sensitivity setting of VU meter LED mode
* -> Software 'sort-of' AGC implementation enables dynamic range detection
* -> Parameter is now the color distance
* - Color distance is now called "Chill", "Regular" and "Tight" instead of delta values of 10, 21 and 42
* - New clock mode: Silent. In silent mode, the clock segments can be purely software PWM dimmed to a duty cycle of 6.25%
* -> Measurements show that this can reduce power consumption up to around 40%!
* - Date format is now customizable between DDMMYY (default) and MMDDYY
* - Time format is now customizable between 12h and 24h
* - Welcome message is now customizable
* - Temperature sensor can now be turned on simply by removing comment on define
* - Fixed an issue where the user won't be able to set 29th February when leap year
*/
// --------- Component Initializer ---------
RTC_DS1307 rtc;
uint8_t global_s, global_m, global_h;
#ifdef TEMPERATURE_ENABLE
OneWire ds(STEM_PIN);
#endif
SoftwareSerial BTSerial(B_TXD, B_RXD);
// --------- Global Variable Initializer ---------
// Interfaces tell what kind of data will shuttle through the shift registers and the display render function(s) will render accordingly.
typedef enum {
INTERFACE_TIME = 0, // Display the current time (default of default)
INTERFACE_DATE = 1, // Display the current date
#ifdef TEMPERATURE_ENABLE
INTERFACE_TEMPERATURE = 2, // Display the current temperature measured by the clock
#endif
INTERFACE_STOPWATCH = 3, // Display stopwatch
INTERFACE_TIMEDATE_SET = 128 // Active when time set or date set is enabled
} INTERFACE_MODE_t;
// Each LED mode is assigned to a number. All number configurations are listed in this enum.
typedef enum {
LED_STATIC = 0, // Static color preset mode. Takes colors out of a predifined constant array
LED_SERIAL_0 = 2, // Serial mode 0, only used for instantly writing colors from the color wheel and mood lighting
LED_SERIAL_1 = 3, // Serial mode 1, used to write colors smoothly over serial communication
LED_FADE = 6, // Dynamic color preset mode: Single color fade through HSL outer radius
LED_CROSSFADE = 7, // Dynamic color preset mode: Cross fade. Each LED has its own color phase with adjustable phase
LED_CHASEFADE = 8, // Dynamic color preset mode: Chase fade. One color chases another along the HSL wheel with adjustable direction
LED_RESISTOR = 10, // Dynamic color preset mode: Resistor colors. Nerd alert! LEDs have the colors of resistors ring codes according to the current time
LED_COP = 11, // Dynamic color preset mode: Police car lights!
LED_MUSIC = 20, // Dynamic color preset mode: Number of the LEDs turned on equal to loudness. Colors same as cross fade with adjustable phase
LED_SILENT = 21 // Static color preset mode with all LEDs off and brightness control down to 6% brightness
} LED_MODE_t;
// Global Display Mode
INTERFACE_MODE_t interface = INTERFACE_TIME;
// Global LED Mode
LED_MODE_t led = LED_STATIC;
// Clock flags
uint8_t clockFlags = 0; // Global clock flags
#define B_12H 0 // Bit 0: 12h/24h option
#define B_INTD 1 // Bit 1: DDMMYY or MMDDYY
#define B_NSHIFT 2 // Bit 2: Night shift
#define B_LZERO 3
// End Clock Flags
char welcomeText[NUM_DIGITS_V];
#ifdef TEMPERATURE_ENABLE
uint32_t ts; // Mean temperature value
#endif
uint8_t isFahrenheit = 0; // Fahrenheit flag
uint8_t INTF0_DM = 0; // Interface 0 dot mode counter
uint8_t INTF0_DP = 0; // Interface 0 dot position
uint8_t INTF0_ds = 0; // Interface 0 dot mode: second flip time delta flag
uint8_t INTF0_dr = FALSE; // Interface 0 dot mode: second flip direction
uint8_t dateSet = FALSE; // Date set flag
uint8_t setOnceFlag = FALSE; // Set once flag. Is used to prevent the clock from ticking on when entered time/date set mode
DateTime *INTF3_TB = nullptr; // Stopwatch backup time
TimeSpan *INTF3_TS = nullptr; // Stopwatch time span
unsigned long INTF3_MILLIS = 0; // Stopwatch milliseconds
unsigned long INTF3_MILLIE = 0; // Stopwatch elapsed millis
enum {INTF3_INITIAL, INTF3_RUNNING, INTF3_PAUSED};
uint8_t INTF3_st = INTF3_INITIAL; // Stopwatch FSM state
// ---- LED Control variables
uint8_t *rgb_arr = NULL; // LED color storage array
uint8_t *target_arr = NULL; // Smooth fade target array
uint32_t t_f; // LED time check
// ---- LED Preset configuration store. ATF1TION: Different ordering!
// | G| R| B|
// ---------------
#define LED0_cOffset 11 // # Single color presets
#define LED0_mcOffset (LED0_cOffset - 1) // # Single color presets - 1
const uint8_t led_scPresets[][3] = {{ 0, 0, 0}, // Off! ("Off")
{255, 255, 255}, // White ("On")
{200, 255, 32}, // Warm White ("LON := Light On")
{ 0, 255, 0}, // Red ("Red")
{255, 0, 0}, // Green ("GRN := Green")
{ 0, 0, 255}, // Blue ("Blue")
{125, 255, 0}, // Yellow ("YELO = Yellow")
{ 30, 255, 0}, // Orange ("ORNG = Orange")
{255, 0, 128}, // Cyan ("Cyan")
{ 0, 255, 170}, // Magenta ("PRED := Purple Red")
{ 0, 200, 255} // Purple ("PRPL := Purple")
};
const uint8_t led_Presets[][NUM_BYTES] = {
{ 0, 200, 255, // Rainbow colors!
0, 0, 255,
255, 0, 0,
128, 255, 0,
30, 255, 0,
0, 255, 0},
{128, 255, 255, // Pastel rainbow!
128, 50, 255,
255, 0, 128,
255, 128, 128,
255, 255, 128,
100, 255, 128},
{255, 0, 0, // Green to blue!
240, 0, 64,
216, 0, 128,
128, 0, 216,
64, 0, 240,
0, 0, 255},
{ 0, 255, 3, // Red to blue!
0, 255, 10,
0, 240, 25,
0, 200, 80,
0, 100, 150,
0, 50, 255},
{ 3, 255, 0, // Red to green!
30, 255, 0,
60, 240, 0,
100, 180, 0,
180, 180, 0,
255, 20, 0}
};
// ---- LED Resistor preset GRB 0: Off 1: Brown 2: Red 3: Orange 4: Yellow 5: Green 6: Blue 7: Purple 8: Gray 9: White
const uint8_t led_Resistor[][3] = {{0, 0, 0}, {128, 255, 64}, {0, 255, 0}, {30, 255, 0}, {125, 255, 0}, {255, 0, 0}, {0, 0, 255}, {0, 200, 255}, {40, 40, 60}, {255, 255, 255}};
uint8_t LED0P = 0; // LED preset mode index
// ---- LED Preset configuration set message
const char LED0PM[][4] = { {' ', 'O', 'F', 'F'},
{' ', ' ', 'O', 'N'},
{' ', 'L', 'O', 'N'},
{' ', 'R', 'E', 'D'},
{' ', 'G', 'R', 'N'},
{'B', 'L', 'U', 'E'},
{'Y', 'E', 'L', 'O'},
{'O', 'R', 'N', 'G'},
{'C', 'Y', 'A', 'N'},
{'P', 'R', 'E', 'D'},
{'P', 'R', 'P', 'L'},
{' ', 'R', 'N', 'B'},
{'P', 'R', 'N', 'B'},
{' ', 'G', 2, 'B'},
{' ', 'R', 2, 'B'},
{' ', 'R', 2, 'G'}};
uint8_t LED6_st = 0; // LED regular fade position
// Lightness & Saturation presets
typedef enum {LED_L_HIGH = 127, LED_L_REG = 90, LED_L_LOW = 50 } LED_L_t;
typedef enum {LED_S_HIGH = 255, LED_S_REG = 196, LED_S_LOW = 127 } LED_S_t;
LED_L_t LED6_l = LED_L_HIGH; // LED regular fade lightness
LED_S_t LED6_s = LED_S_HIGH; // LED regular fade saturation
// LED cross fade starting position
uint8_t LED7_dp = 0; // LED cross fade position
uint8_t LED7_delta = 21; // LED cross fade delta
const char LED7PM[][NUM_DIGITS_V] = { {'C', 'H', 'I', 'L', 'L', ' '},
{'R', 'E', 'G', 'U', 'L', 'A'},
{'T', 'I', 'G', 'H', 'T', ' '}};
uint8_t LED8_ds = 0; // LED chase fade second flip
uint8_t LED8_dp = 0; // LED chase fade direction state
uint8_t LED8_dr = 0; // LED chase fade direction flag
uint8_t LED8_st = 0; // LED chase fade FSM position
uint8_t LED8_ph = 0; // LED chase fade rainbow position
const char LED8PM[][4] = { {' ', 'R', '-', 'L'},
{' ', 'L', '-', 'R'},
{'F', 'L', 'I', 'P'},
{'C', 'L', 'A', 'P'}};
uint8_t LED11_pt = 0; // LED cop mode pattern
uint8_t LED11_st = 0; // LED cop mode FSM position
const uint8_t LED11_colors[][3] = {{ 0, 255, 10}, // Cop red
{ 0, 15, 255}}; // Cop blue
uint8_t LED20_st = 0; // LED microphone mode off fader state
uint8_t LED20_dst = FALSE; // LED microphone mode blink delay state
uint8_t LED20_dp = 0; // LED cross fade position
const uint8_t LED20_samplesCount = 8; // Empirical sample acquisition array length
uint16_t LED20_samples[LED20_samplesCount] = {0}; // Empirical sample acquisition array allocation
uint8_t LED20_sampleIndex = 0; // Buffer push sample index
const uint8_t LED20_analogSamples = 128; // How many samples per reading
const uint8_t LED20_sense = 2; // Noise threshold
const float LED20_linFactor = -0.50; // VU meter log mapping adjust: linFactor in [-1 ... inf], -1 <= lF < 0: log, 0: lin, > 0: exp
enum {LED21_SILENT, LED21_SLEEP, LED21_SHIFTSLEEP}; // 3 States
uint8_t LED21_st = LED21_SILENT; // State: standard silent
LED_MODE_t LED21_shslpst = LED_STATIC; // Shift sleep previous state
uint8_t LED21_DF = 1; // Dim factor, 1 = 50% render duty cycle
const uint8_t LED21_DFMAX = 19; // Max dim factor
uint8_t LED21_DC = 0; // Dim flag used for switching PWM
const char offs[NUM_DIGITS_V] = {' ', ' ', ' ', ' ', ' ', ' '}; // Digit blanking
uint8_t LED21_hEN = 22; // Begin time of night shift (hour)
uint8_t LED21_mEN = 0; // Begin time of night shift (minute)
uint8_t LED21_hDS = 8; // End time of night shift (hour)
uint8_t LED21_mDS = 0; // End time of night shift (minute)
// ---- Menu/Interface selector variables
long p_t[4] = {0, 0, 0, 0}; // Button press timer
const long lp_t = 500; // Long press threshold
uint8_t p[4] = {FALSE, FALSE, FALSE, FALSE}; // Button enable
uint8_t lp[4] = {FALSE, FALSE, FALSE, FALSE}; // Long press enable
uint8_t cF1, cF2, cF3, cF4 = 0; // Check state variable
// Display Render function pointer
void displayWrite_REG(uint8_t, uint8_t, uint32_t, const char *);
void displayWrite_DIM(uint8_t, uint8_t, uint32_t, const char *);
void (*displayWrite)(uint8_t, uint8_t, uint32_t, const char *) = displayWrite_REG;
// Progmem messages (less used, optimized for RAM availability)
const char MSG_COLOR[NUM_DIGITS_V] PROGMEM = {' ', 'C', 'O', 'L', 'O', 'R'};
const char MSG_FADE[NUM_DIGITS_V] PROGMEM = {'C', ' ', 'F', 'A', 'D', 'E'};
const char MSG_CROSSFADE[NUM_DIGITS_V] PROGMEM = {'C', ' ', 'C', 'R', 'F', 'D'};
const char MSG_CHASEFADE[NUM_DIGITS_V] PROGMEM = {'C', ' ', 'C', 'H', 'F', 'D'};
const char MSG_CRCODE[NUM_DIGITS_V] PROGMEM = {'C', 'R', 'C', 'O', 'D', 'E'};
const char MSG_CSOUND[NUM_DIGITS_V] PROGMEM = {'C', 'S', 'O', 'U', 'N', 'D'};
const char MSG_CCOP[NUM_DIGITS_V] PROGMEM = {'C', ' ', ' ', 'C', 'O', 'P'};
const char MSG_CSILENT[NUM_DIGITS_V] PROGMEM = {'C', ' ', 'S', 'L', 'N', 'T'};
const char MSG_LED6_L0[NUM_DIGITS_V] PROGMEM = {' ', ' ', 'H', 'I', 'G', 'H'};
const char MSG_LED6_L1[NUM_DIGITS_V] PROGMEM = {' ', ' ', ' ', 'R', 'E', 'G'};
const char MSG_LED6_L2[NUM_DIGITS_V] PROGMEM = {' ', ' ', ' ', ' ', 'L', 'O'};
const char MSG_INTLDATESET[NUM_DIGITS_V] PROGMEM = {'O', 'O', 'D', 'D', 'Y', 'Y'};
const char MSG_INTLDATERESET[NUM_DIGITS_V] PROGMEM = {'D', 'D', 'O', 'O', 'Y', 'Y'};
const char MSG_LEADINGZERO_ON[NUM_DIGITS_V] PROGMEM = {0, ' ', ' ', ' ', 'O', 'N'};
const char MSG_LEADINGZERO_OFF[NUM_DIGITS_V] PROGMEM = {0, ' ', ' ', 'O', 'F', 'F'};
const char MSG_DEFAULT1[NUM_DIGITS_V] PROGMEM= {'D', 'E', 'F', 'A', 'U', 'L'};
const char MSG_DEFAULT2[NUM_DIGITS_V] PROGMEM = {'S', 'E', 'T', 'I', 'N', 'G'};
const char MSG_DEFAULT3[NUM_DIGITS_V] PROGMEM = {'R', 'E', 'T', 'O', 'R', 'D'};
const char MSG_TIMESYNC1[NUM_DIGITS_V] PROGMEM = {'T', '-', 'D', ' ', ' ', ' '};
const char MSG_TIMESYNC2[NUM_DIGITS_V] PROGMEM = {'S', 'Y', 'N', 'C', 'E', 'D'};
const char MSG_SAVESETTINGS1[NUM_DIGITS_V] PROGMEM = {'A', 'L', 'L', ' ', ' ', ' '};
const char MSG_SAVESETTINGS2[NUM_DIGITS_V] PROGMEM = {'S', 'E', 'T', 'I', 'N', 'G'};
const char MSG_SAVESETTINGS3[NUM_DIGITS_V] PROGMEM = {'S', 'A', 'V', 'E', 'D', ' '};
const char MSG_SILENT1[NUM_DIGITS_V] PROGMEM = {'F', 'U', 'L', 'L', ' ', ' '};
const char MSG_SILENT2[NUM_DIGITS_V] PROGMEM = {'N', 'I', 'G', 'H', 'T', ' '};
const char MSG_SILENT3[NUM_DIGITS_V] PROGMEM = {'S', 'H', 'I', 'F', 'T', ' '};
const char MSG_ON[NUM_DIGITS_V] PROGMEM = {' ', ' ', ' ', ' ', 'O', 'N'};
const char MSG_OFF[NUM_DIGITS_V] PROGMEM = {' ', ' ', ' ', 'O', 'F', 'F'};
const char MSG_ERROR[NUM_DIGITS_V] PROGMEM = {'E', 'R', 'R', 'O', 'R', ' '};
// ------------------------------------------------------------------------------------------
// Time interval updating event class: Clocked FSM
typedef struct intervalEvent{
unsigned long interval;
unsigned long previousMillis;
} intervalEvent;
struct intervalEvent newiE(long p1) {
intervalEvent iE;
iE.interval = p1;
iE.previousMillis = 0;
return iE;
}
void resetiE(intervalEvent *input) {
input->previousMillis = 0;
}
uint8_t updateIntervalEvent(intervalEvent *input) {
unsigned long currentMillis = millis();
if((currentMillis - input->previousMillis) > input->interval) {
input->previousMillis = currentMillis;
return TRUE;
}
else return FALSE;
return FALSE;
}
intervalEvent dotUpdater, jdotUpdater, sdotUpdater, cfUpdater, chUpdater, vuUpdater, vu2Updater, nShiftUpdater;
#ifdef TEMPERATURE_ENABLE
intervalEvent tsUpdater;
#endif
// ------------------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
// Output Pin Initializer
pinMode(LED_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
// Bluetooth Configuration
pinMode(B_GROUND, OUTPUT); // Data Ground Pin (yup that's clumsy)
digitalWrite(B_GROUND, LOW); // Permanent Low
BTSerial.begin(4800);
analogReference(DEFAULT);
// Input Pin Initializer
#ifdef HW_VERSION_22
pinMode(B_F1_PIN, INPUT_PULLUP);
pinMode(B_F2_PIN, INPUT_PULLUP);
pinMode(B_F3_PIN, INPUT_PULLUP);
pinMode(B_F4_PIN, INPUT_PULLUP);
#else
pinMode(B_F1_PIN, INPUT);
pinMode(B_F2_PIN, INPUT);
pinMode(B_F3_PIN, INPUT);
pinMode(B_F4_PIN, INPUT);
#endif
pinMode(MIC_PIN, INPUT);
#ifdef TEMPERATURE_ENABLE
pinMode(STEM_PIN, INPUT);
#endif
// LED initializer
digitalWriteFast(LED_PIN, LOW);
if((rgb_arr = (uint8_t *) malloc(NUM_BYTES))) memset(rgb_arr, 0, NUM_BYTES);
if((target_arr = (uint8_t *) malloc(NUM_BYTES))) memset(target_arr, 0, NUM_BYTES);
render();
// Wire, RTC Initializer
wrInit();
#ifdef FIRSTCONFIG // If first time configuring, load default and save all settings
firstConfig();
saveConfig(1);
#endif
// Initialize global saved values
loadConfig();
// Welcome message, read from EEPROM
welcome(welcomeText);
#ifdef TEMPERATURE_ENABLE
tsUpdater = newiE(7500);
#endif
dotUpdater = newiE(800);
jdotUpdater = newiE(500);
sdotUpdater = newiE(80);
cfUpdater = newiE(25);
chUpdater = newiE(60);
vuUpdater = newiE(90);
vu2Updater = newiE(250);
nShiftUpdater = newiE(1000);
}
void loop() {
// Button check routine
cButtonRoutine();
// Interface render routine
interfaceRoutine();
// LED render routine
ledRoutine();
// Serial routine
serialRoutine();
}
// This is the main VFD Display interface loop routine
void interfaceRoutine() {
// This is the launch interface with standard clock ticking
if(interface == INTERFACE_TIME) {
// If intervall length exceeded, update dot position
// BEGIN OF DOT MODE HANDLER
if(INTF0_DM == 0) {
if(updateIntervalEvent(&dotUpdater)) INTF0_DP++;
if(INTF0_DP == 0) displayWrite(0, 0b00010100, 0, 0);
else if(INTF0_DP == 1) displayWrite(0, 0, 0, 0);
else INTF0_DP = 0;
}
else if(INTF0_DM == 1) {
if(updateIntervalEvent(&jdotUpdater)) INTF0_DP++;
if(INTF0_DP == 0) displayWrite(0, 0b00100001, 0, 0);
else if(INTF0_DP == 1) displayWrite(0, 0b00010010, 0, 0);
else if(INTF0_DP == 2) displayWrite(0, 0b00001100, 0, 0);
else if(INTF0_DP == 3) displayWrite(0, 0b00010010, 0, 0);
else INTF0_DP = 0;
}
else if(INTF0_DM == 2) {
// This function is damn lit. Once it detects a change in second,
// the decimal dot will slide over the displays.
// Get the current time and compare it with the previous timestamp
// DateTime now = rtc.now();
if(INTF0_ds != global_s) {
// Time has changed -> Reset dot position, remember timestamp, change direction
INTF0_DP = 0;
INTF0_ds = global_s;
INTF0_dr = !INTF0_dr;
}
// Next position
if(updateIntervalEvent(&sdotUpdater)) INTF0_DP++;
// From right to left
if(INTF0_dr) {
if(INTF0_DP < 5) displayWrite(0, (1 << INTF0_DP), 0, 0);
else displayWrite(0, 0b00100000, 0, 0);
}
// From left to right
else{
if(INTF0_DP < 5) displayWrite(0, (0b00100000 >> INTF0_DP), 0, 0);
else displayWrite(0, 0b00000001, 0, 0);
}
}
else if(INTF0_DM == 3) displayWrite(0, 0, 0, 0);
else INTF0_DM = 0;
// BEGIN OF BUTTON HANDLER
// Short press on F1 will change interface to date display
if(cF1 == SHORTPRESS) switchInterface(INTERFACE_DATE); // Enter date interface
if(cF1 == LONGPRESS) {
// Enter time set interface
char message[NUM_DIGITS_V] = {'T', ' ', 'S', 'E', 'T', ' '};
displayWrite(3, 0x00, 1000, message);
dateSet = FALSE;
switchInterface(INTERFACE_TIMEDATE_SET);
}
if(cF4 == SHORTPRESS) {
clearInterface();
INTF0_DM++;
// if((INTF0_DM == 2) && (led == LED_SILENT)) INTF0_DM = 3;
}
// Long press will save all settings.
if(cF4 == LONGPRESS) {
clearInterface();
saveConfig(0);
}
}
// This is the date display
else if(interface == INTERFACE_DATE) {
displayWrite(1, 0b00010100, 0, 0);
// Short press on F1 will change interface to temperature display (TEMPERATURE_ENABLE) or clock display (!TEMPERATURE_ENABLE)
if(cF1 == SHORTPRESS) {
#ifdef TEMPERATURE_ENABLE
switchInterface(INTERFACE_TEMPERATURE);
#else
switchInterface(INTERFACE_STOPWATCH);
#endif
}
if(cF1 == LONGPRESS) {
// Enter date set interface
char message[NUM_DIGITS_V] = {'D', ' ', 'S', 'E', 'T', ' '};
displayWrite(3, 0x00, 1000, message);
dateSet = TRUE;
switchInterface(INTERFACE_TIMEDATE_SET);
}
}
#ifdef TEMPERATURE_ENABLE
// This is the temperature sensor interface
else if(interface == INTERFACE_TEMPERATURE) {
// Create temperature reading collector
// Check for value update
if(updateIntervalEvent(&tsUpdater)) {
// Call temperature update routine to update DS18B20 reading
updateTemperature();
}
if(ts != 0) displayWrite(2 + (isFahrenheit << 1), 0b00010000, 0, 0);
// Short press on TEN will change interface to standard clock display
if(cF1 == SHORTPRESS) switchInterface(INTERFACE_STOPWATCH);
if(cF4 == SHORTPRESS) {
clearInterface();
if(isFahrenheit) isFahrenheit = 0;
}
}
#endif
// 3: Stopwatch
else if(interface == INTERFACE_STOPWATCH) {
if(cF1 == SHORTPRESS) switchInterface(INTERFACE_TIME);
if(INTF3_st == INTF3_INITIAL) {
// Initial State
if(cF2 == SHORTPRESS) {
// Start stopwatch
if(!INTF3_TB) INTF3_TB = new DateTime(rtc.now()); // Create new backup time
if(!INTF3_TS) INTF3_TS = new TimeSpan(0); // No difference
INTF3_MILLIS = millis(); // Start milliseconds
INTF3_MILLIE = 0;
INTF3_st = INTF3_RUNNING; // Set state to 1 (running)
clearInterface();
}
char k[NUM_DIGITS_V] = {0};
displayWrite(3, 0b000010100, 0, k);
}
else if(INTF3_st == INTF3_RUNNING) {
// Stopwatch running, get elapsed time
TimeSpan ts = *INTF3_TS + (rtc.now() - *INTF3_TB);
int8_t _h = ts.hours(),
_m = ts.minutes(),
_s = ts.seconds(),
_u = ((INTF3_MILLIE + (millis() - INTF3_MILLIS)) % 1000) / 10;
char k[NUM_DIGITS_V] = {0};
if(_h > 0) {
k[0] = _h / 10;
k[1] = _h % 10;
k[2] = _m / 10;
k[3] = _m % 10;
k[4] = _s / 10;
k[5] = _s % 10;
} else {
k[0] = _m / 10;
k[1] = _m % 10;
k[2] = _s / 10;
k[3] = _s % 10;
k[4] = _u / 10;
k[5] = _u % 10;
}
displayWrite(3, 0x00, 0, k);
if(cF3 == SHORTPRESS) {
// Pause stopwatch
*INTF3_TS = *INTF3_TS + (rtc.now() - *INTF3_TB); // Current elapsed time
INTF3_MILLIE = INTF3_MILLIE + (millis() - INTF3_MILLIS); // Current millis
INTF3_st = INTF3_PAUSED;
clearInterface();
}
}
else if(INTF3_st == INTF3_PAUSED) {
// Stopwatch paused, constant display of current time
int8_t _h = INTF3_TS->hours(),
_m = INTF3_TS->minutes(),
_s = INTF3_TS->seconds(),
_u = (INTF3_MILLIE % 1000) / 10;
char k[NUM_DIGITS_V] = {0};
if(_h > 0) {
k[0] = _h / 10;
k[1] = _h % 10;
k[2] = _m / 10;
k[3] = _m % 10;
k[4] = _s / 10;
k[5] = _s % 10;
} else {
k[0] = _m / 10;
k[1] = _m % 10;
k[2] = _s / 10;
k[3] = _s % 10;
k[4] = _u / 10;
k[5] = _u % 10;
}
displayWrite(3, 0x00, 0, k);
if(cF2 == SHORTPRESS) {
// Reset stopwatch by clearing all time variables
delete INTF3_TS;
delete INTF3_TB;
INTF3_TS = nullptr;
INTF3_TB = nullptr;
INTF3_MILLIS = 0;
INTF3_MILLIE = 0;
INTF3_st = INTF3_INITIAL;
clearInterface();
}
if(cF3 == SHORTPRESS) {
// Resume stopwatch
INTF3_st = INTF3_RUNNING;
*INTF3_TB = rtc.now();
INTF3_MILLIS = millis();
clearInterface();
}
}
}
// 128: Time/Date set menu!
else if(interface == INTERFACE_TIMEDATE_SET) {
// Blink active set in time interval of 800 ms
// Switch between displayWrite(0) and displayWrite(3) for individual inactive segments
// Use intervalEvent jdotUpdater which has the same attributes
static uint8_t offActive = FALSE;
static uint8_t blinkDisplay = 0;
static uint8_t tmpHour, tmpMinute, tmpSecond, tmpDay, tmpMonth, tmpYear = 0;
DateTime now = rtc.now();
if(setOnceFlag == FALSE) {
tmpHour = now.hour();
tmpMinute = now.minute();
tmpSecond = now.second();
tmpDay = now.day();
tmpMonth = now.month();
tmpYear = now.year() % 100;
setOnceFlag = TRUE;
}
if(updateIntervalEvent(&jdotUpdater)) offActive = !offActive; // Flip uint8_t
char tRenderArray[NUM_DIGITS_V] = {0, 0, 0, 0, 0, 0};
if(!dateSet) { // If time set
tRenderArray[5] = tmpSecond % 10;
tRenderArray[4] = tmpSecond / 10;
tRenderArray[3] = tmpMinute % 10;
tRenderArray[2] = tmpMinute / 10;
tRenderArray[1] = tmpHour % 10;
tRenderArray[0] = tmpHour / 10;
}
else{ // If date set
tRenderArray[5] = tmpYear % 10;
tRenderArray[4] = (tmpYear % 100) / 10;
if(rBit(clockFlags, B_INTD)) {
tRenderArray[1] = tmpMonth % 10;
tRenderArray[0] = tmpMonth / 10;
tRenderArray[3] = tmpDay % 10;
tRenderArray[2] = tmpDay / 10;
}else{
tRenderArray[3] = tmpMonth % 10;
tRenderArray[2] = tmpMonth / 10;
tRenderArray[1] = tmpDay % 10;
tRenderArray[0] = tmpDay / 10;
}
}
if(offActive) {
// Blink corresponding display parameter
if(blinkDisplay == 0) {
tRenderArray[0] = ' ';
tRenderArray[1] = ' ';
}
else if(blinkDisplay == 1) {
tRenderArray[2] = ' ';
tRenderArray[3] = ' ';
}
else if(blinkDisplay == 2) {
tRenderArray[4] = ' ';
tRenderArray[5] = ' ';
}
}
displayWrite(3, 0x00, 0, tRenderArray);
// Short press on F1 will leave time set mode and enter time interface again
if(cF1 == SHORTPRESS) {
if(!dateSet) switchInterface(INTERFACE_TIME);
else switchInterface(INTERFACE_DATE);
}
// Long press on F1 will change between 12h and 24h display
if(cF1 == LONGPRESS) {
if(!dateSet) {
tBit(&clockFlags, B_12H); // Flip clockFlags 12h bit
if(rBit(clockFlags, B_12H)) {
char k[NUM_DIGITS_V] = {1, 2, 'H'};
for(uint8_t i = 3; i < NUM_DIGITS_V; i++) k[i] = ' ';
displayWrite(3, 0x00, 1000, k);
}else{
char k[NUM_DIGITS_V] = {2, 4, 'H'};
for(uint8_t i = 3; i < NUM_DIGITS_V; i++) k[i] = ' ';
displayWrite(3, 0x00, 1000, k);
}
}else{
tBit(&clockFlags, B_INTD); // Flip clockFlags international date bit
if(rBit(clockFlags, B_INTD)) {
char k[NUM_DIGITS_V];
for(uint8_t i = 0; i < NUM_DIGITS_V; i++) k[i] = pgm_read_byte_near(MSG_INTLDATESET + i);
displayWrite(3, 0x00, 1000, k);
}else{
char k[NUM_DIGITS_V];
for(uint8_t i = 0; i < NUM_DIGITS_V; i++) k[i] = pgm_read_byte_near(MSG_INTLDATERESET + i);
displayWrite(3, 0x00, 1000, k);
}
}
}
// Short press on F2 changes the active parameter (h/m/s)
if(cF2 == SHORTPRESS) {
clearInterface();
blinkDisplay++;
if(blinkDisplay == 3) blinkDisplay = 0;
}
if(cF3 == SHORTPRESS) {
clearInterface();
// parameter--
if(blinkDisplay == 0) { // Set hour or day
if(!dateSet) { // Set hour
if (tmpHour > 0) tmpHour--;
else if (tmpHour == 0) tmpHour = 23;
}
else{ // Set day or month (B_INTD)
if(rBit(clockFlags, B_INTD)) goto monthsetMinus; // Please don't pick on goto :p
daysetMinus:
uint8_t dMax = 31;
// Leap year detection
if(tmpMonth == 2) dMax = ((tmpYear % 400 == 0) || ((tmpYear % 4 == 0) && (tmpYear % 100 != 0))) ? 29 : 28;
// Short month detection
else if((tmpMonth == 4) || (tmpMonth == 6) || (tmpMonth == 9) || (tmpMonth == 11)) dMax = 30;
if (tmpDay > 1) tmpDay--;
else if (tmpDay == 1) tmpDay = dMax;
}
}
else if(blinkDisplay == 1) {
if(!dateSet) {
if (tmpMinute > 0) tmpMinute--;
else if (tmpMinute == 0) tmpMinute = 59;
}
else{
if(rBit(clockFlags, B_INTD)) goto daysetMinus;
monthsetMinus:
if (tmpMonth > 1) tmpMonth--;
else if (tmpMonth == 1) tmpMonth = 12;
}
}
else if(blinkDisplay == 2) {
if(!dateSet) {
if (tmpSecond > 0) tmpSecond--;
else if (tmpSecond == 0) tmpSecond = 59;
}
else{
if (tmpYear > 0) tmpYear--;
else if (tmpYear == 0) tmpYear = 30;
}
}
}
if(cF4 == SHORTPRESS) {
clearInterface();
// parameter++
if(blinkDisplay == 0) { // Set hour or day
if(!dateSet) { // Set hour
if (tmpHour < 23) tmpHour++;
else if (tmpHour == 23) tmpHour = 0;
}
else{ // Set day or month (B_INTD)
if(rBit(clockFlags, B_INTD)) goto monthsetPlus;
daysetPlus:
uint8_t dMax = 31;
// Leap year detection
if(tmpMonth == 2) dMax = ((tmpYear % 400 == 0) || ((tmpYear % 4 == 0) && (tmpYear % 100 != 0))) ? 29 : 28;
// Short month detection
else if((tmpMonth == 4) || (tmpMonth == 6) || (tmpMonth == 9) || (tmpMonth == 11)) dMax = 30;
if (tmpDay < dMax) tmpDay++;
else if (tmpDay == dMax) tmpDay = 1;
}
}
else if(blinkDisplay == 1) {
if(!dateSet) {
if (tmpMinute < 59) tmpMinute++;
else if (tmpMinute == 59) tmpMinute = 0;
}
else{
if(rBit(clockFlags, B_INTD)) goto daysetPlus;
monthsetPlus:
if (tmpMonth < 12) tmpMonth++;
else if (tmpMonth == 12) tmpMonth = 1;
}
}
else if(blinkDisplay == 2) {
if(!dateSet) {
if (tmpSecond < 59) tmpSecond++;
else if (tmpSecond == 59) tmpSecond = 0;
}
else{
if (tmpYear < 30) tmpYear++;
else if (tmpYear == 30) tmpYear = 0;
}
}
}
// Transfer to RTC
Wire.beginTransmission(0x68);
Wire.write(byte(0));
Wire.write(decToBcd(tmpSecond));
Wire.write(decToBcd(tmpMinute));
Wire.write(decToBcd(tmpHour));
Wire.write(0x06);
Wire.write(decToBcd(tmpDay));
Wire.write(decToBcd(tmpMonth));
Wire.write(decToBcd(tmpYear));
Wire.write(byte(0));
Wire.endTransmission();
setOnceFlag = FALSE; // Reset static flag
}
}
// Button check routine
void cButtonRoutine() {
cF1 = checkOption(B_F1_PIN); // Short press: main interface switch
cF2 = checkOption(B_F2_PIN); // Short press: color switch
cF3 = checkOption(B_F3_PIN);
cF4 = checkOption(B_F4_PIN); // Short press: display mode switch
}
// This is the LED loop routine
void ledRoutine() {
// LED 0: Color preset
if(led == LED_STATIC) {
// If not single Color
if(LED0P > LED0_mcOffset) {
for(uint8_t i = 0; i < NUM_BYTES; i++) target_arr[i] = led_Presets[LED0P - LED0_cOffset][i];
ledSmoothWrite();
}
else{ // Save some RAM
for(uint8_t offset = 0; offset < NUM_BYTES; offset += 3) {
target_arr[offset] = led_scPresets[LED0P][0];
target_arr[offset + 1] = led_scPresets[LED0P][1];
target_arr[offset + 2] = led_scPresets[LED0P][2];
}
ledSmoothWrite();
}
if(cF2 == SHORTPRESS) {
led = LED_FADE; // Switch to regular fade
char k[NUM_DIGITS_V];
for(uint8_t i = 0; i < NUM_DIGITS_V; i++) k[i] = pgm_read_byte_near(MSG_FADE + i);
displayWrite(3, 0x00, 1000, k);
clearInterface();
}
if(cF3 == SHORTPRESS) {
LED0P++;
if(LED0P == 16) LED0P = 0;
// Dynamic memory saving
char LED0PMC[NUM_DIGITS_V];
LED0PMC[0] = 'C';
LED0PMC[1] = ' ';
for(uint8_t i = 2; i < NUM_RGB; i++) LED0PMC[i] = LED0PM[LED0P][i - 2];
displayWrite(3, 0x00, 500, LED0PMC); // Write change message
clearInterface();
}
}
// LED 2: Serial accessible color mode
else if(led == LED_SERIAL_0) {
// ledDirectWrite(scustom_arr);
if(cF2 == SHORTPRESS) {
led = LED_FADE; // Switch to regular fade
char k[NUM_DIGITS_V];
for(uint8_t i = 0; i < NUM_DIGITS_V; i++) k[i] = pgm_read_byte_near(MSG_FADE + i);
displayWrite(3, 0x00, 1000, k);