From 1c86a3a34fdae9af21e09272f47817895ff89250 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Mon, 23 May 2022 16:01:01 +0200 Subject: [PATCH] Fix value_str issue with parts and states --- src/hasp/hasp_attribute.cpp | 11 ++-- src/hasp/hasp_attribute_helper.h | 105 +++++++++++++++---------------- src/hasp/hasp_parser.cpp | 3 +- 3 files changed, 57 insertions(+), 62 deletions(-) diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index 3eb80d347..8478828e7 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -51,7 +51,7 @@ void my_btnmatrix_map_clear(lv_obj_t* obj) lv_btnmatrix_ext_t* ext = (lv_btnmatrix_ext_t*)lv_obj_get_ext_attr(obj); const char** map_p_tmp = ext->map_p; // store current pointer - LOG_VERBOSE(TAG_ATTR, "%s %d %x btn_cnt: %d", __FILE__, __LINE__, map_p_tmp, ext->btn_cnt); + LOG_DEBUG(TAG_ATTR, "%s %d %x btn_cnt: %d", __FILE__, __LINE__, map_p_tmp, ext->btn_cnt); if(ext->map_p && (ext->btn_cnt > 0)) { @@ -147,7 +147,7 @@ const char** my_map_create(const char* payload) } map_data_str[index] = buffer_addr + pos; // save pointer to the last \0 byte - LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); + LOG_DEBUG(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); return map_data_str; } @@ -327,7 +327,7 @@ static void hasp_attribute_get_part_state_new(lv_obj_t* obj, const char* attr_in uint8_t state_num = index % 10; uint8_t part_num = index - state_num; - LOG_VERBOSE(TAG_ATTR, F("Parsed %s to %s with part %d and state %d"), attr_in, attr_out, part_num, state_num); + LOG_DEBUG(TAG_ATTR, F("Parsed %s to %s with part %d and state %d"), attr_in, attr_out, part_num, state_num); #if(LV_SLIDER_PART_INDIC != LV_SWITCH_PART_INDIC) || (LV_SLIDER_PART_KNOB != LV_SWITCH_PART_KNOB) || \ (LV_SLIDER_PART_BG != LV_SWITCH_PART_BG) || (LV_SLIDER_PART_INDIC != LV_ARC_PART_INDIC) || \ @@ -978,7 +978,7 @@ static hasp_attribute_type_t hasp_local_style_attr(lv_obj_t* obj, const char* at if(update) { my_obj_set_value_str_text(obj, part, state, payload); } else { - attr_out_str(obj, attr, lv_obj_get_style_value_str(obj, part)); + attr_out_str(obj, attr, my_obj_get_value_str_text(obj, part, state)); } return HASP_ATTR_TYPE_METHOD_OK; } @@ -1093,8 +1093,7 @@ static hasp_attribute_type_t hasp_process_spinner_attribute(lv_obj_t* obj, uint1 return HASP_ATTR_TYPE_INT; } -static hasp_attribute_type_t hasp_process_slider_attribute(lv_obj_t* obj, uint16_t attr_hash, int32_t& val, - bool update) +static hasp_attribute_type_t hasp_process_slider_attribute(lv_obj_t* obj, uint16_t attr_hash, int32_t& val, bool update) { // We already know it's a slider object switch(attr_hash) { diff --git a/src/hasp/hasp_attribute_helper.h b/src/hasp/hasp_attribute_helper.h index 6129aec09..906f48a66 100644 --- a/src/hasp/hasp_attribute_helper.h +++ b/src/hasp/hasp_attribute_helper.h @@ -345,6 +345,22 @@ static inline void my_btn_set_text(lv_obj_t* obj, const char* value) } } +/** + * Get the value_str for an object part and state. + * @param obj pointer to a object + * @param result text '\0' terminated character string. + */ +const char* my_obj_get_value_str_text(lv_obj_t* obj, uint8_t part, lv_state_t state) +{ + lv_state_t old_state = lv_obj_get_state(obj, part); + lv_obj_set_state(obj, state); + lv_obj_refresh_style(obj, part, LV_STYLE_VALUE_STR); + const char* value_str_p = lv_obj_get_style_value_str(obj, part); + lv_obj_set_state(obj, old_state); + lv_obj_refresh_style(obj, part, LV_STYLE_VALUE_STR); + return value_str_p; +} + /** * Set a new value_str for an object. Memory will be allocated to store the text by the object. * @param obj pointer to a object @@ -353,71 +369,50 @@ static inline void my_btn_set_text(lv_obj_t* obj, const char* value) void my_obj_set_value_str_text(lv_obj_t* obj, uint8_t part, lv_state_t state, const char* text) { // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - lv_state_t old_state = lv_obj_get_state(obj, part); - lv_obj_set_state(obj, state); - const void* value_str_p = lv_obj_get_style_value_str(obj, part); - lv_obj_set_state(obj, old_state); - lv_obj_invalidate(obj); - if(text == NULL || text[0] == 0) { - // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - lv_obj_set_style_local_value_str(obj, part, state, NULL); - lv_mem_free(value_str_p); - // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - return; + // the lower priority state to check inheritance of the value_str against + lv_state_t prev_state; + switch(state) { + case LV_STATE_CHECKED: + case(LV_STATE_PRESSED + LV_STATE_DEFAULT): + case(LV_STATE_DISABLED + LV_STATE_DEFAULT): + prev_state = LV_STATE_DEFAULT; + break; + case(LV_STATE_DISABLED + LV_STATE_CHECKED): + case(LV_STATE_PRESSED + LV_STATE_CHECKED): + prev_state = LV_STATE_CHECKED; + break; } - LV_ASSERT_STR(text); - - if(value_str_p == NULL) { - /*Get the size of the text*/ - size_t len = strlen(text) + 1; - - /*Allocate space for the new text*/ - // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - value_str_p = (char*)lv_mem_alloc(len); - LV_ASSERT_MEM(value_str_p); - if(value_str_p == NULL) return; - - // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - strncpy((char*)value_str_p, text, len); - lv_obj_set_style_local_value_str(obj, part, state, (char*)value_str_p); - // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - return; + const char* prev_value_str_p = NULL; + if(state != LV_STATE_DEFAULT) { + prev_value_str_p = my_obj_get_value_str_text(obj, part, prev_state); } - // lv_obj_set_style_local_value_str(obj, part, state, str_p); - - if(value_str_p == text) { - /*If set its own text then reallocate it (maybe its size changed)*/ - LOG_DEBUG(TAG_ATTR, "%s %d", __FILE__, __LINE__); - return; // don't touch the data - - // value_str_p = lv_mem_realloc(value_str_p, strlen(text) + 1); + // The value_str pointer of the current state to check inheritance + lv_obj_set_state(obj, state); + lv_obj_refresh_style(obj, part, LV_STYLE_VALUE_STR); + const char* curr_value_str_p = lv_obj_get_style_value_str(obj, part); - // LV_ASSERT_MEM(value_str_p); - // if(value_str_p == NULL) return; - } else { - /*Free the old text*/ - if(value_str_p != NULL) { - // LOG_DEBUG(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - lv_mem_free(value_str_p); - value_str_p = NULL; - // LOG_DEBUG(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); - } + // Only free if there is no value_str state inheritance + if(prev_value_str_p != curr_value_str_p && curr_value_str_p != NULL) { + LOG_DEBUG(TAG_ATTR, "Releasing %s", curr_value_str_p); + lv_mem_free(curr_value_str_p); + } - /*Get the size of the text*/ - size_t len = strlen(text) + 1; + /*Get the size of the new text*/ + size_t len = 0; + if(text != NULL && text[0] != '\0') len = strlen(text) + 1; - /*Allocate space for the new text*/ - value_str_p = lv_mem_alloc(len); - LV_ASSERT_MEM(value_str_p); - if(value_str_p != NULL) strcpy((char*)value_str_p, text); - lv_obj_set_style_local_value_str(obj, part, state, (char*)value_str_p); - } + /*Allocate space for the new text*/ + char* str_p = NULL; + if(len > 0) str_p = (char*)lv_mem_alloc(len); + if(str_p != NULL) strncpy(str_p, text, len); + lv_obj_set_style_local_value_str(obj, part, state, str_p); - // LOG_VERBOSE(TAG_ATTR, F("%s %d"), __FILE__, __LINE__); + lv_obj_set_state(obj, old_state); + lv_obj_refresh_style(obj, part, LV_STYLE_VALUE_STR); } void my_list_set_options(lv_obj_t* obj, const char* payload) diff --git a/src/hasp/hasp_parser.cpp b/src/hasp/hasp_parser.cpp index a2f6bbf49..ac5f1b1ab 100644 --- a/src/hasp/hasp_parser.cpp +++ b/src/hasp/hasp_parser.cpp @@ -153,7 +153,8 @@ void Parser::get_event_name(uint8_t eventid, char* buffer, size_t size) uint16_t Parser::get_sdbm(const char* str) { uint16_t hash = 0; - while(char c = *str++) hash = tolower(c) + (hash << 6) - hash; // case insensitive + while(char c = tolower(*str++)) + if(c > 57 || c < 48) hash = c + (hash << 6) - hash; // exclude numbers which can cause collisions return hash; }