examples(micropython): add a few missing MP examples (#3672)

This commit is contained in:
Uli Raich
2022-09-21 11:21:36 +02:00
committed by GitHub
parent d5e6ffdd87
commit 29ec34be77
16 changed files with 1151 additions and 27 deletions

View File

@@ -1,24 +1,38 @@
#!/opt/bin/lv_micropython -i
import lvgl as lv
import display_driver
def event_cb(e):
code = e.get_code()
chart = e.get_target()
if code == lv.EVENT.VALUE_CHANGED:
chart.invalidate()
if code == lv.EVENT.REFR_EXT_DRAW_SIZE:
# s = lv.coord_t.__cast__(e.get_param())
# print("s: {:d}".format(s))
e.set_ext_draw_size(20)
elif code == lv.EVENT.DRAW_POST_END:
id = lv.chart.get_pressed_point(chart)
if id == lv.CHART_POINT_NONE:
id = chart.get_pressed_point()
if id == lv.CHART_POINT_NONE :
return
# print("Selected point ", id)
for i in range(len(series)):
p = lv.point_t()
chart.get_point_pos_by_id(series[i], id, p)
value = series_points[i][id]
buf = lv.SYMBOL.DUMMY + "$" + str(value)
# print("Selected point {:d}".format(id))
ser = chart.get_series_next(None)
while(ser) :
p = lv.point_t()
chart.get_point_pos_by_id(ser, id, p)
# print("point coords: x: {:d}, y: {:d}".format(p.x,p.y))
y_array = chart.get_y_array(ser)
value = y_array[id]
buf = lv.SYMBOL.DUMMY + "{:2d}".format(value)
draw_rect_dsc = lv.draw_rect_dsc_t()
draw_rect_dsc.init()
draw_rect_dsc.bg_color = lv.color_black()
@@ -26,23 +40,27 @@ def event_cb(e):
draw_rect_dsc.radius = 3
draw_rect_dsc.bg_img_src = buf
draw_rect_dsc.bg_img_recolor = lv.color_white()
a = lv.area_t()
coords = lv.area_t()
chart.get_coords(coords)
# print("coords: x1: {:d}, y1: {:d}".format(coords.x1, coords.y1))
a = lv.area_t()
a.x1 = coords.x1 + p.x - 20
a.x2 = coords.x1 + p.x + 20
a.y1 = coords.y1 + p.y - 30
a.y2 = coords.y1 + p.y - 10
clip_area = lv.area_t.__cast__(e.get_param())
lv.draw_rect(a, clip_area, draw_rect_dsc)
# print("a: x1: {:d}, x2: {:d}, y1: {:d}, y2: {:d}".format(a.x1,a.x2,a.y1,a.y2))
draw_ctx = e.get_draw_ctx()
draw_ctx.rect(draw_rect_dsc, a)
ser = chart.get_series_next(ser)
elif code == lv.EVENT.RELEASED:
chart.invalidate()
#
# Add ticks and labels to the axis and demonstrate scrolling
#
# Show the value of the pressed points
#
# Create a chart
@@ -51,6 +69,7 @@ chart.set_size(200, 150)
chart.center()
chart.add_event_cb(event_cb, lv.EVENT.ALL, None)
chart.refresh_ext_draw_size()
# Zoom in a little in X
@@ -59,15 +78,7 @@ chart.set_zoom_x(800)
# Add two data series
ser1 = chart.add_series(lv.palette_main(lv.PALETTE.RED), lv.chart.AXIS.PRIMARY_Y)
ser2 = chart.add_series(lv.palette_main(lv.PALETTE.GREEN), lv.chart.AXIS.PRIMARY_Y)
ser1_p = []
ser2_p = []
for i in range(10):
ser1_p.append(lv.rand(60,90))
ser2_p.append(lv.rand(10,40))
ser1.y_points = ser1_p
ser2.y_points = ser2_p
series = [ser1,ser2]
series_points=[ser1_p,ser2_p]
chart.set_next_value(ser1, lv.rand(60, 90))
chart.set_next_value(ser2, lv.rand(10, 40))

View File

@@ -0,0 +1,83 @@
import time
class LV_Example_Checkbox_2:
def __init__(self):
#
# Checkboxes as radio buttons
#
# The idea is to enable `LV_OBJ_FLAG_EVENT_BUBBLE` on checkboxes and process the
#`LV.EVENT.CLICKED` on the container.
# Since user_data cannot be used to pass parameters in MicroPython I use an instance variable to
# keep the index of the active button
self.active_index_1 = 0
self.active_index_2 = 0
self.style_radio = lv.style_t()
self.style_radio.init()
self.style_radio.set_radius(lv.RADIUS_CIRCLE)
self.style_radio_chk = lv.style_t()
self.style_radio_chk.init()
self.style_radio_chk.init()
self.style_radio_chk.set_bg_img_src(None)
self.cont1 = lv.obj(lv.scr_act())
self.cont1.set_flex_flow(lv.FLEX_FLOW.COLUMN)
self.cont1.set_size(lv.pct(40), lv.pct(80))
self.cont1.add_event_cb(self.radio_event_handler, lv.EVENT.CLICKED, None)
for i in range(5):
txt = "A {:d}".format(i+1)
self.radiobutton_create(self.cont1,txt)
# Make the first checkbox checked
#lv_obj_add_state(lv_obj_get_child(self.cont1, 0), LV_STATE_CHECKED);
self.cont1.get_child(0).add_state(lv.STATE.CHECKED)
self.cont2 = lv.obj(lv.scr_act())
self.cont2.set_flex_flow(lv.FLEX_FLOW.COLUMN)
self.cont2.set_size(lv.pct(40), lv.pct(80))
self.cont2.set_x(lv.pct(50))
self.cont2.add_event_cb(self.radio_event_handler, lv.EVENT.CLICKED, None)
for i in range(3):
txt = "B {:d}".format(i+1)
self.radiobutton_create(self.cont2,txt)
# Make the first checkbox checked*/
self.cont2.get_child(0).add_state(lv.STATE.CHECKED)
def radio_event_handler(self,e):
cont = e.get_current_target()
act_cb = e.get_target()
if cont == self.cont1:
active_id = self.active_index_1
else:
active_id = self.active_index_2
old_cb = cont.get_child(active_id)
# Do nothing if the container was clicked
if act_cb == cont:
return
old_cb.clear_state(lv.STATE.CHECKED) # Uncheck the previous radio button
act_cb.add_state(lv.STATE.CHECKED) # Uncheck the current radio button
if cont == self.cont1:
self.active_index_1 = act_cb.get_index()
# print("active index 1: ", self.active_index_1)
else:
self.active_index_2 = act_cb.get_index()
# print("active index 2: ", self.active_index_2)
print("Selected radio buttons: {:d}, {:d}".format(self.active_index_1, self.active_index_2))
def radiobutton_create(self,parent, txt):
obj = lv.checkbox(parent)
obj.set_text(txt)
obj.add_flag(lv.obj.FLAG.EVENT_BUBBLE)
obj.add_style(self.style_radio, lv.PART.INDICATOR)
obj.add_style(self.style_radio_chk, lv.PART.INDICATOR | lv.STATE.CHECKED)
lv_example_checkbox_2 = LV_Example_Checkbox_2()

View File

@@ -0,0 +1,26 @@
# Create an AZERTY keyboard map
kb_map = ["A", "Z", "E", "R", "T", "Y", "U", "I", "O", "P", lv.SYMBOL.BACKSPACE, "\n",
"Q", "S", "D", "F", "G", "J", "K", "L", "M", lv.SYMBOL.NEW_LINE, "\n",
"W", "X", "C", "V", "B", "N", ",", ".", ":", "!", "?", "\n",
lv.SYMBOL.CLOSE, " ", " ", " ", lv.SYMBOL.OK, None]
# Set the relative width of the buttons and other controls
kb_ctrl = [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
2, lv.btnmatrix.CTRL.HIDDEN | 2, 6, lv.btnmatrix.CTRL.HIDDEN | 2, 2]
# Create a keyboard and add the new map as USER_1 mode
kb = lv.keyboard(lv.scr_act())
kb.set_map(lv.keyboard.MODE.USER_1, kb_map, kb_ctrl)
kb.set_mode(lv.keyboard.MODE.USER_1)
# Create a text area. The keyboard will write here
ta = lv.textarea(lv.scr_act())
ta.align(lv.ALIGN.TOP_MID, 0, 10)
ta.set_size(lv.pct(90), 80)
ta.add_state(lv.STATE.FOCUSED)
kb.set_textarea(ta)

View File

@@ -0,0 +1,175 @@
from micropython import const
def create_text(parent, icon, txt, builder_variant):
obj = lv.menu_cont(parent)
img = None
label = None
if icon :
img = lv.img(obj)
img.set_src(icon)
if txt :
label = lv.label(obj)
label.set_text(txt)
label.set_long_mode(lv.label.LONG.SCROLL_CIRCULAR)
label.set_flex_grow(1)
if builder_variant == LV_MENU_ITEM_BUILDER_VARIANT_2 and icon and txt :
img.add_flag(lv.OBJ_FLAG_FLEX_IN_NEW_TRACK)
img.swap(label)
return obj
def create_slider(parent, icon, txt, min, max, val) :
obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_2)
slider = lv.slider(obj)
slider.set_flex_grow(1)
slider.set_range(min, max)
slider.set_value(val, lv.ANIM.OFF)
if icon == None :
slider.add_flag(lv.obj.FLAG_FLEX.IN_NEW_TRACK)
return obj
def create_switch(parent, icon, txt, chk) :
obj = create_text(parent, icon, txt, LV_MENU_ITEM_BUILDER_VARIANT_1)
sw = lv.switch(obj)
if chk == lv.STATE.CHECKED:
sw.add_state(chk )
else:
sw.add_state(0)
return obj
def back_event_handler(e,menu):
obj = e.get_target()
# menu = lv_event_get_user_data(e);
if menu.back_btn_is_root(obj) :
mbox1 = lv.msgbox(None, "Hello", "Root back btn click.", None, True)
mbox1.center()
def switch_handler(e,menu):
code = e.get_code()
obj = e.get_target()
if code == lv.EVENT.VALUE_CHANGED :
if obj.has_state(lv.STATE.CHECKED) :
menu.set_page(None)
menu.set_sidebar_page(root_page)
# lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(menu), 0), 0), LV_EVENT_CLICKED, NULL);
lv.event_send(menu.get_cur_sidebar_page().get_child(0).get_child(0),lv.EVENT.CLICKED,None)
else :
menu.set_sidebar_page(None)
menu.clear_history() # Clear history because we will be showing the root page later
menu.set_page(root_page)
LV_MENU_ITEM_BUILDER_VARIANT_1 = const(0)
LV_MENU_ITEM_BUILDER_VARIANT_2 = const(1)
LV_HOR_RES = lv.scr_act().get_disp().driver.hor_res
LV_VER_RES = lv.scr_act().get_disp().driver.ver_res
menu = lv.menu(lv.scr_act())
bg_color = menu.get_style_bg_color(0)
if bg_color.color_brightness() > 127 :
menu.set_style_bg_color(menu.get_style_bg_color(0).color_darken(10),0)
else :
menu.set_style_bg_color(menu.get_style_bg_color(0).color_darken(50),0)
menu.set_mode_root_back_btn(lv.menu.ROOT_BACK_BTN.ENABLED)
menu.add_event_cb(lambda evt: back_event_handler(evt,menu), lv.EVENT.CLICKED, None)
menu.set_size(LV_HOR_RES, LV_VER_RES )
menu.center()
# Create sub pages
sub_mechanics_page = lv.menu_page(menu, None)
sub_mechanics_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
lv.menu_separator(sub_mechanics_page)
section = lv.menu_section(sub_mechanics_page);
create_slider(section,lv.SYMBOL.SETTINGS, "Velocity", 0, 150, 120)
create_slider(section,lv.SYMBOL.SETTINGS, "Acceleration", 0, 150, 50)
create_slider(section,lv.SYMBOL.SETTINGS, "Weight limit", 0, 150, 80)
sub_sound_page = lv.menu_page(menu, None)
sub_sound_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
lv.menu_separator(sub_sound_page)
section = lv.menu_section(sub_sound_page)
create_switch(section,lv.SYMBOL.AUDIO, "Sound", False)
sub_display_page = lv.menu_page(menu, None)
sub_display_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
lv.menu_separator(sub_display_page)
section = lv.menu_section(sub_display_page)
create_slider(section,lv.SYMBOL.SETTINGS, "Brightness", 0, 150, 100)
sub_software_info_page = lv.menu_page(menu, None)
sub_software_info_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
section = lv.menu_section(sub_software_info_page)
create_text(section,None, "Version 1.0", LV_MENU_ITEM_BUILDER_VARIANT_1)
sub_legal_info_page = lv.menu_page(menu, None)
sub_legal_info_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
section = lv.menu_section(sub_legal_info_page)
for i in range(15):
create_text(section, None,
"This is a long long long long long long long long long text, if it is long enough it may scroll.",
LV_MENU_ITEM_BUILDER_VARIANT_1)
sub_about_page = lv.menu_page(menu, None)
sub_about_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
lv.menu_separator(sub_about_page)
section = lv.menu_section(sub_about_page)
cont = create_text(section, None, "Software information", LV_MENU_ITEM_BUILDER_VARIANT_1);
menu.set_load_page_event(cont, sub_software_info_page);
cont = create_text(section, None, "Legal information", LV_MENU_ITEM_BUILDER_VARIANT_1);
menu.set_load_page_event(cont, sub_legal_info_page)
sub_menu_mode_page = lv.menu_page(menu, None)
sub_menu_mode_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
lv.menu_separator(sub_menu_mode_page)
section = lv.menu_section(sub_menu_mode_page)
cont = create_switch(section, lv.SYMBOL.AUDIO, "Sidebar enable",True)
cont.get_child(2).add_event_cb(lambda evt: switch_handler(evt,menu), lv.EVENT.VALUE_CHANGED, None)
# Create a root page
root_page = lv.menu_page(menu, "Settings")
root_page.set_style_pad_hor(menu.get_main_header().get_style_pad_left(0),0)
section = lv.menu_section(root_page)
cont = create_text(section, lv.SYMBOL.SETTINGS, "Mechanics", LV_MENU_ITEM_BUILDER_VARIANT_1)
menu.set_load_page_event(cont, sub_mechanics_page);
cont = create_text(section, lv.SYMBOL.AUDIO, "Sound", LV_MENU_ITEM_BUILDER_VARIANT_1);
menu.set_load_page_event(cont, sub_sound_page)
cont = create_text(section, lv.SYMBOL.SETTINGS, "Display", LV_MENU_ITEM_BUILDER_VARIANT_1);
menu.set_load_page_event(cont, sub_display_page)
create_text(root_page, None, "Others", LV_MENU_ITEM_BUILDER_VARIANT_1);
section = lv.menu_section(root_page)
cont = create_text(section, None, "About", LV_MENU_ITEM_BUILDER_VARIANT_1);
menu.set_load_page_event(cont, sub_about_page)
cont = create_text(section, lv.SYMBOL.SETTINGS, "Menu mode", LV_MENU_ITEM_BUILDER_VARIANT_1);
menu.set_load_page_event(cont, sub_menu_mode_page)
menu.set_sidebar_page(root_page)
lv.event_send(menu.get_cur_sidebar_page().get_child(0).get_child(0),lv.EVENT.CLICKED,None)