diff --git a/src/hasp/hasp.cpp b/src/hasp/hasp.cpp index 8f35c96f..6296e10c 100644 --- a/src/hasp/hasp.cpp +++ b/src/hasp/hasp.cpp @@ -250,9 +250,11 @@ void haspProgressMsg(const char * msg) { lv_obj_t * bar = hasp_find_obj_from_parent_id(get_page_obj(255), (uint8_t)10); - char value_str[10]; - snprintf_P(value_str, sizeof(value_str), PSTR("value_str")); - hasp_process_obj_attribute(bar, value_str, msg, true); + if(bar) { + char value_str[10]; + snprintf_P(value_str, sizeof(value_str), PSTR("value_str")); + hasp_process_obj_attribute(bar, value_str, msg, true); + } lv_task_handler(); /* let the GUI do its work */ @@ -343,9 +345,13 @@ void haspSetup(void) if(haspThemeId == 9) haspThemeId = 5; // update old material id if(haspThemeId < 0 || haspThemeId > 5) haspThemeId = 1; // check bounds - lv_theme_t * th = NULL; - lv_theme_hasp_flag_t hasp_flags = LV_THEME_HASP_FLAG_LIGHT; + lv_theme_t * th = NULL; +#if(LV_USE_THEME_HASP == 1) + lv_theme_hasp_flag_t hasp_flags = LV_THEME_HASP_FLAG_LIGHT; +#endif +#if(LV_USE_THEME_MATERIAL == 1) lv_theme_material_flag_t material_flags = LV_THEME_MATERIAL_FLAG_LIGHT; +#endif switch(haspThemeId) { #if(LV_USE_THEME_EMPTY == 1) @@ -418,20 +424,9 @@ void haspSetup(void) Log.error(TAG_HASP, F("Theme could not be loaded")); } - /* ********** Theme Initializations ********** */ - // lv_style_list_t * list; - // static lv_style_t pagefont; - // lv_style_init(&pagefont); - // lv_style_set_text_font(&pagefont, LV_STATE_DEFAULT, defaultFont); - - // list = lv_obj_get_style_list(lv_disp_get_layer_top(NULL), LV_OBJ_PART_MAIN); - // _lv_style_list_add_style(list, &pagefont); - /* Create all screens using the theme */ for(int i = 0; i < (sizeof pages / sizeof *pages); i++) { pages[i] = lv_obj_create(NULL, NULL); - // list = lv_obj_get_style_list(pages[i], LV_OBJ_PART_MAIN); - // _lv_style_list_add_style(list, &pagefont); } #if HASP_USE_WIFI > 0 diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index 7f26cc87..3ea78b78 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -11,27 +11,14 @@ #include "hasp_object.h" #include "hasp_dispatch.h" #include "hasp_attribute.h" +#include "hasp_utilities.h" LV_FONT_DECLARE(unscii_8_icon); extern lv_font_t * haspFonts[8]; +extern const char ** btnmatrix_default_map; // memory pointer to lvgl default btnmatrix map static inline bool only_digits(const char * s); -/* 16-bit hashing function http://www.cse.yorku.ca/~oz/hash.html */ -/* all possible attributes are hashed and checked if they are unique */ -static uint16_t sdbm(const char * str) -{ - uint16_t hash = 0; - char c; - - // while(c = *str++) hash = c + (hash << 6) + (hash << 16) - hash; - while((c = *str++)) { - hash = tolower(c) + (hash << 6) - hash; - } - - return hash; -} - #if 0 static bool attribute_lookup_lv_property(uint16_t hash, uint8_t * prop) { @@ -268,6 +255,67 @@ lv_chart_series_t * lv_chart_get_series(lv_obj_t * chart, uint8_t ser_num) return ser; } +void btnmatrix_clear_map(lv_obj_t * obj) +{ + lv_btnmatrix_ext_t * ext = (lv_btnmatrix_ext_t *)lv_obj_get_ext_attr(obj); + if(ext->map_p && (ext->btn_cnt > 0)) { + const char ** ptr = ext->map_p; + + // The map exists and is not the default lvgl map anymore + if(ptr && btnmatrix_default_map && (ptr != btnmatrix_default_map)) { + lv_btnmatrix_set_map(obj, btnmatrix_default_map); // reset default btnmap + lv_mem_free(ptr); // destroy custom btnmap + } + } +} + +void line_clear_points(lv_obj_t * obj) +{ + lv_line_ext_t * ext = (lv_line_ext_t *)lv_obj_get_ext_attr(obj); + if(ext->point_array && (ext->point_num > 0)) { + const lv_point_t * ptr = ext->point_array; + lv_line_set_points(obj, NULL, 0); + lv_mem_free(ptr); + } +} + +static void line_set_points(lv_obj_t * obj, const char * payload) +{ + line_clear_points(obj); // delete pointmap + + // Create new points + // Reserve memory for JsonDocument + size_t maxsize = (128u * ((strlen(payload) / 128) + 1)) + 256; + DynamicJsonDocument doc(maxsize); + DeserializationError jsonError = deserializeJson(doc, payload); + + if(jsonError) { // Couldn't parse incoming JSON payload + return Log.warning(TAG_ATTR, F("JSON: Failed to parse incoming line points with error: %s"), jsonError.c_str()); + } + + JsonArray arr = doc.as(); // Parse payload + + size_t tot_len = sizeof(lv_point_t *) * (arr.size()); + lv_point_t * point_arr = (lv_point_t *)lv_mem_alloc(tot_len); + if(point_arr == NULL) { + return Log.error(TAG_ATTR, F("Out of memory while creating line points")); + } + memset(point_arr, 0, tot_len); + + size_t index = 0; + for(JsonVariant v : arr) { + JsonArray point = v.as(); // Parse point + point_arr[index].x = point[0].as(); + point_arr[index].y = point[1].as(); + Log.verbose(TAG_ATTR, F(" * Adding point %d: %d,%d"), index, point_arr[index].x, point_arr[index].y); + index++; + } + + lv_line_set_points(obj, point_arr, arr.size()); + + // TO DO : free & destroy previous pointlist! +} + // OK static inline lv_color_t haspLogColor(lv_color_t color) { @@ -822,7 +870,9 @@ static void hasp_local_style_attr(lv_obj_t * obj, const char * attr_p, uint16_t memccpy(str_p, payload, 0, len); lv_obj_set_style_local_value_str(obj, part, state, str_p); - // if(strlen(str) > 0) lv_mem_free(str); // BIG : Memory Leak ! / crashes + if(str != NULL) { +//lv_mem_free(str); // TODO : BIG Memory Leak ! / crashes + } } } else { hasp_out_str(obj, attr, lv_obj_get_style_value_str(obj, part)); @@ -1014,10 +1064,7 @@ static void hasp_process_btnmatrix_attribute(lv_obj_t * obj, const char * attr_p const char ** map_p = lv_btnmatrix_get_map_array(obj); if(update) { // Free previous map - // lv_mem_free(*map_p); - - // if(map_p != lv_btnmatrix_def_map) { - // } + btnmatrix_clear_map(obj); // delete btnmap // Create new map diff --git a/src/hasp/hasp_attribute.h b/src/hasp/hasp_attribute.h index d73309a5..3290cef4 100644 --- a/src/hasp/hasp_attribute.h +++ b/src/hasp/hasp_attribute.h @@ -6,7 +6,7 @@ #include "lvgl.h" #if LVGL_VERSION_MAJOR != 7 -#include "../lv_components.h" + #include "../lv_components.h" #endif #include "hasp_conf.h" @@ -20,6 +20,9 @@ extern "C" { // test lv_chart_series_t * lv_chart_get_series(lv_obj_t * chart, uint8_t ser_num); +void btnmatrix_clear_map(lv_obj_t * obj); +void line_clear_points(lv_obj_t * obj); + void hasp_process_obj_attribute(lv_obj_t * obj, const char * attr_p, const char * payload, bool update); #ifdef __cplusplus diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index 48b2a5b2..3f1ae2b5 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -281,12 +281,13 @@ void guiSetup() size_t guiVDBsize = 4 * 1024u; // 16 KBytes * 2 guiVdbBuffer1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); lv_disp_buf_init(&disp_buf, guiVdbBuffer1, NULL, guiVDBsize); - // guiVdbBuffer2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); + // guiVdbBuffer2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); // lv_disp_buf_init(&disp_buf, guiVdbBuffer1, guiVdbBuffer2, guiVDBsize); #else static lv_color_t * guiVdbBuffer1; size_t guiVDBsize = 16 * 1024u; // 32 KBytes * 2 - guiVdbBuffer1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_8BIT); + guiVdbBuffer1 = + (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, /*MALLOC_CAP_SPIRAM |*/ MALLOC_CAP_8BIT); lv_disp_buf_init(&disp_buf, guiVdbBuffer1, NULL, guiVDBsize); #endif @@ -365,12 +366,6 @@ void guiSetup() disp_drv.buffer = &disp_buf; disp_drv.flush_cb = gui_flush_cb; // static void that uses the appropriate driver - // #if defined(USE_FSMC) - // disp_drv.flush_cb = fsmc_ili9341_flush; - // #else - // disp_drv.flush_cb = tft_espi_flush; - // #endif - if(guiRotation == 0 || guiRotation == 2 || guiRotation == 4 || guiRotation == 6) { /* 1/3=Landscape or 0/2=Portrait orientation */ // Normal width & height @@ -382,7 +377,6 @@ void guiSetup() disp_drv.ver_res = TFT_WIDTH; } lv_disp_drv_register(&disp_drv); - guiStart(); // Ticker /* Initialize Global progress bar*/ lv_obj_t * bar = lv_bar_create(lv_layer_sys(), NULL); @@ -391,7 +385,7 @@ void guiSetup() lv_bar_set_value(bar, 10, LV_ANIM_OFF); lv_obj_set_size(bar, 200, 15); lv_obj_align(bar, lv_layer_sys(), LV_ALIGN_CENTER, 0, -10); - lv_obj_user_data_t udata = (lv_obj_user_data_t){10, 1, 0}; + lv_obj_user_data_t udata = (lv_obj_user_data_t){10, 0, 10}; lv_obj_set_user_data(bar, udata); lv_obj_set_style_local_value_color(bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_COLOR_WHITE); lv_obj_set_style_local_value_align(bar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_ALIGN_CENTER); @@ -431,6 +425,8 @@ void guiSetup() #endif lv_indev_set_cursor(mouse_indev, cursor); /*Connect the image object to the driver*/ } + + // guiStart(); // Ticker } void IRAM_ATTR guiLoop(void)