diff --git a/src/hasp.cpp b/src/hasp.cpp index 4c5372d3..25396d9a 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -4,11 +4,19 @@ #include "hasp_conf.h" #include #include "ArduinoJson.h" +#include "ArduinoLog.h" #include "lvgl.h" #include "lv_conf.h" + +#define LVGL7 1 + +#if LVGL7 +#include "../lib/lvgl/src/lv_widgets/lv_roller.h" +#else #include "lv_theme_hasp.h" #include "lv_objx/lv_roller.h" +#endif #if HASP_USE_QRCODE != 0 #include "lv_qrcode.h" @@ -23,16 +31,22 @@ #include // Include the SPIFFS library #endif -#include "hasp_log.h" +//#include "hasp_log.h" #include "hasp_debug.h" #include "hasp_config.h" #include "hasp_dispatch.h" -#include "hasp_mqtt.h" #include "hasp_wifi.h" #include "hasp_gui.h" #include "hasp_tft.h" +#include "hasp_attr_get.h" +#include "hasp_attr_set.h" #include "hasp.h" +#include "hasp_conf.h" +#if HASP_USE_MQTT +#include "hasp_mqtt.h" +#endif + //#if LV_USE_HASP /********************* @@ -152,6 +166,7 @@ bool get_page_id(lv_obj_t * obj, uint8_t * pageid) lv_obj_t * FindObjFromId(lv_obj_t * parent, uint8_t objid) { + if(objid == 0) return parent; lv_obj_t * child; child = lv_obj_get_child(parent, NULL); while(child) { @@ -180,22 +195,6 @@ bool FindIdFromObj(lv_obj_t * obj, uint8_t * pageid, lv_obj_user_data_t * objid) } //////////////////////////////////////////////////////////////////////////////////////////////////// -void haspGetAttr(String hmiAttribute) -{ // Get the value of a Nextion component attribute - // This will only send the command to the panel requesting the attribute, the actual - // return of that value will be handled by nextionProcessInput and placed into mqttGetSubtopic - /*Serial1.print("get " + hmiAttribute); - Serial1.write(nextionSuffix, sizeof(nextionSuffix));*/ - debugPrintln(String(F("HMI OUT: 'get ")) + hmiAttribute + "'"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -void haspProcessInput() -{ -#if HASP_USE_MQTT > 0 - // mqttSend(topic, value); -#endif -} void haspSendNewEvent(lv_obj_t * obj, char * val) { @@ -266,29 +265,12 @@ void haspSendNewValue(lv_obj_t * obj, int16_t val) haspSendNewValue(obj, (int32_t)val); } -uint32_t get_cpicker_value(lv_obj_t * obj) -{ - lv_color32_t c32; - c32.full = lv_color_to32(lv_cpicker_get_color(obj)); - return (uint32_t)c32.full; -} -void set_cpicker_value(lv_obj_t * obj, uint32_t color) -{ - lv_cpicker_set_color(obj, lv_color_hex(color)); -} - void haspSendNewValue(lv_obj_t * obj, lv_color_t color) { haspSendNewValue(obj, (int32_t)get_cpicker_value(obj)); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void haspSendCmd(String nextionCmd) -{ // Send a raw command to the Nextion panel - /*Serial1.print(utf8ascii(nextionCmd)); - Serial1.write(nextionSuffix, sizeof(nextionSuffix)); - debugPrintln(String(F("HMI OUT: ")) + nextionCmd);*/ -} bool check_obj_type(const char * lvobjtype, lv_hasp_obj_type_t haspobjtype) { @@ -298,13 +280,13 @@ bool check_obj_type(const char * lvobjtype, lv_hasp_obj_type_t haspobjtype) case LV_HASP_LABEL: return (strcmp_P(lvobjtype, PSTR("lv_label")) == 0); case LV_HASP_CHECKBOX: - return (strcmp_P(lvobjtype, PSTR("lv_cb")) == 0); + return (strcmp_P(lvobjtype, PSTR("lv_cb")) == 0) || (strcmp_P(lvobjtype, PSTR("lv_checkbox")) == 0); case LV_HASP_DDLIST: - return (strcmp_P(lvobjtype, PSTR("lv_ddlist")) == 0); + return (strcmp_P(lvobjtype, PSTR("lv_ddlist")) == 0) || (strcmp_P(lvobjtype, PSTR("lv_dropdown")) == 0); case LV_HASP_CPICKER: return (strcmp_P(lvobjtype, PSTR("lv_cpicker")) == 0); case LV_HASP_PRELOADER: - return (strcmp_P(lvobjtype, PSTR("lv_preloader")) == 0); + return (strcmp_P(lvobjtype, PSTR("lv_preload")) == 0) || (strcmp_P(lvobjtype, PSTR("lv_spinner")) == 0); case LV_HASP_SLIDER: return (strcmp_P(lvobjtype, PSTR("lv_slider")) == 0); case LV_HASP_GAUGE: @@ -312,324 +294,30 @@ bool check_obj_type(const char * lvobjtype, lv_hasp_obj_type_t haspobjtype) case LV_HASP_BAR: return (strcmp_P(lvobjtype, PSTR("lv_bar")) == 0); case LV_HASP_LMETER: - return (strcmp_P(lvobjtype, PSTR("lv_lmeter")) == 0); + return (strcmp_P(lvobjtype, PSTR("lv_lmeter")) == 0) || (strcmp_P(lvobjtype, PSTR("lv_linemeter")) == 0); case LV_HASP_ROLLER: return (strcmp_P(lvobjtype, PSTR("lv_roller")) == 0); case LV_HASP_SWITCH: - return (strcmp_P(lvobjtype, PSTR("lv_sw")) == 0); + return (strcmp_P(lvobjtype, PSTR("lv_sw")) == 0) || (strcmp_P(lvobjtype, PSTR("lv_switch")) == 0); case LV_HASP_LED: return (strcmp_P(lvobjtype, PSTR("lv_led")) == 0); + case LV_HASP_CONTAINER: + return (strcmp_P(lvobjtype, PSTR("lv_cont")) == 0) || (strcmp_P(lvobjtype, PSTR("lv_container")) == 0); default: return false; } } -bool haspGetLabelText(lv_obj_t * obj, std::string & strPayload) -{ - if(!obj) { - errorPrintln(F("HASP: %sButton not defined")); - return false; - } - - lv_obj_t * label = lv_obj_get_child_back(obj, NULL); - if(label) { - lv_obj_type_t list; - lv_obj_get_type(label, &list); - - if(check_obj_type(list.type[0], LV_HASP_LABEL)) { - strPayload = lv_label_get_text(label); - return true; - } - - } else { - warningPrintln(F("HASP: %shaspGetLabelText NULL Pointer encountered")); - } - - return false; -} - -void haspSetLabelText(lv_obj_t * obj, const char * value) -{ - if(!obj) { - errorPrintln(F("HASP: %sButton not defined")); - return; - } - - lv_obj_t * label = lv_obj_get_child_back(obj, NULL); - if(label) { - lv_obj_type_t list; - lv_obj_get_type(label, &list); - - if(check_obj_type(list.type[0], LV_HASP_LABEL)) { - debugPrintln(String(F("HASP: Setting value to ")) + String(value)); - lv_label_set_text(label, value); - } - - } else { - warningPrintln(F("HASP: %shaspSetLabelText NULL Pointer encountered")); - } -} - void haspSetToggle(lv_obj_t * obj, bool toggle) { +#if LVGL7 + lv_btn_set_checkable(obj, toggle); +#else lv_btn_set_toggle(obj, toggle); +#endif lv_obj_set_event_cb(obj, toggle ? toggle_event_handler : btn_event_handler); } -bool haspGetObjAttribute(lv_obj_t * obj, String strAttr, std::string & strPayload) -{ - if(!obj) return false; - uint16_t val = 0; - - switch(strAttr.length()) { - case 2: - if(strAttr == F(".x")) { - val = lv_obj_get_x(obj); - } else if(strAttr == F(".y")) { - val = lv_obj_get_y(obj); - } else if(strAttr == F(".w")) { - val = lv_obj_get_width(obj); - } else if(strAttr == F(".h")) { - val = lv_obj_get_height(obj); - } else { - return false; - } - strPayload = String(val).c_str(); - return true; - case 4: - if(strAttr == F(".vis")) { - strPayload = String(!lv_obj_get_hidden(obj)).c_str(); - return true; - } else { - /* .txt and .val depend on objecttype */ - lv_obj_type_t list; - lv_obj_get_type(obj, &list); - - if(strAttr == F(".txt")) { - if(check_obj_type(list.type[0], LV_HASP_BUTTON)) { - return haspGetLabelText(obj, strPayload); - } - if(check_obj_type(list.type[0], LV_HASP_LABEL)) { - strPayload = lv_label_get_text(obj); - return true; - } - if(check_obj_type(list.type[0], LV_HASP_CHECKBOX)) { - strPayload = lv_cb_get_text(obj); - return true; - } - if(check_obj_type(list.type[0], LV_HASP_DDLIST)) { - char buffer[128]; - lv_ddlist_get_selected_str(obj, buffer, sizeof(buffer)); - strPayload = String(buffer).c_str(); - return true; - } - if(check_obj_type(list.type[0], LV_HASP_ROLLER)) { - char buffer[128]; - lv_roller_get_selected_str(obj, buffer, sizeof(buffer)); - strPayload = String(buffer).c_str(); - return true; - } - return false; - } - - if(strAttr == F(".val")) { - if(check_obj_type(list.type[0], LV_HASP_PRELOADER)) return false; - - if(check_obj_type(list.type[0], LV_HASP_BUTTON)) { - if(lv_btn_get_state(obj) == LV_BTN_STATE_TGL_PR || - lv_btn_get_state(obj) == LV_BTN_STATE_TGL_REL) - strPayload = "1"; // It's toggled - else - strPayload = "0"; // Normal btn has no toggle state - } - - if(check_obj_type(list.type[0], LV_HASP_SLIDER)) - strPayload = String(lv_slider_get_value(obj)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_GAUGE)) - strPayload = String(lv_gauge_get_value(obj, 0)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_BAR)) strPayload = String(lv_bar_get_value(obj)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_LMETER)) - strPayload = String(lv_lmeter_get_value(obj)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_CPICKER)) - strPayload = String(get_cpicker_value(obj)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_CHECKBOX)) - strPayload = String(!lv_cb_is_checked(obj) ? 0 : 1).c_str(); - if(check_obj_type(list.type[0], LV_HASP_DDLIST)) - strPayload = String(lv_ddlist_get_selected(obj)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_ROLLER)) - strPayload = String(lv_roller_get_selected(obj)).c_str(); - - if(check_obj_type(list.type[0], LV_HASP_LED)) strPayload = String(lv_led_get_bright(obj)).c_str(); - if(check_obj_type(list.type[0], LV_HASP_SWITCH)) strPayload = String(lv_sw_get_state(obj)).c_str(); - - return true; - } - } - break; - case 7: - if(strAttr == F(".hidden")) { - strPayload = String(!lv_obj_get_hidden(obj)).c_str(); - return true; - } else { - return false; - } - return false; - case 8: - if(strAttr == F(".opacity")) { - strPayload = String(!lv_obj_get_opa_scale_enable(obj) ? 255 : lv_obj_get_opa_scale(obj)).c_str(); - } else if(strAttr == F(".enabled")) { - strPayload = String(lv_obj_get_click(obj)).c_str(); - } else if(strAttr == F(".options")) { - /* options depend on objecttype */ - lv_obj_type_t list; - lv_obj_get_type(obj, &list); - - if(check_obj_type(list.type[0], LV_HASP_DDLIST)) { - strPayload = lv_ddlist_get_options(obj); - return true; - } - if(check_obj_type(list.type[0], LV_HASP_ROLLER)) { - strPayload = lv_roller_get_options(obj); - return true; - } - return false; - } - break; - default: - errorPrintln(F("HASP: %sUnknown property")); - return false; - } - return false; -} - -void haspSetObjAttribute(lv_obj_t * obj, String strAttr, String strPayload) -{ - if(!obj) return; - uint16_t val = (uint16_t)strPayload.toInt(); - - switch(strAttr.length()) { - case 2: - if(strAttr == F(".x")) { - lv_obj_set_x(obj, val); - return; - } else if(strAttr == F(".y")) { - lv_obj_set_y(obj, val); - return; - } else if(strAttr == F(".w")) { - lv_obj_set_width(obj, val); - return; - } else if(strAttr == F(".h")) { - lv_obj_set_height(obj, val); - return; - } - break; - case 4: - if(strAttr == F(".vis")) { - lv_obj_set_hidden(obj, val == 0); - return; - } else { - /* .txt and .val depend on objecttype */ - lv_obj_type_t list; - lv_obj_get_type(obj, &list); - - if(strAttr == F(".txt")) { // In order of likelihood to occur - if(check_obj_type(list.type[0], LV_HASP_BUTTON)) { - haspSetLabelText(obj, strPayload.c_str()); - return; - } else if(check_obj_type(list.type[0], LV_HASP_LABEL)) { - lv_label_set_text(obj, strPayload.c_str()); - return; - } else if(check_obj_type(list.type[0], LV_HASP_CHECKBOX)) { - lv_cb_set_text(obj, strPayload.c_str()); - return; - } - } - - if(strAttr == F(".val")) { // In order of likelihood to occur - int16_t intval = (int16_t)strPayload.toInt(); - - if(check_obj_type(list.type[0], LV_HASP_BUTTON)) { - if(lv_btn_get_toggle(obj)) { - lv_btn_set_state(obj, val == 0 ? LV_BTN_STATE_REL : LV_BTN_STATE_TGL_REL); - return; - } - - } else if(check_obj_type(list.type[0], LV_HASP_CHECKBOX)) { - lv_cb_set_checked(obj, val != 0); - return; - } else if(check_obj_type(list.type[0], LV_HASP_SLIDER)) { - lv_slider_set_value(obj, intval, LV_ANIM_ON); - return; - } - - else if(check_obj_type(list.type[0], LV_HASP_SWITCH)) { - val == 0 ? lv_sw_off(obj, LV_ANIM_ON) : lv_sw_on(obj, LV_ANIM_ON); - return; - } else if(check_obj_type(list.type[0], LV_HASP_LED)) { - lv_led_set_bright(obj, (uint8_t)val); - return; - } else if(check_obj_type(list.type[0], LV_HASP_GAUGE)) { - lv_gauge_set_value(obj, 0, intval); - return; - } else if(check_obj_type(list.type[0], LV_HASP_DDLIST)) { - lv_ddlist_set_selected(obj, val); - return; - } else if(check_obj_type(list.type[0], LV_HASP_ROLLER)) { - lv_roller_set_selected(obj, val, LV_ANIM_ON); - return; - } else if(check_obj_type(list.type[0], LV_HASP_BAR)) { - lv_bar_set_value(obj, intval, LV_ANIM_OFF); - return; - } else if(check_obj_type(list.type[0], LV_HASP_LMETER)) { - lv_lmeter_set_value(obj, intval); - return; - } else if(check_obj_type(list.type[0], LV_HASP_CPICKER)) { - set_cpicker_value(obj, (uint32_t)strPayload.toInt()); - return; - } - } - } - break; - case 7: - if(strAttr == F(".hidden")) { - lv_obj_set_hidden(obj, val == 0); - return; - } else if(strAttr == F(".toggle")) { - lv_obj_type_t list; - lv_obj_get_type(obj, &list); - if(check_obj_type(list.type[0], LV_HASP_BUTTON)) { - haspSetToggle(obj, strPayload.toInt() > 0); - return; - } - } - break; - case 8: - if(strAttr == F(".opacity")) { - lv_obj_set_opa_scale_enable(obj, val < 255); - lv_obj_set_opa_scale(obj, val < 255 ? val : 255); - return; - } else if(strAttr == F(".enabled")) { - lv_obj_set_click(obj, val != 0); - return; - } else if(strAttr == F(".options")) { - /* .options depend on objecttype */ - lv_obj_type_t list; - lv_obj_get_type(obj, &list); - - if(check_obj_type(list.type[0], LV_HASP_DDLIST)) { - lv_ddlist_set_options(obj, strPayload.c_str()); - return; - } else if(check_obj_type(list.type[0], LV_HASP_ROLLER)) { - lv_roller_ext_t * ext = (lv_roller_ext_t *)lv_obj_get_ext_attr(obj); - lv_roller_set_options(obj, strPayload.c_str(), ext->mode); - return; - } - } - break; - } - errorPrintln(F("HASP: %sUnknown property")); -} - void haspProcessAttribute(uint8_t pageid, uint8_t objid, String strAttr, String strPayload) { lv_obj_t * obj = FindObjFromId((uint8_t)pageid, (uint8_t)objid); @@ -640,9 +328,11 @@ void haspProcessAttribute(uint8_t pageid, uint8_t objid, String strAttr, String /* publish the change */ std::string strValue = ""; if(haspGetObjAttribute(obj, strAttr, strValue)) { +#if HASP_USE_MQTT > 0 mqttSendNewValue(pageid, objid, String(strValue.c_str())); +#endif } else { - warningPrintln(String(F("HASP: %sUnknown property: ")) + strAttr); + Log.warning(F("HASP: Unknown property: %s"), strAttr.c_str()); } } // payload } // obj @@ -650,40 +340,6 @@ void haspProcessAttribute(uint8_t pageid, uint8_t objid, String strAttr, String //////////////////////////////////////////////////////////////////////////////////////////////////// -/* -static int16_t get_obj_id(lv_obj_t * obj) -{ - lv_obj_t * page = lv_scr_act(); - int16_t id = -1; - lv_obj_t * thisobj = NULL; - while(obj != thisobj && id < 256) { - thisobj = lv_obj_get_child_back(page, thisobj); - id++; - } - if(obj != thisobj) { - return -1; - } else { - return id; - } -} - -static int16_t get_obj(uint8_t pageid, uint8_t objid, lv_obj_t * obj) -{ - lv_obj_t * page = lv_scr_act(); - int16_t id = -1; - lv_obj_t * thisobj = NULL; - while(obj != thisobj && id < 256) { - thisobj = lv_obj_get_child_back(page, thisobj); - id++; - } - if(obj != thisobj) { - return -1; - } else { - return id; - } -} -*/ - /** * Connection lost GUI */ @@ -695,6 +351,9 @@ void haspWakeUp() void haspDisconnect() { +#if LVGL7 + +#else /* Create a dark plain style for a message box's background (modal)*/ lv_style_copy(&style_mbox_bg, &lv_style_plain); style_mbox_bg.body.main_color = LV_COLOR_BLACK; @@ -702,6 +361,7 @@ void haspDisconnect() style_mbox_bg.body.opa = LV_OPA_60; lv_obj_set_style(lv_disp_get_layer_sys(NULL), &style_mbox_bg); +#endif lv_obj_set_click(lv_disp_get_layer_sys(NULL), true); lv_obj_set_event_cb(lv_disp_get_layer_sys(NULL), NULL); lv_obj_set_user_data(lv_disp_get_layer_sys(NULL), 255); @@ -715,7 +375,7 @@ void haspDisconnect() void haspReconnect() { /*Revert the top layer to not block*/ - lv_obj_set_style(lv_disp_get_layer_sys(NULL), &lv_style_transp); + // lv_obj_set_style(lv_disp_get_layer_sys(NULL), &lv_style_transp); lv_obj_set_click(lv_disp_get_layer_sys(NULL), false); lv_obj_set_event_cb(lv_disp_get_layer_sys(NULL), btn_event_handler); /* @@ -755,11 +415,13 @@ void haspDisplayAP(const char * ssid, const char * pass) #endif lv_obj_t * panel = lv_cont_create(pages[0], NULL); - lv_obj_set_style(panel, &lv_style_pretty); + // lv_obj_set_style(panel, &lv_style_pretty); +#if HASP_USE_QRCODE != 0 lv_obj_align(panel, qr, LV_ALIGN_OUT_TOP_MID, 0, -20); +#endif lv_label_set_align(panel, LV_LABEL_ALIGN_CENTER); lv_cont_set_fit(panel, LV_FIT_TIGHT); - lv_cont_set_layout(panel, LV_LAYOUT_COL_M); + // lv_cont_set_layout(panel, LV_LAYOUT_COL_M); txt = String(LV_SYMBOL_WIFI) + String(ssid); lv_obj_t * network = lv_label_create(panel, NULL); @@ -774,7 +436,7 @@ void haspDisplayAP(const char * ssid, const char * pass) lv_obj_set_event_cb(lv_disp_get_layer_sys(NULL), gotoPage1); haspFirstSetup(); - lv_obj_set_style(lv_disp_get_layer_sys(NULL), &lv_style_transp); + // lv_obj_set_style(lv_disp_get_layer_sys(NULL), &lv_style_transp); } static void kb_event_cb(lv_obj_t * event_kb, lv_event_t event) @@ -791,13 +453,23 @@ static void kb_event_cb(lv_obj_t * event_kb, lv_event_t event) if(child->user_data) { lv_obj_user_data_t objid = 10; if(objid == child->user_data) { +#if LVGL7 + strncpy(ssid, lv_textarea_get_text(child), sizeof(ssid)); + settings[FPSTR(F_CONFIG_SSID)] = ssid; + // if(kb != NULL) lv_keyboard_set_ta(kb, child); +#else strncpy(ssid, lv_ta_get_text(child), sizeof(ssid)); settings[FPSTR(F_CONFIG_SSID)] = ssid; if(kb != NULL) lv_kb_set_ta(kb, child); +#endif } objid = 20; if(objid == child->user_data) { +#if LVGL7 + strncpy(pass, lv_textarea_get_text(child), sizeof(pass)); +#else strncpy(pass, lv_ta_get_text(child), sizeof(pass)); +#endif settings[FPSTR(F_CONFIG_PASS)] = pass; } } @@ -817,8 +489,11 @@ static void kb_event_cb(lv_obj_t * event_kb, lv_event_t event) } else { /* prevent double presses, swipes and ghost press on tiny keyboard */ +#if LVGL7 + if(event == LV_EVENT_RELEASED) lv_keyboard_def_event_cb(event_kb, LV_EVENT_VALUE_CHANGED); +#else if(event == LV_EVENT_RELEASED) lv_kb_def_event_cb(event_kb, LV_EVENT_VALUE_CHANGED); - +#endif /* Just call the regular event handler */ // lv_kb_def_event_cb(event_kb, event); } @@ -827,7 +502,11 @@ static void ta_event_cb(lv_obj_t * ta, lv_event_t event) { if(event == LV_EVENT_CLICKED) { /* Focus on the clicked text area */ +#if LVGL7 + // if(kb != NULL) lv_keyboard_set_ta(kb, ta); +#else if(kb != NULL) lv_kb_set_ta(kb, ta); +#endif } else if(event == LV_EVENT_INSERT) { @@ -861,6 +540,8 @@ void haspFirstSetup(void) labelpos = LV_ALIGN_OUT_LEFT_MID; } +#if LVGL7 +#else lv_style_copy(&rel_style, &lv_style_btn_rel); rel_style.body.radius = 0; rel_style.body.border.width = 1; @@ -910,6 +591,7 @@ void haspFirstSetup(void) lv_kb_set_ta(kb, oneline_ta); /* Focus it on one of the text areas to start */ lv_kb_set_cursor_manage(oneline_ta, true); /* Automatically show/hide cursors on text areas */ +#endif } /** @@ -938,84 +620,83 @@ void haspSetup(JsonObject settings) lv_zifont_init(); - if(lv_zifont_font_init(&defaultFont, haspZiFontPath, 24) != 0) { - errorPrintln(String(F("HASP: %sFailed to set the custom font to ")) + String(haspZiFontPath)); - defaultFont = NULL; // Use default font - } + /* if(lv_zifont_font_init(&defaultFont, haspZiFontPath, 24) != 0) { + errorPrintln(String(F("HASP: %sFailed to set the custom font to ")) + String(haspZiFontPath)); + defaultFont = LV_FONT_DEFAULT; // Use default font + }*/ - lv_theme_t * th; - switch(haspThemeId) { -#if LV_USE_THEME_ALIEN == 1 - case 1: - th = lv_theme_alien_init(haspThemeHue, defaultFont); - break; -#endif -#if LV_USE_THEME_NIGHT == 1 - case 2: - th = lv_theme_night_init(haspThemeHue, defaultFont); // heavy - break; -#endif -#if LV_USE_THEME_MONO == 1 - case 3: - th = lv_theme_mono_init(haspThemeHue, defaultFont); // lightweight - break; -#endif -#if LV_USE_THEME_MATERIAL == 1 - case 4: - th = lv_theme_material_init(haspThemeHue, defaultFont); - break; -#endif -#if LV_USE_THEME_ZEN == 1 - case 5: - th = lv_theme_zen_init(haspThemeHue, defaultFont); // lightweight - break; -#endif -#if LV_USE_THEME_NEMO == 1 - case 6: - th = lv_theme_nemo_init(haspThemeHue, defaultFont); // heavy - break; -#endif -#if LV_USE_THEME_TEMPL == 1 - case 7: - th = lv_theme_templ_init(haspThemeHue, defaultFont); // lightweight, not for production... - break; -#endif -#if LV_USE_THEME_HASP == 1 - case 8: - th = lv_theme_hasp_init(haspThemeHue, defaultFont); - break; -#endif - case 0: -#if LV_USE_THEME_DEFAULT == 1 - th = lv_theme_default_init(haspThemeHue, defaultFont); -#else - th = lv_theme_hasp_init(512, defaultFont); -#endif - break; + lv_theme_t * th = lv_theme_material_init(LV_COLOR_PURPLE, LV_COLOR_ORANGE, LV_THEME_DEFAULT_FLAGS, LV_FONT_DEFAULT, + LV_FONT_DEFAULT, LV_FONT_DEFAULT, LV_FONT_DEFAULT); - default: - th = lv_theme_hasp_init(512, defaultFont); - debugPrintln(F("HASP: Unknown theme selected")); - } + /* + lv_theme_t * th; + switch(haspThemeId) { + #if LV_USE_THEME_ALIEN == 1 + case 1: + th = lv_theme_alien_init(haspThemeHue, defaultFont); + break; + #endif + #if LV_USE_THEME_NIGHT == 1 + case 2: + th = lv_theme_night_init(haspThemeHue, defaultFont); // heavy + break; + #endif + #if LV_USE_THEME_MONO == 1 + case 3: + th = lv_theme_mono_init(haspThemeHue, defaultFont); // lightweight + break; + #endif + #if LV_USE_THEME_MATERIAL == 1 + case 4: + // th = lv_theme_material_init(haspThemeHue, defaultFont); + break; + #endif + #if LV_USE_THEME_ZEN == 1 + case 5: + th = lv_theme_zen_init(haspThemeHue, defaultFont); // lightweight + break; + #endif + #if LV_USE_THEME_NEMO == 1 + case 6: + th = lv_theme_nemo_init(haspThemeHue, defaultFont); // heavy + break; + #endif + #if LV_USE_THEME_TEMPL == 1 + case 7: + th = lv_theme_templ_init(haspThemeHue, defaultFont); // lightweight, not for production... + break; + #endif + #if LV_USE_THEME_HASP == 1 + case 8: + th = lv_theme_hasp_init(haspThemeHue, defaultFont); + break; + #endif + case 0: + #if LV_USE_THEME_DEFAULT == 1 + th = lv_theme_default_init(haspThemeHue, defaultFont); + #else + th = lv_theme_hasp_init(512, defaultFont); + #endif + break; - if(th) { - debugPrintln(F("HASP: Custom theme loaded")); - } else { - errorPrintln(F("HASP: %sNo theme could be loaded")); - } - lv_theme_set_current(th); + default: + th = lv_theme_hasp_init(512, defaultFont); + debugPrintln(F("HASP: Unknown theme selected")); + } + if(th) { + debugPrintln(F("HASP: Custom theme loaded")); + } else { + errorPrintln(F("HASP: %sNo theme could be loaded")); + } + // lv_theme_set_current(th); + */ /*Create a screen*/ for(uint8_t i = 0; i < (sizeof pages / sizeof *pages); i++) { pages[i] = lv_obj_create(NULL, NULL); // lv_obj_set_size(pages[0], hres, vres); } - for(uint8_t i = 0; i < (sizeof pages / sizeof *pages); i++) { - pages[i] = lv_obj_create(NULL, NULL); - // lv_obj_set_size(pages[0], hres, vres); - } - /* if(lv_zifont_font_init(&haspFonts[0], "/fonts/HMI FrankRuhlLibre 24.zi", 24) != 0) { errorPrintln(String(F("HASP: %sFailed to set the custom font to 0"))); @@ -1037,95 +718,10 @@ void haspSetup(JsonObject settings) } */ - /* - lv_obj_t * obj; - - obj = lv_label_create(lv_layer_sys(), NULL); - lv_obj_set_size(obj, hres, 30); - lv_obj_set_pos(obj, lv_disp_get_hor_res(NULL) / 2 - 40, lv_disp_get_ver_res(NULL) / 2 + 40 + 20); - lv_label_set_text(obj, String(F("#ffffff " LV_SYMBOL_WIFI " Connecting... #")).c_str()); - lv_label_set_recolor(obj, true); - - obj = lv_btn_create(lv_layer_sys(), NULL); - lv_obj_set_size(obj, 80, 80); - lv_obj_set_pos(obj, lv_disp_get_hor_res(NULL) / 2 - 40, lv_disp_get_ver_res(NULL) / 2 - 40 - 20); - */ haspDisconnect(); haspLoadPage(haspPagesPath); haspSetPage(haspStartPage); guiSetDim(haspStartDim); - - // lv_obj_t * img_bin_t = lv_img_create(pages[current_page], NULL); /*Crate an image object*/ - // lv_img_set_src(img_bin_t, "F:/dogsmall(1).bin"); /*Set the created file as image (a red rose)*/ - // lv_obj_set_pos(img_bin_t, 64, 64); /*Set the positions*/ - - // // lv_page_set_style(page, LV_PAGE_STYLE_SB, &style_sb); /*Set the scrollbar style*/ - - // // lv_obj_t *img1 = lv_img_create(pages[2], NULL); - // // lv_img_set_src(img1, &xmass); - - // // lv_obj_set_protect(page1, LV_PROTECT_POS); - // // lv_obj_set_protect(page2, LV_PROTECT_POS); - - // /*Create a page*/ - // /* lv_obj_t * page1 = lv_obj_create(screen, NULL); - // lv_obj_set_size(page1, hres, vres); - // lv_obj_align(page1, NULL, LV_ALIGN_CENTER, 0, 0); - // //lv_page_set_style(page, LV_PAGE_STYLE_SB, &style_sb); /*Set the scrollbar style*/ - - // /* default style*/ - // static lv_style_t style_btnm_rel; /* Une variable pour,!enregistrer le style normal */ - // lv_style_copy(&style_btnm_rel, &lv_style_plain); /* Initialise a partir d un,!style included */ - // style_btnm_rel.body.border.color = lv_color_hex3(0x269); - // style_btnm_rel.body.border.width = 1; - // style_btnm_rel.body.padding.inner = -5; - // style_btnm_rel.body.padding.left = 0; - // style_btnm_rel.body.padding.right = 0; - // style_btnm_rel.body.padding.bottom = 0; - // style_btnm_rel.body.padding.top = 0; - // style_btnm_rel.body.main_color = lv_color_hex3(0xADF); - // style_btnm_rel.body.grad_color = lv_color_hex3(0x46B); - // style_btnm_rel.body.shadow.width = 0; - // style_btnm_rel.body.radius = 0; - // // style_btnm_rel.body.shadow.type = LV_SHADOW_BOTTOM; - // // style_btnm_rel.body.radius = LV_RADIUS_CIRCLE; - // style_btnm_rel.text.color = lv_color_hex3(0xDEF); - - // static lv_style_t style_btnm_pr; /* Une variable pour,!enregistrer le style pressed */ - // lv_style_copy(&style_btnm_pr, &style_btnm_rel); /* Initialise a partir du,!style released */ - // style_btnm_pr.body.border.width = 1; - // style_btnm_pr.body.border.color = lv_color_hex3(0x46B); - // style_btnm_pr.body.main_color = lv_color_hex3(0x8BD); - // style_btnm_pr.body.grad_color = lv_color_hex3(0x24A); - // style_btnm_pr.body.shadow.width = 0; - // style_btnm_pr.body.radius = 0; - // style_btnm_pr.text.color = lv_color_hex3(0xBCD); - - // lv_obj_t * btnm1 = lv_btnm_create(pages[0], NULL); - // /* style should set before map */ - // lv_btnm_set_style(btnm1, LV_BTN_STYLE_REL, &style_btnm_rel); /* Dfinit le style,!released du bouton */ - // lv_btnm_set_style(btnm1, LV_BTN_STYLE_PR, &style_btnm_pr); /* Dfinit le style,!pressed du bouton */ - // lv_btnm_set_style(btnm1, LV_BTN_STYLE_TGL_REL, &style_btnm_rel); /* Dfinit le style,!released du bouton */ - // lv_btnm_set_style(btnm1, LV_BTN_STYLE_TGL_PR, &style_btnm_pr); /* Dfinit le style,!pressed du bouton */ - - // lv_btnm_set_map(btnm1, btnm_map1); - - // lv_btnm_set_style(btnm1, LV_BTNM_STYLE_BG, &lv_style_transp); - // lv_obj_set_size(btnm1, hres, vres); - // // lv_btnm_set_style(btnm1, LV_BTNM_STYLE, &style) - // // lv_btnm_set_btn_width(btnm1, 10, 2); /*Make "Action1" twice as wide as "Action2"*/ - // lv_obj_align(btnm1, NULL, LV_ALIGN_CENTER, 0, 0); - // lv_obj_set_event_cb(btnm1, btnmap_event_handler); - - // lv_obj_t * btnm2 = lv_btnm_create(pages[1], NULL); - // /* style should set before map */ - // lv_btnm_set_style(btnm2, LV_BTNM_STYLE_BG, &lv_style_transp); - // lv_btnm_set_map(btnm2, btnm_map2); - // lv_obj_set_size(btnm2, hres, vres); - // // lv_btnm_set_style(btnm1, LV_BTNM_STYLE, &style) - // // lv_btnm_set_btn_width(btnm1, 10, 2); /*Make "Action1" twice as wide as "Action2"*/ - // lv_obj_align(btnm2, NULL, LV_ALIGN_CENTER, 0, 0); - // lv_obj_set_event_cb(btnm2, btnmap_event_handler); } /********************** @@ -1148,8 +744,6 @@ void hasp_background(uint16_t pageid, uint16_t imageid) if(!thisobj) return; - - switch (imageid) { case 0: @@ -1206,55 +800,31 @@ static void btn_event_handler(lv_obj_t * obj, lv_event_t event) if(obj != lv_disp_get_layer_sys(NULL)) { if(!FindIdFromObj(obj, &pageid, &objid)) { - errorPrintln(F("HASP: %sEvent for unknown object")); + Log.warning(F("HASP: Event for unknown object")); return; } } switch(event) { case LV_EVENT_PRESSED: - // strcat_P(buffer, PSTR("Pressed Down")); - // debugPrintln(buffer); - // eventid = 1; memcpy_P(buffer, PSTR("DOWN"), sizeof(buffer)); break; - case LV_EVENT_CLICKED: - // strcat_P(buffer, PSTR("Released Up")); - // debugPrintln(buffer); // UP = the same object was release then was pressed and press was not lost! - // eventid = 0; memcpy_P(buffer, PSTR("UP"), sizeof(buffer)); break; - case LV_EVENT_SHORT_CLICKED: - // strcat_P(buffer, PSTR("Short Click")); - // debugPrintln(buffer); - // eventid = 2; memcpy_P(buffer, PSTR("SHORT"), sizeof(buffer)); break; - case LV_EVENT_LONG_PRESSED: - // strcat_P(buffer, PSTR("Long Press")); - // debugPrintln(buffer); - // eventid = 3; memcpy_P(buffer, PSTR("LONG"), sizeof(buffer)); break; - case LV_EVENT_LONG_PRESSED_REPEAT: - // strcat_P(buffer, PSTR("Long Hold")); - // debugPrintln(buffer); - // eventid = 4; memcpy_P(buffer, PSTR("HOLD"), sizeof(buffer)); break; - case LV_EVENT_PRESS_LOST: - // strcat_P(buffer, PSTR("Lost Press")); - // debugPrintln(buffer); - // eventid = 9; memcpy_P(buffer, PSTR("LOST"), sizeof(buffer)); break; - case LV_EVENT_PRESSING: case LV_EVENT_FOCUSED: case LV_EVENT_DEFOCUSED: @@ -1263,27 +833,63 @@ static void btn_event_handler(lv_obj_t * obj, lv_event_t event) case LV_EVENT_VALUE_CHANGED: strcat_P(buffer, PSTR("Value Changed")); - debugPrintln(buffer); + Log.notice(buffer); return; case LV_EVENT_DELETE: - sprintf(buffer, PSTR("HASP: Object deleted"), event); - debugPrintln(buffer); + Log.notice(PSTR("HASP: Object deleted"), event); return; default: - sprintf(buffer, PSTR("HASP: Unknown Event %d occured"), event); - debugPrintln(buffer); + Log.warning(F("HASP: Unknown Event %d occured"), event); return; } - // printf("p[%u].b[%u].val = %u ", pageid, objid, eventid); if(obj == lv_disp_get_layer_sys(NULL)) { - mqttSendState("wakeuptouch", buffer); +#if HASP_USE_MQTT > 0 + mqttSendState("wakeuptouch", buffer); // TO DO: Memory optimization ! +#endif } else { +#if HASP_USE_MQTT > 0 mqttSendNewEvent(pageid, objid, buffer); +#endif } } +// ##################### Event Handlers by Version ######################################################## + +#if LVGL7 +static void btnmap_event_handler(lv_obj_t * obj, lv_event_t event) +{ + // if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_btnmatrix_get_pressed_btn(obj)); +} + +static void toggle_event_handler(lv_obj_t * obj, lv_event_t event) +{ + bool toggled = + lv_btn_get_state(obj) == LV_BTN_STATE_CHECKED_PRESSED || lv_btn_get_state(obj) == LV_BTN_STATE_CHECKED_RELEASED; + if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, toggled); +} + +static void switch_event_handler(lv_obj_t * obj, lv_event_t event) +{ + if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_switch_get_state(obj)); +} + +static void checkbox_event_handler(lv_obj_t * obj, lv_event_t event) +{ + if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_checkbox_is_checked(obj)); +} + +static void ddlist_event_handler(lv_obj_t * obj, lv_event_t event) +{ + if(event == LV_EVENT_VALUE_CHANGED) { + haspSendNewValue(obj, lv_dropdown_get_selected(obj)); + char buffer[128]; + lv_dropdown_get_selected_str(obj, buffer, sizeof(buffer)); + haspSendNewValue(obj, String(buffer)); + } +} +#else static void btnmap_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_btnm_get_pressed_btn(obj)); @@ -1295,16 +901,6 @@ static void toggle_event_handler(lv_obj_t * obj, lv_event_t event) if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, toggled); } -static void slider_event_handler(lv_obj_t * obj, lv_event_t event) -{ - if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_slider_get_value(obj)); -} - -static void cpicker_event_handler(lv_obj_t * obj, lv_event_t event) -{ - if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_cpicker_get_color(obj)); -} - static void switch_event_handler(lv_obj_t * obj, lv_event_t event) { if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_sw_get_state(obj)); @@ -1324,6 +920,17 @@ static void ddlist_event_handler(lv_obj_t * obj, lv_event_t event) haspSendNewValue(obj, String(buffer)); } } +#endif + +static void slider_event_handler(lv_obj_t * obj, lv_event_t event) +{ + if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_slider_get_value(obj)); +} + +static void cpicker_event_handler(lv_obj_t * obj, lv_event_t event) +{ + if(event == LV_EVENT_VALUE_CHANGED) haspSendNewValue(obj, lv_cpicker_get_color(obj)); +} static void roller_event_handler(lv_obj_t * obj, lv_event_t event) { @@ -1355,11 +962,11 @@ void haspClearPage(uint16_t pageid) { lv_obj_t * page = get_page(pageid); if(!page) { - errorPrintln(F("HASP: %sPage ID not defined")); + Log.warning(F("HASP: Page ID %u not defined"), pageid); } else if(page == lv_layer_sys() || page == lv_layer_top()) { - errorPrintln(F("HASP: %sCannot clear a layer")); + Log.warning(F("HASP: Cannot clear a layer")); } else { - debugPrintln(String(F("HASP: Clearing page ")) + String(pageid)); + Log.notice(F("HASP: Clearing page %u"), pageid); lv_obj_clean(pages[pageid]); } } @@ -1373,11 +980,11 @@ void haspSetPage(uint16_t pageid) { lv_obj_t * page = get_page(pageid); if(!page) { - errorPrintln(F("HASP: %sPage ID not defined")); + Log.warning(F("HASP: Page ID %u not defined"), pageid); } else if(page == lv_layer_sys() || page == lv_layer_top()) { - errorPrintln(F("HASP: %sCannot change to a layer")); + Log.warning(F("HASP: %sCannot change to a layer")); } else { - debugPrintln(String(F("HASP: Changing page to ")) + String(pageid)); + Log.notice(F("HASP: Changing page to %u"), pageid); lv_scr_load(page); current_page = pageid; } @@ -1393,7 +1000,7 @@ void haspNewObject(const JsonObject & config) /* Page selection */ lv_obj_t * page = get_page(pageid); if(!page) { - errorPrintln(F("HASP: %sPage ID not defined")); + Log.warning(F("HASP: Page ID %u not defined"), pageid); return; } /* save the current pageid */ @@ -1407,34 +1014,34 @@ void haspNewObject(const JsonObject & config) uint8_t parentid = config[F("parentid")].as(); parent_obj = FindObjFromId(page, parentid); if(!parent_obj) { - errorPrintln(F("HASP: %sParent ID not found")); - parent_obj = page; + Log.warning(F("HASP: Parent ID p[%u].b[%u] not found"), pageid, parentid); + parent_obj = page; // create on the page instead ?? } else { - debugPrintln(F("HASP: Parent ID found")); + Log.trace(F("HASP: Parent ID p[%u].b[%u] found"), pageid, parentid); } } /* Input cache and validation */ - int16_t min = config[F("min")].as(); - int16_t max = config[F("max")].as(); - int16_t val = config[F("val")].as(); - if(min >= max) { - min = 0; - max = 100; - } - bool enabled = config[F("enable")].as() | true; - lv_coord_t width = config[F("w")].as(); - lv_coord_t height = config[F("h")].as(); - if(width == 0) width = 32; - if(height == 0) height = 32; - uint8_t objid = config[F("objid")].as(); - uint8_t id = config[F("id")].as(); - uint8_t styleid = config[F("styleid")].as(); + // int16_t min = config[F("min")].as(); + // int16_t max = config[F("max")].as(); + // int16_t val = config[F("val")].as(); + // if(min >= max) { + // min = 0; + // max = 100; + //} + // bool enabled = config[F("enable")].as() | true; + // lv_coord_t width = config[F("w")].as(); + // lv_coord_t height = config[F("h")].as(); + // if(width == 0) width = 32; + // if(height == 0) height = 32; + uint8_t objid = config[F("objid")].as(); + uint8_t id = config[F("id")].as(); + // uint8_t styleid = config[F("styleid")].as(); /* Define Objects*/ lv_obj_t * obj = FindObjFromId(parent_obj, id); if(obj) { - warningPrintln(F("HASP: %sObject ID already exists!")); + Log.warning(F("HASP: Object ID %u already exists!"), id); return; } @@ -1442,30 +1049,35 @@ void haspNewObject(const JsonObject & config) switch(objid) { /* ----- Basic Objects ------ */ case LV_HASP_BUTTON: { - obj = lv_btn_create(parent_obj, NULL); - bool toggle = config[F("toggle")].as(); - haspSetToggle(obj, toggle); + obj = lv_btn_create(parent_obj, NULL); + // bool toggle = config[F("toggle")].as(); + // haspSetToggle(obj, toggle); // lv_btn_set_toggle(obj, toggle); // if(config[F("txt")]) { label = lv_label_create(obj, NULL); - lv_label_set_text(label, config[F("txt")].as().c_str()); - lv_obj_set_opa_scale_enable(label, true); - lv_obj_set_opa_scale(label, LV_OPA_COVER); + // lv_label_set_text(label, config[F("txt")].as().c_str()); + // haspSetOpacity(obj, LV_OPA_COVER); //} // lv_obj_set_event_cb(obj, btn_event_handler); + break; } case LV_HASP_CHECKBOX: { +#if LVGL7 + obj = lv_checkbox_create(parent_obj, NULL); + // if(config[F("txt")]) lv_checkbox_set_text(obj, config[F("txt")].as().c_str()); +#else obj = lv_cb_create(parent_obj, NULL); - if(config[F("txt")]) lv_cb_set_text(obj, config[F("txt")].as().c_str()); + // if(config[F("txt")]) lv_cb_set_text(obj, config[F("txt")].as().c_str()); +#endif lv_obj_set_event_cb(obj, checkbox_event_handler); break; } case LV_HASP_LABEL: { obj = lv_label_create(parent_obj, NULL); - if(config[F("txt")]) { - lv_label_set_text(obj, config[F("txt")].as().c_str()); - } + // if(config[F("txt")]) { + // lv_label_set_text(obj, config[F("txt")].as().c_str()); + // } /*if(styleid < sizeof labelStyles / sizeof *labelStyles) { debugPrintln(String(F("HASP: Styleid set to ")) + styleid); lv_label_set_style(obj, LV_LABEL_STYLE_MAIN, &labelStyles[styleid]); @@ -1497,22 +1109,25 @@ void haspNewObject(const JsonObject & config) case LV_HASP_CPICKER: { obj = lv_cpicker_create(parent_obj, NULL); // lv_cpicker_set_value(obj, (uint8_t)val); - bool rect = config[F("rect")].as(); - lv_cpicker_set_type(obj, rect ? LV_CPICKER_TYPE_RECT : LV_CPICKER_TYPE_DISC); + // bool rect = config[F("rect")].as(); + // lv_cpicker_set_type(obj, rect ? LV_CPICKER_TYPE_RECT : LV_CPICKER_TYPE_DISC); lv_obj_set_event_cb(obj, cpicker_event_handler); break; } #if LV_USE_PRELOAD != 0 case LV_HASP_PRELOADER: { +#if LVGL7 + obj = lv_spinner_create(parent_obj, NULL); +#else obj = lv_preload_create(parent_obj, NULL); +#endif break; } #endif /* ----- Range Objects ------ */ case LV_HASP_SLIDER: { obj = lv_slider_create(parent_obj, NULL); - lv_slider_set_range(obj, min, max); - lv_slider_set_value(obj, val, LV_ANIM_OFF); + lv_slider_set_range(obj, 0, 100); lv_obj_set_event_cb(obj, slider_event_handler); // bool knobin = config[F("knobin")].as() | true; // lv_slider_set_knob_in(obj, knobin); @@ -1520,48 +1135,68 @@ void haspNewObject(const JsonObject & config) } case LV_HASP_GAUGE: { obj = lv_gauge_create(parent_obj, NULL); - lv_gauge_set_range(obj, min, max); - lv_gauge_set_value(obj, val, LV_ANIM_OFF); + lv_gauge_set_range(obj, 0, 100); + // lv_gauge_set_value(obj, val, LV_ANIM_OFF); lv_obj_set_event_cb(obj, btn_event_handler); break; } case LV_HASP_BAR: { obj = lv_bar_create(parent_obj, NULL); - lv_bar_set_range(obj, min, max); - lv_bar_set_value(obj, val, LV_ANIM_OFF); + lv_bar_set_range(obj, 0, 100); + // lv_bar_set_value(obj, val, LV_ANIM_OFF); lv_obj_set_event_cb(obj, btn_event_handler); break; } case LV_HASP_LMETER: { +#if LVGL7 + obj = lv_linemeter_create(parent_obj, NULL); + lv_linemeter_set_range(obj, 0, 100); + // lv_linemeter_set_value(obj, val); +#else obj = lv_lmeter_create(parent_obj, NULL); - lv_lmeter_set_range(obj, min, max); - lv_lmeter_set_value(obj, val); + lv_lmeter_set_range(obj, 0, 100); + // lv_lmeter_set_value(obj, val); +#endif lv_obj_set_event_cb(obj, btn_event_handler); break; } /* ----- On/Off Objects ------ */ case LV_HASP_SWITCH: { - obj = lv_sw_create(parent_obj, NULL); - bool state = config[F("val")].as(); - if(state) lv_sw_on(obj, LV_ANIM_OFF); + // bool state = config[F("val")].as(); +#if LVGL7 + obj = lv_switch_create(parent_obj, NULL); + // if(state) lv_switch_on(obj, LV_ANIM_OFF); +#else + obj = lv_sw_create(parent_obj, NULL); + // if(state) lv_sw_on(obj, LV_ANIM_OFF); +#endif lv_obj_set_event_cb(obj, switch_event_handler); break; } case LV_HASP_LED: { obj = lv_led_create(parent_obj, NULL); - lv_led_set_bright(obj, config[F("val")].as() | 0); + // lv_led_set_bright(obj, config[F("val")].as() | 0); lv_obj_set_event_cb(obj, btn_event_handler); break; } /**/ case LV_HASP_DDLIST: { +#if LVGL7 + obj = lv_dropdown_create(parent_obj, NULL); + // if(config[F("txt")]) lv_dropdown_set_options(obj, config[F("txt")].as().c_str()); + // lv_dropdown_set_selected(obj, val); + // lv_dropdown_set_fix_width(obj, width); + lv_dropdown_set_draw_arrow(obj, true); + lv_dropdown_set_anim_time(obj, 250); +#else obj = lv_ddlist_create(parent_obj, NULL); - if(config[F("txt")]) lv_ddlist_set_options(obj, config[F("txt")].as().c_str()); - lv_ddlist_set_selected(obj, val); - lv_ddlist_set_fix_width(obj, width); + // if(config[F("txt")]) lv_ddlist_set_options(obj, config[F("txt")].as().c_str()); + // lv_ddlist_set_selected(obj, val); + // lv_ddlist_set_fix_width(obj, width); lv_ddlist_set_draw_arrow(obj, true); lv_ddlist_set_anim_time(obj, 250); +#endif 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); @@ -1570,10 +1205,13 @@ void haspNewObject(const JsonObject & config) case LV_HASP_ROLLER: { obj = lv_roller_create(parent_obj, NULL); bool infinite = config[F("infinite")].as(); - if(config[F("txt")]) lv_roller_set_options(obj, config[F("txt")].as().c_str(), infinite); - lv_roller_set_selected(obj, val, LV_ANIM_ON); - lv_roller_set_fix_width(obj, width); - lv_roller_set_visible_row_count(obj, config[F("rows")].as()); + // if(config[F("txt")]) lv_roller_set_options(obj, config[F("txt")].as().c_str(), infinite); + // lv_roller_set_selected(obj, val, LV_ANIM_ON); +#if LVGL7 + // lv_roller_set_fix_width(obj, width); +#else + // lv_roller_set_fix_width(obj, width); +#endif // lv_obj_align(obj, NULL, LV_ALIGN_IN_TOP_MID, 0, 20); lv_obj_set_event_cb(obj, roller_event_handler); break; @@ -1581,52 +1219,59 @@ void haspNewObject(const JsonObject & config) /* ----- Other Object ------ */ default: - errorPrintln(F("HASP: %sUnsupported Object ID")); + Log.warning(F("HASP: Unsupported Object ID %u"), objid); return; } if(!obj) { - errorPrintln(F("HASP: %sObject is NULL")); + Log.warning(F("HASP: Object is NULL, skipping...")); return; } - if(!config[F("opacity")].isNull()) { - uint8_t opacity = config[F("opacity")].as(); - lv_obj_set_opa_scale_enable(obj, opacity < 255); - lv_obj_set_opa_scale(obj, opacity < 255 ? opacity : 255); - } + /* + if(!config[F("opacity")].isNull()) { + uint8_t opacity = config[F("opacity")].as(); + haspSetOpacity(obj, opacity); + } - bool hidden = config[F("hidden")].as(); - lv_obj_set_hidden(obj, hidden); - lv_obj_set_click(obj, enabled); + bool hidden = config[F("hidden")].as(); + lv_obj_set_hidden(obj, hidden); + lv_obj_set_click(obj, enabled); - lv_obj_set_pos(obj, config[F("x")].as(), config[F("y")].as()); - lv_obj_set_width(obj, width); - if(objid != LV_HASP_DDLIST && objid != LV_HASP_ROLLER) - lv_obj_set_height(obj, height); // ddlist and roller have auto height + lv_obj_set_pos(obj, config[F("x")].as(), config[F("y")].as()); + lv_obj_set_width(obj, width); + */ + String k, v; lv_obj_set_user_data(obj, id); + for(JsonPair keyValue : config) { + k = keyValue.key().c_str(); + if(k != F("page") && k != F("id") && k != F("objid") && k != F("parentid")) { + v = keyValue.value().as(); + haspSetObjAttribute(obj, k, v); + Log.trace(F(" * %s => %s"), k.c_str(), v.c_str()); + } + } + /** testing start **/ lv_obj_user_data_t temp; if(!FindIdFromObj(obj, &pageid, &temp)) { - errorPrintln(F("HASP: %sLost track of the created object, not found!")); + Log.error(F("HASP: Lost track of the created object, not found!")); return; } /** testing end **/ - char msg[128]; lv_obj_type_t list; lv_obj_get_type(obj, &list); - sprintf_P(msg, PSTR("HASP: * p[%u].b[%u] = %s"), pageid, temp, list.type[0]); - debugPrintln(msg); + Log.verbose(F("HASP: * p[%u].b[%u] = %s"), pageid, temp, list.type[0]); /* Double-check */ lv_obj_t * test = FindObjFromId(pageid, (uint8_t)temp); if(test != obj) { - errorPrintln(F("HASP: %sObjects DO NOT match!")); + Log.error(F("HASP: Objects DO NOT match!")); } else { - // debugPrintln(F("Objects match!")); + Log.trace(F("Objects match!")); } } @@ -1635,17 +1280,16 @@ void haspLoadPage(String pages) char msg[128]; if(!SPIFFS.begin()) { - errorPrintln(String(F("HASP: %sFS not mounted. Failed to load ")) + pages.c_str()); + Log.error(F("HASP: FS not mounted. Failed to load %s"), pages.c_str()); return; } if(!SPIFFS.exists(pages)) { - errorPrintln(String(F("HASP: %sNon existing file ")) + pages.c_str()); + Log.error(F("HASP: Non existing file %s"), pages.c_str()); return; } - sprintf_P(msg, PSTR("HASP: Loading file %s"), pages.c_str()); - debugPrintln(msg); + Log.notice(F("HASP: Loading file %s"), pages.c_str()); File file = SPIFFS.open(pages, "r"); // ReadBufferingStream bufferingStream(file, 256); @@ -1653,15 +1297,13 @@ void haspLoadPage(String pages) uint8_t savedPage = current_page; while(deserializeJson(config, file) == DeserializationError::Ok) { - // serializeJson(config, Serial); - // Serial.println(); + serializeJson(config, Serial); + Serial.println(); haspNewObject(config.as()); } current_page = savedPage; - sprintf_P(msg, PSTR("HASP: File %s loaded"), pages.c_str()); - debugPrintln(msg); - + Log.notice(F("HASP: File %s loaded"), pages.c_str()); file.close(); } @@ -1676,9 +1318,7 @@ bool haspGetConfig(const JsonObject & settings) settings[FPSTR(F_CONFIG_ZIFONT)] = haspZiFontPath; settings[FPSTR(F_CONFIG_PAGES)] = haspPagesPath; - serializeJson(settings, Serial); - Serial.println(); - + configOutput(settings); return true; } @@ -1705,4 +1345,4 @@ bool haspSetConfig(const JsonObject & settings) } return changed; -} \ No newline at end of file +} diff --git a/src/hasp.h b/src/hasp.h index fa13c723..aabea76d 100644 --- a/src/hasp.h +++ b/src/hasp.h @@ -6,6 +6,11 @@ #ifndef HASP_H #define HASP_H +#include +#include "lvgl.h" +#include "hasp_conf.h" +#include "hasp_debug.h" + #ifdef __cplusplus extern "C" { #endif @@ -14,21 +19,6 @@ extern "C" { * INCLUDES *********************/ -#include -#include "lvgl.h" - -#include "hasp_conf.h" - -#include "hasp_debug.h" - -/* #ifdef LV_CONF_INCLUDE_SIMPLE -#include "lvgl.h" -#include "hasp_conf.h" -#else -#include "../lvgl/lvgl.h" -#include "hasp_conf.h" -#endif */ - #if HASP_USE_APP /********************* @@ -94,6 +84,9 @@ void haspWakeUp(void); bool haspGetConfig(const JsonObject & settings); bool haspSetConfig(const JsonObject & settings); +bool check_obj_type(const char * lvobjtype, lv_hasp_obj_type_t haspobjtype); +void haspSetToggle(lv_obj_t * obj, bool toggle); + /********************** * MACROS **********************/