From 06b3013de5dcf1b57005e2a3fd8e1447cc5b638b Mon Sep 17 00:00:00 2001 From: Uli Raich Date: Wed, 28 Jul 2021 14:51:41 +0200 Subject: [PATCH] feat(examples) add MicroPython version of lv_example_anim_3 and allow loading roller font dynamically (#2412) --- examples/anim/lv_example_anim_3.py | 125 ++++++++++++++++++ examples/anim/lv_example_anim_timeline_1.py | 0 .../widgets/roller/lv_example_roller_3.py | 16 ++- 3 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 examples/anim/lv_example_anim_3.py mode change 100755 => 100644 examples/anim/lv_example_anim_timeline_1.py diff --git a/examples/anim/lv_example_anim_3.py b/examples/anim/lv_example_anim_3.py new file mode 100644 index 000000000..8e687d8aa --- /dev/null +++ b/examples/anim/lv_example_anim_3.py @@ -0,0 +1,125 @@ +from micropython import const + +# the example show the use of cubic-bezier3 in animation. +# the control point P1,P2 of cubic-bezier3 can be adjusted by slider. +# and the chart shows the cubic-bezier3 in real time. you can click +# run button see animation in current point of cubic-bezier3. +# + +CHART_POINTS_NUM = const(256) +def LV_GRID_FR(x): + return lv.COORD.MAX -100 +x + +class LvExampleAnim_3(): + # + # create an animation + # + def __init__(self): + # Create a container with grid + col_dsc = [LV_GRID_FR(1), 200, LV_GRID_FR(1), lv.GRID_TEMPLATE.LAST] + row_dsc = [30, 10, 10, LV_GRID_FR(1),lv.GRID_TEMPLATE.LAST] + + self.p1 = 0 + self.p2 = 0 + self.cont = lv.obj(lv.scr_act()) + self.cont.set_style_pad_all(2, lv.PART.MAIN) + self.cont.set_style_pad_column(10, lv.PART.MAIN) + self.cont.set_style_pad_row(10, lv.PART.MAIN) + self.cont.set_grid_dsc_array(col_dsc, row_dsc) + self.cont.set_size(320, 240) + self.cont.center() + self.page_obj_init(self.cont) + + self.a = lv.anim_t() + self.a.init() + self.a.set_var(self.anim_obj) + end = self.cont.get_style_width(lv.PART.MAIN) - self.anim_obj.get_style_width(lv.PART.MAIN) - 10 + self.a.set_values(5, end) + self.a.set_time(2000) + self.a.set_custom_exec_cb(lambda a,val: self.anim_x_cb(self.anim_obj,val)) + self.a.set_path_cb(self.anim_path_bezier3_cb) + self.refer_chart_cubic_bezier() + + def page_obj_init(self,par): + self.anim_obj = lv.obj(par) + self.anim_obj.set_size(30, 30) + self.anim_obj.set_align(lv.ALIGN.TOP_LEFT) + self.anim_obj.clear_flag(lv.obj.FLAG.SCROLLABLE) + self.anim_obj.set_style_bg_color(lv.palette_main(lv.PALETTE.RED), lv.PART.MAIN) + self.anim_obj.set_grid_cell(lv.GRID_ALIGN.START, 0, 1,lv.GRID_ALIGN.START, 0, 1) + + self.p1_label = lv.label(par) + self.p2_label = lv.label(par) + self.p1_label.set_text("p1:0"); + self.p2_label.set_text("p2:0") + self.p1_label.set_grid_cell(lv.GRID_ALIGN.START, 0, 1,lv.GRID_ALIGN.START, 1, 1) + self.p2_label.set_grid_cell(lv.GRID_ALIGN.START, 0, 1,lv.GRID_ALIGN.START, 2, 1) + + self.p1_slider = lv.slider(par) + self.p2_slider = lv.slider(par) + self.p1_slider.set_range(0, 1024) + self.p2_slider.set_range(0, 1024) + self.p1_slider.set_style_pad_all(2, lv.PART.KNOB) + self.p2_slider.set_style_pad_all(2, lv.PART.KNOB) + self.p1_slider.add_event_cb(self.slider_event_cb, lv.EVENT.VALUE_CHANGED, None) + self.p2_slider.add_event_cb(self.slider_event_cb, lv.EVENT.VALUE_CHANGED, None) + self.p1_slider.set_grid_cell(lv.GRID_ALIGN.STRETCH, 1, 1,lv.GRID_ALIGN.START, 1, 1) + self.p2_slider.set_grid_cell(lv.GRID_ALIGN.STRETCH, 1, 1,lv.GRID_ALIGN.START, 2, 1) + + self.run_btn = lv.btn(par) + self.run_btn.add_event_cb(self.run_btn_event_handler, lv.EVENT.CLICKED, None) + btn_label = lv.label(self.run_btn) + btn_label.set_text(lv.SYMBOL.PLAY) + btn_label.center() + self.run_btn.set_grid_cell(lv.GRID_ALIGN.STRETCH, 2, 1,lv.GRID_ALIGN.STRETCH, 1, 2) + + self.chart = lv.chart(par) + self.chart.set_style_pad_all(0, lv.PART.MAIN) + self.chart.set_style_size(0, lv.PART.INDICATOR) + self.chart.set_type(lv.chart.TYPE.SCATTER) + self.ser1 = self.chart.add_series(lv.palette_main(lv.PALETTE.RED), lv.chart.AXIS.PRIMARY_Y) + self.chart.set_range(lv.chart.AXIS.PRIMARY_Y, 0, 1024) + self.chart.set_range(lv.chart.AXIS.PRIMARY_X, 0, 1024) + self.chart.set_point_count(CHART_POINTS_NUM); + self.chart.set_grid_cell(lv.GRID_ALIGN.STRETCH, 0, 3,lv.GRID_ALIGN.STRETCH, 3, 1) + + def refer_chart_cubic_bezier(self): + for i in range(CHART_POINTS_NUM+1): + t = i * (1024 // CHART_POINTS_NUM) + step = lv.bezier3(t, 0, self.p1, self.p2, 1024) + self.chart.set_value_by_id2(self.ser1, i, t, step) + self.chart.refresh() + + def slider_event_cb(self,e): + slider = e.get_target() + + if slider == self.p1_slider: + label = self.p1_label; + self.p1 = slider.get_value() + label.set_text("p1: {:d}".format(self.p1)) + else: + label = self.p2_label + self.p2 = slider.get_value() + label.set_text("p1: {:d}".format(self.p2)) + + self.refer_chart_cubic_bezier() + + + def run_btn_event_handler(self,e): + + code = e.get_code() + if code == lv.EVENT.CLICKED: + lv.anim_t.start(self.a) + + def anim_x_cb(self, var, v): + var.set_style_translate_x(v, lv.PART.MAIN) + + def anim_path_bezier3_cb(self,a): + t = lv.map(a.act_time, 0, a.time, 0, 1024) + step = lv.bezier3(t, 0, self.p1, self.p2, 1024) + new_value = step * (a.end_value - a.start_value) + new_value = new_value >> 10 + new_value += a.start_value + return new_value + +lv_example_anim_3 = LvExampleAnim_3() diff --git a/examples/anim/lv_example_anim_timeline_1.py b/examples/anim/lv_example_anim_timeline_1.py old mode 100755 new mode 100644 diff --git a/examples/widgets/roller/lv_example_roller_3.py b/examples/widgets/roller/lv_example_roller_3.py index f6b6e2ace..7a3a656a1 100644 --- a/examples/widgets/roller/lv_example_roller_3.py +++ b/examples/widgets/roller/lv_example_roller_3.py @@ -1,8 +1,5 @@ -#!/opt/bin/lv_micropython -i -import utime as time -import lvgl as lv -import display_driver -import usys as sys +import fs_driver +import sys class Lv_Roller_3(): @@ -29,6 +26,15 @@ class Lv_Roller_3(): #if LV_FONT_MONTSERRAT_22 # lv_obj_set_style_text_font(roller1, &lv_font_montserrat_22, LV_PART_SELECTED); #endif + try: + roller1.set_style_text_font(lv.font_montserrat_22,lv.PART.SELECTED) + except: + fs_drv = lv.fs_drv_t() + fs_driver.fs_register(fs_drv, 'S') + print("montserrat-22 not enabled in lv_conf.h, dynamically loading the font") + font_montserrat_22 = lv.font_load("S:" + "../../assets/font/montserrat-22.fnt") + roller1.set_style_text_font(font_montserrat_22,lv.PART.SELECTED) + roller1.set_options("\n".join([ "January", "February",