diff --git a/src/dev/esp32/esp32.cpp b/src/dev/esp32/esp32.cpp index 2dce3065..1fedba6b 100644 --- a/src/dev/esp32/esp32.cpp +++ b/src/dev/esp32/esp32.cpp @@ -16,7 +16,7 @@ #include "esp_adc_cal.h" #include "hasp_debug.h" -#include "hasp/hasp_utilities.h" +// #include "hasp/hasp_utilities.h" #define BACKLIGHT_CHANNEL 0 diff --git a/src/dev/esp8266/esp8266.cpp b/src/dev/esp8266/esp8266.cpp index 172a17a1..a428645b 100644 --- a/src/dev/esp8266/esp8266.cpp +++ b/src/dev/esp8266/esp8266.cpp @@ -10,7 +10,7 @@ #include "hasp_conf.h" #include "hasp_debug.h" -#include "hasp/hasp_utilities.h" +// #include "hasp/hasp_utilities.h" #define BACKLIGHT_CHANNEL 0 diff --git a/src/dev/posix/hasp_posix.cpp b/src/dev/posix/hasp_posix.cpp index 18e84404..41f7bc42 100644 --- a/src/dev/posix/hasp_posix.cpp +++ b/src/dev/posix/hasp_posix.cpp @@ -6,7 +6,7 @@ #include "hasp_posix.h" #include "hasp_conf.h" -#include "hasp/hasp_utilities.h" +// #include "hasp/hasp_utilities.h" #include "hasp_debug.h" #include "display/monitor.h" diff --git a/src/dev/stm32f4/stm32f4.cpp b/src/dev/stm32f4/stm32f4.cpp index f9bb220e..70df3c16 100644 --- a/src/dev/stm32f4/stm32f4.cpp +++ b/src/dev/stm32f4/stm32f4.cpp @@ -9,7 +9,7 @@ #include "hasp_conf.h" #include "hasp_debug.h" -#include "hasp/hasp_utilities.h" +// #include "hasp/hasp_utilities.h" #define BACKLIGHT_CHANNEL 0 diff --git a/src/dev/win32/hasp_win32.cpp b/src/dev/win32/hasp_win32.cpp index b6389d52..627b454a 100644 --- a/src/dev/win32/hasp_win32.cpp +++ b/src/dev/win32/hasp_win32.cpp @@ -9,7 +9,7 @@ #include "hasp_win32.h" #include "hasp_conf.h" -#include "hasp/hasp_utilities.h" +// #include "hasp/hasp_utilities.h" #include "hasp_debug.h" #include "display/monitor.h" diff --git a/src/hasp/hasp_attribute.cpp b/src/hasp/hasp_attribute.cpp index 7838bccd..e4de0ae7 100644 --- a/src/hasp/hasp_attribute.cpp +++ b/src/hasp/hasp_attribute.cpp @@ -735,7 +735,7 @@ static void hasp_local_style_attr(lv_obj_t* obj, const char* attr_p, uint16_t at // test_prop(attr_hash); hasp_attribute_get_part_state(obj, attr_p, attr, part, state); - attr_hash = Utilities::get_sdbm(attr); // attribute name without the index number + attr_hash = Parser::get_sdbm(attr); // attribute name without the index number /* ***** WARNING **************************************************** * when using hasp_out use attr_p for the original attribute name @@ -920,7 +920,7 @@ static void hasp_local_style_attr(lv_obj_t* obj, const char* attr_p, uint16_t at case ATTR_BORDER_SIDE: return attribute_border_side(obj, part, state, update, attr_p, (lv_border_side_t)var); case ATTR_BORDER_POST: - return attribute_border_post(obj, part, state, update, attr_p, Utilities::is_true(payload)); + return attribute_border_post(obj, part, state, update, attr_p, Parser::is_true(payload)); case ATTR_BORDER_OPA: return attribute_border_opa(obj, part, state, update, attr_p, (lv_opa_t)var); case ATTR_BORDER_COLOR: { @@ -987,7 +987,7 @@ static void hasp_local_style_attr(lv_obj_t* obj, const char* attr_p, uint16_t at case ATTR_LINE_DASH_GAP: return attribute_line_dash_gap(obj, part, state, update, attr_p, (lv_style_int_t)var); case ATTR_LINE_ROUNDED: - return attribute_line_rounded(obj, part, state, update, attr_p, Utilities::is_true(payload)); + return attribute_line_rounded(obj, part, state, update, attr_p, Parser::is_true(payload)); case ATTR_LINE_OPA: return attribute_line_opa(obj, part, state, update, attr_p, (lv_opa_t)var); case ATTR_LINE_COLOR: { @@ -1045,7 +1045,7 @@ static void hasp_local_style_attr(lv_obj_t* obj, const char* attr_p, uint16_t at /* Pattern attributes */ case ATTR_PATTERN_REPEAT: - return attribute_pattern_repeat(obj, part, state, update, attr_p, Utilities::is_true(payload)); + return attribute_pattern_repeat(obj, part, state, update, attr_p, Parser::is_true(payload)); case ATTR_PATTERN_OPA: return attribute_pattern_opa(obj, part, state, update, attr_p, (lv_opa_t)var); case ATTR_PATTERN_RECOLOR_OPA: @@ -1093,7 +1093,7 @@ static void hasp_process_arc_attribute(lv_obj_t* obj, const char* attr_p, uint16 case ATTR_ADJUSTABLE: if(update) { - bool toggle = Utilities::is_true(payload); + bool toggle = Parser::is_true(payload); lv_arc_set_adjustable(obj, toggle); lv_obj_set_event_cb(obj, toggle ? slider_event_handler : generic_event_handler); } else { @@ -1418,7 +1418,7 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attr_p, const char* p char* attr = (char*)attr_p; if(*attr == '.') attr++; // strip leading '.' - uint16_t attr_hash = Utilities::get_sdbm(attr); + uint16_t attr_hash = Parser::get_sdbm(attr); // LOG_VERBOSE(TAG_ATTR,"%s => %d", attr, attr_hash); /* 16-bit Hash Lookup Table */ @@ -1487,13 +1487,12 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attr_p, const char* p break; // attribute_found case ATTR_VIS: - update ? lv_obj_set_hidden(obj, !Utilities::is_true(payload)) + update ? lv_obj_set_hidden(obj, !Parser::is_true(payload)) : attr_out_int(obj, attr, !lv_obj_get_hidden(obj)); break; // attribute_found case ATTR_HIDDEN: - update ? lv_obj_set_hidden(obj, Utilities::is_true(payload)) - : attr_out_int(obj, attr, lv_obj_get_hidden(obj)); + update ? lv_obj_set_hidden(obj, Parser::is_true(payload)) : attr_out_int(obj, attr, lv_obj_get_hidden(obj)); break; // attribute_found case ATTR_TXT: // TODO: remove @@ -1517,7 +1516,7 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attr_p, const char* p break; // attribute_found case ATTR_VAL: - if(!hasp_process_obj_attribute_val(obj, attr, atoi(payload), Utilities::is_true(payload), update)) + if(!hasp_process_obj_attribute_val(obj, attr, atoi(payload), Parser::is_true(payload), update)) goto attribute_not_found; break; // attribute_found @@ -1535,8 +1534,7 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attr_p, const char* p break; // attribute_found case ATTR_ENABLED: - update ? lv_obj_set_click(obj, Utilities::is_true(payload)) - : attr_out_int(obj, attr, lv_obj_get_click(obj)); + update ? lv_obj_set_click(obj, Parser::is_true(payload)) : attr_out_int(obj, attr, lv_obj_get_click(obj)); break; // attribute_found case ATTR_SRC: @@ -1624,7 +1622,7 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attr_p, const char* p case ATTR_TOGGLE: if(check_obj_type(obj, LV_HASP_BUTTON)) { if(update) { - bool toggle = Utilities::is_true(payload); + bool toggle = Parser::is_true(payload); lv_btn_set_checkable(obj, toggle); lv_obj_set_event_cb(obj, toggle ? toggle_event_handler : generic_event_handler); } else { @@ -1704,13 +1702,15 @@ void hasp_process_obj_attribute(lv_obj_t* obj, const char* attr_p, const char* p } break; - case ATTR_MAP: // TODO: remove temp MAP, use options instead + case ATTR_ONE_CHECK: if(check_obj_type(obj, LV_HASP_BTNMATRIX)) { - my_btnmatrix_map_create(obj, payload); - } else { - goto attribute_not_found; + if(update) { + lv_btnmatrix_set_one_check(obj, Parser::is_true(payload)); + } else { + attr_out_int(obj, attr_p, lv_btnmatrix_get_one_check(obj)); + } } - break; // attribute_found + break; case ATTR_DELETE: if(!lv_obj_get_parent(obj)) { diff --git a/src/hasp/hasp_attribute.h b/src/hasp/hasp_attribute.h index c5b093de..5f2effba 100644 --- a/src/hasp/hasp_attribute.h +++ b/src/hasp/hasp_attribute.h @@ -316,7 +316,7 @@ _HASP_ATTRIBUTE(SCALE_END_LINE_WIDTH, scale_end_line_width, lv_style_int_t) #define ATTR_SHOW_SELECTED 56029 // Buttonmatrix -#define ATTR_MAP 45628 +#define ATTR_ONE_CHECK 45935 /* hasp user data */ #define ATTR_ACTION 42102 diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 442152c7..f26e7b72 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -216,9 +216,9 @@ static void dispatch_gpio(const char* topic, const char* payload) // val = gpio_get_relay_value(pin); } else { topic += 5; - if(Utilities::is_only_digits(topic)) { + if(Parser::is_only_digits(topic)) { pin = atoi(topic); - val = Utilities::is_true(payload); + val = Parser::is_true(payload); // gpio_set_relay_value(pin, val); return; } @@ -229,9 +229,9 @@ static void dispatch_gpio(const char* topic, const char* payload) if(strlen(payload) == 0) { } else { topic += 3; - if(Utilities::is_only_digits(topic)) { + if(Parser::is_only_digits(topic)) { pin = atoi(topic); - val = Utilities::is_true(payload); + val = Parser::is_true(payload); // gpio_set_led_value(pin, val); return; } @@ -243,9 +243,9 @@ static void dispatch_gpio(const char* topic, const char* payload) } else { topic += 3; - if(Utilities::is_only_digits(topic)) { + if(Parser::is_only_digits(topic)) { pin = atoi(topic); - val = Utilities::is_true(payload); + val = Parser::is_true(payload); // gpio_set_pwm_value(pin, val); return; } @@ -740,7 +740,7 @@ void dispatch_page(const char*, const char* page) } lv_scr_load_anim_t animation = LV_SCR_LOAD_ANIM_NONE; - if(Utilities::is_only_digits(page)) { + if(Parser::is_only_digits(page)) { uint8_t pageid = atoi(page); dispatch_set_page(pageid, animation); } else if(!strcasecmp_P(page, PSTR("prev"))) { @@ -801,7 +801,7 @@ void dispatch_moodlight(const char* topic, const char* payload) } else { if(!json[F("state")].isNull()) - moodlight.power = Utilities::is_true(json[F("state")].as().c_str()); + moodlight.power = Parser::is_true(json[F("state")].as().c_str()); if(!json["r"].isNull()) moodlight.r = json["r"].as(); if(!json["g"].isNull()) moodlight.g = json["g"].as(); @@ -850,7 +850,7 @@ void dispatch_backlight(const char*, const char* payload) { // Set the current state if(strlen(payload) != 0) { - bool power = Utilities::is_true(payload); + bool power = Parser::is_true(payload); if(haspDevice.get_backlight_power() != power) { haspDevice.set_backlight_power(power); diff --git a/src/hasp/hasp_object.cpp b/src/hasp/hasp_object.cpp index 5e90bdcc..d208fd34 100644 --- a/src/hasp/hasp_object.cpp +++ b/src/hasp/hasp_object.cpp @@ -381,7 +381,7 @@ void hasp_new_object(const JsonObject& config, uint8_t& saved_page_id) if(config[FPSTR(FP_OBJ)].isNull()) { return; // comments } else { - sdbm = Utilities::get_sdbm(config[FPSTR(FP_OBJ)].as()); + sdbm = Parser::get_sdbm(config[FPSTR(FP_OBJ)].as()); config.remove(FPSTR(FP_OBJ)); } } else { @@ -397,7 +397,7 @@ void hasp_new_object(const JsonObject& config, uint8_t& saved_page_id) obj = lv_btnmatrix_create(parent_obj, NULL); if(obj) { lv_btnmatrix_set_recolor(obj, true); - lv_obj_set_event_cb(obj, selector_event_handler); + 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 diff --git a/src/hasp/hasp_parser.cpp b/src/hasp/hasp_parser.cpp index e004cb00..535ba971 100644 --- a/src/hasp/hasp_parser.cpp +++ b/src/hasp/hasp_parser.cpp @@ -2,9 +2,12 @@ For full license information read the LICENSE file in the project folder */ #include +#include +#include #ifdef ARDUINO #include "pgmspace.h" +#include "Arduino.h" #endif #include "lvgl.h" @@ -42,7 +45,7 @@ bool Parser::haspPayloadToColor(const char* payload, lv_color32_t& color) } /* 16-bit RGB565 Color Scheme*/ - if(Utilities::is_only_digits(payload)) { + if(Parser::is_only_digits(payload)) { uint16_t c = atoi(payload); /* Initial colors */ @@ -60,7 +63,7 @@ bool Parser::haspPayloadToColor(const char* payload, lv_color32_t& color) /* Named colors */ size_t numColors = sizeof(haspNamedColors) / sizeof(haspNamedColors[0]); - uint16_t sdbm = Utilities::get_sdbm(payload); + uint16_t sdbm = Parser::get_sdbm(payload); #ifdef ARDUINO for(size_t i = 0; i < numColors; i++) { @@ -140,4 +143,57 @@ void Parser::get_event_name(uint8_t eventid, char* buffer, size_t size) default: memcpy_P(buffer, PSTR("unknown"), size); } -} \ No newline at end of file +} + +/* 16-bit hashing function http://www.cse.yorku.ca/~oz/hash.html */ +/* all possible attributes are hashed and checked if they are unique */ +uint16_t Parser::get_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; +} + +bool Parser::is_true(const char* s) +{ + return (!strcasecmp_P(s, PSTR("true")) || !strcasecmp_P(s, PSTR("on")) || !strcasecmp_P(s, PSTR("yes")) || + !strcmp_P(s, PSTR("1"))); +} + +bool Parser::is_only_digits(const char* s) +{ + size_t digits = 0; + while(*(s + digits) != '\0' && isdigit(*(s + digits))) { + digits++; + } + return strlen(s) == digits; +} + +int Parser::format_bytes(size_t filesize, char* buf, size_t len) +{ + if(filesize < 1024) return snprintf_P(buf, len, PSTR("%d B"), filesize); + + char labels[] = "kMGT"; + filesize = filesize * 10 / 1024; // multiply by 10 for 1 decimal place + int unit = 0; + + while(filesize >= 10240 && unit < sizeof(labels) - 1) { // it is multiplied by 10 + unit++; + filesize = filesize / 1024; + } + + return snprintf_P(buf, len, PSTR("%d.%d %ciB"), filesize / 10, filesize % 10, labels[unit]); +} + +#ifndef ARDUINO +long map(long x, long in_min, long in_max, long out_min, long out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} +#endif \ No newline at end of file diff --git a/src/hasp/hasp_parser.h b/src/hasp/hasp_parser.h index 2550d74b..28c7c01c 100644 --- a/src/hasp/hasp_parser.h +++ b/src/hasp/hasp_parser.h @@ -13,8 +13,16 @@ class Parser { static bool haspPayloadToColor(const char* payload, lv_color32_t& color); static bool get_event_state(uint8_t eventid); static void get_event_name(uint8_t eventid, char* buffer, size_t size); + static uint16_t get_sdbm(const char* str); + static bool is_true(const char* s); + static bool is_only_digits(const char* s); + static int format_bytes(size_t filesize, char* buf, size_t len); }; +#ifndef ARDUINO +long map(long x, long in_min, long in_max, long out_min, long out_max); +#endif + /* Named COLOR attributes */ #define ATTR_RED 177 #define ATTR_TAN 7873 diff --git a/src/hasp/hasp_utilities.cpp b/src/hasp/hasp_utilities.cpp index 1787c2a2..42d4ccd8 100644 --- a/src/hasp/hasp_utilities.cpp +++ b/src/hasp/hasp_utilities.cpp @@ -1,62 +1,3 @@ -#include -#include - -#ifdef ARDUINO -#include "Arduino.h" -#endif #include "hasp_conf.h" #include "hasp_utilities.h" - -/* 16-bit hashing function http://www.cse.yorku.ca/~oz/hash.html */ -/* all possible attributes are hashed and checked if they are unique */ -uint16_t Utilities::get_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; -} - -bool Utilities::is_true(const char* s) -{ - return (!strcasecmp_P(s, PSTR("true")) || !strcasecmp_P(s, PSTR("on")) || !strcasecmp_P(s, PSTR("yes")) || - !strcmp_P(s, PSTR("1"))); -} - -bool Utilities::is_only_digits(const char* s) -{ - size_t digits = 0; - while(*(s + digits) != '\0' && isdigit(*(s + digits))) { - digits++; - } - return strlen(s) == digits; -} - -int Utilities::format_bytes(size_t filesize, char* buf, size_t len) -{ - if(filesize < 1024) return snprintf_P(buf, len, PSTR("%d B"), filesize); - - char labels[] = "kMGT"; - filesize = filesize * 10 / 1024; // multiply by 10 for 1 decimal place - int unit = 0; - - while(filesize >= 10240 && unit < sizeof(labels) - 1) { // it is multiplied by 10 - unit++; - filesize = filesize / 1024; - } - - return snprintf_P(buf, len, PSTR("%d.%d %ciB"), filesize / 10, filesize % 10, labels[unit]); -} - -#ifndef ARDUINO -long map(long x, long in_min, long in_max, long out_min, long out_max) -{ - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; -} -#endif \ No newline at end of file diff --git a/src/hasp/hasp_utilities.h b/src/hasp/hasp_utilities.h index a94a6f80..53ef86ff 100644 --- a/src/hasp/hasp_utilities.h +++ b/src/hasp/hasp_utilities.h @@ -7,16 +7,6 @@ #include class Utilities { - - public: - static uint16_t get_sdbm(const char* str); - static bool is_true(const char* s); - static bool is_only_digits(const char* s); - static int format_bytes(size_t filesize, char* buf, size_t len); }; -#ifndef ARDUINO -long map(long x, long in_min, long in_max, long out_min, long out_max); -#endif - #endif \ No newline at end of file diff --git a/src/hasplib.h b/src/hasplib.h index 989b53c0..6a447243 100644 --- a/src/hasplib.h +++ b/src/hasplib.h @@ -7,7 +7,6 @@ #include "hasp/hasp_object.h" #include "hasp/hasp_page.h" #include "hasp/hasp_parser.h" -#include "hasp/hasp_utilities.h" #include "hasp/hasp_lvfs.h" #include "hasp/lv_theme_hasp.h" \ No newline at end of file diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 8d64d5cb..54fcdbfb 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -19,7 +19,7 @@ #include "hasp_config.h" #include "hal/hasp_hal.h" -#include "hasp/hasp_utilities.h" +// #include "hasp/hasp_utilities.h" #include "hasp/hasp_dispatch.h" #include "hasp/hasp_page.h" #include "hasp/hasp.h" @@ -567,7 +567,7 @@ void webHandleInfo() httpMessage += F("s"); httpMessage += F("
Free Memory: "); - Utilities::format_bytes(haspDevice.get_free_heap(), size_buf, sizeof(size_buf)); + Parser::format_bytes(haspDevice.get_free_heap(), size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
Memory Fragmentation: "); httpMessage += String(haspDevice.get_heap_fragmentation()); @@ -575,10 +575,10 @@ void webHandleInfo() #if ARDUINO_ARCH_ESP32 if(psramFound()) { httpMessage += F("
Free PSRam: "); - Utilities::format_bytes(ESP.getFreePsram(), size_buf, sizeof(size_buf)); + Parser::format_bytes(ESP.getFreePsram(), size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
PSRam Size: "); - Utilities::format_bytes(ESP.getPsramSize(), size_buf, sizeof(size_buf)); + Parser::format_bytes(ESP.getPsramSize(), size_buf, sizeof(size_buf)); httpMessage += size_buf; } #endif @@ -587,10 +587,10 @@ void webHandleInfo() lv_mem_monitor_t mem_mon; lv_mem_monitor(&mem_mon); httpMessage += F("

LVGL Memory: "); - Utilities::format_bytes(mem_mon.total_size, size_buf, sizeof(size_buf)); + Parser::format_bytes(mem_mon.total_size, size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
LVGL Free: "); - Utilities::format_bytes(mem_mon.free_size, size_buf, sizeof(size_buf)); + Parser::format_bytes(mem_mon.free_size, size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
LVGL Fragmentation: "); httpMessage += mem_mon.frag_pct; @@ -693,15 +693,15 @@ void webHandleInfo() #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) httpMessage += F("
Flash Chip Size: "); - Utilities::format_bytes(ESP.getFlashChipSize(), size_buf, sizeof(size_buf)); + Parser::format_bytes(ESP.getFlashChipSize(), size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
Program Size: "); - Utilities::format_bytes(ESP.getSketchSize(), size_buf, sizeof(size_buf)); + Parser::format_bytes(ESP.getSketchSize(), size_buf, sizeof(size_buf)); httpMessage += size_buf; httpMessage += F("
Free Program Space: "); - Utilities::format_bytes(ESP.getFreeSketchSpace(), size_buf, sizeof(size_buf)); + Parser::format_bytes(ESP.getFreeSketchSpace(), size_buf, sizeof(size_buf)); httpMessage += size_buf; #endif