-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM5Dial_thermostat_3.0.ino
1268 lines (945 loc) · 42.7 KB
/
M5Dial_thermostat_3.0.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
//#define SUPLA_DISABLE_LOGS
#include <Wire.h>
#include <M5Dial.h>
#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/control/relay.h>
#include <supla/control/button.h>
#include <supla/control/action_trigger.h>
#include <supla/storage/littlefs_config.h>
#include <supla/network/esp_web_server.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/network/html/custom_parameter.h>
#include <supla/network/html/custom_text_parameter.h>
#include <supla/network/html/time_parameters.h>
#include <supla/network/html/hvac_parameters.h>
#include <supla/device/supla_ca_cert.h>
#include <supla/events.h>
#include <supla/storage/eeprom.h>
#include <supla/control/virtual_relay.h>
#include <supla/sensor/SHT3x.h>
#include <supla/sensor/virtual_therm_hygro_meter.h>
#include <supla/control/hvac_base.h>
#include <supla/control/internal_pin_output.h>
#include "TempDisplay.h"
#include "multi_virtual_relay.h"
#include "xiaomi_therm_hygro_meter.h"
#include "M5RTC.h"
#include "OCRAExtended16.h"
#include "menus.h"
#include <NimBLEDevice.h>
#if !CONFIG_BT_NIMBLE_EXT_ADV
# error Must enable extended advertising, see nimconfig.h file.
#endif
#include "encoder_pcnt.h"
#define DEFAULT_SS_SCAN_INTERVAL 120
#define DEFAULT_BT_SCAN_INTERVAL 97
#define DEFAULT_BT_SCAN_WINDOW 37
#define DEFAULT_SCREEN_SAVER_INTERVAL 30
#define SHT3X_MIN_TEMPERATURE -400
#define SHT3X_MAX_TEMPERATURE 1250
#undef USE_HARDCODED_DATA
#define USE_HARDCODED_DATA
// Supla section
Supla::Eeprom eeprom;
Supla::ESPWifi wifi;
Supla::LittleFsConfig configSupla(3000);
Supla::EspWebServer suplaServer;
Supla::Html::DeviceInfo htmlDeviceInfo(&SuplaDevice);
Supla::Html::WifiParameters htmlWifi;
Supla::Html::ProtocolParameters htmlProto;
Supla::Html::TimeParameters htmlTimeParameters(&SuplaDevice);
Supla::Html::HvacParameters htmlHvacParameters;
Supla::Sensor::SHT3x *SHT31;
Supla::Sensor::ProgDisplay *DPrograms;
Supla::Control::Multi_VirtualRelay *PDstep_mvr;
Supla::Control::HvacBase *M5Dial_hvac;
#define MAX_SENSORS 9
#define THT_OUTPUT_PIN 2
Supla::Sensor::XiaomiThermHygroMeter *xiaomiSensors[MAX_SENSORS];
Supla::Sensor::XiaomiCalcThermHygroMeter *hvac_therm;
int32_t Sensors_cnt = 0;
bool local_mode = true;
int32_t ScreenSaverTimer = 0;
int32_t ssScanIntervalTimer = 0;
int32_t ssScanInterval = DEFAULT_SS_SCAN_INTERVAL;
int32_t btScanInterval = DEFAULT_BT_SCAN_INTERVAL;
int32_t btScanWindow = DEFAULT_BT_SCAN_WINDOW;
int32_t ScreenSaverInterval = DEFAULT_SCREEN_SAVER_INTERVAL;
int32_t MenuRedrawTimer = 0;
//end of Supla section
NimBLEScan *pBLEScan;
NimBLEClient *pClient;
static constexpr const char* const week_days[7] = { "ND", "PN", "WT", "ŚR", "CZ", "PT", "SB"};
long nm_old_position = 0;
long nm_new_position = 0;
long nm_position_delta = 0;
long nm_menu_level = 0;
long nm_prev_menu[4] = { 0, 0, 0, 0};
long nm_menu_position = 0;
long nm_menu_end = 2;
long nm_menu_max = 7;
bool nm_menu_redraw = true;
bool screen_saver_on = false;
bool nm_config_mode = false;
bool ms_tht_change = true;
time_t tht_timer_time = 0;
uint32_t tht_timer_temp = 2100;
m5::touch_state_t touch_prev_state;
M5Canvas canvas(&M5Dial.Display);
//custom config section
const char XIAOMI_CNT[] = "Xiaomi_cnt";
const static char *XIAOMI_PARAMS[] PROGMEM = {
"Xiaomi#1", "Xiaomi#2", "Xiaomi#3", "Xiaomi#4", "Xiaomi#5", "Xiaomi#6", "Xiaomi#7", "Xiaomi#8", "Xiaomi#9" };
const static char *LOCS_PARAMS[] PROGMEM = {
"loc#1", "loc#2", "loc#3", "loc#4", "loc#5", "loc#6", "loc#7", "loc#8", "loc#9" };
const static char HVAC_T_TIME [] PROGMEM = "Hvac_T_time";
const static char HVAC_T_TEMP [] PROGMEM = "Hvac_T_temp";
const static char BT_SCAN_INTERVAL_PARAM [] PROGMEM = "BT_scan_interval";
const static char BT_SCAN_WINDOW_PARAM [] PROGMEM = "BT_scan_window";
const static char SCRS_INTERVAL_PARAM [] PROGMEM = "scrs_interval";
#ifdef USE_HARDCODED_DATA
#include "hcd/hcd_data.h"
#endif //USE_HARDCODED_DATA
char xiaomiBleDeviceMacs[MAX_SENSORS][18];
char xiaomiBleDeviceLocs[MAX_SENSORS][32];
const static char *program_names[] PROGMEM = {
"minimum",
"średnia",
"maximum",
"lokalizacja 1",
"lokalizacja 2",
"lokalizacja 3",
"lokalizacja 4",
"lokalizacja 5",
"lokalizacja 6",
"lokalizacja 7",
"lokalizacja 8",
"lokalizacja 9",
"avg - lokalizacja 1",
"avg - lokalizacja 2",
"avg - lokalizacja 3",
"avg - lokalizacja 4",
"avg - lokalizacja 5",
"avg - lokalizacja 6",
"avg - lokalizacja 7",
"avg - lokalizacja 8",
"avg - lokalizacja 9"
};
double sensors_temperature [MAX_SENSORS];
double sensors_humidity [MAX_SENSORS];
byte sensors_battery [MAX_SENSORS];
int sensors_rssi [MAX_SENSORS];
byte sensors_tick [MAX_SENSORS];
std::string strServiceData;
class MyAdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
if (advertisedDevice->haveName() && advertisedDevice->haveServiceData()) {
int serviceDataCount = advertisedDevice->getServiceDataCount();
strServiceData = advertisedDevice->getServiceData(0);
String macAddress = advertisedDevice->getAddress().toString().c_str();
macAddress.toUpperCase();
for (int i = 0; i < Sensors_cnt; i++) {
if (macAddress == xiaomiBleDeviceMacs[i]) {
int16_t rawTemperature = (strServiceData[7] | (strServiceData[6] << 8));
//Serial.print("raw Temperature = ");
//Serial.println(rawTemperature);
//Serial.println(macAddress);
float current_temperature = rawTemperature * 0.1;
byte current_humidity = (float)(strServiceData[8]);
byte current_batt = (float)(strServiceData[9]);
sensors_temperature[i] = current_temperature;
sensors_humidity[i] = current_humidity;
sensors_battery[i] = current_batt;
sensors_rssi[i] = advertisedDevice->getRSSI();
sensors_tick[i] = 0;
break;
}
}
}
}
};
String getAllHeap(){
char temp[300];
sprintf(temp, "Heap: Free:%i, Min:%i, Size:%i, Alloc:%i", ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getHeapSize(), ESP.getMaxAllocHeap());
return temp;
}
Supla::Sensor::DisplayAH *THT_dah;
void setup() {
Serial.begin(115200);
Wire.begin();
auto M5cfg = M5.config();
M5Dial.begin(M5cfg, false, false);
init_encoder();
eeprom.setStateSavePeriod(5000);
new Supla::Html::CustomParameter(XIAOMI_CNT,"Ilość czujników (1-9)", 0);
for (int j = 0; j < MAX_SENSORS; j++) {
new Supla::Html::CustomTextParameter(XIAOMI_PARAMS[j], "Adres czujnika #", 17);
new Supla::Html::CustomTextParameter(LOCS_PARAMS[j], "lokalizacja czujnika #", 32);
}
Supla::Storage::Init();
auto cfg = Supla::Storage::ConfigInstance();
#ifdef USE_HARDCODED_DATA
if (cfg) {
char buf[100];
if (!cfg->getGUID(buf)) {
cfg->setGUID(USER_GUID);
cfg->setAuthKey(USER_AUTH_KEY);
cfg->setWiFiSSID(SUPLA_WIFI_SSID);
cfg->setWiFiPassword(SUPLA_WIFI_PASS);
cfg->setSuplaServer(SUPLA_SVR);
cfg->setEmail(SUPLA_EMAIL);
}
cfg->getInt32(XIAOMI_CNT, &Sensors_cnt);
if (Sensors_cnt == 0) {
Sensors_cnt = HCD_SENSORS;
cfg->setInt32(XIAOMI_CNT, Sensors_cnt);
for (int j = 0; j < HCD_SENSORS; j++) {
cfg->setString(XIAOMI_PARAMS[j],xiaomiBleDeviceMacsTEMP[j][0]);
cfg->setString(LOCS_PARAMS[j],xiaomiBleDeviceMacsTEMP[j][1]);
}
}
}
#endif //USE_HARDCODED_DATA
if (cfg) {
if(!cfg->getInt32(XIAOMI_CNT, &Sensors_cnt)) Sensors_cnt = 0;
for (int j = 0; j < Sensors_cnt; j++) {
cfg->getString(XIAOMI_PARAMS[j],xiaomiBleDeviceMacs[j],18);
cfg->getString(LOCS_PARAMS[j],xiaomiBleDeviceLocs[j],31);
}
uint32_t t_param_tmp;
if (cfg->getUInt32(HVAC_T_TIME, &t_param_tmp)) tht_timer_time = (time_t)t_param_tmp;
if (cfg->getUInt32(HVAC_T_TEMP, &t_param_tmp)) tht_timer_temp = t_param_tmp;
if (cfg->getUInt32(BT_SCAN_INTERVAL_PARAM, &t_param_tmp)) btScanInterval = t_param_tmp;
if (cfg->getUInt32(BT_SCAN_WINDOW_PARAM, &t_param_tmp)) btScanWindow = t_param_tmp;
if (cfg->getUInt32(SCRS_INTERVAL_PARAM, &t_param_tmp)) ScreenSaverInterval = t_param_tmp;
}
for (int i = 0; i < Sensors_cnt; i++) {
sensors_temperature[i] = -275.0;
sensors_humidity[i] = -1;
sensors_battery[i] = 255;
sensors_rssi[i] = -100;
sensors_tick[i] = 0;
}
NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE);
NimBLEDevice::setScanDuplicateCacheSize(200);
NimBLEDevice::init("M5Dial");
pBLEScan = NimBLEDevice::getScan(); //create new scan
// Set the callback for when devices are discovered, no duplicates.
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false);
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser.
pBLEScan->setInterval(btScanInterval); // How often the scan occurs / switches channels; in milliseconds,
pBLEScan->setWindow(btScanWindow); // How long to scan during the interval; in milliseconds.
pBLEScan->setMaxResults(0); // do not store the scan results, use callback only.
auto output = new Supla::Control::InternalPinOutput(THT_OUTPUT_PIN);
M5Dial_hvac = new Supla::Control::HvacBase(output);
htmlHvacParameters.setHvacPtr(M5Dial_hvac);
SHT31 = new Supla::Sensor::SHT3x(0x44);
SHT31->setInitialCaption("pomiar lokalny");
THT_dah = new Supla::Sensor::DisplayAH(&M5Dial, &nm_menu_redraw);
DPrograms = new Supla::Sensor::ProgDisplay(0,Sensors_cnt, &sensors_temperature[0]);
DPrograms->setInitialCaption("aktualny program");
PDstep_mvr = new Supla::Control::Multi_VirtualRelay(DPrograms);
PDstep_mvr->setInitialCaption("zmiana programu");
PDstep_mvr->setDefaultFunction(SUPLA_CHANNELFNC_POWERSWITCH);
PDstep_mvr->addAction(Supla::TURN_ON, THT_dah,Supla::ON_CHANGE);
hvac_therm = new Supla::Sensor::XiaomiCalcThermHygroMeter(DPrograms, &sensors_temperature[0], &sensors_humidity[0], Sensors_cnt, SHT31);
M5Dial_hvac->setMainThermometerChannelNo(4);
for (int i = 0; i < Sensors_cnt; i++) {
xiaomiSensors[i] = new Supla::Sensor::XiaomiThermHygroMeter(&sensors_temperature[i], &sensors_humidity[i],&sensors_battery[i],&sensors_rssi[i], xiaomiBleDeviceMacs[i]);
xiaomiSensors[i]->setInitialCaption(xiaomiBleDeviceLocs[i]);
xiaomiSensors[i]->getChannel()->setFlag(SUPLA_CHANNEL_FLAG_CHANNELSTATE);
}
M5Dial_hvac->addAction(Supla::TURN_ON, THT_dah,Supla::ON_HVAC_MODE_OFF);
M5Dial_hvac->addAction(Supla::TURN_ON, THT_dah,Supla::ON_HVAC_MODE_HEAT);
M5Dial_hvac->addAction(Supla::TURN_ON, THT_dah,Supla::ON_HVAC_WEEKLY_SCHEDULE_ENABLED);
M5Dial_hvac->addAction(Supla::TURN_ON, THT_dah,Supla::ON_HVAC_WEEKLY_SCHEDULE_DISABLED);
M5Dial_hvac->addAction(Supla::TURN_ON, THT_dah,Supla::ON_HVAC_STANDBY);
M5Dial_hvac->addAction(Supla::TURN_ON, THT_dah,Supla::ON_HVAC_HEATING);
M5Dial_hvac->allowWrapAroundTemperatureSetpoints();
//M5Dial_hvac->setDefaultTemperatureRoomMin(SUPLA_CHANNELFNC_HVAC_THERMOSTAT, 0);
new Supla::M5RTC(&M5Dial.Rtc);
SuplaDevice.setStatusFuncImpl(&status_func);
SuplaDevice.setSuplaCACert(suplaCACert);
SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);
SuplaDevice.setName("M5dial Supla test");
//wifi.enableSSL(true);
SuplaDevice.setActivityTimeout(240);
SuplaDevice.begin();
//M5Dial.Rtc.setDateTime( { { 2024, 05, 05 }, { 20, 30, 00 } } );
M5Dial.Display.begin();
M5Dial.Display.setColorDepth(8);
canvas.setColorDepth(8);
canvas.createSprite(M5Dial.Display.width(),M5Dial.Display.height());
canvas.fillScreen(TFT_NAVY);
canvas.setTextDatum(middle_center);
canvas.loadFont(SegoeUISemiBold20);
canvas.setTextSize(1);
canvas.pushSprite(0,0);
ScreenSaverTimer = millis();
ssScanIntervalTimer = millis();
MenuRedrawTimer = millis();
}
void status_func(int status, const char *msg) {
if (status != STATUS_REGISTERED_AND_READY) local_mode = true;
else local_mode = false;
if (status == STATUS_REGISTER_IN_PROGRESS) screen_saver_on = false;
}
void loop() {
SuplaDevice.iterate();
M5Dial.update();
for (int i = 0; i < Sensors_cnt; i++)
if (sensors_tick[i] > 5) {
sensors_temperature[i] = -275;
sensors_humidity[i] = -1;
sensors_battery[i] = 255;
sensors_rssi[i] = -100;
sensors_tick[i] = 0;
}
if ((millis() - ssScanIntervalTimer) > (ssScanInterval * 1000)) {
ssScanIntervalTimer = millis();
for (int i = 0; i < Sensors_cnt; i++) {
xiaomiSensors[i]->setTemp(sensors_temperature[i]);
xiaomiSensors[i]->setHumi(sensors_humidity[i]);
sensors_tick[i] = sensors_tick[i] + 1;
}
nm_menu_redraw = true;
}
if (M5Dial_hvac->isCountdownEnabled()&&((millis() - MenuRedrawTimer) > 1000)) {
MenuRedrawTimer = millis();
nm_menu_redraw = true;
}
//BLE section
if(pBLEScan->isScanning() == false) {
// Start scan with: duration = 0 seconds(forever), no scan end callback, not a continuation of a previous scan.
pBLEScan->start(0, nullptr, false);}
//end of BLE section
auto t = M5Dial.Touch.getDetail();
if (touch_prev_state != t.state){
touch_prev_state = t.state;
//if ((t.state == m5::touch_state_t::touch_end)||(t.state == m5::touch_state_t::touch_end)){
ScreenSaverTimer = millis();
if (screen_saver_on) {
screen_saver_on = false;
nm_menu_redraw = true;
}
//}
}
if ((ScreenSaverInterval > 0) && (!screen_saver_on)) {
if (millis() - ScreenSaverTimer > (ScreenSaverInterval * 1000)) screen_saver_on = true;
}
if (M5Dial.BtnA.wasReleaseFor(15000)) {
SuplaDevice.softRestart();
}
if (M5Dial.BtnA.wasReleaseFor(5000)) {
screen_saver_on = false;
nm_config_mode = true;
nm_menu_redraw = true;
SuplaDevice.enterConfigMode();
}
if (!screen_saver_on){
if (M5Dial.BtnA.wasReleased()){
screen_saver_on = false;
ScreenSaverTimer = millis();
if (nm_menu_level == nm_menu_end){
nm_menu_level--;
nm_menu_position = nm_prev_menu[nm_menu_level];
nm_old_position = 0;
reset_encoder();
nm_position_delta = 0;
nm_menu_redraw = true;
}
else {
nm_prev_menu[nm_menu_level] = nm_menu_position;
nm_menu_position = 0;
nm_old_position = 0;
nm_menu_level++;
reset_encoder();
nm_position_delta = 0;
nm_menu_redraw = true;
}
}
nm_new_position = get_encoder()/4;
nm_position_delta = nm_new_position - nm_old_position;
if (nm_position_delta != 0){
screen_saver_on = false;
ScreenSaverTimer = millis();
nm_old_position = nm_new_position;
nm_new_position = nm_new_position + nm_position_delta;
nm_menu_position = nm_menu_position + nm_position_delta;
if (nm_menu_position < 0) nm_menu_position = nm_menu_max;
if (nm_menu_position > nm_menu_max) nm_menu_position = 0;
nm_menu_redraw = true;
}
if (nm_config_mode) {
canvas.fillScreen(TFT_NAVY);
canvas.setTextDatum(middle_center);
canvas.setTextSize(1);
canvas.loadFont(SegoeUISemiBold20);
canvas.drawString(F("SuplaDevice config mode "),120,100);
canvas.drawString(F("http://192.168.4.1/"),120,140);
canvas.pushSprite(0,0);
}
else
if (nm_menu_redraw){
nm_menu_redraw = false;
switch (nm_menu_level){
case 0:
if ((ms_tht_change) && (nm_position_delta != 0)) {
M5Dial_hvac->handleAction(0,Supla::TOGGLE_OFF_MANUAL_WEEKLY_SCHEDULE_MODES);
M5Dial.Speaker.tone(8000, 20);
}
drawMenu0();
break;
case 1:
nm_menu_max = MENU_1_ITEMS - 1;
if (nm_position_delta != 0) M5Dial.Speaker.tone(8000, 20);
drawMenu1(MENU_1_ANGLE, MENU_1_OFFSET, menu_1_lines, menu_1_labels_1[0], menu_1_labels_2[0], menu_1_labels_3[0]);
break;
case 2:
if (nm_position_delta != 0) M5Dial.Speaker.tone(8000, 20);
drawMenu2M();
break;
case 3:
if (nm_position_delta != 0) M5Dial.Speaker.tone(8000, 20);
if (nm_prev_menu[1] == MENU_11_POS) drawMenu3_11(nm_prev_menu[2]);
else
if (nm_prev_menu[1] == MENU_12_POS) drawMenu3_12(nm_prev_menu[2]);
else
if (nm_prev_menu[1] == MENU_13_POS) drawMenu3_13(nm_prev_menu[2]);
else
if (nm_prev_menu[1] == MENU_14_POS) drawMenu3_14(nm_prev_menu[2]);
break;
}
}
}
else {
canvas.fillScreen(TFT_BLACK);
canvas.pushSprite(0,0);}
}
void drawFlameIcon(int32_t start_x, int32_t start_y, int outer_color, int inner_color){
canvas.drawBezier(start_x,start_y,start_x-20,start_y-10,start_x-10,start_y-30,start_x,start_y-40,outer_color);
canvas.drawBezier(start_x,start_y,start_x+20,start_y-10,start_x+10,start_y-30,start_x,start_y-40,outer_color);
canvas.drawBezier(start_x,start_y,start_x-10,start_y-10,start_x-5,start_y-15,start_x,start_y-25,inner_color);
canvas.drawBezier(start_x,start_y,start_x+10,start_y-10,start_x+5,start_y-15,start_x,start_y-25,inner_color);
canvas.floodFill(start_x,start_y-35,outer_color);
canvas.floodFill(start_x,start_y-10,inner_color);
}
void drawCalendarIcon(int32_t start_x, int32_t start_y, int outer_color, int inner_color){
canvas.fillRect(start_x,start_y,40,40,outer_color);
canvas.fillRect(start_x + 2,start_y + 2,36,36,inner_color);
canvas.drawRect(start_x,start_y,40,10,outer_color);
for (int i = 1; i < 8; i++) canvas.drawPixel(start_x + (i * 5), start_y + 10, outer_color);
for (int i = 1; i < 8; i++) canvas.drawPixel(start_x + (i * 5), start_y + 20, outer_color);
for (int i = 1; i < 8; i++) canvas.drawPixel(start_x + (i * 5), start_y + 30, outer_color);
}
void drawWrenchIcon(int32_t start_x, int32_t start_y, int outer_color, int inner_color){
canvas.fillCircle(start_x, start_y,10, outer_color);
canvas.fillRect(start_x - 5,start_y - 10,10,10,inner_color);
canvas.fillRect(start_x - 5,start_y,10,30,outer_color);
}
void drawDisabledIcon(int32_t start_x, int32_t start_y, int outer_color, int inner_color){
canvas.fillCircle(start_x, start_y,20, outer_color);
canvas.fillRect(start_x - 10,start_y - 5, 20,10, inner_color);
}
void drawMenu0(){
char tempstr_fmt[25];
canvas.clearClipRect();
canvas.setTextColor(TFT_WHITE);
canvas.fillScreen(TFT_NAVY);
canvas.setTextSize(1);
canvas.fillRoundRect(90,70,60,60,10, lgfx::v1::color332(0,255,0));
canvas.fillRoundRect(95,75,50,50,10, lgfx::v1::color332(128,128,128));
if (M5Dial_hvac->getChannel()->isHvacFlagHeating()) drawFlameIcon(120,120,TFT_RED, TFT_YELLOW);
else drawFlameIcon(120,120,TFT_LIGHTGREY, TFT_DARKGREY);
if (M5Dial_hvac->getPrimaryTemp() > INT16_MIN)
canvas.drawString(String(((double)M5Dial_hvac->getPrimaryTemp())/100,1)+"°C",40, 100);
else
canvas.drawString("--- °C",40, 100);
canvas.drawString(String(((double)M5Dial_hvac->getTemperatureSetpointHeat())/100,1)+"°C",200, 100);
canvas.setTextColor(TFT_GREEN);
if (M5Dial_hvac->isWeeklyScheduleEnabled()) drawCalendarIcon(100,160, TFT_GREEN, TFT_NAVY);
if (M5Dial_hvac->isManualModeEnabled()) drawWrenchIcon(120,160,TFT_GREEN, TFT_NAVY);
if (M5Dial_hvac->isCountdownEnabled()) {
canvas.fillRoundRect(80,160,80,40,10, lgfx::v1::color332(0,255,0));
canvas.fillRoundRect(85,165,70,30,10, lgfx::v1::color332(128,128,128));
time_t timer_diff = M5Dial_hvac->getCountDownTimerEnds()-Supla::Clock::GetTimeStamp();
uint16_t timer_mins = timer_diff / 60;
uint16_t timer_secs = timer_diff - (timer_mins * 60);
canvas.drawString(String(timer_mins) + ":" + String(timer_secs),120,180);
}
if (M5Dial_hvac->isThermostatDisabled()) drawDisabledIcon(120,180,TFT_RED,TFT_WHITE);
canvas.setTextColor(TFT_WHITE);
auto dt = M5Dial.Rtc.getDateTime();
sprintf (tempstr_fmt,"%02d.%02d.%04d(%s)",dt.date.date,dt.date.month,dt.date.year,week_days[dt.date.weekDay]);
canvas.loadFont(SegoeUI16);
canvas.drawString(tempstr_fmt, 120,45);
sprintf (tempstr_fmt,"%02d:%02d", dt.time.hours, dt.time.minutes);
canvas.drawString(tempstr_fmt, 120,20);
canvas.loadFont(SegoeUISemiBold20);
canvas.setTextSize(1);
canvas.pushSprite(0,0);
}
void drawMenu1(int menu_angle, int menu_offset,const byte *menu_lines, const char *menu_labels_1, const char *menu_labels_2, const char *menu_labels_3){
canvas.fillScreen(TFT_NAVY);
int start_angle = nm_menu_position*menu_angle + menu_offset;
int end_angle = start_angle + menu_angle;
canvas.fillArc(120, 120, 100, 120, start_angle, end_angle, TFT_GREEN);
canvas.fillArc(120,120,90,99,0,360,TFT_GREEN);
canvas.setTextColor(TFT_WHITE);
canvas.fillCircle(120, 120, 89, TFT_NAVY);
switch (*(menu_lines + nm_menu_position)) {
case 1:
canvas.drawString((menu_labels_1 + (12 * nm_menu_position)), 120, 120);
break;
case 2:
canvas.drawString((menu_labels_1 + (12 * nm_menu_position)), 120, 100);
canvas.drawString((menu_labels_2 + (12 * nm_menu_position)), 120, 140);
break;
case 3:
canvas.drawString((menu_labels_1 + (12 * nm_menu_position)), 120, 80);
canvas.drawString((menu_labels_2 + (12 * nm_menu_position)), 120, 120);
canvas.drawString((menu_labels_3 + (12 * nm_menu_position)), 120, 160);
break;
}
canvas.pushSprite(0,0);
}
void drawMenu2M() {
switch (nm_prev_menu[1]) {
case MENU_11_POS:
nm_menu_end = 3;
nm_menu_max = MENU_11_ITEMS - 1;
drawMenu1(MENU_11_ANGLE, MENU_11_OFFSET, menu_11_lines, menu_11_labels_1[0], menu_11_labels_2[0], menu_11_labels_3[0]);
break;
case MENU_12_POS:
nm_menu_end = 3;
nm_menu_max = MENU_12_ITEMS - 1;
drawMenu1(MENU_12_ANGLE, MENU_12_OFFSET, menu_12_lines, menu_12_labels_1[0], menu_12_labels_2[0], menu_12_labels_3[0]);
break;
case MENU_13_POS:
nm_menu_end = 3;
nm_menu_max = MENU_13_ITEMS - 1;
drawMenu1(MENU_13_ANGLE, MENU_13_OFFSET, menu_13_lines, menu_13_labels_1[0], menu_13_labels_2[0], menu_13_labels_3[0]);
break;
case MENU_14_POS:
nm_menu_end = 3;
nm_menu_max = MENU_14_ITEMS - 1;
drawMenu1(MENU_14_ANGLE, MENU_14_OFFSET, menu_14_lines, menu_14_labels_1[0], menu_14_labels_2[0], menu_14_labels_3[0]);
break;
default:
drawMenu2(nm_prev_menu[1]);
break;
}
}
void drawMenu2(long selector){
canvas.setTextColor(TFT_WHITE);
canvas.fillCircle(120,120,100,TFT_NAVY);
canvas.drawString(menu_1_labels_1[selector], 120, 80);
switch (selector){
case MENU_1_TMODES: //thermostat modes
nm_menu_max = 2;
if (nm_position_delta !=0) M5Dial_hvac->handleAction(0,Supla::TOGGLE_OFF_MANUAL_WEEKLY_SCHEDULE_MODES);
nm_drawTMGauge();
break;
case MENU_1_TEMPSET: //setpoint temperature
nm_menu_max = 1;
if (nm_position_delta > 0) M5Dial_hvac->handleAction(0, Supla::INCREASE_HEATING_TEMPERATURE);
if (nm_position_delta < 0) M5Dial_hvac->handleAction(0, Supla::DECREASE_HEATING_TEMPERATURE);
nm_drawTempScaleGauge("TEMPERATURA",M5Dial_hvac->getTemperatureSetpointHeat(), M5Dial_hvac->getTemperatureRoomMin(),M5Dial_hvac->getTemperatureRoomMax());
break;
case MENU_1_TIMER: //timer submenu
break;
case MENU_1_TSTS: //active main screen = user can change thermostat modes using encoder
break;
case 4: //weekly schedule programs submenu
break;
case MENU_1_TSEL:// programy obliczania temperatury - min max avg...
nm_menu_max = DPrograms->getPCount() - 1;
if (nm_position_delta !=0) DPrograms->setDP(nm_menu_position);
else nm_menu_position = DPrograms->getDP();
nm_drawPrograms(DPrograms);
break;
case MENU_1_STATUS:// status
nm_menu_max = 1;
nm_drawStatus();
break;
case MENU_1_SENSORS: //czujniki
nm_menu_max = Sensors_cnt-1;
nm_drawSensors();
break;
case MENU_1_MMENU: //menu główne
nm_menu_level = 0;
nm_menu_redraw = true;
break;
default:
break;
}
canvas.pushSprite(0,0);
}
void drawMenu3_11(long selector){
canvas.setTextColor(TFT_WHITE);
canvas.fillCircle(120,120,100,TFT_NAVY);
canvas.drawString(menu_11_labels_1[selector], 120, 80);
switch (selector){
case 0: //temperatura program 1
nm_drawProgramTempGauge(menu_11_labels_1[selector],1);
break;
case 1: //temperatura program 2
nm_drawProgramTempGauge(menu_11_labels_1[selector],2);
break;
case 2: //temperatura program 3
nm_drawProgramTempGauge(menu_11_labels_1[selector],3);
break;
case 3: //temperatura program 4
nm_drawProgramTempGauge(menu_11_labels_1[selector],4);
break;
case 4: //previous menu
nm_menu_end = 2;
nm_menu_level = 1;
nm_menu_position = nm_prev_menu[1];
nm_menu_redraw = true;
break;
default:
break;
}
canvas.pushSprite(0,0);
}
void drawMenu3_12(long selector){
auto cfg = Supla::Storage::ConfigInstance();
canvas.setTextColor(TFT_WHITE);
canvas.fillCircle(120,120,100,TFT_NAVY);
canvas.drawString(menu_12_labels_1[selector], 120, 80);
switch (selector){
case 0: //timer on/off
nm_menu_max = 1;
if (nm_position_delta !=0)
if (M5Dial_hvac->isCountdownEnabled()) M5Dial_hvac->applyNewRuntimeSettings(SUPLA_HVAC_MODE_NOT_SET, 0);
else M5Dial_hvac->applyNewRuntimeSettings(SUPLA_HVAC_MODE_NOT_SET,tht_timer_temp, INT16_MIN, tht_timer_time * 60);
nm_drawOnOffGauge(M5Dial_hvac->isCountdownEnabled());
break;
case 1: //czas timera - minuty
nm_menu_max = 0;
if (nm_position_delta > 0) tht_timer_time += 15;
if (nm_position_delta < 0) tht_timer_time -= 15;
if (tht_timer_time < 0) tht_timer_time = 0;
if (cfg && (nm_position_delta != 0)) {
cfg->setUInt32(HVAC_T_TIME, tht_timer_time);
cfg->saveWithDelay(5000);
}
nm_drawScaleGauge("TIMER", "minuty", tht_timer_time, 0, 1440);
break;
case 2: //temperatura timera
nm_menu_max = 0;
if (nm_position_delta > 0) tht_timer_temp += 50;
if (nm_position_delta < 0) tht_timer_temp -= 50;
if (tht_timer_temp < 0) tht_timer_temp = 0;
if (cfg && (nm_position_delta != 0)) {
cfg->setUInt32(HVAC_T_TEMP, tht_timer_temp);
cfg->saveWithDelay(5000);
}
nm_drawTempScaleGauge("TIMER", tht_timer_temp, M5Dial_hvac->getTemperatureRoomMin(),M5Dial_hvac->getTemperatureRoomMax());
break;
case 3: //previous menu
nm_menu_end = 2;
nm_menu_level = 1;
nm_menu_position = nm_prev_menu[1];
nm_menu_redraw = true;
break;
default:
break;
}
canvas.pushSprite(0,0);
}
void drawMenu3_13(long selector){
canvas.setTextColor(TFT_WHITE);
canvas.fillCircle(120,120,100,TFT_NAVY);
canvas.drawString(menu_13_labels_1[selector], 120, 80);
switch (selector) {
case MENU_13_HIST: //histeresis
nm_menu_max = 0;
if (nm_position_delta > 0)
M5Dial_hvac->setTemperatureHisteresis(M5Dial_hvac->getTemperatureHisteresis() + 10);
if (nm_position_delta < 0)
M5Dial_hvac->setTemperatureHisteresis(M5Dial_hvac->getTemperatureHisteresis() - 10);
nm_drawTempScaleGauge("HISTEREZA",M5Dial_hvac->getTemperatureHisteresis(),M5Dial_hvac->getTemperatureHisteresisMin(), M5Dial_hvac->getTemperatureHisteresisMax());
break;
case MENU_13_TEMPMIN: //min room temperature
nm_menu_max = 0;
if (nm_position_delta > 0)
M5Dial_hvac->setTemperatureRoomMin(M5Dial_hvac->getTemperatureRoomMin() + 50);
if (nm_position_delta < 0)
M5Dial_hvac->setTemperatureRoomMin(M5Dial_hvac->getTemperatureRoomMin() - 50);
nm_drawTempScaleGauge("MIN. TEMP.", M5Dial_hvac->getTemperatureRoomMin(), 0, 10000);
break;
case MENU_13_TEMPMAX: //max room temperature
nm_menu_max = 0;
if (nm_position_delta > 0) //SUPLA_CHANNELFNC_HVAC_THERMOSTAT
M5Dial_hvac->setTemperatureRoomMax(M5Dial_hvac->getTemperatureRoomMax() + 50);
if (nm_position_delta < 0) //SUPLA_CHANNELFNC_HVAC_THERMOSTAT
M5Dial_hvac->setTemperatureRoomMax(M5Dial_hvac->getTemperatureRoomMax() - 50);
nm_drawTempScaleGauge("MAX. TEMP.", M5Dial_hvac->getTemperatureRoomMax(), 0, 10000);
break;
case MENU_13_HISTMIN: //min room temperature
nm_menu_max = 0;
if (nm_position_delta > 0)
M5Dial_hvac->setTemperatureHisteresisMin(M5Dial_hvac->getTemperatureHisteresisMin() + 10);
if (nm_position_delta < 0)
M5Dial_hvac->setTemperatureHisteresisMin(M5Dial_hvac->getTemperatureHisteresisMin() - 10);
nm_drawTempScaleGauge("MIN. HIST.", M5Dial_hvac->getTemperatureHisteresisMin(), 0, 5000);
break;
case MENU_13_HISTMAX: //max room temperature
nm_menu_max = 0;
if (nm_position_delta > 0)
M5Dial_hvac->setTemperatureHisteresisMax(M5Dial_hvac->getTemperatureHisteresisMax() + 10);
if (nm_position_delta < 0)
M5Dial_hvac->setTemperatureHisteresisMax(M5Dial_hvac->getTemperatureHisteresisMax() - 10);
nm_drawTempScaleGauge("MAX. HIST.", M5Dial_hvac->getTemperatureHisteresisMax(), 0, 5000);
break;
case MENU_13_TSSM: //TemperatureSetpointChangeSwitchesToManualMode
nm_menu_max = 1;
if (nm_position_delta !=0)
M5Dial_hvac->setTemperatureSetpointChangeSwitchesToManualMode(!M5Dial_hvac->isTemperatureSetpointChangeSwitchesToManualMode());
nm_drawOnOffGauge(M5Dial_hvac->isTemperatureSetpointChangeSwitchesToManualMode());
break;
case MENU_13_AFRAOH: //AntiFreezeAndHeatProtectionEnabled
nm_menu_max = 1;
if (nm_position_delta !=0)
M5Dial_hvac->setAntiFreezeAndHeatProtectionEnabled(!M5Dial_hvac->isAntiFreezeAndHeatProtectionEnabled());
nm_drawOnOffGauge(M5Dial_hvac->isAntiFreezeAndHeatProtectionEnabled());
break;
case MENU_13_TEMPAFR: //anti-freeze temperature
nm_menu_max = 0;
if (M5Dial_hvac->getTemperatureFreezeProtection() == INT16_MIN)
M5Dial_hvac->setTemperatureFreezeProtection(M5Dial_hvac->getTemperatureRoomMin());
if (nm_position_delta > 0)
M5Dial_hvac->setTemperatureFreezeProtection(M5Dial_hvac->getTemperatureFreezeProtection() + 50);
if (nm_position_delta < 0)
M5Dial_hvac->setTemperatureFreezeProtection(M5Dial_hvac->getTemperatureFreezeProtection() - 50);
nm_drawTempScaleGauge("TEMP. P-ZAMARZ.", M5Dial_hvac->getTemperatureFreezeProtection(), M5Dial_hvac->getTemperatureRoomMin(), M5Dial_hvac->getTemperatureRoomMax());
break;
case MENU_13_TEMPAOH: //anti-overheat temperature
nm_menu_max = 0;
if (M5Dial_hvac->getTemperatureHeatProtection() == INT16_MIN)
M5Dial_hvac->setTemperatureHeatProtection(M5Dial_hvac->getTemperatureRoomMax());
if (nm_position_delta > 0)
M5Dial_hvac->setTemperatureHeatProtection(M5Dial_hvac->getTemperatureHeatProtection() + 50);
if (nm_position_delta < 0)
M5Dial_hvac->setTemperatureHeatProtection(M5Dial_hvac->getTemperatureHeatProtection() - 50);
nm_drawTempScaleGauge("TEMP. P-PRZEGRZ.", M5Dial_hvac->getTemperatureHeatProtection(), M5Dial_hvac->getTemperatureRoomMin(), M5Dial_hvac->getTemperatureRoomMax());
break;
case MENU_13_MINON: //minOnTime
nm_menu_max = 0;
if (nm_position_delta > 0) M5Dial_hvac->setMinOnTimeS(M5Dial_hvac->getMinOnTimeS() + 10);
if (nm_position_delta < 0) M5Dial_hvac->setMinOnTimeS(M5Dial_hvac->getMinOnTimeS() - 10);
nm_drawScaleGauge("MIN. ON", "sekundy",M5Dial_hvac->getMinOnTimeS(), 0, 600);
break;
case MENU_13_MINOFF: //minOFFTime
nm_menu_max = 0;
if (nm_position_delta > 0) M5Dial_hvac->setMinOffTimeS(M5Dial_hvac->getMinOffTimeS() + 10);
if (nm_position_delta < 0) M5Dial_hvac->setMinOffTimeS(M5Dial_hvac->getMinOffTimeS() - 10);
nm_drawScaleGauge("MIN. OFF", "sekundy",M5Dial_hvac->getMinOffTimeS(), 0, 600);
break;
case MENU_13_MENUUP: //previous menu
nm_menu_end = 2;
nm_menu_level = 1;
nm_menu_position = nm_prev_menu[1];
nm_menu_redraw = true;