Add labelStyles support

This commit is contained in:
fvanroie 2020-02-11 00:38:29 +01:00
parent 35ec178a2c
commit dd385b5868
4 changed files with 55 additions and 26 deletions

View File

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

View File

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

View File

@ -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<lv_coord_t>();
if(width == 0) width = 32;
if(height == 0) height = 32;
uint8_t objid = config[F("objid")].as<uint8_t>();
uint8_t id = config[F("id")].as<uint8_t>();
uint8_t objid = config[F("objid")].as<uint8_t>();
uint8_t id = config[F("id")].as<uint8_t>();
uint8_t styleid = config[F("styleid")].as<uint8_t>();
/* 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<String>().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>();
uint8_t padv = config[F("padv")].as<uint8_t>();
@ -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<bool>();
if(state) lv_led_on(obj);
obj = lv_led_create(parent_obj, NULL);
lv_led_set_bright(obj, config[F("val")].as<uint8_t>() | 0);
lv_obj_set_event_cb(obj, btn_event_handler);
break;
}

View File

@ -29,7 +29,7 @@ extern "C" {
#include "hasp_conf.h"
#endif */
#if LV_USE_HASP
#if HASP_USE_APP
/*********************
* DEFINES