-
Notifications
You must be signed in to change notification settings - Fork 7
/
QlockWiFive.ino
1206 lines (1131 loc) · 36.4 KB
/
QlockWiFive.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
/******************************************************************************
QLOCKWORK
Eine Firmware der Selbstbau-QLOCKTWO.
@mc ESP8266
@created 01.02.2017
******************************************************************************/
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <ArduinoOTA.h>
#include <Wire.h>
#include <RtcDS3231.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <Syslog.h>
#include "Button.h"
#include "Configuration.h"
#include "Debug.h"
#include "Modes.h"
#include "Renderer.h"
#include "Colors.h"
#include "Languages.h"
#if defined(LED_DRIVER_SK6812) && defined(LED_RGBW)
#include "LedDriverNeopixel.h"
#else
#include "LedDriver.h"
#endif
#include "Settings.h"
#define FIRMWARE_VERSION "qw20170401"
/******************************************************************************
Init
******************************************************************************/
Renderer renderer;
#if defined(LED_DRIVER_SK6812) && defined(LED_RGBW)
LedDriverNeoPixel ledDriver(PIN_LEDS_DATA);
#else
LedDriver ledDriver;
#endif
Settings settings;
WiFiUDP Udp;
ESP8266WebServer server(80);
Debug debug;
#ifdef RTC_BACKUP
RtcDS3231<TwoWire> Rtc(Wire);
#endif
#ifdef IR_REMOTE
IRrecv irrecv(PIN_IR_RECEIVER);
#endif
#ifdef SYSLOG_SERVER
Syslog syslog(Udp, SYSLOG_SERVER, SYSLOG_PORT, HOSTNAME, "", SYSLOG_FACILITY);
#endif
#ifdef BUTTONS
Button minusButton(PIN_M_PLUS, BUTTONS_PRESSING_AGAINST);
Button plusButton(PIN_H_PLUS, BUTTONS_PRESSING_AGAINST);
Button extModeDoubleButton(PIN_M_PLUS, PIN_H_PLUS, (byte)BUTTONS_PRESSING_AGAINST);
Button modeButton(PIN_MODE, BUTTONS_PRESSING_AGAINST);
#endif
word matrix[16];
boolean ScreenBufferNeedsUpdate = true;
IPAddress timeServerIP;
uint8_t packetBuffer[48];
Mode mode = STD_MODE_NORMAL;
Mode lastMode = mode;
uint8_t fallBackCounter = 0;
uint8_t lastDay = 0;
uint8_t lastHour = 0;
uint8_t lastMinute = 0;
uint8_t lastFiveMinute = 0;
boolean timerSet = false;
uint16_t timerMinutes = 0;
time_t timer = 0;
time_t lastTime = 0;
uint8_t alarmOn = 0;
uint8_t testColumn = 0;
uint32_t lastBrightnessCheck = 0;
uint8_t ledBrightness = 0;
uint8_t lv = 0;
uint16_t minBrightness = 1023;
uint16_t maxBrightness = 0;
uint16_t lastValue = 0;
int8_t maxColor = sizeof(defaultColors) / 3 - 1;
#ifdef IR_REMOTE
decode_results irDecodeResults;
#endif
#ifdef TIMEZONE_PST
TimeChangeRule usPDT = { "PDT", Second, dowSunday, Mar, 2, -420 };
TimeChangeRule usPST = { "PST", First, dowSunday, Nov, 2, -480 };
Timezone timeZone(usPDT, usPST);
#endif
#ifdef TIMEZONE_MST
TimeChangeRule usMDT = { "MDT", Second, dowSunday, Mar, 2, -360 };
TimeChangeRule usMST = { "MST", First, dowSunday, Nov, 2, -420 };
Timezone timeZone(usMDT, usMST);
#endif
#ifdef TIMEZONE_CST
TimeChangeRule usCDT = { "CDT", Second, dowSunday, Mar, 2, -300 };
TimeChangeRule usCST = { "CST", First, dowSunday, Nov, 2, -360 };
Timezone timeZone(usCDT, usCST);
#endif
#ifdef TIMEZONE_EST
TimeChangeRule usEDT = { "EDT", Second, Sun, Mar, 2, -240 };
TimeChangeRule usEST = { "EST", First, Sun, Nov, 2, -300 };
Timezone timeZone(usEDT, usEST);
#endif
#ifdef TIMEZONE_GMT
TimeChangeRule BST = { "BST", Last, Sun, Mar, 1, 60 };
TimeChangeRule GMT = { "GMT", Last, Sun, Oct, 2, 0 };
Timezone timeZone(BST, GMT);
#endif
#ifdef TIMEZONE_CET
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 };
TimeChangeRule CET = { "CET", Last, Sun, Oct, 3, 60 };
Timezone timeZone(CEST, CET);
#endif
#ifdef TIMEZONE_AEST
TimeChangeRule aEDT = { "AEDT", First, Sun, Oct, 2, 660 };
TimeChangeRule aEST = { "AEST", First, Sun, Apr, 3, 600 };
Timezone timeZone(aEDT, aEST);
#endif
/******************************************************************************
setup()
******************************************************************************/
void setup() {
// Seriellen Port initialisieren
Serial.begin(SERIAL_SPEED);
delay(1000);
DEBUG_PRINTLN();
DEBUG_PRINTLN("QLOCKWORK");
DEBUG_PRINT("Firmware: ");
DEBUG_PRINTLN(FIRMWARE_VERSION);
// LDR, Buzzer und LED initialisieren
DEBUG_PRINTLN("Stetting up LDR, Buzzer and LED.");
pinMode(PIN_LDR, INPUT);
pinMode(PIN_BUZZER, OUTPUT);
#ifdef BOARD_LED
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, HIGH);
#endif
#ifdef IR_REMOTE
// IR-Empfaenger initialisieren
DEBUG_PRINTLN("Starting IR receiver.");
irrecv.enableIRIn();
#endif
// WiFi und Dienste initialisieren
renderer.clearScreenBuffer(matrix);
matrix[0] = 0b0000000000000000;
matrix[1] = 0b0000111000000000;
matrix[2] = 0b0011111110000000;
matrix[3] = 0b1111111111100000;
matrix[4] = 0b0111111111000000;
matrix[5] = 0b0011111110000000;
matrix[6] = 0b0001111100000000;
matrix[7] = 0b0000111000000000;
matrix[8] = 0b0000010000000000;
matrix[9] = 0b0000000000000000;
ledBrightness = settings.getBrightness();
ledDriver.setBrightness(100);
ledDriver.writeScreenBufferToLEDs(matrix, 0); // Color 0: weiss
delay(1000);
WiFi.hostname(HOSTNAME);
WiFiManager wifiManager;
//wifiManager.resetSettings();
wifiManager.setTimeout(60);
wifiManager.autoConnect(HOSTNAME);
if (WiFi.status() != WL_CONNECTED) {
DEBUG_PRINTLN("Error connecting to WiFi. Shutting down WiFi.");
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("ER", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText("OR", Renderer::TEXT_POS_BOTTOM, matrix);
ledDriver.writeScreenBufferToLEDs(matrix, 1); // Color 1: rot
WiFi.mode(WIFI_OFF);
digitalWrite(PIN_BUZZER, HIGH);
delay(1500);
digitalWrite(PIN_BUZZER, LOW);
}
else {
DEBUG_PRINTLN("WiFi connected.");
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("OK", Renderer::TEXT_POS_MIDDLE, matrix);
ledDriver.writeScreenBufferToLEDs(matrix, 2); // Color 2: gruen
for (uint8_t i = 0; i < 3; i++) {
digitalWrite(PIN_BUZZER, HIGH);
delay(100);
digitalWrite(PIN_BUZZER, LOW);
delay(100);
}
}
DEBUG_PRINTLN("Starting mDNS responder.");
MDNS.begin(HOSTNAME);
DEBUG_PRINTLN("Starting WebServer on Port 80.");
setupWebServer();
DEBUG_PRINTLN("Starting UDP on Port 2390.");
Udp.begin(2390);
DEBUG_PRINTLN("Starting Arduino-OTA service.");
ArduinoOTA.setPassword((const char*)OTA_PASS);
ArduinoOTA.begin();
DEBUG_PRINT("Free RAM: ");
DEBUG_PRINTLN(system_get_free_heap_size());
#ifdef SYSLOG_SERVER
syslog.log(LOG_INFO, "Firmware: " + String(FIRMWARE_VERSION));
syslog.log(LOG_DEBUG, "Free RAM: " + String(system_get_free_heap_size()));
#endif
// Zeitprovider setzen
#ifdef RTC_BACKUP
Rtc.Begin();
Rtc.SetIsRunning(true);
if (WiFi.status() == WL_CONNECTED) {
DEBUG_PRINTLN("Setting ESP from NTP with RTC backup.");
setSyncProvider(getNtpTime);
setSyncInterval(3600);
}
else {
DEBUG_PRINTLN("Setting ESP from RTC.");
setSyncProvider(getRtcTime);
setSyncInterval(3600);
}
#else
if (WiFi.status() == WL_CONNECTED) {
DEBUG_PRINTLN("Setting ESP from NTP.");
setSyncProvider(getNtpTime);
setSyncInterval(3600);
}
else DEBUG_PRINTLN("No provider for setting the time found.");
#endif
// sonstiges
lastDay = day();
lastHour = hour();
lastFiveMinute = minute() / 5;
lastMinute = minute();
lastTime = now();
}
/******************************************************************************
loop()
******************************************************************************/
void loop() {
#ifdef DEBUG_FPS
debug.debugFps();
#endif
// Jeden Tag ausfuehren
if (day() != lastDay) {
lastDay = day();
DEBUG_PRINTLN("Neuen Tag erreicht.");
#ifdef SYSLOG_SERVER
syslog.log(LOG_DEBUG, "Free RAM: " + String(system_get_free_heap_size()));
#endif
//if (settings.getColor() < maxColor) settings.setColor(settings.getColor() + 1);
//else settings.setColor(0);
}
// Jede Stunde ausfuehren
if (hour() != lastHour) {
lastHour = hour();
DEBUG_PRINTLN("Volle Stunde erreicht.");
#if defined(RTC_BACKUP) && defined(SYSLOG_SERVER)
syslog.log(LOG_INFO, "Temperature: " + String(Rtc.GetTemperature().AsFloat() + RTC_TEMP_OFFSET) + "C / " + String((Rtc.GetTemperature().AsFloat() + RTC_TEMP_OFFSET) * 1.8 + 32) + "F");
#endif
}
// Alle fuenf Minuten ausfuehren
// lastFiveMinute ist vom Typ uint8_t. Dadurch werden die Nachkommastellen verworfen.
if ((minute() / 5) != lastFiveMinute) {
lastFiveMinute = minute() / 5;
DEBUG_PRINTLN("Volle fuenf Minuten erreicht.");
}
// Jede Minute ausfuehren
if (minute() != lastMinute) {
lastMinute = minute();
#ifdef DEBUG
debug.debugTime("Time:", now());
#endif
if (mode == STD_MODE_NORMAL) ScreenBufferNeedsUpdate = true;
}
// Jede Sekunde ausfuehren
if (now() != lastTime) {
lastTime = now();
// Fallback zur Zeitanzeige runterzaehlen
if (fallBackCounter > 1) fallBackCounter--;
else if (fallBackCounter == 1) {
fallBackCounter = 0;
setMode(STD_MODE_NORMAL);
}
// Display braucht Sekundenupdate
switch (mode) {
case STD_MODE_SECONDS:
#ifdef RTC_BACKUP
case STD_MODE_TEMP:
#endif
case STD_MODE_SET_TIMER:
case STD_MODE_TIMER:
case STD_MODE_ALARM_1:
case STD_MODE_SET_ALARM_1:
case STD_MODE_ALARM_2:
case STD_MODE_SET_ALARM_2:
#ifdef LDR
case EXT_MODE_LDR:
#endif
case EXT_MODE_COLOR:
case EXT_MODE_TIMEOUT:
case EXT_MODE_LANGUAGE:
case EXT_MODE_TIMESET:
case EXT_MODE_IT_IS:
case EXT_MODE_DAYSET:
case EXT_MODE_MONTHSET:
case EXT_MODE_YEARSET:
case EXT_MODE_NIGHTOFF:
case EXT_MODE_NIGHTON:
case EXT_MODE_TEST:
ScreenBufferNeedsUpdate = true;
break;
default:
break;
}
#ifdef BOARD_LED
// LED auf dem Board blinken lassen
if (digitalRead(PIN_LED) == LOW) digitalWrite(PIN_LED, HIGH);
else digitalWrite(PIN_LED, LOW);
#endif
// Alarm
if (settings.getAlarm1() && (hour() == hour(settings.getAlarmTime1())) && (minute() == minute(settings.getAlarmTime1())) && (second() == 0)) alarmOn = 60;
if (settings.getAlarm2() && (hour() == hour(settings.getAlarmTime2())) && (minute() == minute(settings.getAlarmTime2())) && (second() == 0)) alarmOn = 60;
// Timer
if (timerSet && (now() == timer)) {
setMode(STD_MODE_SET_TIMER);
timerMinutes = 0;
timerSet = false;
alarmOn = 60;
}
// ggf. Laerm machen
if (alarmOn) {
if (second() % 2 == 0) digitalWrite(PIN_BUZZER, HIGH);
else digitalWrite(PIN_BUZZER, LOW);
alarmOn--;
}
else digitalWrite(PIN_BUZZER, LOW);
// Nachtmodus ein/aus
if ((hour() == hour(settings.getNightOffTime())) && (minute() == minute(settings.getNightOffTime())) && (second() == 0)) setMode(STD_MODE_BLANK);
if ((hour() == hour(settings.getNightOnTime())) && (minute() == minute(settings.getNightOnTime())) && (second() == 0)) setMode(lastMode);
}
// Immer ausfuehren
// HTTP- und OTA-Anfragen entgegennehmen
server.handleClient();
ArduinoOTA.handle();
uint8_t ledBrightnessOld = ledBrightness;
// LDR ggf. lesen und Helligkeit einstellen
#ifdef LDR
if (!settings.getUseLdr()) ledBrightness = settings.getBrightness();
else {
if (millis() > (lastBrightnessCheck + 25)) {
uint16_t val = (1023 - analogRead(PIN_LDR));
if ((val > (lastValue + 25)) || (val < (lastValue - 25))) {
if (val < minBrightness) minBrightness = val;
if (val > maxBrightness) maxBrightness = val + 1;
lv = map(val, minBrightness, maxBrightness, 0, 255);
lastValue = val;
}
if (ledBrightness < lv) ledBrightness++;
if (ledBrightness > lv) ledBrightness--;
lastBrightnessCheck = millis();
}
}
#else
ledBrightness = settings.getBrightness();
#endif
if(ledBrightness != ledBrightnessOld) {
ledDriver.setBrightness(constrain(ledBrightness, MIN_BRIGHTNESS, MAX_BRIGHTNESS));
ScreenBufferNeedsUpdate = true;
//ledDriver.show();
}
#ifdef IR_REMOTE
// IR-Empfaenger abfragen und ggf. reagieren
if (irrecv.decode(&irDecodeResults)) {
DEBUG_PRINT("IR signal: ");
DEBUG_PRINTLN(irDecodeResults.value);
remoteAction(irDecodeResults.value);
irrecv.resume();
}
#endif
#ifdef BUTTONS
// M+ und H+ im STD_MODE_BLANK gedrueckt?
if ((mode == STD_MODE_BLANK) && extModeDoubleButton.pressed()) {
doubleExtModePressed();
}
// Taste Minuten++ (brighness++) gedrueckt?
if (plusButton.pressed()) {
buttonPlusPressed();
}
// Taste Stunden++ (brightness--) gedrueckt?
if (minusButton.pressed()) {
buttonMinusPressed();
}
// Taste Moduswechsel gedrueckt?
if (modeButton.pressed()) {
modePressed();
}
#endif
// Wenn noetig, den Screenbuffer neu erstellen und in die LEDs schreiben
if (ScreenBufferNeedsUpdate) {
ScreenBufferNeedsUpdate = false;
switch (mode) {
case STD_MODE_NORMAL:
renderer.clearScreenBuffer(matrix);
renderer.setTime(hour(), minute(), settings.getLanguage(), matrix);
renderer.setCorners(minute(), matrix);
if (!settings.getEsIst() && ((minute() / 5) % 6)) renderer.clearEntryWords(settings.getLanguage(), matrix);
if (settings.getAlarm1() || settings.getAlarm2()) renderer.activateAlarmLed(matrix);
break;
case STD_MODE_AMPM:
renderer.clearScreenBuffer(matrix);
if (isAM()) renderer.setSmallText("AM", Renderer::TEXT_POS_MIDDLE, matrix);
else renderer.setSmallText("PM", Renderer::TEXT_POS_MIDDLE, matrix);
break;
case STD_MODE_SECONDS:
renderer.clearScreenBuffer(matrix);
renderer.setCorners(minute(), matrix);
for (uint8_t i = 0; i < 7; i++) {
matrix[1 + i] |= (zahlenGross[second() / 10][i]) << 11;
matrix[1 + i] |= (zahlenGross[second() % 10][i]) << 5;
}
break;
case STD_MODE_DATE:
renderer.clearScreenBuffer(matrix);
if (day() < 10) renderer.setSmallText(("0" + String(day())), Renderer::TEXT_POS_TOP, matrix);
else renderer.setSmallText(String(day()), Renderer::TEXT_POS_TOP, matrix);
if (month() < 10) renderer.setSmallText(("0" + String(month())), Renderer::TEXT_POS_BOTTOM, matrix);
else renderer.setSmallText(String(month()), Renderer::TEXT_POS_BOTTOM, matrix);
renderer.setPixelInScreenBuffer(5, 4, matrix);
renderer.setPixelInScreenBuffer(5, 9, matrix);
break;
#ifdef RTC_BACKUP
case STD_MODE_TEMP:
renderer.clearScreenBuffer(matrix);
for (uint8_t i = 0; i < 7; i++) {
matrix[1 + i] |= (zahlenGross[(Rtc.GetTemperature().AsWholeDegrees() + RTC_TEMP_OFFSET) / 10][i]) << 11;
matrix[1 + i] |= (zahlenGross[(Rtc.GetTemperature().AsWholeDegrees() + RTC_TEMP_OFFSET) % 10][i]) << 5;
}
renderer.setPixelInScreenBuffer(4, 0, matrix); // LED rechts oben setzen als "Grad"
DEBUG_PRINTLN(String(Rtc.GetTemperature().AsFloat() + RTC_TEMP_OFFSET));
break;
#endif
case STD_MODE_SET_TIMER:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("TR", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else renderer.setSmallText(String(timerMinutes), Renderer::TEXT_POS_BOTTOM, matrix);
break;
case STD_MODE_TIMER:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("TR", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText(String((timer - now() + 60) / 60), Renderer::TEXT_POS_BOTTOM, matrix);
DEBUG_PRINTLN(String(timer - now()));
break;
case STD_MODE_ALARM_1:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("A1", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else {
if (settings.getAlarm1()) {
renderer.setSmallText("EN", Renderer::TEXT_POS_BOTTOM, matrix);
renderer.activateAlarmLed(matrix);
}
else renderer.setSmallText("DA", Renderer::TEXT_POS_BOTTOM, matrix);
}
break;
case STD_MODE_SET_ALARM_1:
renderer.clearScreenBuffer(matrix);
if (second() % 2 == 0) {
renderer.setTime(hour(settings.getAlarmTime1()), minute(settings.getAlarmTime1()), settings.getLanguage(), matrix);
renderer.clearEntryWords(settings.getLanguage(), matrix);
renderer.setAMPM(hour(settings.getAlarmTime1()), settings.getLanguage(), matrix);
renderer.activateAlarmLed(matrix);
}
break;
case STD_MODE_ALARM_2:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("A2", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else {
if (settings.getAlarm2()) {
renderer.setSmallText("EN", Renderer::TEXT_POS_BOTTOM, matrix);
renderer.activateAlarmLed(matrix);
}
else renderer.setSmallText("DA", Renderer::TEXT_POS_BOTTOM, matrix);
}
break;
case STD_MODE_SET_ALARM_2:
renderer.clearScreenBuffer(matrix);
if (second() % 2 == 0) {
renderer.setTime(hour(settings.getAlarmTime2()), minute(settings.getAlarmTime2()), settings.getLanguage(), matrix);
renderer.clearEntryWords(settings.getLanguage(), matrix);
renderer.setAMPM(hour(settings.getAlarmTime2()), settings.getLanguage(), matrix);
renderer.activateAlarmLed(matrix);
}
break;
case EXT_MODE_TEXT_MAIN:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("MA", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText("IN", Renderer::TEXT_POS_BOTTOM, matrix);
break;
#ifdef LDR
case EXT_MODE_LDR:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("LD", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else {
if (settings.getUseLdr()) renderer.setSmallText("EN", Renderer::TEXT_POS_BOTTOM, matrix);
else renderer.setSmallText("DA", Renderer::TEXT_POS_BOTTOM, matrix);
}
break;
#endif
case EXT_MODE_BRIGHTNESS:
renderer.clearScreenBuffer(matrix);
for (uint8_t xb = 0; xb < map(settings.getBrightness(), 0, 255, 1, 10); xb++) {
for (uint8_t yb = 0; yb <= xb; yb++) matrix[9 - yb] |= 1 << (14 - xb);
}
break;
case EXT_MODE_COLOR:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("CO", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else renderer.setSmallText(String(settings.getColor()), Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_TIMEOUT:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("FB", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else renderer.setSmallText(String(settings.getTimeout()), Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_LANGUAGE:
renderer.clearScreenBuffer(matrix);
if (second() % 2 == 0) {
if (sLanguage[settings.getLanguage()].length() == 2) renderer.setSmallText(sLanguage[settings.getLanguage()], Renderer::TEXT_POS_MIDDLE, matrix);
else {
renderer.setSmallText(sLanguage[settings.getLanguage()].substring(0, 2), Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText(sLanguage[settings.getLanguage()].substring(2, 4), Renderer::TEXT_POS_BOTTOM, matrix);
}
}
break;
case EXT_MODE_TEXT_TIME:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("TI", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText("ME", Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_TIMESET:
renderer.clearScreenBuffer(matrix);
if (second() % 2 == 0) {
renderer.setTime(hour(), minute(), settings.getLanguage(), matrix);
renderer.setCorners(minute(), matrix);
renderer.clearEntryWords(settings.getLanguage(), matrix);
renderer.setAMPM(hour(), settings.getLanguage(), matrix);
}
break;
case EXT_MODE_IT_IS:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("IT", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else {
if (settings.getEsIst()) renderer.setSmallText("EN", Renderer::TEXT_POS_BOTTOM, matrix);
else renderer.setSmallText("DA", Renderer::TEXT_POS_BOTTOM, matrix);
}
break;
case EXT_MODE_DAYSET:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("DD", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else renderer.setSmallText(String(day()), Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_MONTHSET:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("MM", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else renderer.setSmallText(String(month()), Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_YEARSET:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("YY", Renderer::TEXT_POS_TOP, matrix);
if (second() % 2 == 0) for (uint8_t i = 5; i < 10; i++) matrix[i] = 0;
else renderer.setSmallText(String(year()).substring(2, 4), Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_TEXT_NIGHTOFF:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("NT", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText("OF", Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_NIGHTOFF:
renderer.clearScreenBuffer(matrix);
if (second() % 2 == 0) {
renderer.setTime(hour(settings.getNightOffTime()), minute(settings.getNightOffTime()), settings.getLanguage(), matrix);
renderer.clearEntryWords(settings.getLanguage(), matrix);
renderer.setAMPM(hour(settings.getNightOffTime()), settings.getLanguage(), matrix);
}
break;
case EXT_MODE_TEXT_NIGHTON:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("NT", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText("ON", Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_NIGHTON:
renderer.clearScreenBuffer(matrix);
if (second() % 2 == 0) {
renderer.setTime(hour(settings.getNightOnTime()), minute(settings.getNightOnTime()), settings.getLanguage(), matrix);
renderer.clearEntryWords(settings.getLanguage(), matrix);
renderer.setAMPM(hour(settings.getNightOnTime()), settings.getLanguage(), matrix);
}
break;
case EXT_MODE_TEXT_TEST:
renderer.clearScreenBuffer(matrix);
renderer.setSmallText("TE", Renderer::TEXT_POS_TOP, matrix);
renderer.setSmallText("ST", Renderer::TEXT_POS_BOTTOM, matrix);
break;
case EXT_MODE_TEST:
renderer.clearScreenBuffer(matrix);
if (testColumn == 10) testColumn = 0;
matrix[testColumn] = 0b1111111111110000;
testColumn++;
break;
case STD_MODE_BLANK:
renderer.clearScreenBuffer(matrix);
break;
default:
break;
}
#if defined(IR_LETTER_OFF)
// Die LED hinter dem IR-Sensor abschalten.
renderer.unsetPixelInScreenBuffer(8, 9, matrix);
#endif
// Screenbuffer in die LEDs schreiben
ledDriver.writeScreenBufferToLEDs(matrix, settings.getColor());
#ifdef DEBUG_MATRIX
// Screenbuffer auf der Konsole ausgeben
debug.debugScreenBuffer(matrix);
#endif
}
}
/******************************************************************************
"M+" & "H+" gedrueckt im BLANK MODE
******************************************************************************/
#ifdef BUTTONS
void doubleExtModePressed() {
DEBUG_PRINTLN(F("Minutes plus AND hours plus pressed in STD_MODE_BLANK..."));
DEBUG_FLUSH();
unsigned long initialMillis = millis();
while (minusButton.pressedRaw() && plusButton.pressedRaw()) {
}
setMode(EXT_MODE_START);
DEBUG_PRINT(F("Entering EXT_MODEs, mode is now "));
DEBUG_PRINT(mode);
DEBUG_PRINTLN(F("..."));
DEBUG_FLUSH();
}
#endif
/******************************************************************************
"Mode" gedrueckt
******************************************************************************/
void modePressed() {
// Alarm abschalten
if (alarmOn) {
alarmOn = false;
digitalWrite(PIN_BUZZER, LOW);
return;
}
// Nachtmode abschalten
if (mode == STD_MODE_BLANK) {
setMode(STD_MODE_NORMAL);
return;
}
// Mode weiterschalten und ggf. Fallback setzen
setMode(mode++);
switch (mode) {
case STD_MODE_COUNT:
case EXT_MODE_COUNT:
setMode(STD_MODE_NORMAL);
break;
case STD_MODE_TIMER:
if (!timerSet) setMode(mode++);
break;
case STD_MODE_SET_ALARM_1:
if (!settings.getAlarm1()) setMode(mode++);;
break;
case STD_MODE_SET_ALARM_2:
if (!settings.getAlarm2()) setMode(mode++);;
break;
#ifdef LDR
case EXT_MODE_BRIGHTNESS:
if (settings.getUseLdr()) setMode(mode++);
break;
#endif
case EXT_MODE_TEST:
testColumn = 0;
return;
case STD_MODE_AMPM:
case STD_MODE_SECONDS:
case STD_MODE_DATE:
#ifdef RTC_BACKUP
case STD_MODE_TEMP:
#endif
fallBackCounter = settings.getTimeout();
break;
default:
fallBackCounter = 0;
break;
}
// Einstellungen sichern
settings.saveToEEPROM();
#ifdef RTC_BACKUP
// RTC stellen
Rtc.SetDateTime(now());
#endif
}
/******************************************************************************
"+" gedrueckt
******************************************************************************/
void buttonPlusPressed() {
DEBUG_PRINTLN("+ pressed.");
ScreenBufferNeedsUpdate = true;
switch (mode) {
case STD_MODE_SET_TIMER:
if (timerMinutes < 100) timerMinutes++;
timer = now() + timerMinutes * 60;
timerSet = true;
break;
case STD_MODE_ALARM_1:
settings.toggleAlarm1();
break;
case STD_MODE_SET_ALARM_1:
settings.setAlarmTime1(settings.getAlarmTime1() + 3600);
#ifdef DEBUG
debug.debugTime("Alarm 1:", settings.getAlarmTime1());
#endif
break;
case STD_MODE_ALARM_2:
settings.toggleAlarm2();
break;
case STD_MODE_SET_ALARM_2:
settings.setAlarmTime2(settings.getAlarmTime2() + 3600);
#ifdef DEBUG
debug.debugTime("Alarm 2:", settings.getAlarmTime2());
#endif
break;
case EXT_MODE_TEXT_MAIN:
setMode(EXT_MODE_TEXT_TIME);
break;
#ifdef LDR
case EXT_MODE_LDR:
settings.toggleUseLdr();
break;
#endif
case EXT_MODE_BRIGHTNESS:
settings.setBrightness(constrain(settings.getBrightness() + 10, 0, 255));
DEBUG_PRINTLN(settings.getBrightness());
break;
case EXT_MODE_COLOR:
if (settings.getColor() < maxColor) settings.setColor(settings.getColor() + 1);
else settings.setColor(0);
break;
case EXT_MODE_TIMEOUT:
if (settings.getTimeout() < 99) settings.setTimeout(settings.getTimeout() + 1);
break;
case EXT_MODE_LANGUAGE:
if (settings.getLanguage() < LANGUAGE_COUNT - 1) settings.setLanguage(settings.getLanguage() + 1);
else settings.setLanguage(0);
break;
case EXT_MODE_TEXT_TIME:
setMode(EXT_MODE_TEXT_TEST);
break;
case EXT_MODE_TIMESET:
setTime(hour() + 1, minute(), second(), day(), month(), year());
#ifdef DEBUG
debug.debugTime("Time set:", now());
#endif
break;
case EXT_MODE_IT_IS:
settings.toggleEsIst();
break;
case EXT_MODE_DAYSET:
setTime(hour(), minute(), second(), day() + 1, month(), year());
break;
case EXT_MODE_MONTHSET:
setTime(hour(), minute(), second(), day(), month() + 1, year());
break;
case EXT_MODE_YEARSET:
setTime(hour(), minute(), second(), day(), month(), year() + 1);
break;
case EXT_MODE_NIGHTOFF:
settings.setNightOffTime(settings.getNightOffTime() + 3600);
#ifdef DEBUG
debug.debugTime("Night Off:", settings.getNightOffTime());
#endif
break;
case EXT_MODE_NIGHTON:
settings.setNightOnTime(settings.getNightOnTime() + 3600);
#ifdef DEBUG
debug.debugTime("Night On:", settings.getNightOnTime());
#endif
break;
case EXT_MODE_TEXT_TEST:
setMode(EXT_MODE_TEXT_MAIN);
break;
default:
break;
}
}
/******************************************************************************
"-" gedrueckt
******************************************************************************/
void buttonMinusPressed() {
DEBUG_PRINTLN("- pressed.");
ScreenBufferNeedsUpdate = true;
switch (mode) {
case STD_MODE_SET_TIMER:
if (timerMinutes > 0) {
timerMinutes--;
if (timerMinutes == 0) timerSet = false;
else {
timer = now() + timerMinutes * 60;
timerSet = true;
}
}
break;
case STD_MODE_ALARM_1:
settings.toggleAlarm1();
break;
case STD_MODE_SET_ALARM_1:
settings.setAlarmTime1(settings.getAlarmTime1() + 300);
#ifdef DEBUG
debug.debugTime("Alarm 1:", settings.getAlarmTime1());
#endif
break;
case STD_MODE_ALARM_2:
settings.toggleAlarm2();
break;
case STD_MODE_SET_ALARM_2:
settings.setAlarmTime2(settings.getAlarmTime2() + 300);
#ifdef DEBUG
debug.debugTime("Alarm 2:", settings.getAlarmTime2());
#endif
break;
case EXT_MODE_TEXT_MAIN:
setMode(EXT_MODE_TEXT_TEST);
break;
#ifdef LDR
case EXT_MODE_LDR:
settings.toggleUseLdr();
break;
#endif
case EXT_MODE_BRIGHTNESS:
settings.setBrightness(constrain(settings.getBrightness() - 10, 0, 255));
DEBUG_PRINTLN(settings.getBrightness());
break;
case EXT_MODE_COLOR:
if (settings.getColor() > 0) settings.setColor(settings.getColor() - 1);
else settings.setColor(maxColor);
break;
case EXT_MODE_TIMEOUT:
if (settings.getTimeout() > 0) settings.setTimeout(settings.getTimeout() - 1);
break;
case EXT_MODE_LANGUAGE:
if (settings.getLanguage() > 0) settings.setLanguage(settings.getLanguage() - 1);
else settings.setLanguage(LANGUAGE_COUNT - 1);
break;
case EXT_MODE_TEXT_TIME:
setMode(EXT_MODE_TEXT_MAIN);
break;
case EXT_MODE_TIMESET:
setTime(hour(), minute() + 1, 0, day(), month(), year());
#ifdef DEBUG
debug.debugTime("Time set:", now());
#endif
break;
case EXT_MODE_IT_IS:
settings.toggleEsIst();
break;
case EXT_MODE_DAYSET:
setTime(hour(), minute(), second(), day() - 1, month(), year());
break;
case EXT_MODE_MONTHSET:
setTime(hour(), minute(), second(), day(), month() - 1, year());
break;
case EXT_MODE_YEARSET:
setTime(hour(), minute(), second(), day(), month(), year() - 1);
break;
case EXT_MODE_NIGHTOFF:
settings.setNightOffTime(settings.getNightOffTime() + 300);
#ifdef DEBUG
debug.debugTime("Night Off:", settings.getNightOffTime());
#endif
break;
case EXT_MODE_NIGHTON:
settings.setNightOnTime(settings.getNightOnTime() + 300);
#ifdef DEBUG
debug.debugTime("Night On:", settings.getNightOnTime());
#endif
break;
case EXT_MODE_TEXT_TEST:
setMode(EXT_MODE_TEXT_TIME);
break;
default:
break;
}
}
/******************************************************************************
IR empfangen
******************************************************************************/
#ifdef IR_REMOTE
void remoteAction(uint32_t irDecodeResults) {
switch (irDecodeResults) {
case IR_CODE_ONOFF:
setDisplayToToggle();
break;
case IR_CODE_TIME:
settings.saveToEEPROM();
#ifdef RTC_BACKUP
Rtc.SetDateTime(now());
#endif
if (alarmOn) {
alarmOn = false;
digitalWrite(PIN_BUZZER, LOW);
}
setMode(STD_MODE_NORMAL);
break;
case IR_CODE_MODE:
modePressed();
break;
case IR_CODE_EXTMODE:
if (mode < EXT_MODE_START) setMode(EXT_MODE_START);
else modePressed();
break;
case IR_CODE_PLUS:
buttonPlusPressed();
break;
case IR_CODE_MINUS:
buttonMinusPressed();
break;
default:
break;
}
}
#endif
/******************************************************************************
Sonstiges
******************************************************************************/