Allow jsonl for existing objects

This commit is contained in:
fvanroie 2020-12-31 03:34:00 +01:00
parent 0112980ad1
commit 977ee521d1

View File

@ -224,11 +224,11 @@ void hasp_send_obj_attribute_int(lv_obj_t * obj, const char * attribute, int32_t
void hasp_send_obj_attribute_color(lv_obj_t * obj, const char * attribute, lv_color_t color)
{
char buffer[32];
char buffer[40]; // "#ffffff","r":"255","g":"255","b":"255"
lv_color32_t c32;
c32.full = lv_color_to32(color);
snprintf(buffer, sizeof(buffer), PSTR("#%02x%02x%02x\",\"r\":\"%d\",\"g\":\"%d\",\"b\":\"%d"), c32.ch.red, c32.ch.green, c32.ch.blue, c32.ch.red,
c32.ch.green, c32.ch.blue);
snprintf(buffer, sizeof(buffer), PSTR("#%02x%02x%02x\",\"r\":\"%d\",\"g\":\"%d\",\"b\":\"%d"), c32.ch.red,
c32.ch.green, c32.ch.blue, c32.ch.red, c32.ch.green, c32.ch.blue);
hasp_send_obj_attribute_str(obj, attribute, buffer);
}
@ -442,8 +442,8 @@ static void cpicker_event_handler(lv_obj_t * obj, lv_event_t event)
char color[6];
snprintf_P(color, sizeof(color), PSTR("color"));
// if(event == LV_EVENT_VALUE_CHANGED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj));
if(event == LV_EVENT_RELEASED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj));
if(event == LV_EVENT_VALUE_CHANGED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj));
// if(event == LV_EVENT_RELEASED) hasp_send_obj_attribute_color(obj, color, lv_cpicker_get_color(obj));
}
/**
@ -500,28 +500,21 @@ void hasp_process_attribute(uint8_t pageid, uint8_t objid, const char * attr, co
*/
void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
{
/* Validate page */
// uint8_t pageid = config[F("page")].isNull() ? haspGetPage() : config[F("page")].as<uint8_t>();
uint8_t pageid = config[F("page")].isNull() ? saved_page_id : config[F("page")].as<uint8_t>();
/* Page selection */
lv_obj_t * page = get_page_obj(pageid);
if(!page) {
/* Page selection: page is the default parent_obj */
uint8_t pageid = config[F("page")].isNull() ? saved_page_id : config[F("page")].as<uint8_t>();
lv_obj_t * parent_obj = get_page_obj(pageid);
if(!parent_obj) {
return Log.warning(TAG_HASP, F("Page ID %u not defined"), pageid);
} else {
saved_page_id = pageid; /* save the current pageid */
}
/* Validate type */
if(config[F("objid")].isNull()) return; // comments
lv_obj_t * parent_obj = page;
// lv_obj_t * parent_obj = page;
if(!config[F("parentid")].isNull()) {
uint8_t parentid = config[F("parentid")].as<uint8_t>();
parent_obj = hasp_find_obj_from_parent_id(page, parentid);
parent_obj = hasp_find_obj_from_parent_id(parent_obj, parentid);
if(!parent_obj) {
return Log.warning(TAG_HASP, F("Parent ID p[%u].b[%u] not found, skipping..."), pageid, parentid);
// parent_obj = page; // don't create on the page instead ??
} else {
Log.verbose(TAG_HASP, F("Parent ID p[%u].b[%u] found"), pageid, parentid);
}
@ -534,256 +527,279 @@ void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
/* Define Objects*/
lv_obj_t * obj = hasp_find_obj_from_parent_id(parent_obj, id);
if(obj) {
return Log.warning(TAG_HASP, F("Object ID %u already exists!"), id);
}
// return Log.warning(TAG_HASP, F("Object ID %u already exists!"), id);
switch(objid) {
/* ----- Basic Objects ------ */
case LV_HASP_BTNMATRIX:
obj = lv_btnmatrix_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btnmap_event_handler);
break;
case LV_HASP_TABLE:
obj = lv_table_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, table_event_handler);
break;
} else {
case LV_HASP_BUTTON:
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, "");
lbl->user_data.objid = LV_HASP_LABEL;
lv_obj_align(lbl, NULL, LV_ALIGN_CENTER, 0, 0);
/* Validate type */
if(config[F("objid")].isNull()) return; // comments
switch(objid) {
/* ----- Basic Objects ------ */
case LV_HASP_BTNMATRIX:
obj = lv_btnmatrix_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btnmap_event_handler);
break;
case LV_HASP_TABLE:
obj = lv_table_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, table_event_handler);
break;
case LV_HASP_BUTTON:
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, "");
lbl->user_data.objid = LV_HASP_LABEL;
lv_obj_align(lbl, NULL, LV_ALIGN_CENTER, 0, 0);
}
lv_obj_set_event_cb(obj, btn_event_handler);
}
lv_obj_set_event_cb(obj, btn_event_handler);
break;
case LV_HASP_CHECKBOX:
obj = lv_checkbox_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, checkbox_event_handler);
break;
case LV_HASP_LABEL: {
obj = lv_label_create(parent_obj, NULL);
/* click area padding */
// uint8_t padh = config[F("padh")].as<uint8_t>();
// uint8_t padv = config[F("padv")].as<uint8_t>();
/* text align */
// if(padh > 0 || padv > 0) {
// lv_obj_set_ext_click_area(obj, padh, padh, padv, padv);
// }
// if(!config[F("align")].isNull()) {
// lv_label_set_align(obj, LV_LABEL_ALIGN_CENTER);
// }
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_IMAGE: {
obj = lv_img_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_ARC: {
obj = lv_arc_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_CONTAINER: {
obj = lv_cont_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_OBJECT: {
obj = lv_obj_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_PAGE: {
obj = lv_page_create(parent_obj, NULL);
// No event handler for pages
break;
}
break;
case LV_HASP_CHECKBOX:
obj = lv_checkbox_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, checkbox_event_handler);
break;
case LV_HASP_LABEL: {
obj = lv_label_create(parent_obj, NULL);
/* click area padding */
// uint8_t padh = config[F("padh")].as<uint8_t>();
// uint8_t padv = config[F("padv")].as<uint8_t>();
/* text align */
// if(padh > 0 || padv > 0) {
// lv_obj_set_ext_click_area(obj, padh, padh, padv, padv);
// }
// if(!config[F("align")].isNull()) {
// lv_label_set_align(obj, LV_LABEL_ALIGN_CENTER);
// }
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_IMAGE: {
obj = lv_img_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_ARC: {
obj = lv_arc_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_CONTAINER: {
obj = lv_cont_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_OBJECT: {
obj = lv_obj_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_PAGE: {
obj = lv_page_create(parent_obj, NULL);
// No event handler for pages
break;
}
#if LV_USE_WIN && LVGL_VERSION_MAJOR == 7
case LV_HASP_WINDOW: {
obj = lv_win_create(parent_obj, NULL);
// No event handler for pages
break;
}
case LV_HASP_WINDOW: {
obj = lv_win_create(parent_obj, NULL);
// No event handler for pages
break;
}
#endif
#if LVGL_VERSION_MAJOR == 8
case LV_HASP_LED: {
obj = lv_led_create(parent_obj);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_TILEVIEW: {
obj = lv_tileview_create(parent_obj);
// No event handler for tileviews
break;
}
case LV_HASP_TABVIEW: {
obj = lv_tabview_create(parent_obj, LV_DIR_TOP, 100);
// No event handler for tabs
if(obj) {
lv_obj_t * tab;
tab = lv_tabview_add_tab(obj, "tab 1");
// lv_obj_set_user_data(tab, id + 1);
tab = lv_tabview_add_tab(obj, "tab 2");
// lv_obj_set_user_data(tab, id + 2);
tab = lv_tabview_add_tab(obj, "tab 3");
// lv_obj_set_user_data(tab, id + 3);
case LV_HASP_LED: {
obj = lv_led_create(parent_obj);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_TILEVIEW: {
obj = lv_tileview_create(parent_obj);
// No event handler for tileviews
break;
}
case LV_HASP_TABVIEW: {
obj = lv_tabview_create(parent_obj, LV_DIR_TOP, 100);
// No event handler for tabs
if(obj) {
lv_obj_t * tab;
tab = lv_tabview_add_tab(obj, "tab 1");
// lv_obj_set_user_data(tab, id + 1);
tab = lv_tabview_add_tab(obj, "tab 2");
// lv_obj_set_user_data(tab, id + 2);
tab = lv_tabview_add_tab(obj, "tab 3");
// lv_obj_set_user_data(tab, id + 3);
}
break;
}
break;
}
#else
case LV_HASP_LED: {
obj = lv_led_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_TILEVIEW: {
obj = lv_tileview_create(parent_obj, NULL);
// No event handler for tileviews
break;
}
case LV_HASP_TABVIEW: {
obj = lv_tabview_create(parent_obj, NULL);
// No event handler for tabs
if(obj) {
lv_obj_t * tab;
tab = lv_tabview_add_tab(obj, "tab 1");
// lv_obj_set_user_data(tab, id + 1);
tab = lv_tabview_add_tab(obj, "tab 2");
// lv_obj_set_user_data(tab, id + 2);
tab = lv_tabview_add_tab(obj, "tab 3");
// lv_obj_set_user_data(tab, id + 3);
case LV_HASP_LED: {
obj = lv_led_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, btn_event_handler);
break;
}
case LV_HASP_TILEVIEW: {
obj = lv_tileview_create(parent_obj, NULL);
// No event handler for tileviews
break;
}
case LV_HASP_TABVIEW: {
obj = lv_tabview_create(parent_obj, NULL);
// No event handler for tabs
if(obj) {
lv_obj_t * tab;
tab = lv_tabview_add_tab(obj, "tab 1");
// lv_obj_set_user_data(tab, id + 1);
tab = lv_tabview_add_tab(obj, "tab 2");
// lv_obj_set_user_data(tab, id + 2);
tab = lv_tabview_add_tab(obj, "tab 3");
// lv_obj_set_user_data(tab, id + 3);
}
break;
}
break;
}
#endif
/* ----- Color Objects ------ */
case LV_HASP_CPICKER: {
obj = lv_cpicker_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, cpicker_event_handler);
break;
}
/* ----- Color Objects ------ */
case LV_HASP_CPICKER: {
obj = lv_cpicker_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, cpicker_event_handler);
break;
}
#if LV_USE_PRELOAD != 0
case LV_HASP_PRELOADER: {
obj = lv_spinner_create(parent_obj, NULL);
break;
}
case LV_HASP_PRELOADER: {
obj = lv_spinner_create(parent_obj, NULL);
break;
}
#endif
/* ----- Range Objects ------ */
case LV_HASP_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);
/* ----- Range Objects ------ */
case LV_HASP_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);
}
// bool knobin = config[F("knobin")].as<bool>() | true;
// lv_slider_set_knob_in(obj, knobin);
break;
}
// bool knobin = config[F("knobin")].as<bool>() | true;
// lv_slider_set_knob_in(obj, knobin);
break;
}
case LV_HASP_GAUGE: {
obj = lv_gauge_create(parent_obj, NULL);
if(obj) {
lv_gauge_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
case LV_HASP_GAUGE: {
obj = lv_gauge_create(parent_obj, NULL);
if(obj) {
lv_gauge_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
}
break;
}
break;
}
case LV_HASP_BAR: {
obj = lv_bar_create(parent_obj, NULL);
if(obj) {
lv_bar_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
case LV_HASP_BAR: {
obj = lv_bar_create(parent_obj, NULL);
if(obj) {
lv_bar_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
}
break;
}
break;
}
case LV_HASP_LMETER: {
obj = lv_linemeter_create(parent_obj, NULL);
if(obj) {
lv_linemeter_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
case LV_HASP_LMETER: {
obj = lv_linemeter_create(parent_obj, NULL);
if(obj) {
lv_linemeter_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
}
break;
}
break;
}
case LV_HASP_CHART: {
obj = lv_chart_create(parent_obj, NULL);
if(obj) {
lv_chart_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_event_handler);
case LV_HASP_CHART: {
obj = lv_chart_create(parent_obj, NULL);
if(obj) {
lv_chart_set_range(obj, 0, 100);
lv_obj_set_event_cb(obj, btn_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_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 = lv_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);
lv_chart_series_t * ser = lv_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);
}
break;
}
break;
}
/* ----- On/Off Objects ------ */
case LV_HASP_SWITCH: {
obj = lv_switch_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, switch_event_handler);
break;
}
/* ----- List Object ------- */
case LV_HASP_DDLIST: {
obj = lv_dropdown_create(parent_obj, NULL);
if(obj) {
lv_dropdown_set_draw_arrow(obj, true);
// lv_dropdown_set_anim_time(obj, 200);
lv_obj_set_top(obj, true);
/* ----- On/Off Objects ------ */
case LV_HASP_SWITCH: {
obj = lv_switch_create(parent_obj, NULL);
if(obj) lv_obj_set_event_cb(obj, switch_event_handler);
break;
}
/* ----- List Object ------- */
case LV_HASP_DDLIST: {
obj = lv_dropdown_create(parent_obj, NULL);
if(obj) {
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, ddlist_event_handler);
}
break;
}
case LV_HASP_ROLLER: {
obj = lv_roller_create(parent_obj, NULL);
// lv_obj_align(obj, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
lv_obj_set_event_cb(obj, ddlist_event_handler);
if(obj) {
lv_roller_set_auto_fit(obj, false);
lv_obj_set_event_cb(obj, roller_event_handler);
}
break;
}
break;
}
case LV_HASP_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, roller_event_handler);
}
break;
/* ----- Other Object ------ */
// default:
// return Log.warning(TAG_HASP, F("Unsupported Object ID %u"), objid);
}
/* ----- Other Object ------ */
// default:
// return Log.warning(TAG_HASP, F("Unsupported Object ID %u"), objid);
/* No object was actually created */
if(!obj) {
return Log.error(TAG_HASP, F("Object ID %u is NULL, skipping..."), id);
}
// 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);
/* id tag the object */
// lv_obj_set_user_data(obj, id);
obj->user_data.id = id;
obj->user_data.objid = objid; //& 0b11111;
obj->user_data.groupid = groupid; // & 0b111;
/** testing start **/
uint8_t temp;
if(!hasp_find_id_from_obj(obj, &pageid, &temp)) {
return Log.error(TAG_HASP, F("Lost track of the created object, not found!"));
}
/** verbose reporting **/
lv_obj_type_t list;
lv_obj_get_type(obj, &list);
Log.verbose(TAG_HASP, F(" * p[%u].b[%u] = %s"), pageid, temp, list.type[0]);
/* test double-check */
lv_obj_t * test = hasp_find_obj_from_parent_id(get_page_obj(pageid), (uint8_t)temp);
if(test != obj) {
return Log.error(TAG_HASP, F("Objects DO NOT match!"));
}
}
/* No object was actually created */
if(!obj) {
return Log.warning(TAG_HASP, F("Object ID %u is NULL, skipping..."), id);
}
// 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);
/* id tag the object */
// lv_obj_set_user_data(obj, id);
obj->user_data.id = id;
obj->user_data.objid = objid; //& 0b11111;
obj->user_data.groupid = groupid; // & 0b111;
/* do not process these attributes */
config.remove(F("page"));
config.remove(F("id"));
config.remove(F("objid"));
config.remove(F("parentid"));
String v((char *)0);
v.reserve(64);
@ -792,21 +808,4 @@ void hasp_new_object(const JsonObject & config, uint8_t & saved_page_id)
hasp_process_obj_attribute(obj, keyValue.key().c_str(), v.c_str(), true);
// Log.verbose(TAG_HASP,F(" * %s => %s"), keyValue.key().c_str(), v.c_str());
}
/** testing start **/
uint8_t temp;
if(!hasp_find_id_from_obj(obj, &pageid, &temp)) {
return Log.error(TAG_HASP, F("Lost track of the created object, not found!"));
}
/** verbose reporting **/
lv_obj_type_t list;
lv_obj_get_type(obj, &list);
Log.verbose(TAG_HASP, F(" * p[%u].b[%u] = %s"), pageid, temp, list.type[0]);
/* test double-check */
lv_obj_t * test = hasp_find_obj_from_parent_id(get_page_obj(pageid), (uint8_t)temp);
if(test != obj) {
return Log.error(TAG_HASP, F("Objects DO NOT match!"));
}
}