Merge Utilities class into Parser class

This commit is contained in:
fvanroie 2021-04-15 04:30:22 +02:00
parent c7eb8bb55c
commit 3b0aebf02c
15 changed files with 111 additions and 117 deletions

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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)) {

View File

@ -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

View File

@ -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<std::string>().c_str());
moodlight.power = Parser::is_true(json[F("state")].as<std::string>().c_str());
if(!json["r"].isNull()) moodlight.r = json["r"].as<uint8_t>();
if(!json["g"].isNull()) moodlight.g = json["g"].as<uint8_t>();
@ -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);

View File

@ -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<const char*>());
sdbm = Parser::get_sdbm(config[FPSTR(FP_OBJ)].as<const char*>());
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

View File

@ -2,9 +2,12 @@
For full license information read the LICENSE file in the project folder */
#include <cstdlib>
#include <cctype>
#include <string>
#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);
}
}
}
/* 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

View File

@ -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

View File

@ -1,62 +1,3 @@
#include <cctype>
#include <string>
#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

View File

@ -7,16 +7,6 @@
#include <string>
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

View File

@ -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"

View File

@ -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("<br/><b>Free Memory: </b>");
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("<br/><b>Memory Fragmentation: </b>");
httpMessage += String(haspDevice.get_heap_fragmentation());
@ -575,10 +575,10 @@ void webHandleInfo()
#if ARDUINO_ARCH_ESP32
if(psramFound()) {
httpMessage += F("<br/><b>Free PSRam: </b>");
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("<br/><b>PSRam Size: </b>");
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("</p><p><b>LVGL Memory: </b>");
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("<br/><b>LVGL Free: </b>");
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("<br/><b>LVGL Fragmentation: </b>");
httpMessage += mem_mon.frag_pct;
@ -693,15 +693,15 @@ void webHandleInfo()
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
httpMessage += F("<br/><b>Flash Chip Size: </b>");
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("</br><b>Program Size: </b>");
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("<br/><b>Free Program Space: </b>");
Utilities::format_bytes(ESP.getFreeSketchSpace(), size_buf, sizeof(size_buf));
Parser::format_bytes(ESP.getFreeSketchSpace(), size_buf, sizeof(size_buf));
httpMessage += size_buf;
#endif