From dd385b5868f7d23c8e6c652af43701aed468689a Mon Sep 17 00:00:00 2001 From: fvanroie Date: Tue, 11 Feb 2020 00:38:29 +0100 Subject: [PATCH] Add labelStyles support --- lib/lv_lib_zifont/lv_zifont.cpp | 31 ++++++++++++---------- lib/lv_lib_zifont/lv_zifont.h | 2 +- src/hasp.cpp | 46 +++++++++++++++++++++++++-------- src/hasp.h | 2 +- 4 files changed, 55 insertions(+), 26 deletions(-) diff --git a/lib/lv_lib_zifont/lv_zifont.cpp b/lib/lv_lib_zifont/lv_zifont.cpp index 352dec98..f9efd64e 100644 --- a/lib/lv_lib_zifont/lv_zifont.cpp +++ b/lib/lv_lib_zifont/lv_zifont.cpp @@ -101,22 +101,27 @@ bool openFont(File & file, const char * filename) { file = SPIFFS.open(filename, "r"); if(!file) { - errorPrintln(String(F("FONT: %sOpening font: ")) + String(filename)); + String error = String(F("FONT: %sOpening font: ")); + error += String(filename); + errorPrintln(error); return false; } return true; } -int lv_zifont_font_init(lv_font_t * font, const char * font_path, uint16_t size) +int lv_zifont_font_init(lv_font_t ** font, const char * font_path, uint16_t size) { charInBuffer = 0; // invalidate any previous cache + if(!*font) *font = (lv_font_t *)lv_mem_alloc(sizeof(lv_font_t)); + LV_ASSERT_MEM(*font); + lv_font_fmt_zifont_dsc_t * dsc; - if(!font->dsc) { + if(!(*font)->dsc) { dsc = (lv_font_fmt_zifont_dsc_t *)lv_mem_alloc(sizeof(lv_font_fmt_zifont_dsc_t)); LV_ASSERT_MEM(dsc); } else { - dsc = (lv_font_fmt_zifont_dsc_t *)font->dsc; + dsc = (lv_font_fmt_zifont_dsc_t *)(*font)->dsc; } if(dsc == NULL) return ZIFONT_ERROR_OUT_OF_MEMORY; int error = 0; @@ -204,16 +209,16 @@ int lv_zifont_font_init(lv_font_t * font, const char * font_path, uint16_t size) dsc->last_glyph_dsc = NULL; dsc->last_glyph_id = 0; - font->get_glyph_dsc = lv_font_get_glyph_dsc_fmt_zifont; /*Function pointer to get glyph's data*/ - font->get_glyph_bitmap = lv_font_get_bitmap_fmt_zifont; /*Function pointer to get glyph's bitmap*/ - font->line_height = dsc->CharHeight; /*The maximum line height required by the font*/ - font->base_line = 0; /*Baseline measured from the bottom of the line*/ - font->dsc = dsc; /* header data struct */ /*The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */ - font->subpx = 0; + (*font)->get_glyph_dsc = lv_font_get_glyph_dsc_fmt_zifont; /*Function pointer to get glyph's data*/ + (*font)->get_glyph_bitmap = lv_font_get_bitmap_fmt_zifont; /*Function pointer to get glyph's bitmap*/ + (*font)->line_height = dsc->CharHeight; /*The maximum line height required by the font*/ + (*font)->base_line = 0; /*Baseline measured from the bottom of the line*/ + (*font)->dsc = dsc; /* header data struct */ /*The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */ + (*font)->subpx = 0; - if(font->user_data != (char *)font_path) { - if(font->user_data) free(font->user_data); - font->user_data = (char *)font_path; + if((*font)->user_data != (char *)font_path) { + if((*font)->user_data) free((*font)->user_data); + (*font)->user_data = (char *)font_path; } return ZIFONT_NO_ERROR; } diff --git a/lib/lv_lib_zifont/lv_zifont.h b/lib/lv_lib_zifont/lv_zifont.h index 438f1bba..fd56e807 100644 --- a/lib/lv_lib_zifont/lv_zifont.h +++ b/lib/lv_lib_zifont/lv_zifont.h @@ -86,7 +86,7 @@ typedef struct * GLOBAL PROTOTYPES **********************/ int lv_zifont_init(void); -int lv_zifont_font_init(lv_font_t * font, const char * font_path, uint16_t size); +int lv_zifont_font_init(lv_font_t ** font, const char * font_path, uint16_t size); /********************** * MACROS diff --git a/src/hasp.cpp b/src/hasp.cpp index adc29bf5..90e25590 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -422,9 +422,7 @@ bool haspGetObjAttribute(lv_obj_t * obj, String strAttr, std::string & strPayloa 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) != 255 ? 0 : 1).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; @@ -521,7 +519,7 @@ void haspSetObjAttribute(lv_obj_t * obj, String strAttr, String strPayload) 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); else if(check_obj_type(list.type[0], LV_HASP_LED)) - val == 0 ? lv_led_off(obj) : lv_led_on(obj); + lv_led_set_bright(obj, (uint8_t)val); else if(check_obj_type(list.type[0], LV_HASP_GAUGE)) lv_gauge_set_value(obj, 0, intval); else if(check_obj_type(list.type[0], LV_HASP_DDLIST)) @@ -771,10 +769,9 @@ void haspSetup(JsonObject settings) // static lv_font_t * // my_font = (lv_font_t *)lv_mem_alloc(sizeof(lv_font_t)); - defaultFont = (lv_font_t *)lv_mem_alloc(sizeof(lv_font_t)); lv_zifont_init(); - if(lv_zifont_font_init(defaultFont, haspZiFontPath, 24) != 0) { + 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 } @@ -847,6 +844,29 @@ void haspSetup(JsonObject settings) // 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"))); + defaultFont = NULL; // Use default font + } + if(lv_zifont_font_init(&haspFonts[1], "/fonts/HMI FiraSans 24.zi", 24) != 0) { + errorPrintln(String(F("HASP: %sFailed to set the custom font to 1"))); + defaultFont = NULL; // Use default font + } + if(lv_zifont_font_init(&haspFonts[2], "/fonts/HMI AbrilFatface 24.zi", 24) != 0) { + errorPrintln(String(F("HASP: %sFailed to set the custom font to 2"))); + defaultFont = NULL; // Use default font + } + + for(int i = 0; i < 3; i++) { + lv_style_copy(&labelStyles[i], &lv_style_pretty_color); + labelStyles[i].text.font = haspFonts[i]; + } + /* lv_obj_t * obj; @@ -1196,8 +1216,9 @@ void haspNewObject(const JsonObject & config) 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 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; @@ -1228,6 +1249,10 @@ void haspNewObject(const JsonObject & config) if(config[F("txt")]) { lv_label_set_text(obj, config[F("txt")].as().c_str()); } + if(styleid < sizeof labelStyles / sizeof *labelStyles) { + debugPrintln("HASP: Styleid set to " + styleid); + lv_label_set_style(obj, LV_LABEL_STYLE_MAIN, &labelStyles[styleid]); + } /* click area padding */ uint8_t padh = config[F("padh")].as(); uint8_t padv = config[F("padv")].as(); @@ -1305,9 +1330,8 @@ void haspNewObject(const JsonObject & config) break; } case LV_HASP_LED: { - obj = lv_led_create(parent_obj, NULL); - bool state = config[F("val")].as(); - if(state) lv_led_on(obj); + obj = lv_led_create(parent_obj, NULL); + lv_led_set_bright(obj, config[F("val")].as() | 0); lv_obj_set_event_cb(obj, btn_event_handler); break; } diff --git a/src/hasp.h b/src/hasp.h index 3fbe6750..83d571d2 100644 --- a/src/hasp.h +++ b/src/hasp.h @@ -29,7 +29,7 @@ extern "C" { #include "hasp_conf.h" #endif */ -#if LV_USE_HASP +#if HASP_USE_APP /********************* * DEFINES