mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-28 13:36:27 +08:00
lv_theme: add live update feature
This commit is contained in:
+10
-5
@@ -24,11 +24,6 @@
|
|||||||
#define LV_MEM_CUSTOM_ALLOC malloc /*Wrapper to malloc*/
|
#define LV_MEM_CUSTOM_ALLOC malloc /*Wrapper to malloc*/
|
||||||
#define LV_MEM_CUSTOM_FREE free /*Wrapper to free*/
|
#define LV_MEM_CUSTOM_FREE free /*Wrapper to free*/
|
||||||
#endif /*LV_MEM_CUSTOM*/
|
#endif /*LV_MEM_CUSTOM*/
|
||||||
#define LV_TICK_CUSTOM 0 /*1: use a custom tick source (removing the need to manually update the tick with `lv_tick_inc`) */
|
|
||||||
#if LV_TICK_CUSTOM == 1
|
|
||||||
#define LV_TICK_CUSTOM_INCLUDE "Arduino.h" /*Header for the sys time function*/
|
|
||||||
#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) /*Expression evaluating to current systime in ms*/
|
|
||||||
#endif /*LV_TICK_CUSTOM*/
|
|
||||||
|
|
||||||
/*===================
|
/*===================
|
||||||
Graphical settings
|
Graphical settings
|
||||||
@@ -95,6 +90,14 @@
|
|||||||
#define LV_COMPILER_NON_CONST_INIT_SUPPORTED 0 /* 1: Initialization with non constant values are supported (In Visual studio it is not supported)*/
|
#define LV_COMPILER_NON_CONST_INIT_SUPPORTED 0 /* 1: Initialization with non constant values are supported (In Visual studio it is not supported)*/
|
||||||
//#define _CRT_SECURE_NO_WARNINGS /* Visual Studio needs it to use `strcpy`, `sprintf` etc*/
|
//#define _CRT_SECURE_NO_WARNINGS /* Visual Studio needs it to use `strcpy`, `sprintf` etc*/
|
||||||
|
|
||||||
|
/*HAL settings*/
|
||||||
|
#define LV_TICK_CUSTOM 0 /*1: use a custom tick source (removing the need to manually update the tick with `lv_tick_inc`) */
|
||||||
|
#if LV_TICK_CUSTOM == 1
|
||||||
|
#define LV_TICK_CUSTOM_INCLUDE "Arduino.h" /*Header for the sys time function*/
|
||||||
|
#define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis()) /*Expression evaluating to current systime in ms*/
|
||||||
|
#endif /*LV_TICK_CUSTOM*/
|
||||||
|
|
||||||
|
|
||||||
/*Log settings*/
|
/*Log settings*/
|
||||||
#define USE_LV_LOG 1 /*Enable/disable the log module*/
|
#define USE_LV_LOG 1 /*Enable/disable the log module*/
|
||||||
#if USE_LV_LOG
|
#if USE_LV_LOG
|
||||||
@@ -113,6 +116,8 @@
|
|||||||
/*================
|
/*================
|
||||||
* THEME USAGE
|
* THEME USAGE
|
||||||
*================*/
|
*================*/
|
||||||
|
#define LV_THEME_LIVE_UPDATE 0 /*Enable to change the theme in run time. Requires 8..10 kB extra RAM*/
|
||||||
|
|
||||||
#define USE_LV_THEME_TEMPL 0 /*Just for test*/
|
#define USE_LV_THEME_TEMPL 0 /*Just for test*/
|
||||||
#define USE_LV_THEME_DEFAULT 0 /*Built mainly from the built-in styles. Consumes very few RAM*/
|
#define USE_LV_THEME_DEFAULT 0 /*Built mainly from the built-in styles. Consumes very few RAM*/
|
||||||
#define USE_LV_THEME_ALIEN 0 /*Dark futuristic theme*/
|
#define USE_LV_THEME_ALIEN 0 /*Dark futuristic theme*/
|
||||||
|
|||||||
+56
-1
@@ -7,6 +7,7 @@
|
|||||||
* INCLUDES
|
* INCLUDES
|
||||||
*********************/
|
*********************/
|
||||||
#include "lv_theme.h"
|
#include "lv_theme.h"
|
||||||
|
#include "../lv_core/lv_obj.h"
|
||||||
|
|
||||||
/*********************
|
/*********************
|
||||||
* DEFINES
|
* DEFINES
|
||||||
@@ -23,7 +24,23 @@
|
|||||||
/**********************
|
/**********************
|
||||||
* STATIC VARIABLES
|
* STATIC VARIABLES
|
||||||
**********************/
|
**********************/
|
||||||
|
|
||||||
|
#if LV_THEME_LIVE_UPDATE == 0
|
||||||
static lv_theme_t * current_theme;
|
static lv_theme_t * current_theme;
|
||||||
|
#else
|
||||||
|
/* If live update is used then a big `lv_style_t` array is used to store the real styles of the theme not only pointers.
|
||||||
|
* On `lv_theme_set_current` the styles of the theme are copied to this array.
|
||||||
|
* The pointers in `current_theme` are initialized to point to the styles in the array.
|
||||||
|
* This way the theme styles will always point to the same memory address even after theme is change.
|
||||||
|
* (The pointers in the theme points to the styles declared by the theme itself) */
|
||||||
|
|
||||||
|
/* Store the styles in this array.
|
||||||
|
* Can't determine the size in compile time because sizeof is not evaluated (should be `sizeof(lv_theme_t) / sizeof(lv_style_t*)`).
|
||||||
|
* Error will be generated in run time if too small.*/
|
||||||
|
static lv_style_t th_styles[100];
|
||||||
|
static bool inited = false;
|
||||||
|
static lv_theme_t current_theme;
|
||||||
|
#endif
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* MACROS
|
* MACROS
|
||||||
@@ -38,9 +55,42 @@ static lv_theme_t * current_theme;
|
|||||||
* From now, all the created objects will use styles from this theme by default
|
* From now, all the created objects will use styles from this theme by default
|
||||||
* @param th pointer to theme (return value of: 'lv_theme_init_xxx()')
|
* @param th pointer to theme (return value of: 'lv_theme_init_xxx()')
|
||||||
*/
|
*/
|
||||||
void lv_theme_set_current(lv_theme_t * th)
|
void lv_theme_set_current(lv_theme_t *th)
|
||||||
{
|
{
|
||||||
|
#if LV_THEME_LIVE_UPDATE == 0
|
||||||
current_theme = th;
|
current_theme = th;
|
||||||
|
#else
|
||||||
|
uint32_t style_num = sizeof(lv_theme_t) / sizeof(lv_style_t*); /*Number of styles in a theme*/
|
||||||
|
|
||||||
|
if(!inited) {
|
||||||
|
/*It's not sure `th_styles` is big enough. Check it now!*/
|
||||||
|
if(style_num > sizeof(th_styles) / sizeof(lv_style_t)) {
|
||||||
|
LV_LOG_ERROR("Themes: th_styles array is too small. Increase it's size!");
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Initialize the style pointers `current_theme` to point to the `th_styles` style array */
|
||||||
|
uint16_t i;
|
||||||
|
lv_style_t ** cur_th_style_p = (lv_style_t **) ¤t_theme;
|
||||||
|
for(i = 0; i < style_num; i++) {
|
||||||
|
uint64_t adr = (uint64_t)&th_styles[i];
|
||||||
|
memcpy(&cur_th_style_p[i], &adr, sizeof(lv_style_t*));
|
||||||
|
}
|
||||||
|
inited = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*Copy the styles pointed by the new theme to the `th_styles` style array*/
|
||||||
|
uint16_t i;
|
||||||
|
lv_style_t ** th_style = (lv_style_t **) th;
|
||||||
|
for(i = 0; i < style_num; i++) {
|
||||||
|
uint64_t s = (uint64_t)th_style[i];
|
||||||
|
if(s) memcpy(&th_styles[i], (void*)s, sizeof(lv_style_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Let the object know their style might change*/
|
||||||
|
lv_obj_report_style_mod(NULL);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +99,12 @@ void lv_theme_set_current(lv_theme_t * th)
|
|||||||
*/
|
*/
|
||||||
lv_theme_t * lv_theme_get_current(void)
|
lv_theme_t * lv_theme_get_current(void)
|
||||||
{
|
{
|
||||||
|
#if LV_THEME_LIVE_UPDATE == 0
|
||||||
return current_theme;
|
return current_theme;
|
||||||
|
#else
|
||||||
|
if(!inited) return NULL;
|
||||||
|
else return ¤t_theme;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
|
|||||||
Reference in New Issue
Block a user