-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCORE_Display.ipp
More file actions
744 lines (634 loc) · 27.5 KB
/
CORE_Display.ipp
File metadata and controls
744 lines (634 loc) · 27.5 KB
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
#pragma once
#include "CORE_Display.h"
#include "CORE_Display_aux_draw.ipp"
#include "Serial/packaging.h"
#include "Serial/flags.h"
#include "SD_card.h"
#include "Custom_Bitmaps.h"
#include "Configuration.h"
// from mseconds to days, print on targ
inline void ms2str(char* targ, const size_t lim, uint64_t to_c)
{
constexpr uint64_t lim_millisec = 1000;
constexpr uint64_t lim_seconds = 60 * lim_millisec;
constexpr uint64_t lim_minutes = 60 * lim_seconds;
constexpr uint64_t lim_hours = 24 * lim_minutes;
// then days
if (to_c == 0) {
snprintf(targ, lim, "inf");
}
else if (to_c < lim_millisec) {
snprintf(targ, lim, "%04llu ms", to_c);
}
else if (to_c < lim_seconds) {
double calc = static_cast<double>(to_c) / lim_millisec; // sec
snprintf(targ, lim, "%05.2lf seg", calc);
}
else if (to_c < lim_minutes) {
double calc = static_cast<double>(to_c) / lim_seconds; // min
snprintf(targ, lim, "%05.2lf min", calc);
}
else if (to_c < lim_hours) {
double calc = static_cast<double>(to_c) / lim_minutes; // hour
snprintf(targ, lim, "%05.2lf hora%s", calc, calc >= 2.0 ? "s" : "");
}
else {
double calc = static_cast<double>(to_c) / lim_hours; // day
snprintf(targ, lim, "%05.2lf dia%s", calc, calc >= 2.0 ? "s" : "");
}
}
inline void CoreDisplay::task_touch()
{
m_touch_history[1] = m_touch_history[0];
if (m_touch_history[0].state = m_tft->getTouch(&m_touch_history[0].x, &m_touch_history[0].y))
{
m_screen_saver.next_step = get_time_ms();
if (m_screen_saver.state != 0) m_screen_saver.state = 3; // touch event on screen saver saves 3
}
if (!m_touch_history[0].state && m_touch_history[1].state) {// must be screen on!
if (m_screen_saver.state == 0) _task_state_only_if_touch(m_touch_history[0]);
else if (m_screen_saver.state > 2) m_screen_saver.state = 0; // reset history
}
if (m_animations_check_time.is_time()) _task_update_animations();
}
inline void CoreDisplay::task_display()
{
//_task_update_screen_saver_state();
_display_draw_bar_stuff(); // always
switch(m_state.state) {
case core_states::STATE_HOME:
case core_states::STATE_CONFIG:
{
for(size_t p = 0; p < DisplayColors::item_resumed_amount_on_screen; ++p)
m_draw_lines[p].draw(static_cast<int32_t>(p));
}
break;
case core_states::STATE_DETAILS:
{
m_draw_full_graph->draw();
}
break;
case core_states::STATE_QRCODE:
{
m_draw_qrcode->draw();
}
break;
}
}
inline bool CoreDisplay::__is_touch_on(const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h)
{
const auto& m_now = m_touch_history[0];
return m_now.x >= x && m_now.y >= y && m_now.x < (x + w) && m_now.y < (y + h);
}
inline void CoreDisplay::_task_state_only_if_touch(const touch_event& event)
{
using namespace DisplayColors;
// do tasky tasks related to on_touch event
//LOGI(e_LOG_TAG::TAG_CORE, "Touch called.");
auto old_state = m_state.state;
uint16_t buttons{};
for(size_t p = 0; p < 5; ++p) buttons |= (static_cast<uint16_t>(__is_touch_on(480 - bar_right_width, bar_top_height + (p * bar_right_each_height), 40, 60)) << p);
// = = = = = TEST BODY BLOCKS = = = = = //
_task_work_body_blocks_event();
// = = = = = TEST LATERAL BUTTONS = = = = = //
switch(buttons) {
case 0x01: // home
m_state.state = core_states::STATE_HOME;
m_state.offset = 0;
m_state.offset_max = CS::d2u(CS::device_id::_MAX) - item_resumed_amount_on_screen;
break;
case 0x02: // up
if (m_state.offset > 0) --m_state.offset;
break;
case 0x04: // down
if (m_state.offset < m_state.offset_max) ++m_state.offset;
break;
case 0x08: // qrcode
m_state.state = core_states::STATE_QRCODE;
m_state.offset = 0;
m_state.offset_max = 0;
break;
case 0x10: // config
m_state.state = core_states::STATE_CONFIG;
m_state.offset = 0;
m_state.offset_max = 0;
break;
default: // if somehow
break;
}
// just check just in case...
if (m_state.offset > m_state.offset_max) m_state.offset = m_state.offset_max;
if (old_state != m_state.state) _set_all_state_has_changed();
//LOGI_NOSD(e_LOG_TAG::TAG_CORE, "Var %u", m_state.offset);
_task_update_animations(); // update as soon as possible on touch
}
// == == == == == == UPDATE ITEMS ON SCREEN TO DRAW LATER! == == == == == == //
inline void CoreDisplay::_task_update_animations()
{
using namespace DisplayColors;
switch(m_state.state) {
case core_states::STATE_HOME:
{
const auto& off_now = m_state.offset;
const MyI2Ccomm& com = GET(MyI2Ccomm);
for (size_t p = 0; p < item_resumed_amount_on_screen; ++p)
{
const CS::device_id real_off = static_cast<CS::device_id>((p + static_cast<size_t>(m_state.offset)) % static_cast<size_t>(CS::d2u(CS::device_id::_MAX))); // just to be sure 100%
const MyI2Ccomm::device& dev = com.get_device_configurations(real_off, 0); // real_off cannot be bigger than expected already
const bool is_on = com.is_device_online(real_off);
const bool is_bad = com.is_device_with_issue(real_off);
const uint16_t fill =
is_bad ? item_has_issues_bg_color : (is_on ? item_online_bg_color : item_offline_bg_color);
const uint16_t border =
is_bad ? item_has_issues_bg_color_border : (is_on ? item_online_bg_color_border : item_offline_bg_color_border);
m_draw_lines[p].set_fill_color(fill);
m_draw_lines[p].set_border_color(border);
if (dev.m_map.size() != 0) {
std::lock_guard<std::mutex> l(dev.m_map_mtx); // security first
const unsigned sel = (get_time_ms() / 3000) % dev.m_map.size();
const i2c_data_pair& sel_pair = *std::next(dev.m_map.begin(), sel);
const std::string tmp = sel_pair.first + ": " + sel_pair.second;
const uint64_t time_since = get_time_ms() - dev.m_update_time;
char buf_time_since[64];
ms2str(buf_time_since, sizeof(buf_time_since), time_since);
const std::string tmp2 = "Atualizado faz: " + std::string(buf_time_since);
m_draw_lines[p].set_texts(
get_fancy_name_for(real_off),
tmp.c_str(),
tmp2.c_str()
);
}
else {
m_draw_lines[p].set_texts(
get_fancy_name_for(real_off),
"Sem dados",
""
);
}
}
}
break;
case core_states::STATE_CONFIG:
{
char buf_time_since[64];
for (size_t p = 0; p < item_resumed_amount_on_screen; ++p)
{
switch(p) {
case static_cast<size_t>(core_settings_buttons::BTN_SCREEN_SAVER):
ms2str(buf_time_since, sizeof(buf_time_since), GET(MyConfig).get_core_display_screen_saver_steps_time());
m_draw_lines[p].set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_lines[p].set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_lines[p].set_texts(
"Tempo de desligamento de tela:",
buf_time_since,
""
);
break;
case static_cast<size_t>(core_settings_buttons::BTN_SAVE_SPEED):
{
const uint64_t total_time_shown = GET(MyConfig).get_i2c_packaging_delay() * i2c_values_history_size;
ms2str(buf_time_since, sizeof(buf_time_since), total_time_shown);
const std::string res = "(" + std::string(buf_time_since) + " no grafico)";
ms2str(buf_time_since, sizeof(buf_time_since), GET(MyConfig).get_i2c_packaging_delay());
m_draw_lines[p].set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_lines[p].set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_lines[p].set_texts(
"Tempo de dados dos dispositivos:",
buf_time_since,
res.c_str()
);
}
break;
case static_cast<size_t>(core_settings_buttons::BTN_REDO_CALIBRATION_SCREEN):
m_draw_lines[p].set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_lines[p].set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_lines[p].set_texts(
"Recalibrar a tela",
"Ao clicar, siga as setas nos blocos vermelhos para calibrar",
""
);
break;
case static_cast<size_t>(core_settings_buttons::BTN_ENABLE_HOTSPOT):
m_draw_lines[p].set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_lines[p].set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_lines[p].set_texts(
"Hotspot para monitoramento pela rede",
"Habilitar ou desabilitar conectividade WiFi",
GET(MyConfig).get_wifi_hotspot() ? "LIGADO" : "DESLIGADO"
);
break;
default:
m_draw_lines[p].set_texts("","","");
break;
}
}
}
break;
case core_states::STATE_DETAILS:
{
const MyI2Ccomm::device& dev = GET(MyI2Ccomm).get_device_configurations(m_device_select, 0);
m_state.offset_max = dev.m_map.size() == 0 ? 0 : (dev.m_map.size() - 1);
m_draw_full_graph->update_with(m_device_select, m_state.offset);
for (size_t p = 0; p < item_resumed_amount_on_screen; ++p) m_draw_lines[p].set_texts("", "", "");
}
break;
case core_states::STATE_QRCODE:
{
m_draw_qrcode->update();
for (size_t p = 0; p < item_resumed_amount_on_screen; ++p) m_draw_lines[p].set_texts("", "", "");
}
break;
}
}
inline void CoreDisplay::_task_calibrate_display_once()
{
MySDcard& sd = GET(MySDcard);
uint16_t calibrationData[6]{}; // 14 bytes actually
m_tft->fillScreen(TFT_BLACK);
m_tft->calibrateTouch(calibrationData, TFT_WHITE, TFT_RED, 15);
m_tft->fillScreen(TFT_BLACK);
sd.async_overwrite_on(core_display_config_ini, (char*)calibrationData, 14);
}
inline void CoreDisplay::_display_draw_static_overlay()
{
using namespace DisplayColors;
m_tft->fillRect(0, 0, 480, bar_top_height, bar_top_color); // top bar bg
m_tft->fillRect(480 - bar_right_width, bar_top_height, 480, 320, bar_right_color); // right bar menu
m_tft->fillRect(0, 20, 440, 320, body_color); // body
m_tft->drawBitmap(442, 28, Bitmaps::config_icon_home, 40, 46, TFT_BLACK);
m_tft->drawBitmap(447, 92, Bitmaps::config_icon_up, 26, 35, TFT_BLACK);
m_tft->drawBitmap(447, 152, Bitmaps::config_icon_down, 26, 35, TFT_BLACK);
m_tft->drawBitmap(444, 214, Bitmaps::qr_code_icon, 32, 32, TFT_BLACK);
m_tft->drawBitmap(444, 274, Bitmaps::config_icon_config, 32, 32, TFT_BLACK);
}
inline void CoreDisplay::_display_draw_bar_stuff()
{
using namespace DisplayColors;
MySDcard& sd = GET(MySDcard);
MyWiFiPortal& wifi = GET(MyWiFiPortal);
// What do I want on my top bar?
/*
1. SD card usage / online
2. Devices connected
3. I2C time (delay)
*/
char buf[128];
snprintf(buf, 128, "Time: %llu; SD: %s; CPU: %.1f%% %.1f%%; WiFi: %s / %s",//; RAM %.1f%%", // SD usage: %.1f%%;
(get_time_ms() / 1000),
sd.is_online() ? "ON" : "OFF", //sd.sd_used_perc() * 100.0f,
CPU::get_cpu_usage(0) * 100.0f,
CPU::get_cpu_usage(1) * 100.0f,
wifi.get_ssid().c_str(),
wifi.get_password().c_str()
//CPU::get_ram_usage() * 100.0f
);
m_tft->setTextSize(1);
m_tft->setTextColor(TFT_BLACK, bar_top_color); // top bar
m_tft->drawString(buf, 2, 2, 2);
}
inline void CoreDisplay::_set_all_state_has_changed()
{
for(size_t p = 0; p < DisplayColors::item_resumed_amount_on_screen; ++p) m_draw_lines[p].set_state_changed();
m_draw_full_graph->set_state_changed();
m_draw_qrcode->set_state_changed();
}
inline void CoreDisplay::_task_work_body_blocks_event()
{
using namespace DisplayColors;
switch(m_state.state) {
case core_states::STATE_HOME:
// check for touch on devices
{
for (size_t p = 0; p < item_resumed_amount_on_screen; ++p)
{
const int32_t begin_top = bar_top_height + p * item_resumed_height_max;
if (__is_touch_on(0, begin_top, item_resumed_width_max, item_resumed_height_max)) {
size_t calc = p + m_state.offset;
//LOGI_NOSD(e_LOG_TAG::TAG_CORE, "Calc %zu", calc);
if (calc >= static_cast<size_t>(CS::d2u(CS::device_id::_MAX))) m_device_select = CS::device_id::DHT22_SENSOR;
else m_device_select = static_cast<CS::device_id>(calc);
m_state.state = core_states::STATE_DETAILS;
m_state.offset = 0;
m_state.offset_max = GET(MyI2Ccomm).get_device_configurations(m_device_select, 0).m_map.size();
m_draw_full_graph->update_with(m_device_select, m_state.offset);
break;
}
}
}
break;
case core_states::STATE_CONFIG:
// touch on configurations
{
for (size_t p = 0; p < item_resumed_amount_on_screen; ++p)
{
const int32_t begin_top = bar_top_height + p * item_resumed_height_max;
if (__is_touch_on(0, begin_top, item_resumed_width_max, item_resumed_height_max)) {
const size_t calc = p + m_state.offset;
//LOGI_NOSD(e_LOG_TAG::TAG_CORE, "Calc %zu", calc);
MyConfig& cfg = GET(MyConfig);
if (calc >= static_cast<size_t>(core_settings_buttons::_MAX)) break;
switch(static_cast<core_settings_buttons>(calc)){
case core_settings_buttons::BTN_SCREEN_SAVER:
{
switch(cfg.get_core_display_screen_saver_steps_time()) {
case 0:
cfg.set_core_display_screen_saver_steps_time(10000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "10 seconds");
break;
case 10000:
cfg.set_core_display_screen_saver_steps_time(20000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "20 seconds");
break;
case 20000:
cfg.set_core_display_screen_saver_steps_time(30000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "30 seconds");
break;
case 30000:
cfg.set_core_display_screen_saver_steps_time(45000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "45 seconds");
break;
case 45000:
cfg.set_core_display_screen_saver_steps_time(60000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "1 minute");
break;
case 60000:
cfg.set_core_display_screen_saver_steps_time(120000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "2 minutes");
break;
case 120000:
cfg.set_core_display_screen_saver_steps_time(300000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "5 minutes");
break;
case 300000:
cfg.set_core_display_screen_saver_steps_time(600000);
//m_draw_lines[p].set_texts(nullptr, nullptr, "10 minutes");
break;
case 600000:
cfg.set_core_display_screen_saver_steps_time(0);
//m_draw_lines[p].set_texts(nullptr, nullptr, "infinite");
break;
default:
cfg.set_core_display_screen_saver_steps_time(10000);
break;
}
cfg.save();
}
break;
case core_settings_buttons::BTN_SAVE_SPEED:
{
switch(cfg.get_i2c_packaging_delay()) {
//case 1000:
// cfg.set_i2c_packaging_delay(2000); // 2 s
// break;
//case 2000:
// cfg.set_i2c_packaging_delay(3000); // 3 s
// break;
//case 3000:
// cfg.set_i2c_packaging_delay(4000); // 4 s
// break;
//case 4000:
// cfg.set_i2c_packaging_delay(5000); // 5 s
// break;
case 5000:
cfg.set_i2c_packaging_delay(7500); // 7.5 s
break;
case 7500:
cfg.set_i2c_packaging_delay(10000); // 10 s
break;
case 10000:
cfg.set_i2c_packaging_delay(15000); // 15 s
break;
case 15000:
cfg.set_i2c_packaging_delay(20000); // 20 s
break;
case 20000:
cfg.set_i2c_packaging_delay(30000); // 30 s
break;
case 30000:
cfg.set_i2c_packaging_delay(45000); // 45 s
break;
case 45000:
cfg.set_i2c_packaging_delay(60000); // 1 min
break;
case 60000:
cfg.set_i2c_packaging_delay(90000); // 1.5 min
break;
case 90000:
cfg.set_i2c_packaging_delay(120000); // 2 min
break;
case 120000:
cfg.set_i2c_packaging_delay(180000); // 3 min
break;
case 180000:
cfg.set_i2c_packaging_delay(300000); // 5 min
break;
case 300000:
cfg.set_i2c_packaging_delay(600000); // 10 min
break;
case 600000:
cfg.set_i2c_packaging_delay(1200000); // 20 min
break;
case 1200000:
cfg.set_i2c_packaging_delay(1800000); // 30 min
break;
case 1800000:
cfg.set_i2c_packaging_delay(3600000); // 1 h
break;
case 3600000:
cfg.set_i2c_packaging_delay(7200000); // 2 h
break;
case 7200000:
cfg.set_i2c_packaging_delay(10800000); // 3 h
break;
case 10800000:
cfg.set_i2c_packaging_delay(21600000); // 6 h
break;
case 21600000:
cfg.set_i2c_packaging_delay(43200000); // 12 h
break;
case 43200000:
cfg.set_i2c_packaging_delay(86400000); // 24 h
break;
default:
cfg.set_i2c_packaging_delay(5000);
break;
}
cfg.save();
}
break;
case core_settings_buttons::BTN_REDO_CALIBRATION_SCREEN:
_task_calibrate_display_once(); // this will lock.
_display_draw_static_overlay();
_set_all_state_has_changed();
break;
case core_settings_buttons::BTN_ENABLE_HOTSPOT:
{
const auto current_wifi = !cfg.get_wifi_hotspot();
cfg.set_wifi_hotspot(current_wifi);
async_class_method(CoreDisplay, _check_wifi, cpu_core_id_for_wifi_setup);
}
break;
default:
break;
}
break; // of if
}
}
}
break;
case core_states::STATE_DETAILS:
// no touch related stuff. Each one has its own max offset that each shows graph and current value
// on touch is should do nothing actually.
{
//m_state.offset_max = GET(MyI2Ccomm).get_device_configurations(m_device_select, 0).m_map.size();
}
break;
case core_states::STATE_QRCODE:
{
m_draw_qrcode->update();
// nothing to do related to touch
}
break;
//case core_states::STATE_DEBUG:
// // on any touch on debug, go back
// m_state.state = core_states::STATE_HOME;
// m_state.offset = 0;
// m_state.offset_max = CS::d2u(CS::device_id::_MAX) - item_resumed_amount_on_screen;
// break;
}
}
inline void CoreDisplay::_check_wifi()
{
auto& wifi = GET(MyWiFiPortal);
if (GET(MyConfig).get_wifi_hotspot()) {
LOGI(e_LOG_TAG::TAG_CORE, "WiFi initializing...");
wifi.start();
LOGI(e_LOG_TAG::TAG_CORE, "WiFi initialized with QR code ready.");
}
else {
LOGI(e_LOG_TAG::TAG_CORE, "Stopping WiFi...");
wifi.stop();
LOGI(e_LOG_TAG::TAG_CORE, "WiFi stuff cleaned.");
}
}
inline void CoreDisplay::async_display_screen_saver()
{
LOGI(e_LOG_TAG::TAG_CORE, "Display screen brightness starting soon...");
while(!m_is_tft_ready) SLEEP(100);
LOGI(e_LOG_TAG::TAG_CORE, "Display screen brightness running!");
uint8_t& curr_state = m_screen_saver.state;
uint8_t& curr_level = m_screen_saver.current_level;
uint64_t& time_event = m_screen_saver.next_step; // time for next bright down
MyConfig& cfg = GET(MyConfig);
while(1) {
CPU::AutoWait autotime(core_display_led_pwn_delay); // loop control
// if it's time, increase counter, lower screen brightness. If 0, keep on.
if (cfg.get_core_display_screen_saver_steps_time() == 0) {
curr_state = 0;
time_event = 0;
}
else if (get_time_ms() > time_event + GET(MyConfig).get_core_display_screen_saver_steps_time()) {
time_event += cfg.get_core_display_screen_saver_steps_time();
if (curr_state < 2) ++curr_state;
}
switch(curr_state) {
case 1: // half
{
constexpr int target_val = 18;
curr_level = static_cast<uint8_t>((static_cast<int>(curr_level) * 8 + target_val) / 9);
if (abs(static_cast<int>(curr_level) - target_val) < 4) curr_level = target_val;
analogWrite(core_display_led_pwm_pin, curr_level);
}
break;
case 2: // off
{
constexpr int target_val = 0;
curr_level = static_cast<uint8_t>((static_cast<int>(curr_level) * 8 + target_val) / 9);
if (abs(static_cast<int>(curr_level) - target_val) < 4) curr_level = target_val;
analogWrite(core_display_led_pwm_pin, curr_level);
}
break;
default: // on
{
constexpr int target_val = 255;
curr_level = static_cast<uint8_t>((static_cast<int>(curr_level) + target_val) / 2);
if (abs(static_cast<int>(curr_level) - target_val) < 4) curr_level = target_val;
analogWrite(core_display_led_pwm_pin, curr_level);
}
break;
}
}
vTaskDelete(NULL);
}
inline void CoreDisplay::async_display_caller()
{
MySDcard& sd = GET(MySDcard);
LOGI(e_LOG_TAG::TAG_CORE, "Initializing Display...");
m_tft = std::shared_ptr<TFT_eSPI>(new TFT_eSPI());
m_draw_lines = std::unique_ptr<DisplayLineBlock[]>(new DisplayLineBlock[DisplayColors::item_resumed_amount_on_screen]);
for(size_t p = 0; p < DisplayColors::item_resumed_amount_on_screen; ++p) {
m_draw_lines[p].set_tft(m_tft);
m_draw_lines[p].set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_lines[p].set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_lines[p].set_font_color(DisplayColors::item_font_color);
m_draw_lines[p].set_nodata_color(DisplayColors::body_color);
}
m_draw_full_graph = std::unique_ptr<DisplayFullBlockGraph>(new DisplayFullBlockGraph{});
{
m_draw_full_graph->set_tft(m_tft);
m_draw_full_graph->set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_full_graph->set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_full_graph->set_font_color(DisplayColors::item_font_color);
m_draw_full_graph->set_nodata_color(DisplayColors::body_color);
}
m_draw_qrcode = std::unique_ptr<DisplayQRcodeDrawer>(new DisplayQRcodeDrawer());
{
m_draw_qrcode->set_tft(m_tft);
m_draw_qrcode->set_fill_color(DisplayColors::item_offline_bg_color);
m_draw_qrcode->set_border_color(DisplayColors::item_offline_bg_color_border);
m_draw_qrcode->set_font_color(DisplayColors::item_font_color);
m_draw_qrcode->set_nodata_color(DisplayColors::body_color);
}
LOGI(e_LOG_TAG::TAG_CORE, "Setting up Display...");
m_tft->init();
m_tft->setRotation(3);
m_tft->fillScreen(TFT_BLACK);
analogWrite(core_display_led_pwm_pin, 255);
LOGI(e_LOG_TAG::TAG_CORE, "Getting Display calibration...");
SLEEP(25);
uint16_t calibrationData[6]{}; // 14 bytes actually + 1 extra byte for flag
if (sd.read_from(core_display_config_ini, (char*)calibrationData, 14, 0) != 14) {
LOGI(e_LOG_TAG::TAG_CORE, "No configuration has been found for touch calibration. Triggering one.");
_task_calibrate_display_once();
LOGI(e_LOG_TAG::TAG_CORE, "Calibration applied.");
}
else {
LOGI(e_LOG_TAG::TAG_CORE, "Calibration good.");
m_tft->setTouch(calibrationData);
}
LOGI(e_LOG_TAG::TAG_CORE, "Resetting timers...");
m_screen_saver.next_step = get_time_ms();
LOGI(e_LOG_TAG::TAG_CORE, "Ready.");
m_is_tft_ready = true;
async_class_method(CoreDisplay, _check_wifi, cpu_core_id_for_wifi_setup);
_display_draw_static_overlay();
SLEEP(25);
while(1) {
CPU::AutoWait autotime(core_display_main_delay); // loop control
task_touch();
SLEEP(15);
if (m_screen_saver.state != 2) { // only update screen if makes sense
vTaskPrioritySet(NULL, core_thread_priority);
task_display();
}
else {
vTaskPrioritySet(NULL, tskIDLE_PRIORITY);
}
SLEEP(15);
}
}
inline CoreDisplay::CoreDisplay()
{
async_class_method_pri(CoreDisplay, async_display_caller, core_thread_priority, cpu_core_id_for_core);
async_class_method_pri(CoreDisplay, async_display_screen_saver, core_led_pwm_thread_priority, cpu_core_id_for_display_pwm);
}