Skip to content

Commit

Permalink
feat(simulator): upgrade to use lvgl's property API
Browse files Browse the repository at this point in the history
Signed-off-by: Neo Xu <[email protected]>
  • Loading branch information
XuNeo committed Aug 3, 2024
1 parent 4b4b84b commit c5ba19b
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 104 deletions.
17 changes: 17 additions & 0 deletions examples/align.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
lvgl.Object{
w = 10,
h = 10,
bg_color = "#c00",
align = {
type = lvgl.ALIGN.BOTTOM_MID,
x_ofs = 10,
y_ofs = -200,
},
}

lvgl.Object{
w = 10,
h = 10,
align = lvgl.ALIGN.CENTER,
}

3 changes: 2 additions & 1 deletion examples/analogTime.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local analogTime = lvgl.AnalogTime {
border_width = 0,
border_width = 1,
border_color = "#F00",
x = lvgl.HOR_RES() // 2,
y = lvgl.VER_RES() // 2,
hands = {
Expand Down
10 changes: 3 additions & 7 deletions examples/pointer.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
local root = lvgl.Object {
w = 0,
w = 1,
h = 0,
pad_all = 0,
align = lvgl.ALIGN.CENTER,
border_width = 1,
border_color = "#F00",
flag_scrollable = false,
}

local pointer = root:Pointer {
src = SCRIPT_PATH .. "/assets/second.png",
pivot = { 20, 233 },
x = -20,
y = -233,
border_width = 1,
Expand All @@ -21,12 +23,6 @@ local pointer = root:Pointer {
},
}

-- lvgl requires src set firstly, since lua cannot guarantee table order,
-- we set pivot after image src is set.
pointer:set {
pivot = { x = 20, y = 233 },
}

print("img w,h: ", pointer:get_img_size())

pointer:Anim{
Expand Down
17 changes: 17 additions & 0 deletions examples/property.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local root = lvgl.Object{
bg_color = "#c00",
align = {
type = lvgl.ALIGN.BOTTOM_MID,
x_ofs = 10,
y_ofs = -200,
},
}

local t = {

}

t.x = 123

print("root.x", root.x)
root.y = 0
68 changes: 17 additions & 51 deletions simulator/widgets/analog_time.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#include <lauxlib.h>
#include <lua.h>

#include <lvgl.h>
#include <stdlib.h>

#include <luavgl.h>

#include "lv_analog_time.h"
Expand All @@ -13,11 +7,14 @@ static int luavgl_analog_time_create(lua_State *L)
return luavgl_obj_create_helper(L, lv_analog_time_create);
}

static void _lv_analog_time_set_hands(void *obj, lua_State *L)
static int analog_time_set_hands(lua_State *L, lv_obj_t *obj, bool set)
{
if (!set)
return 0;

if (!lua_istable(L, -1)) {
luaL_argerror(L, -1, "expect date table.");
return;
return 1;
}

const void *hour, *minute, *second;
Expand All @@ -35,47 +32,17 @@ static void _lv_analog_time_set_hands(void *obj, lua_State *L)
lua_pop(L, 1);

lv_analog_time_set_hands(obj, hour, minute, second);
return 1;
}

/* clang-format off */
static const luavgl_value_setter_t analog_time_property_table[] = {
{"hands", SETTER_TYPE_STACK, {.setter_stack = _lv_analog_time_set_hands}},
{"period", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_analog_time_set_period}},
static const luavgl_property_ops_t analog_time_property_ops[] = {
{.name = "hands", .ops = analog_time_set_hands},
};

/* clang-format on */

static int luavgl_analog_time_set_property_kv(lua_State *L, void *data)
{
lv_obj_t *obj = data;
int ret = luavgl_set_property(L, obj, analog_time_property_table);

if (ret == 0) {
return 0;
}

/* a base obj property? */
ret = luavgl_obj_set_property_kv(L, obj);
if (ret != 0) {
printf("unkown property for analog_time: %s\n", lua_tostring(L, -2));
}

return -1;
}

static int luavgl_analog_time_set(lua_State *L)
{
lv_obj_t *obj = luavgl_to_obj(L, 1);

if (!lua_istable(L, -1)) {
luaL_error(L, "expect a table on 2nd para.");
return 0;
}

luavgl_iterate(L, -1, luavgl_analog_time_set_property_kv, obj);

return 0;
}
static const luavgl_table_t analog_time_property_table = {
.len = sizeof(analog_time_property_ops) / sizeof(luavgl_property_ops_t),
.array = analog_time_property_ops,
};

static int luavgl_analog_time_pause(lua_State *L)
{
Expand All @@ -92,11 +59,11 @@ static int luavgl_analog_time_resume(lua_State *L)
}

static const rotable_Reg luavgl_analog_time_methods[] = {
{"set", LUA_TFUNCTION, {luavgl_analog_time_set} },
{"pause", LUA_TFUNCTION, {luavgl_analog_time_pause} },
{"resume", LUA_TFUNCTION, {luavgl_analog_time_resume}},
{"pause", LUA_TFUNCTION, {luavgl_analog_time_pause} },
{"resume", LUA_TFUNCTION, {luavgl_analog_time_resume} },
{"__property", LUA_TLIGHTUSERDATA, {.ptr = &analog_time_property_table}},

{0, 0, {0} },
{0, 0, {0} },
};

void luavgl_analog_time_init(lua_State *L)
Expand All @@ -106,8 +73,7 @@ void luavgl_analog_time_init(lua_State *L)
lua_pop(L, 1);

luaL_getmetatable(L, "widgets");
lua_getfield(L, -1, "__index");
lua_pushcfunction(L, luavgl_analog_time_create);
lua_setfield(L, -2, "AnalogTime");
lua_pop(L, 2);
lua_pop(L, 1);
}
3 changes: 1 addition & 2 deletions simulator/widgets/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ void luavgl_extension_init(lua_State *L)
lua_pop(L, 1);

luaL_getmetatable(L, "widgets");
lua_getfield(L, -1, "__index");
lua_pushcfunction(L, luavgl_extension_create);
lua_setfield(L, -2, "Extension");
lua_pop(L, 2);
lua_pop(L, 1);
}
47 changes: 44 additions & 3 deletions simulator/widgets/lv_analog_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "lv_analog_time.h"
#include "lv_pointer.h"

