From 61ddc402ff328ffa5001732284a02f5887703832 Mon Sep 17 00:00:00 2001 From: lyon Date: Thu, 16 Jun 2022 20:02:01 +0800 Subject: [PATCH] support style for lvgl --- examples/lvgl/lv_style1.py | 24 +++++++++++++ package/pika_lvgl/pika_lvgl.c | 23 ++++++++++++ package/pika_lvgl/pika_lvgl.pyi | 25 +++++++++++++ package/pika_lvgl/pika_lvgl_lv_obj.c | 6 ++++ package/pika_lvgl/pika_lvgl_lv_style_t.c | 46 ++++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 examples/lvgl/lv_style1.py create mode 100644 package/pika_lvgl/pika_lvgl_lv_style_t.c diff --git a/examples/lvgl/lv_style1.py b/examples/lvgl/lv_style1.py new file mode 100644 index 000000000..b838167c8 --- /dev/null +++ b/examples/lvgl/lv_style1.py @@ -0,0 +1,24 @@ +import pika_lvgl as lv +import PikaStdLib +mem = PikaStdLib.MemChecker() + +style = lv.style_t() +style.init() + +# Set a background color and a radius +style.set_radius(5) +style.set_bg_opa(lv.OPA.COVER) +style.set_bg_color(lv.palette_lighten(lv.PALETTE.GREY, 1)) + +# Add outline +style.set_outline_width(2) +style.set_outline_color(lv.palette_main(lv.PALETTE.BLUE)) +style.set_outline_pad(8) + +# Create an object with the new style +obj = lv.obj(lv.scr_act()) +obj.add_style(style, 0) +obj.center() + +print('mem used max: %0.2f kB' % (mem.getMax())) +print('mem used now: %0.2f kB' % (mem.getNow())) diff --git a/package/pika_lvgl/pika_lvgl.c b/package/pika_lvgl/pika_lvgl.c index defd076ee..3cce58639 100644 --- a/package/pika_lvgl/pika_lvgl.c +++ b/package/pika_lvgl/pika_lvgl.c @@ -2,6 +2,7 @@ #include "BaseObj.h" #include "lvgl.h" #include "pika_lvgl_arc.h" +#include "pika_lvgl_lv_color_t.h" #include "pika_lvgl_lv_obj.h" PikaObj* pika_lv_event_listener_g; @@ -129,6 +130,10 @@ void pika_lvgl___init__(PikaObj* self) { obj_setInt(self, "EVENT.LAYOUT_CHANGED", LV_EVENT_LAYOUT_CHANGED); obj_setInt(self, "EVENT.GET_SELF_SIZE", LV_EVENT_GET_SELF_SIZE); obj_setInt(self, "EVENT.PREPROCESS", LV_EVENT_PREPROCESS); + + obj_newDirectObj(self, "OPA", New_TinyObj); + obj_setInt(self, "OPA.TRANSP", LV_OPA_TRANSP); + obj_setInt(self, "OPA.COVER", LV_OPA_COVER); } PikaObj* pika_lvgl_obj(PikaObj* self, PikaObj* parent) { @@ -138,3 +143,21 @@ PikaObj* pika_lvgl_obj(PikaObj* self, PikaObj* parent) { obj_setPtr(new_obj, "lv_obj", lv_obj); return new_obj; } + +PikaObj* pika_lvgl_palette_lighten(PikaObj* self, int lvl, int p) { + PikaObj* new_obj = newNormalObj(New_pika_lvgl_lv_color_t); + lv_color_t lv_color = lv_palette_lighten(p, lvl); + args_setStruct(new_obj->list, "lv_color_struct", lv_color); + lv_color_t* plv_color = args_getStruct(new_obj->list, "lv_color_struct"); + obj_setPtr(new_obj, "lv_color",plv_color); + return new_obj; +} + +PikaObj* pika_lvgl_palette_main(PikaObj* self, int p) { + PikaObj* new_obj = newNormalObj(New_pika_lvgl_lv_color_t); + lv_color_t lv_color = lv_palette_main(p); + args_setStruct(new_obj->list, "lv_color_struct", lv_color); + obj_setPtr(new_obj, "lv_color", + args_getStruct(new_obj->list, "lv_color_struct")); + return new_obj; +} diff --git a/package/pika_lvgl/pika_lvgl.pyi b/package/pika_lvgl/pika_lvgl.pyi index b8e7d5fd2..f783882a6 100644 --- a/package/pika_lvgl/pika_lvgl.pyi +++ b/package/pika_lvgl/pika_lvgl.pyi @@ -101,6 +101,11 @@ class PALETTE: NONE: int +class OPA: + TRANSP: int + COVER: int + + class ANIM: OFF: int ON: int @@ -110,6 +115,25 @@ class lv_event(TinyObj): def get_code(self) -> int: ... +class lv_color_t(TinyObj): + ... + + +def palette_lighten(p: int, lvl: int) -> lv_color_t: ... +def palette_main(p: int) -> lv_color_t: ... + + +class style_t(TinyObj): + def __init__(self): ... + def init(self): ... + def set_radius(self, radius: int): ... + def set_bg_opa(self, opa: int): ... + def set_bg_color(self, color: lv_color_t): ... + def set_outline_width(self, w: int): ... + def set_outline_color(self, color: lv_color_t): ... + def set_outline_pad(self, pad: int): ... + + class lv_obj(TinyObj): def center(self): ... def set_size(self, size_x: int, size_y: int): ... @@ -119,6 +143,7 @@ class lv_obj(TinyObj): def set_width(self, w: int): ... def add_state(self, state: int): ... def add_event_cb(self, event_cb: any, filter: int, user_data: pointer): ... + def add_style(self, style: style_t, selector: int): ... def obj(parent: lv_obj) -> lv_obj: ... diff --git a/package/pika_lvgl/pika_lvgl_lv_obj.c b/package/pika_lvgl/pika_lvgl_lv_obj.c index da6820f29..f49203475 100644 --- a/package/pika_lvgl/pika_lvgl_lv_obj.c +++ b/package/pika_lvgl/pika_lvgl_lv_obj.c @@ -91,3 +91,9 @@ void pika_lvgl_lv_obj_add_event_cb(PikaObj* self, obj_newDirectObj(self, "_event_evt", New_pika_lvgl_lv_event); eventLicener_registEvent(pika_lv_event_listener_g, (uintptr_t)lv_obj, self); } + +void pika_lvgl_lv_obj_add_style(PikaObj* self, int selector, PikaObj* style) { + lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj"); + lv_state_t* lv_style = obj_getPtr(style, "lv_style"); + lv_obj_add_style(lv_obj, lv_style, selector); +} diff --git a/package/pika_lvgl/pika_lvgl_lv_style_t.c b/package/pika_lvgl/pika_lvgl_lv_style_t.c new file mode 100644 index 000000000..c0bf5f57d --- /dev/null +++ b/package/pika_lvgl/pika_lvgl_lv_style_t.c @@ -0,0 +1,46 @@ +#include "lvgl.h" +#include "pika_lvgl_style_t.h" + +void pika_lvgl_style_t_init(PikaObj* self) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_style_init(lv_style); +} + +void pika_lvgl_style_t_set_bg_color(PikaObj* self, PikaObj* color) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_color_t* lv_color = obj_getPtr(color, "lv_color"); + lv_style_set_bg_color(lv_style, *lv_color); +} + +void pika_lvgl_style_t_set_bg_opa(PikaObj* self, int opa) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_style_set_bg_opa(lv_style, opa); +} + +void pika_lvgl_style_t_set_outline_color(PikaObj* self, PikaObj* color) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_color_t* lv_color = obj_getPtr(color, "lv_color"); + lv_style_set_outline_color(lv_style, *lv_color); +} + +void pika_lvgl_style_t_set_outline_pad(PikaObj* self, int pad) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_style_set_outline_pad(lv_style, pad); +} + +void pika_lvgl_style_t_set_outline_width(PikaObj* self, int w) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_style_set_outline_width(lv_style, w); +} + +void pika_lvgl_style_t_set_radius(PikaObj* self, int radius) { + lv_style_t* lv_style = obj_getPtr(self, "lv_style"); + lv_style_set_radius(lv_style, radius); +} + +void pika_lvgl_style_t___init__(PikaObj* self) { + lv_style_t lv_style_stack = {0}; + args_setStruct(self->list, "lv_style_struct", lv_style_stack); + lv_style_t* lv_style = args_getStruct(self->list, "lv_style_struct"); + obj_setPtr(self, "lv_style", lv_style); +}