forked from HASwitchPlate/openHASP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasp_object.cpp
744 lines (646 loc) · 26.9 KB
/
hasp_object.cpp
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
/* MIT License - Copyright (c) 2019-2024 Francis Van Roie
For full license information read the LICENSE file in the project folder */
/* ********************************************************************************************
*
* HASP Object Handlers
* - Object Finders : Convert from object pointers to and from p[x].b[y] IDs
* - Value Dispatchers : Forward output data to the dispatcher
* - Attribute processor : Decide if an attribute needs updating or querying and forward
* - Object creator : Creates an object from a line of jsonl
*
******************************************************************************************** */
#include "hasplib.h"
const char** btnmatrix_default_map; // memory pointer to lvgl default btnmatrix map
const char* msgbox_default_map[] = {"OK", ""}; // memory pointer to hasp default msgbox map
// ##################### Object Finders ########################################################
// Return a child object from a parent with a specific objid
lv_obj_t* hasp_find_obj_from_parent_id(lv_obj_t* parent, uint8_t objid)
{
if(objid == 0 || parent == nullptr) return parent;
lv_obj_t* child;
child = lv_obj_get_child(parent, NULL);
while(child) {
/* child found, return it */
if(objid == child->user_data.id) return child;
/* check grandchildren */
lv_obj_t* grandchild = hasp_find_obj_from_parent_id(child, objid);
if(grandchild) return grandchild; /* grandchild found, return it */
/* check tabs */
if(obj_check_type(child, LV_HASP_TABVIEW)) {
uint16_t tabcount = lv_tabview_get_tab_count(child);
for(uint16_t i = 0; i < tabcount; i++) {
lv_obj_t* tab = lv_tabview_get_tab(child, i);
// LOG_DEBUG(TAG_HASP, "Found tab %i", i);
if(tab->user_data.id && objid == tab->user_data.id) return tab; /* tab found, return it */
/* check grandchildren */
grandchild = hasp_find_obj_from_parent_id(tab, objid);
if(grandchild) return grandchild; /* grandchild found, return it */
}
}
/* try next sibling */
child = lv_obj_get_child(parent, child);
}
return NULL;
}
// Return the object with a specific pageid and objid
lv_obj_t* hasp_find_obj_from_page_id(uint8_t pageid, uint8_t objid)
{
return hasp_find_obj_from_parent_id(haspPages.get_obj(pageid), objid);
}
// Return the pageid and objid of an object
bool hasp_find_id_from_obj(const lv_obj_t* obj, uint8_t* pageid, uint8_t* objid)
{
if(!obj || !haspPages.get_id(obj, pageid)) return false;
if(obj->user_data.id == 0 && obj != haspPages.get_obj(*pageid)) return false;
*objid = obj->user_data.id;
return true;
}
void hasp_object_tree(const lv_obj_t* parent, uint8_t pageid, uint16_t level)
{
if(parent == nullptr) return;
/* Output parent info */
char indent[31];
memset(indent, 32, 31);
if(level < 15) indent[level * 2] = 0;
indent[30] = 0;
LOG_VERBOSE(TAG_HASP, F("%s- " HASP_OBJECT_NOTATION ": %s"), indent, pageid, parent->user_data.id,
obj_get_type_name(parent));
lv_obj_t* child;
child = lv_obj_get_child(parent, NULL);
while(child) {
/* child found, process it */
hasp_object_tree(child, pageid, level + 1);
/* try next sibling */
child = lv_obj_get_child(parent, child);
}
/* check tabs */
if(obj_check_type(parent, LV_HASP_TABVIEW)) {
#if 1
uint16_t tabcount = lv_tabview_get_tab_count(parent);
for(uint16_t i = 0; i < tabcount; i++) {
lv_obj_t* tab = lv_tabview_get_tab(parent, i);
LOG_VERBOSE(TAG_HASP, "Found tab %i", i);
if(tab->user_data.objid) hasp_object_tree(tab, pageid, level + 1);
}
#endif
}
}
// ##################### Value Dispatchers ########################################################
/* Sends the data out on the state/pxby topic */
void object_dispatch_state(uint8_t pageid, uint8_t btnid, const char* payload)
{
char topic[64];
char* pagename = haspPages.get_name(pageid);
if(pagename)
snprintf_P(topic, sizeof(topic), PSTR("%s.b%u"), pagename, btnid);
else
snprintf_P(topic, sizeof(topic), PSTR(HASP_OBJECT_NOTATION), pageid, btnid);
dispatch_state_subtopic(topic, payload);
}
// ##################### State Changers ########################################################
// Recursive function that goes over all objects only ONCE
void object_set_group_values(lv_obj_t* parent, hasp_update_value_t& value)
{
if(parent == nullptr) return;
// Update object if it's in the same group
if(value.group == parent->user_data.groupid && value.obj != parent) {
attribute_set_normalized_value(parent, value);
}
/* check tabs */
if(obj_get_type(parent) == LV_HASP_TABVIEW) {
uint16_t tabcount = lv_tabview_get_tab_count(parent);
for(uint16_t i = 0; i < tabcount; i++) {
lv_obj_t* tab = lv_tabview_get_tab(parent, i);
object_set_group_values(tab, value);
}
} else {
lv_obj_t* child;
child = lv_obj_get_child(parent, NULL);
while(child) {
object_set_group_values(child, value);
child = lv_obj_get_child(parent, child);
}
}
}
// SHOULD only by called from DISPATCH
void object_set_normalized_group_values(hasp_update_value_t& value)
{
if(value.group == 0 || value.min == value.max) return;
uint8_t page = haspPages.get();
object_set_group_values(haspPages.get_obj(page), value); // Update visible objects first
for(uint8_t i = 0; i < HASP_NUM_PAGES; i++) {
if(i != page) object_set_group_values(haspPages.get_obj(i), value);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Used in the dispatcher
void hasp_process_attribute(uint8_t pageid, uint8_t objid, const char* attr, const char* payload, bool update)
{
if(lv_obj_t* obj = hasp_find_obj_from_page_id(pageid, objid)) {
hasp_process_obj_attribute(obj, attr, payload, update); // || strlen(payload) > 0);
} else {
LOG_WARNING(TAG_HASP, F(D_OBJECT_UNKNOWN " " HASP_OBJECT_NOTATION), pageid, objid);
}
}
// ##################### Object Creator ########################################################
// Called from hasp_new_object or TAG_JSON to process all attributes
int hasp_parse_json_attributes(lv_obj_t* obj, const JsonObject& doc)
{
int i = 0;
#if HASP_TARGET_PC || defined(ESP32)
std::string v;
v.reserve(64);
for(JsonPair keyValue : doc) {
// LOG_VERBOSE(TAG_HASP, F(D_BULLET "%s=%s"), keyValue.key().c_str(),
// keyValue.value().as<std::string>().c_str());
v = keyValue.value().as<std::string>();
hasp_process_obj_attribute(obj, keyValue.key().c_str(), keyValue.value().as<std::string>().c_str(), true);
i++;
}
#else
String v((char*)0);
v.reserve(64);
for(JsonPair keyValue : doc) {
// LOG_DEBUG(TAG_HASP, F(D_BULLET "%s=%s"), keyValue.key().c_str(), keyValue.value().as<String>().c_str());
v = keyValue.value().as<String>();
hasp_process_obj_attribute(obj, keyValue.key().c_str(), keyValue.value().as<String>().c_str(), true);
i++;
}
#endif
// LOG_DEBUG(TAG_HASP, F("%d keys processed"), i);
return i;
}
static void object_add_task(lv_obj_t* obj, lv_task_cb_t task_xcb, uint16_t interval)
{
hasp_task_user_data_t* user_data = (hasp_task_user_data_t*)lv_mem_alloc(sizeof(hasp_task_user_data_t));
if(!user_data) return;
user_data->obj = obj;
user_data->templ = (char*)D_TIMESTAMP;
user_data->interval = interval;
lv_task_t* task = lv_task_create(task_xcb, interval, LV_TASK_PRIO_LOWEST, (void*)user_data);
lv_task_set_repeat_count(task, -1); // Infinite
lv_task_ready(task); // trigger it
// (void)task; // unused
}
/**
* Create a new object according to the json config
* @param config Json representation for this object
* @param saved_page_id the pageid to use when no pageid is specified in the Json, updated when it is specified so
* following objects in the file can share the pageid
*/
void hasp_new_object(const JsonObject& config, uint8_t& saved_page_id)
{
/* Skip line detection */
if(!config[FPSTR(FP_SKIP)].isNull() && config[FPSTR(FP_SKIP)].as<bool>()) return;
/* Page selection */
uint8_t pageid = saved_page_id;
if(!config[FPSTR(FP_PAGE)].isNull()) {
pageid = config[FPSTR(FP_PAGE)].as<uint8_t>();
config.remove(FPSTR(FP_PAGE));
}
/* Page with pageid is the default parent_obj */
lv_obj_t* parent_obj = haspPages.get_obj(pageid);
if(!parent_obj) {
LOG_WARNING(TAG_HASP, F(D_OBJECT_PAGE_UNKNOWN), pageid);
return;
} else {
saved_page_id = pageid; /* save the current pageid for next objects */
}
/* A custom parentid was set */
if(!config[FPSTR(FP_PARENTID)].isNull()) {
uint8_t parentid = config[FPSTR(FP_PARENTID)].as<uint8_t>();
parent_obj = hasp_find_obj_from_parent_id(parent_obj, parentid);
if(!parent_obj) {
LOG_WARNING(TAG_HASP, F("Parent ID " HASP_OBJECT_NOTATION " not found, skipping..."), pageid, parentid);
return;
} else {
LOG_VERBOSE(TAG_HASP, F("Parent ID " HASP_OBJECT_NOTATION " found"), pageid, parentid);
}
config.remove(FPSTR(FP_PARENTID));
}
uint16_t sdbm = 0;
uint8_t id = config[FPSTR(FP_ID)].as<uint8_t>();
config.remove(FPSTR(FP_ID));
/* Create the object if it does not exist */
lv_obj_t* obj = hasp_find_obj_from_parent_id(parent_obj, id);
if(!obj) {
/* Create the object first */
/* Validate type */
// if(config[FPSTR(FP_OBJID)].isNull()) { // TODO: obsolete objid
if(config[FPSTR(FP_OBJ)].isNull()) {
return; // comments
} else {
sdbm = Parser::get_sdbm(config[FPSTR(FP_OBJ)].as<const char*>());
config.remove(FPSTR(FP_OBJ));
}
// } else {
// LOG_WARNING(TAG_HASP, F(D_ATTRIBUTE_OBSOLETE D_ATTRIBUTE_INSTEAD), "objid",
// "obj"); // TODO: obsolete objid
// sdbm = config[FPSTR(FP_OBJID)].as<uint8_t>();
// config.remove(FPSTR(FP_OBJID));
// }
switch(sdbm) {
/* ----- Custom Objects ------ */
case LV_HASP_ALARM:
case HASP_OBJ_ALARM:
obj = lv_obj_create(parent_obj, NULL);
if(obj) obj->user_data.objid = LV_HASP_ALARM;
break;
/* ----- Basic Objects ------ */
case LV_HASP_BTNMATRIX:
case HASP_OBJ_BTNMATRIX:
obj = lv_btnmatrix_create(parent_obj, NULL);
if(obj) {
lv_btnmatrix_set_recolor(obj, true);
if(obj_check_type(parent_obj, LV_HASP_ALARM))
lv_obj_set_event_cb(obj, alarm_event_handler);
else
lv_obj_set_event_cb(obj, btnmatrix_event_handler);
lv_btnmatrix_ext_t* ext = (lv_btnmatrix_ext_t*)lv_obj_get_ext_attr(obj);
btnmatrix_default_map = ext->map_p; // store the static pointer to the default lvgl btnmap
obj->user_data.objid = LV_HASP_BTNMATRIX;
}
break;
#if LV_USE_TABLE > 0
case LV_HASP_TABLE:
case HASP_OBJ_TABLE:
obj = lv_table_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, selector_event_handler);
obj->user_data.objid = LV_HASP_TABLE;
}
break;
#endif
case LV_HASP_BUTTON:
case HASP_OBJ_BTN:
obj = lv_btn_create(parent_obj, NULL);
if(obj) {
lv_obj_t* lbl = lv_label_create(obj, NULL);
if(lbl) {
lv_label_set_text(lbl, "");
lv_label_set_recolor(lbl, true);
lbl->user_data.objid = LV_HASP_LABEL;
lv_obj_align(lbl, NULL, LV_ALIGN_CENTER, 0, 0);
}
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_BUTTON;
}
break;
case LV_HASP_CHECKBOX:
case HASP_OBJ_CHECKBOX:
obj = lv_checkbox_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, toggle_event_handler);
obj->user_data.objid = LV_HASP_CHECKBOX;
}
break;
case LV_HASP_LABEL:
case HASP_OBJ_LABEL:
obj = lv_label_create(parent_obj, NULL);
if(obj) {
lv_label_set_long_mode(obj, LV_LABEL_LONG_CROP);
lv_label_set_recolor(obj, true);
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_LABEL;
// if(id >= 250) object_add_task(obj, event_timer_clock, 1000);
}
break;
case LV_HASP_TEXTAREA:
case HASP_OBJ_TEXTAREA:
obj = lv_textarea_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, textarea_event_handler);
lv_textarea_set_cursor_click_pos(obj, true);
obj->user_data.objid = LV_HASP_TEXTAREA;
}
break;
case LV_HASP_IMAGE:
case HASP_OBJ_IMG:
obj = lv_img_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_IMAGE;
}
break;
#if HASP_USE_QRCODE > 0
case LV_HASP_QRCODE:
case HASP_OBJ_QRCODE:
obj = lv_qrcode_create(parent_obj, 140, LV_COLOR_BLACK, LV_COLOR_WHITE);
if(obj) {
lv_obj_set_event_cb(obj, delete_event_handler);
obj->user_data.objid = LV_HASP_QRCODE;
}
break;
#endif
case LV_HASP_ARC:
case HASP_OBJ_ARC:
obj = lv_arc_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_ARC;
}
break;
case LV_HASP_CONTAINER:
case HASP_OBJ_CONT:
obj = lv_cont_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_CONTAINER;
}
break;
case LV_HASP_OBJECT:
case HASP_OBJ_OBJ:
obj = lv_obj_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_OBJECT;
}
break;
#if LVGL_VERSION_MAJOR == 7 && LV_USE_PAGE
case LV_HASP_PAGE:
case HASP_OBJ_PAGE:
obj = lv_page_create(parent_obj, NULL);
if(obj) obj->user_data.objid = LV_HASP_PAGE;
// No event handler for pages
break;
#endif
#if LV_USE_WIN && LVGL_VERSION_MAJOR == 7
case LV_HASP_WINDOW:
case HASP_OBJ_WIN:
obj = lv_win_create(parent_obj, NULL);
if(obj) obj->user_data.objid = LV_HASP_WINDOW;
// No event handler for pages
break;
#endif
#if LVGL_VERSION_MAJOR == 8
case LV_HASP_LED:
case HASP_OBJ_LED:
obj = lv_led_create(parent_obj);
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_LED;
}
break;
case LV_HASP_TILEVIEW:
case HASP_OBJ_TILEVIEW:
obj = lv_tileview_create(parent_obj);
if(obj) obj->user_data.objid = LV_HASP_TILEVIEW;
// No event handler for tileviews
break;
case LV_HASP_TABVIEW:
case HASP_OBJ_TABVIEW:
obj = lv_tabview_create(parent_obj, LV_DIR_TOP, 100);
// No event handler for tabs
if(obj) {
lv_obj_set_event_cb(obj, selector_event_handler);
obj->user_data.objid = LV_HASP_TABVIEW;
}
break;
#else
case LV_HASP_LED:
case HASP_OBJ_LED:
obj = lv_led_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_LED;
}
break;
#if LV_USE_TILEVIEW > 0
case LV_HASP_TILEVIEW:
case HASP_OBJ_TILEVIEW:
obj = lv_tileview_create(parent_obj, NULL);
if(obj) obj->user_data.objid = LV_HASP_TILEVIEW;
// No event handler for tileviews
break;
#endif
case LV_HASP_TABVIEW:
case HASP_OBJ_TABVIEW:
obj = lv_tabview_create(parent_obj, NULL);
// No event handler for tabs
if(obj) {
lv_obj_set_event_cb(obj, selector_event_handler);
obj->user_data.objid = LV_HASP_TABVIEW;
}
break;
case LV_HASP_TAB:
case HASP_OBJ_TAB:
if(parent_obj && parent_obj->user_data.objid == LV_HASP_TABVIEW) {
obj = lv_tabview_add_tab(parent_obj, "Tab");
if(obj) {
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_TAB;
}
} else {
LOG_WARNING(TAG_HASP, F("Parent of a tab must be a tabview object"));
return;
}
break;
#endif
/* ----- Color Objects ------ */
case LV_HASP_CPICKER:
case HASP_OBJ_CPICKER:
obj = lv_cpicker_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, cpicker_event_handler);
obj->user_data.objid = LV_HASP_CPICKER;
}
break;
#if LV_USE_SPINNER != 0
case LV_HASP_SPINNER:
case HASP_OBJ_SPINNER:
obj = lv_spinner_create(parent_obj, NULL);
if(obj) {
obj->user_data.objid = LV_HASP_SPINNER;
lv_obj_set_event_cb(obj, generic_event_handler);
}
break;
#endif
/* ----- Range Objects ------ */
case LV_HASP_SLIDER:
case HASP_OBJ_SLIDER:
obj = lv_slider_create(parent_obj, NULL);
if(obj) {
lv_slider_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, slider_event_handler);
obj->user_data.objid = LV_HASP_SLIDER;
}
// bool knobin = config[F("knobin")].as<bool>() | true;
// lv_slider_set_knob_in(obj, knobin);
break;
case LV_HASP_GAUGE:
case HASP_OBJ_GAUGE:
obj = lv_gauge_create(parent_obj, NULL);
if(obj) {
lv_gauge_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_GAUGE;
}
break;
case LV_HASP_LINE:
case HASP_OBJ_LINE:
obj = lv_line_create(parent_obj, NULL);
if(obj) {
lv_obj_set_style_local_line_width(obj, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 1);
lv_obj_set_event_cb(obj, delete_event_handler);
obj->user_data.objid = LV_HASP_LINE;
}
break;
case LV_HASP_BAR:
case HASP_OBJ_BAR:
obj = lv_bar_create(parent_obj, NULL);
if(obj) {
lv_bar_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_BAR;
}
break;
case LV_HASP_LINEMETER:
case HASP_OBJ_LMETER: // obsolete
case HASP_OBJ_LINEMETER:
obj = lv_linemeter_create(parent_obj, NULL);
if(obj) {
lv_linemeter_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, generic_event_handler);
obj->user_data.objid = LV_HASP_LINEMETER;
}
break;
#if LV_USE_SPINBOX > 0
case LV_HASP_SPINBOX:
case HASP_OBJ_SPINBOX:
obj = lv_spinbox_create(parent_obj, NULL);
if(obj) {
lv_spinbox_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, slider_event_handler);
obj->user_data.objid = LV_HASP_SPINBOX;
}
break;
#endif
case LV_HASP_LIST:
case HASP_OBJ_LIST:
obj = lv_list_create(parent_obj, NULL);
if(obj) {
// Callbacks are set on the individual buttons
obj->user_data.objid = LV_HASP_LIST;
}
break;
#if LV_USE_CHART > 0
case LV_HASP_CHART:
case HASP_OBJ_CHART:
obj = lv_chart_create(parent_obj, NULL);
if(obj) {
lv_chart_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, generic_event_handler);
lv_chart_add_series(obj, LV_COLOR_RED);
lv_chart_add_series(obj, LV_COLOR_GREEN);
lv_chart_add_series(obj, LV_COLOR_BLUE);
lv_chart_series_t* ser = my_chart_get_series(obj, 2);
lv_chart_set_next(obj, ser, 10);
lv_chart_set_next(obj, ser, 20);
lv_chart_set_next(obj, ser, 30);
lv_chart_set_next(obj, ser, 40);
obj->user_data.objid = LV_HASP_CHART;
}
break;
#endif
/* ----- On/Off Objects ------ */
case LV_HASP_SWITCH:
case HASP_OBJ_SWITCH:
obj = lv_switch_create(parent_obj, NULL);
if(obj) {
lv_obj_set_event_cb(obj, toggle_event_handler);
obj->user_data.objid = LV_HASP_SWITCH;
}
break;
/* ----- List Object ------- */
case LV_HASP_DROPDOWN:
case HASP_OBJ_DROPDOWN:
obj = lv_dropdown_create(parent_obj, NULL);
if(obj) {
lv_dropdown_set_text(obj, NULL); // Clear default text
lv_dropdown_set_draw_arrow(obj, true);
// lv_dropdown_set_anim_time(obj, 200);
lv_obj_set_top(obj, true);
// lv_obj_align(obj, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
lv_obj_set_event_cb(obj, selector_event_handler);
obj->user_data.objid = LV_HASP_DROPDOWN;
}
break;
case LV_HASP_ROLLER:
case HASP_OBJ_ROLLER:
obj = lv_roller_create(parent_obj, NULL);
// lv_obj_align(obj, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
if(obj) {
lv_roller_set_auto_fit(obj, false);
lv_obj_set_event_cb(obj, selector_event_handler);
obj->user_data.objid = LV_HASP_ROLLER;
}
break;
case LV_HASP_MSGBOX:
case HASP_OBJ_MSGBOX:
obj = lv_msgbox_create(parent_obj, NULL);
if(obj) {
/* Assign default OK btnmap and enable recolor */
if(msgbox_default_map) lv_msgbox_add_btns(obj, msgbox_default_map);
lv_msgbox_ext_t* ext = (lv_msgbox_ext_t*)lv_obj_get_ext_attr(obj);
if(ext && ext->btnm) lv_btnmatrix_set_recolor(ext->btnm, true);
/* msgbox parameters */
lv_obj_align(obj, NULL, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_auto_realign(obj, true);
lv_obj_set_event_cb(obj, msgbox_event_handler);
obj->user_data.objid = LV_HASP_MSGBOX;
}
break;
#if LV_USE_CALENDAR > 0
case LV_HASP_CALENDER:
case HASP_OBJ_CALENDAR:
obj = lv_calendar_create(parent_obj, NULL);
// lv_obj_align(obj, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
if(obj) {
lv_obj_set_event_cb(obj, calendar_event_handler);
obj->user_data.objid = LV_HASP_CALENDER;
object_add_task(obj, pageid, id, event_timer_calendar, 5000);
}
break;
#endif
/* ----- Other Object ------ */
// default:
// return LOG_WARNING(TAG_HASP, F("Unsupported Object ID %u"), objid);
}
/* No object was actually created */
if(!obj) {
LOG_ERROR(TAG_HASP, F(D_OBJECT_CREATE_FAILED), id);
return;
}
// Prevent losing press when the press is slid out of the objects.
// (E.g. a Button can be released out of it if it was being pressed)
lv_obj_add_protect(obj, LV_PROTECT_PRESS_LOST);
lv_obj_set_gesture_parent(obj, false);
lv_obj_set_click(obj, true);
/* id tag the object */
obj->user_data.id = id;
#ifdef HASP_DEBUG
uint8_t temp; // needed for debug tests
(void)temp;
/** testing start **/
if(!hasp_find_id_from_obj(obj, &pageid, &temp)) {
LOG_ERROR(TAG_HASP, F(D_OBJECT_LOST));
return;
}
#endif
/** verbose reporting **/
LOG_VERBOSE(TAG_HASP, F(D_BULLET HASP_OBJECT_NOTATION " = %s"), pageid, id, obj_get_type_name(obj));
#ifdef HASP_DEBUG
/* test double-check */
lv_obj_t* test = hasp_find_obj_from_page_id(pageid, (uint8_t)temp);
if(test != obj || temp != id) {
LOG_ERROR(TAG_HASP, F(D_OBJECT_MISMATCH));
return;
} else {
// object created successfully
}
#endif
} else {
// object already exists
}
hasp_parse_json_attributes(obj, config);
}