#include "lv_obj_property_names.h"

#include <stdint.h>
#include <time.h>

#if LV_USE_ANALOG_TIME
Expand Down Expand Up @@ -37,11 +40,35 @@ static lv_obj_t* create_hand(lv_obj_t* obj, const char* img);
* STATIC VARIABLES
**********************/

#if LV_USE_OBJ_PROPERTY
static const lv_property_ops_t properties[] = {
{
.id = LV_PROPERTY_ANALOG_TIME_PERIOD,
.setter = lv_analog_time_set_period,
.getter = lv_analog_time_get_period,
},
};
#endif

const lv_obj_class_t lv_analog_time_class = {
.constructor_cb = lv_analog_time_constructor,
.destructor_cb = lv_analog_time_destructor,
.instance_size = sizeof(lv_analog_time_t),
.base_class = &lv_obj_class,
.name = "analogtime",

#if LV_USE_OBJ_PROPERTY
.prop_index_start = LV_PROPERTY_ANALOG_TIME_START,
.prop_index_end = LV_PROPERTY_ANALOG_TIME_END,
.properties = properties,
.properties_count = sizeof(properties) / sizeof(properties[0]),

#if LV_USE_OBJ_PROPERTY_NAME
.property_names = lv_analog_time_property_names,
.names_count = sizeof(lv_analog_time_property_names) / sizeof(lv_property_name_t),
#endif

#endif
};

/**********************
Expand Down Expand Up @@ -149,23 +176,37 @@ void lv_analog_time_set_period(lv_obj_t* obj, uint32_t period)
lv_timer_set_period(analog->timer, period);
}

uint32_t lv_analog_time_get_period(lv_obj_t* obj)
{
lv_analog_time_t* analog = (lv_analog_time_t*)obj;
return analog->period;
}

/**********************
* STATIC FUNCTIONS
**********************/

static void ext_draw_size_event_cb(lv_event_t * e)
{
lv_coord_t * cur_size = lv_event_get_param(e);
*cur_size = LV_MAX(*cur_size, LV_HOR_RES);
}

