diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-11-12 19:14:09 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-11-12 19:14:09 +1100 |
| commit | 8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de (patch) | |
| tree | 02b6cf23f591915747ec2994381854a79979c4a0 /lib/luavgl/src/widgets | |
| parent | 8471046a95ab9e00f7d42b56dbbc9ce3e5b424b9 (diff) | |
| download | tangara-fw-8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de.tar.gz | |
Convert the main menu screen to lua lol
Diffstat (limited to 'lib/luavgl/src/widgets')
| -rw-r--r-- | lib/luavgl/src/widgets/calendar.c | 169 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/checkbox.c | 74 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/dropdown.c | 188 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/img.c | 196 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/keyboard.c | 76 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/label.c | 155 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/led.c | 91 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/list.c | 89 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/roller.c | 136 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/textarea.c | 117 | ||||
| -rw-r--r-- | lib/luavgl/src/widgets/widgets.c | 133 |
11 files changed, 1424 insertions, 0 deletions
diff --git a/lib/luavgl/src/widgets/calendar.c b/lib/luavgl/src/widgets/calendar.c new file mode 100644 index 00000000..2fb8fd9b --- /dev/null +++ b/lib/luavgl/src/widgets/calendar.c @@ -0,0 +1,169 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_calendar_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_calendar_create); +} + +static void _lv_calendar_set_today(void *obj, lua_State *L) +{ + if (!lua_istable(L, -1)) { + luaL_argerror(L, -1, "expect date table."); + return; + } + + uint32_t year, month, day; + lua_getfield(L, -1, "year"); + year = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "month"); + month = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "day"); + day = lua_tointeger(L, -1); + lua_pop(L, 1); + + lv_calendar_set_today_date(obj, year, month, day); +} + +static void _lv_calendar_set_showed(void *obj, lua_State *L) +{ + if (!lua_istable(L, -1)) { + luaL_argerror(L, -1, "expect date table."); + return; + } + + uint32_t year, month; + lua_getfield(L, -1, "year"); + year = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "month"); + month = lua_tointeger(L, -1); + lua_pop(L, 1); + + lv_calendar_set_showed_date(obj, year, month); +} + +static const luavgl_value_setter_t calendar_property_table[] = { + {"today", SETTER_TYPE_STACK, {.setter_stack = _lv_calendar_set_today} }, + {"showed", SETTER_TYPE_STACK, {.setter_stack = _lv_calendar_set_showed}}, +}; + +LUALIB_API int luavgl_calendar_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, calendar_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for calendar.\n"); + } + + return ret; +} + +static int luavgl_calendar_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_calendar_set_property_kv, obj); + + return 0; +} + +static inline int calendar_new_date(lua_State *L, + const lv_calendar_date_t *date) +{ + lua_createtable(L, 0, 3); + lua_pushinteger(L, date->year); + lua_setfield(L, -2, "year"); + + lua_pushinteger(L, date->month); + lua_setfield(L, -2, "month"); + + lua_pushinteger(L, date->day); + lua_setfield(L, -2, "day"); + + return 1; +} + +static int luavgl_calendar_get_today(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + const lv_calendar_date_t *date = lv_calendar_get_today_date(obj); + + return calendar_new_date(L, date); +} + +static int luavgl_calendar_get_showed(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + const lv_calendar_date_t *date = lv_calendar_get_showed_date(obj); + + return calendar_new_date(L, date); +} + +static int luavgl_calendar_get_pressed(lua_State *L) +{ + lv_calendar_date_t date; + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_calendar_get_pressed_date(obj, &date); + + return calendar_new_date(L, &date); +} + +static int luavgl_calendar_get_btnm(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_obj_t *btm = lv_calendar_get_btnmatrix(obj); + lua_pushlightuserdata(L, btm); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_isnoneornil(L, -1)) { + luavgl_add_lobj(L, btm)->lua_created = false; + } + + return 1; /* obj userdata is already on stack */ +} + +static int luavgl_calendar_create_arrow(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_calendar_header_arrow_create); +} + +static int luavgl_calendar_create_dropdown(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_calendar_header_dropdown_create); +} + +static const luaL_Reg luavgl_calendar_methods[] = { + {"set", luavgl_calendar_set }, + {"get_today", luavgl_calendar_get_today }, + {"get_showed", luavgl_calendar_get_showed }, + {"get_pressed", luavgl_calendar_get_pressed }, + {"get_btnm", luavgl_calendar_get_btnm }, + {"Arrow", luavgl_calendar_create_arrow }, + {"Dropdown", luavgl_calendar_create_dropdown}, + + {NULL, NULL }, +}; + +static void luavgl_calendar_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_calendar_class, "lv_calendar", + luavgl_calendar_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/checkbox.c b/lib/luavgl/src/widgets/checkbox.c new file mode 100644 index 00000000..e7885c8e --- /dev/null +++ b/lib/luavgl/src/widgets/checkbox.c @@ -0,0 +1,74 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_checkbox_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_checkbox_create); +} + +static void _lv_checkbox_set_txt(void *obj, lua_State *L) +{ + if (!lua_isstring(L, -1)) { + luaL_argerror(L, -1, "only support string for text."); + return; + } + + lv_checkbox_set_text(obj, lua_tostring(L, -1)); +} + +static const luavgl_value_setter_t checkbox_property_table[] = { + {"text", SETTER_TYPE_STACK, {.setter_stack = _lv_checkbox_set_txt}}, +}; + +LUALIB_API int luavgl_checkbox_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, checkbox_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for checkbox.\n"); + } + + return ret; +} + +static int luavgl_checkbox_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_checkbox_set_property_kv, obj); + + return 0; +} + +static int luavgl_checkbox_get_text(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushstring(L, lv_checkbox_get_text(obj)); + return 1; +} + +static const luaL_Reg luavgl_checkbox_methods[] = { + {"set", luavgl_checkbox_set }, + {"get_text", luavgl_checkbox_get_text}, + + {NULL, NULL }, +}; + +static void luavgl_checkbox_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_checkbox_class, "lv_checkbox", + luavgl_checkbox_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/dropdown.c b/lib/luavgl/src/widgets/dropdown.c new file mode 100644 index 00000000..a211c10b --- /dev/null +++ b/lib/luavgl/src/widgets/dropdown.c @@ -0,0 +1,188 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_dropdown_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_dropdown_create); +} + +static void _lv_dropdown_set_text(void *obj, lua_State *L) +{ + const char *str = lua_tostring(L, -1); + + lv_dropdown_set_text(obj, str); +} + +/* clang-format off */ +static const luavgl_value_setter_t dropdown_property_table[] = { + {"text", SETTER_TYPE_STACK, {.setter_stack = _lv_dropdown_set_text}}, + {"options", SETTER_TYPE_STACK, {.setter_stack = _lv_dummy_set}}, + {"selected", 0, {.setter = (setter_int_t)lv_dropdown_set_selected}}, + {"dir", 0, {.setter = (setter_int_t)lv_dropdown_set_dir}}, + {"symbol", SETTER_TYPE_IMGSRC, {.setter_pointer = (setter_pointer_t)lv_dropdown_set_symbol}}, + {"highlight", 0, {.setter = (setter_int_t)lv_dropdown_set_selected_highlight}}, +}; +/* clang-format on */ + +LUALIB_API int luavgl_dropdown_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, dropdown_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for dropdown.\n"); + } + + return ret; +} + +static int luavgl_dropdown_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; + } + + /* set options firstly, otherwise set selected may fail */ + lua_getfield(L, -1, "options"); + if (lua_type(L, -1) == LUA_TSTRING) { + lv_dropdown_set_options(obj, lua_tostring(L, -1)); + } + lua_pop(L, 1); + + luavgl_iterate(L, -1, luavgl_dropdown_set_property_kv, obj); + + return 0; +} + +static int luavgl_dropdown_get(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + + if (lua_type(L, 2) != LUA_TSTRING) { + return luaL_argerror(L, 2, "expect string"); + } + + const char *key = lua_tostring(L, 2); + if (strcmp(key, "list") == 0) { + lv_obj_t *list = lv_dropdown_get_list(obj); + lua_pushlightuserdata(L, list); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_isnoneornil(L, -1)) { + lua_pop(L, 1); + luavgl_add_lobj(L, list)->lua_created = false; + } + return 1; + } + + if (strcmp(key, "text") == 0) { + lua_pushstring(L, lv_dropdown_get_text(obj)); + return 1; + } + + if (strcmp(key, "options") == 0) { + lua_pushstring(L, lv_dropdown_get_options(obj)); + return 1; + } + + if (strcmp(key, "selected") == 0) { + lua_pushinteger(L, lv_dropdown_get_selected(obj)); + return 1; + } + + if (strcmp(key, "option_cnt") == 0) { + lua_pushinteger(L, lv_dropdown_get_option_cnt(obj)); + return 1; + } + + if (strcmp(key, "selected_str") == 0) { + char buf[64]; + lv_dropdown_get_selected_str(obj, buf, sizeof(buf)); + lua_pushstring(L, buf); + return 1; + } + + if (strcmp(key, "option_index") == 0) { + const char *option = lua_tostring(L, 3); + lua_pushinteger(L, lv_dropdown_get_option_index(obj, option)); + return 1; + } + + if (strcmp(key, "symbol") == 0) { + lua_pushlightuserdata(L, (void *)lv_dropdown_get_symbol(obj)); + return 1; + } + + if (strcmp(key, "dir") == 0) { + lua_pushinteger(L, lv_dropdown_get_dir(obj)); + return 1; + } + + return 0; +} + +static int luavgl_dropdown_open(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_dropdown_open(obj); + return 0; +} + +static int luavgl_dropdown_close(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_dropdown_close(obj); + return 0; +} + +static int luavgl_dropdown_is_open(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushboolean(L, lv_dropdown_is_open(obj)); + return 1; +} + +static int luavgl_dropdown_add_option(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + const char *str = lua_tostring(L, 2); + uint32_t pos = lua_tointeger(L, 3); + lv_dropdown_add_option(obj, str, pos); + + return 0; +} + +static int luavgl_dropdown_clear_option(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + + lv_dropdown_clear_options(obj); + return 0; +} + +static const luaL_Reg luavgl_dropdown_methods[] = { + {"set", luavgl_dropdown_set }, + {"get", luavgl_dropdown_get }, + {"open", luavgl_dropdown_open }, + {"close", luavgl_dropdown_close }, + {"is_open", luavgl_dropdown_is_open }, + {"add_option", luavgl_dropdown_add_option }, + {"clear_option", luavgl_dropdown_clear_option}, + + {NULL, NULL }, +}; + +static void luavgl_dropdown_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_dropdown_class, "lv_dropdown", + luavgl_dropdown_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/img.c b/lib/luavgl/src/widgets/img.c new file mode 100644 index 00000000..9332cd99 --- /dev/null +++ b/lib/luavgl/src/widgets/img.c @@ -0,0 +1,196 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_img_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_img_create); +} + +static void _lv_img_set_pivot(void *obj, lua_State *L) +{ + if (!lua_istable(L, -1)) { + luaL_argerror(L, -1, "should be table."); + debug("para should be table."); + return; + } + + lua_getfield(L, -1, "x"); + lv_coord_t x = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "y"); + lv_coord_t y = lua_tointeger(L, -1); + lua_pop(L, 1); + + lv_img_set_pivot(obj, x, y); +} + +static const luavgl_value_setter_t img_property_table[] = { + {"src", + SETTER_TYPE_IMGSRC, {.setter_pointer = (setter_pointer_t)lv_img_set_src}}, + {"offset_x", 0, {.setter = (setter_int_t)lv_img_set_offset_x} }, + {"offset_y", 0, {.setter = (setter_int_t)lv_img_set_offset_y} }, + {"angle", 0, {.setter = (setter_int_t)lv_img_set_angle} }, + {"zoom", 0, {.setter = (setter_int_t)lv_img_set_zoom} }, + {"antialias", 0, {.setter = (setter_int_t)lv_img_set_antialias} }, + {"pivot", SETTER_TYPE_STACK, {.setter_stack = _lv_img_set_pivot} }, +}; + +LUALIB_API int luavgl_img_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, img_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for image.\n"); + } + + return ret; +} + +static int luavgl_img_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; + } + + /* lvgl requires img src should be set firstly */ + lua_getfield(L, -1, "src"); + if (!lua_isnoneornil(L, -1)) { + const char *src = NULL; + if (lua_isuserdata(L, -1)) { + src = lua_touserdata(L, -1); + debug("set img src to user data: %p\n", src); + } else { + src = lua_tostring(L, -1); + } + lv_img_set_src(obj, src); + } + lua_pop(L, 1); + + luavgl_iterate(L, -1, luavgl_img_set_property_kv, obj); + + return 0; +} + +/** + * img.set_src(img, "path") + */ +static int luavgl_img_set_src(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + const char *src = luavgl_toimgsrc(L, 2); + if (src != NULL) { + lv_img_set_src(obj, src); + } + + return 0; +} + +/** + * img:set_offset({x=10}) + * img:set_offset({x=10}) + * img:set_offset({x=10, y=100}) + */ +static int luavgl_img_set_offset(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + + if (!lua_istable(L, -1)) { + luaL_argerror(L, -1, "should be table {x=0,y=0,anim=true}"); + } + + lv_coord_t v; + lua_getfield(L, -1, "x"); + if (!lua_isnil(L, -1)) { + v = lua_tointeger(L, -1); + lua_pop(L, 1); + lv_img_set_offset_x(obj, v); + } + + lua_getfield(L, -1, "y"); + if (!lua_isnil(L, -1)) { + v = lua_tointeger(L, -1); + lua_pop(L, 1); + lv_img_set_offset_y(obj, v); + } + + return 0; +} + +/** + * img:set_pivot({x=10, y=100}) + */ +static int luavgl_img_set_pivot(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + + if (!lua_istable(L, -1)) { + luaL_argerror(L, -1, "should be table {x=0,y=0,anim=true}"); + } + + lv_coord_t x = 0, y = 0; + lua_getfield(L, -1, "x"); + x = lua_tointeger(L, -1); + + lua_getfield(L, -1, "y"); + y = lua_tointeger(L, -1); + + lv_img_set_pivot(obj, x, y); + + return 0; +} + +/** + * return image size w, h + * img:get_img_size() -- get size of this image + * img:get_img_size("src") -- get size of img "src" + */ +static int luavgl_get_img_size(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + + const void *src = NULL; + if (lua_isnoneornil(L, 2)) { + src = lv_img_get_src(obj); + } else { + src = luavgl_toimgsrc(L, 2); + } + + lv_img_header_t header; + if (src == NULL || lv_img_decoder_get_info(src, &header) != LV_RES_OK) { + lua_pushnil(L); + lua_pushnil(L); + } else { + lua_pushinteger(L, header.w); + lua_pushinteger(L, header.h); + } + + return 2; +} + +static const luaL_Reg luavgl_img_methods[] = { + {"set", luavgl_img_set }, + + {"set_src", luavgl_img_set_src }, + {"set_offset", luavgl_img_set_offset}, + {"set_pivot", luavgl_img_set_pivot }, + {"get_img_size", luavgl_get_img_size }, + + {NULL, NULL }, +}; + +static void luavgl_img_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_img_class, "lv_img", luavgl_img_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/keyboard.c b/lib/luavgl/src/widgets/keyboard.c new file mode 100644 index 00000000..e7121de0 --- /dev/null +++ b/lib/luavgl/src/widgets/keyboard.c @@ -0,0 +1,76 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_keyboard_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_keyboard_create); +} + +static void _lv_keyboard_set_textarea(void *obj, lua_State *L) +{ + lv_obj_t *ta = luavgl_to_obj(L, -1); + if (ta->class_p != &lv_textarea_class) { + luaL_argerror(L, -1, "expect textarea obj"); + return; + } + lv_keyboard_set_textarea(obj, ta); +} + +/* clang-format off */ +static const luavgl_value_setter_t keyboard_property_table[] = { + {"textarea", SETTER_TYPE_STACK, {.setter_stack = _lv_keyboard_set_textarea}}, + {"mode", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_keyboard_set_mode}}, + {"popovers", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_keyboard_set_popovers}}, +}; +/* clang-format on */ + +LUALIB_API int luavgl_keyboard_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, keyboard_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for keyboard.\n"); + } + + return ret; +} + +static int luavgl_keyboard_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_keyboard_set_property_kv, obj); + + return 0; +} + +static const luaL_Reg luavgl_keyboard_methods[] = { + {"set", luavgl_keyboard_set}, + + {NULL, NULL }, +}; + +static void luavgl_keyboard_init(lua_State *L) +{ + static const luaL_Reg btm_methods[] = { + {NULL, NULL}, + }; + luavgl_obj_newmetatable(L, &lv_btnmatrix_class, "lv_btnm", btm_methods); + lua_pop(L, 1); + + luavgl_obj_newmetatable(L, &lv_keyboard_class, "lv_keyboard", + luavgl_keyboard_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/label.c b/lib/luavgl/src/widgets/label.c new file mode 100644 index 00000000..701c56f3 --- /dev/null +++ b/lib/luavgl/src/widgets/label.c @@ -0,0 +1,155 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_label_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_label_create); +} + +static void _lv_label_set_txt(void *obj, lua_State *L) +{ + if (!lua_isstring(L, -1)) { + luaL_argerror(L, -1, "only support string for text."); + return; + } + + lv_label_set_text(obj, lua_tostring(L, -1)); +} + +static const luavgl_value_setter_t label_property_table[] = { + {"text", SETTER_TYPE_STACK, {.setter_stack = _lv_label_set_txt} }, + {"long_mode", 0, {.setter = (setter_int_t)lv_label_set_long_mode} }, + {"recolor", 0, {.setter = (setter_int_t)lv_label_set_recolor} }, +#if LVGL_VERSION_MAJOR == 9 + {"text_selection_start", + 0, {.setter = (setter_int_t)lv_label_set_text_selection_start}}, + {"text_selection_end", + 0, {.setter = (setter_int_t)lv_label_set_text_selection_end} }, +#else + {"text_selection_start", + 0, + {.setter = (setter_int_t)lv_label_set_text_sel_start}}, + {"text_selection_end", + 0, + {.setter = (setter_int_t)lv_label_set_text_sel_end}}, +#endif +}; + +LUALIB_API int luavgl_label_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, label_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for label.\n"); + } + + return ret; +} + +static int luavgl_label_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_label_set_property_kv, obj); + + return 0; +} + +static int luavgl_label_get_text(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushstring(L, lv_label_get_text(obj)); + return 1; +} + +static int luavgl_label_get_long_mode(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushinteger(L, lv_label_get_long_mode(obj)); + return 1; +} + +static int luavgl_label_get_recolor(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushinteger(L, lv_label_get_recolor(obj)); + return 1; +} + +static int luavgl_label_ins_text(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + uint32_t pos = luavgl_tointeger(L, 2); + const char *txt = lua_tostring(L, 3); + + lv_label_ins_text(obj, pos, txt); + return 0; +} + +static int luavgl_label_cut_text(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + uint32_t pos = luavgl_tointeger(L, 2); + uint32_t cnt = luavgl_tointeger(L, 3); + + lv_label_cut_text(obj, pos, cnt); + return 0; +} + +/* demo purpose, there is no need to use set_text_static */ +static int luavgl_label_set_text_static(lua_State *L) +{ + const char *str = lua_tostring(L, 2); + luavgl_obj_t *lobj = luavgl_to_lobj(L, 1); + if (lobj->obj == NULL) { + return luaL_error(L, "obj null."); + } + + luavgl_obj_getuserdatauv(L, 1); + + /* uservalue is on top */ + lua_pushvalue(L, 2); + lua_setfield(L, -2, "text_static"); + + lv_label_set_text_static(lobj->obj, str); + return 0; +} + +static int luavgl_label_tostring(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushfstring(L, "lv_label:%p, text: %s", obj, lv_label_get_text(obj)); + return 1; +} + +static const luaL_Reg luavgl_label_methods[] = { + {"set", luavgl_label_set }, + {"set_text_static", luavgl_label_set_text_static}, + {"get_text", luavgl_label_get_text }, + {"get_long_mode", luavgl_label_get_long_mode }, + {"get_recolor", luavgl_label_get_recolor }, + {"ins_text", luavgl_label_ins_text }, + {"cut_text", luavgl_label_cut_text }, + + {NULL, NULL }, +}; + +static void luavgl_label_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_label_class, "lv_label", luavgl_label_methods); + lua_pushcfunction(L, luavgl_label_tostring); + lua_setfield(L, -2, "__tostring"); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/led.c b/lib/luavgl/src/widgets/led.c new file mode 100644 index 00000000..38f22a05 --- /dev/null +++ b/lib/luavgl/src/widgets/led.c @@ -0,0 +1,91 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_led_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_led_create); +} + +/* clang-format off */ +static const luavgl_value_setter_t led_property_table[] = { + {"color", SETTER_TYPE_COLOR, {.setter = (setter_int_t)lv_led_set_color}}, + {"brightness", 0, {.setter = (setter_int_t)lv_led_set_brightness}}, +}; +/* clang-format on */ + +LUALIB_API int luavgl_led_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, led_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for led.\n"); + } + + return ret; +} + +static int luavgl_led_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_led_set_property_kv, obj); + + return 0; +} + +static int luavgl_led_on(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_led_on(obj); + return 0; +} + +static int luavgl_led_off(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_led_off(obj); + return 0; +} + +static int luavgl_led_toggle(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_led_toggle(obj); + return 0; +} + +static int luavgl_led_get_brightness(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushinteger(L, lv_led_get_brightness(obj)); + return 1; +} + +static const luaL_Reg luavgl_led_methods[] = { + {"set", luavgl_led_set }, + {"on", luavgl_led_on }, + {"off", luavgl_led_off }, + {"toggle", luavgl_led_toggle }, + + {"get_brightness", luavgl_led_get_brightness}, + + {NULL, NULL }, +}; + +static void luavgl_led_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_led_class, "lv_led", luavgl_led_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/list.c b/lib/luavgl/src/widgets/list.c new file mode 100644 index 00000000..825a7bff --- /dev/null +++ b/lib/luavgl/src/widgets/list.c @@ -0,0 +1,89 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_list_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_list_create); +} + +/* clang-format off */ +static const luavgl_value_setter_t list_property_table[] = { + {"dummy", SETTER_TYPE_STACK, {.setter_stack = _lv_dummy_set}}, +}; +/* clang-format on */ + +LUALIB_API int luavgl_list_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, list_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for list.\n"); + } + + return ret; +} + +static int luavgl_list_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_list_set_property_kv, obj); + + return 0; +} + +static int luavgl_list_add_text(lua_State *L) +{ + lv_obj_t *list = luavgl_to_obj(L, 1); + const char *str = lua_tostring(L, 2); + lv_obj_t *obj = lv_list_add_text(list, str); + luavgl_add_lobj(L, obj)->lua_created = true; + return 1; +} + +static int luavgl_list_add_btn(lua_State *L) +{ + lv_obj_t *list = luavgl_to_obj(L, 1); + const void *icon = luavgl_toimgsrc(L, 2); + const char *str = lua_tostring(L, 3); + lv_obj_t *obj = lv_list_add_btn(list, icon, str); + luavgl_add_lobj(L, obj)->lua_created = true; + return 1; +} + +static int luavgl_get_btn_text(lua_State *L) +{ + lv_obj_t *list = luavgl_to_obj(L, 1); + lv_obj_t *btn = luavgl_to_obj(L, 2); + + lua_pushstring(L, lv_list_get_btn_text(list, btn)); + return 1; +} + +static const luaL_Reg luavgl_list_methods[] = { + {"set", luavgl_list_set }, + + {"add_text", luavgl_list_add_text}, + {"add_btn", luavgl_list_add_btn }, + {"get_btn_text", luavgl_get_btn_text }, + + {NULL, NULL }, +}; + +static void luavgl_list_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_list_class, "lv_list", luavgl_list_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/roller.c b/lib/luavgl/src/widgets/roller.c new file mode 100644 index 00000000..574fc066 --- /dev/null +++ b/lib/luavgl/src/widgets/roller.c @@ -0,0 +1,136 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_roller_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_roller_create); +} + +static void _lv_roller_set_options(void *obj, lua_State *L) +{ + int type = lua_type(L, -1); + if (type == LUA_TSTRING) { + lv_roller_set_options(obj, lua_tostring(L, -1), 0); + } else if (type == LUA_TTABLE) { + lua_getfield(L, -1, "options"); + if (!lua_isstring(L, -1)) { + luaL_error(L, "expect string."); + return; + } + + const char *options = lua_tostring(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "mode"); + lv_roller_mode_t mode = lua_tointeger(L, -1); + lua_pop(L, 1); + lv_roller_set_options(obj, options, mode); + return; + } + + luaL_error(L, "expect string or table."); +} + +static void _lv_roller_set_selected(void *obj, lua_State *L) +{ + int type = lua_type(L, -1); + uint16_t selected; + int anim = 0; + + if (type == LUA_TTABLE) { + lua_getfield(L, -1, "selected"); + selected = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "anim"); + anim = luavgl_tointeger(L, -1); + lua_pop(L, 1); + } else { + selected = lua_tointeger(L, -1); + } + + lv_roller_set_selected(obj, selected, anim); +} + +static const luavgl_value_setter_t roller_property_table[] = { + {"options", SETTER_TYPE_STACK, {.setter_stack = _lv_roller_set_options} }, + {"selected", SETTER_TYPE_STACK, {.setter_stack = _lv_roller_set_selected} }, + {"visible_cnt", + 0, {.setter = (setter_int_t)lv_roller_set_visible_row_count}}, +}; + +LUALIB_API int luavgl_roller_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, roller_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for roller.\n"); + } + + return ret; +} + +static int luavgl_roller_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_roller_set_property_kv, obj); + + return 0; +} + +static int luavgl_roller_get_options(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushstring(L, lv_roller_get_options(obj)); + return 1; +} + +static int luavgl_roller_get_selected(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushinteger(L, lv_roller_get_selected(obj)); + return 1; +} + +static int luavgl_roller_get_selected_str(lua_State *L) +{ + char buf[64]; + lv_obj_t *obj = luavgl_to_obj(L, 1); + lv_roller_get_selected_str(obj, buf, sizeof(buf)); + lua_pushstring(L, buf); + return 1; +} + +static int luavgl_roller_get_options_cnt(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushinteger(L, lv_roller_get_option_cnt(obj)); + return 1; +} + +static const luaL_Reg luavgl_roller_methods[] = { + {"set", luavgl_roller_set }, + {"get_options", luavgl_roller_get_options }, + {"get_selected", luavgl_roller_get_selected }, + {"get_selected_str", luavgl_roller_get_selected_str}, + {"get_options_cnt", luavgl_roller_get_options_cnt }, + + {NULL, NULL }, +}; + +static void luavgl_roller_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_roller_class, "lv_roller", luavgl_roller_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/textarea.c b/lib/luavgl/src/widgets/textarea.c new file mode 100644 index 00000000..7c6170c1 --- /dev/null +++ b/lib/luavgl/src/widgets/textarea.c @@ -0,0 +1,117 @@ +#include "luavgl.h" +#include "private.h" + +static int luavgl_textarea_create(lua_State *L) +{ + return luavgl_obj_create_helper(L, lv_textarea_create); +} + +static void _lv_textarea_set_txt(void *obj, lua_State *L) +{ + if (!lua_isstring(L, -1)) { + luaL_argerror(L, -1, "expect string"); + return; + } + + lv_textarea_set_text(obj, lua_tostring(L, -1)); +} + +static void _lv_textarea_set_placeholder_txt(void *obj, lua_State *L) +{ + if (!lua_isstring(L, -1)) { + luaL_argerror(L, -1, "expect string"); + return; + } + + lv_textarea_set_placeholder_text(obj, lua_tostring(L, -1)); +} + +static void _lv_textarea_set_password_bullet(void *obj, lua_State *L) +{ + if (!lua_isstring(L, -1)) { + luaL_argerror(L, -1, "expect string"); + return; + } + + lv_textarea_set_password_bullet(obj, lua_tostring(L, -1)); +} + +static void _lv_textarea_set_accepted_chars(void *obj, lua_State *L) +{ + if (!lua_isstring(L, -1)) { + luaL_argerror(L, -1, "expect string"); + return; + } + + lv_textarea_set_accepted_chars(obj, lua_tostring(L, -1)); +} + +/* clang-format off */ +static const luavgl_value_setter_t textarea_property_table[] = { + {"text", SETTER_TYPE_STACK, {.setter_stack = _lv_textarea_set_txt}}, + {"placeholder", SETTER_TYPE_STACK, {.setter_stack = _lv_textarea_set_placeholder_txt}}, + {"cursor", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_textarea_set_cursor_pos}}, + {"password_mode", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_textarea_set_password_mode}}, + {"one_line", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_textarea_set_one_line}}, + {"password_bullet", SETTER_TYPE_STACK, {.setter_stack = _lv_textarea_set_password_bullet}}, + {"accepted_chars", SETTER_TYPE_STACK, {.setter_stack = _lv_textarea_set_accepted_chars}}, + {"max_length", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_textarea_set_max_length}}, + {"password_show_time", SETTER_TYPE_INT, {.setter = (setter_int_t)lv_textarea_set_password_show_time}}, +}; +/* clang-format on */ + +LUALIB_API int luavgl_textarea_set_property_kv(lua_State *L, void *data) +{ + lv_obj_t *obj = data; + int ret = luavgl_set_property(L, obj, textarea_property_table); + + if (ret == 0) { + return 0; + } + + /* a base obj property? */ + ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for textarea: %s\n", lua_tostring(L, -2)); + } + + return -1; +} + +static int luavgl_textarea_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_textarea_set_property_kv, obj); + + return 0; +} + +/** + * obj:get_text() + */ +static int luavgl_textarea_get_text(lua_State *L) +{ + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushstring(L, lv_textarea_get_text(obj)); + return 1; +} + +static const luaL_Reg luavgl_textarea_methods[] = { + {"set", luavgl_textarea_set }, + {"get_text", luavgl_textarea_get_text}, + + {NULL, NULL }, +}; + +static void luavgl_textarea_init(lua_State *L) +{ + luavgl_obj_newmetatable(L, &lv_textarea_class, "lv_textarea", + luavgl_textarea_methods); + lua_pop(L, 1); +} diff --git a/lib/luavgl/src/widgets/widgets.c b/lib/luavgl/src/widgets/widgets.c new file mode 100644 index 00000000..d26cdf95 --- /dev/null +++ b/lib/luavgl/src/widgets/widgets.c @@ -0,0 +1,133 @@ +#include "luavgl.h" +#include "private.h" + +#if LV_USE_CALENDAR +#include "calendar.c" +#endif + +#if LV_USE_CHECKBOX +#include "checkbox.c" +#endif + +#if LV_USE_DROPDOWN +#include "dropdown.c" +#endif + +#if LV_USE_IMG +#include "img.c" +#endif + +#if LV_USE_KEYBOARD +#include "keyboard.c" +#endif + +#if LV_USE_LABEL +#include "label.c" +#endif + +#if LV_USE_LED +#include "led.c" +#endif + +#if LV_USE_LIST +#include "list.c" +#endif + +#if LV_USE_ROLLER +#include "roller.c" +#endif + +#if LV_USE_TEXTAREA +#include "textarea.c" +#endif + +static int luavgl_obj_create(lua_State *L); + +static const luaL_Reg widget_create_methods[] = { + {"Object", luavgl_obj_create }, + +#if LV_USE_CALENDAR + {"Calendar", luavgl_calendar_create}, +#endif + +#if LV_USE_CHECKBOX + {"Checkbox", luavgl_checkbox_create}, +#endif + +#if LV_USE_DROPDOWN + {"Dropdown", luavgl_dropdown_create}, +#endif + +#if LV_USE_IMG + {"Image", luavgl_img_create }, +#endif + +#if LV_USE_KEYBOARD + {"Keyboard", luavgl_keyboard_create}, +#endif + +#if LV_USE_LABEL + {"Label", luavgl_label_create }, +#endif + +#if LV_USE_LED + {"Led", luavgl_led_create }, +#endif + +#if LV_USE_LIST + {"List", luavgl_list_create }, +#endif + +#if LV_USE_ROLLER + {"Roller", luavgl_roller_create }, +#endif + +#if LV_USE_TEXTAREA + {"Textarea", luavgl_textarea_create}, +#endif + {NULL, NULL } +}; + +static void luavgl_widgets_init(lua_State *L) +{ +#if LV_USE_IMG + luavgl_img_init(L); +#endif + +#if LV_USE_LABEL + luavgl_label_init(L); +#endif + +#if LV_USE_LED + luavgl_led_init(L); +#endif + +#if LV_USE_LIST + luavgl_list_init(L); +#endif + +#if LV_USE_TEXTAREA + luavgl_textarea_init(L); +#endif + +#if LV_USE_KEYBOARD + luavgl_keyboard_init(L); +#endif + +#if LV_USE_CHECKBOX + luavgl_checkbox_init(L); +#endif + +#if LV_USE_CALENDAR + luavgl_calendar_init(L); +#endif + +#if LV_USE_ROLLER + luavgl_roller_init(L); +#endif + +#if LV_USE_DROPDOWN + luavgl_dropdown_init(L); +#endif + +} |