static void lv_analog_time_constructor(const lv_obj_class_t* class_p,
lv_obj_t* obj)
{
LV_UNUSED(class_p);
LV_TRACE_OBJ_CREATE("begin");

lv_analog_time_t* analog = (lv_analog_time_t*)obj;
lv_obj_t * parent = lv_obj_get_parent(obj);
analog->period = 60 * 60 * 1000; /* default to 1Hour = 60min*60sec*1000ms*/
lv_obj_add_flag(lv_obj_get_parent(obj), LV_OBJ_FLAG_OVERFLOW_VISIBLE);
lv_obj_add_flag(parent, LV_OBJ_FLAG_OVERFLOW_VISIBLE);
lv_obj_add_event_cb(parent, ext_draw_size_event_cb, LV_EVENT_REFR_EXT_DRAW_SIZE, NULL);

/* hands pivot is place to 0, 0 */
lv_obj_remove_style_all(obj);
lv_obj_set_size(obj, 0, 0);
lv_obj_set_size(obj, 1, 0);

analog->timer = lv_timer_create(timer_cb, UNIT_ONE_MINUTE, obj);
lv_timer_pause(analog->timer);
Expand Down Expand Up @@ -275,7 +316,7 @@ static void update_time(lv_obj_t* obj)
static void timer_cb(lv_timer_t* t)
{
/* timeup */
update_time(t->user_data);
update_time(lv_timer_get_user_data(t));
}

/* examples */
Expand Down
10 changes: 10 additions & 0 deletions simulator/widgets/lv_analog_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern "C" {

#include <lvgl.h>

#include "lv_property_extend.h"

#if LV_USE_ANALOG_TIME

/*********************
Expand All @@ -21,6 +23,13 @@ extern "C" {
* TYPEDEFS
**********************/

#if LV_USE_OBJ_PROPERTY
enum {
LV_PROPERTY_ID(ANALOG_TIME, PERIOD, LV_PROPERTY_TYPE_INT, 0),
LV_PROPERTY_ANALOG_TIME_END,
};
#endif

typedef struct {
lv_obj_t obj;
lv_obj_t* hour;
Expand Down Expand Up @@ -50,6 +59,7 @@ void lv_analog_time_set_hands(lv_obj_t* obj, const void* hour,
void lv_analog_time_pause(lv_obj_t* obj);
void lv_analog_time_resume(lv_obj_t* obj);
void lv_analog_time_set_period(lv_obj_t* obj, uint32_t period);
uint32_t lv_analog_time_get_period(lv_obj_t* obj);

/**********************
* MACROS
Expand Down
23 changes: 23 additions & 0 deletions simulator/widgets/lv_analog_time_properties.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

/**
* GENERATED FILE, DO NOT EDIT IT!
* @file lv_analog_time_properties.c
*/

#include "lv_analog_time.h"

#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME

#if LV_USE_ANALOG_TIME
/**
* Analog_time widget property names, name must be in order.
* Generated code from properties.py
*/
/* *INDENT-OFF* */
const lv_property_name_t lv_analog_time_property_names[1] = {
{"period", LV_PROPERTY_ANALOG_TIME_PERIOD,},
};
#endif /*LV_USE_ANALOG_TIME*/

/* *INDENT-ON* */
#endif
15 changes: 15 additions & 0 deletions simulator/widgets/lv_obj_property_names.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

/**
* @file lv_obj_property_names.h
* GENERATED FILE, DO NOT EDIT IT!
*/
#ifndef _LV_OBJ_PROPERTY_NAMES_H
#define _LV_OBJ_PROPERTY_NAMES_H

#include "lvgl.h"

#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME

extern const lv_property_name_t lv_analog_time_property_names[1];
#endif
#endif
7 changes: 7 additions & 0 deletions simulator/widgets/lv_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
static void lv_pointer_constructor(const lv_obj_class_t* class_p,
lv_obj_t* obj);
static void angle_update(lv_obj_t* obj);
static void ext_draw_size_event_cb(lv_event_t * e);

/**********************
* STATIC VARIABLES
Expand All @@ -46,6 +47,7 @@ lv_obj_t* lv_pointer_create(lv_obj_t* parent)

/* pointer can rotate out of parent's area. */
lv_obj_add_flag(parent, LV_OBJ_FLAG_OVERFLOW_VISIBLE);
lv_obj_add_event_cb(parent, ext_draw_size_event_cb, LV_EVENT_REFR_EXT_DRAW_SIZE, NULL);
return obj;
}

Expand Down Expand Up @@ -76,6 +78,11 @@ void lv_pointer_set_range(lv_obj_t* obj, int value_start, int value_range,
/**********************
* STATIC FUNCTIONS
**********************/
static void ext_draw_size_event_cb(lv_event_t * e)
{
lv_coord_t * cur_size = lv_event_get_param(e);
*cur_size = LV_MAX(*cur_size, LV_HOR_RES);
}

static void lv_pointer_constructor(const lv_obj_class_t* class_p,
lv_obj_t* obj)
Expand Down
Loading

0 comments on commit c5ba19b

Please sign in to comment.