examples(micropython): add a few missing MP examples (#3672)
This commit is contained in:
63
examples/others/gridnav/lv_example_gridnav_1.py
Normal file
63
examples/others/gridnav/lv_example_gridnav_1.py
Normal file
@@ -0,0 +1,63 @@
|
||||
#
|
||||
# Demonstrate a a basic grid navigation
|
||||
#
|
||||
# It's assumed that the default group is set and
|
||||
# there is a keyboard indev
|
||||
|
||||
cont1 = lv.obj(lv.scr_act())
|
||||
lv.gridnav_add(cont1, lv.GRIDNAV_CTRL.NONE)
|
||||
|
||||
# Use flex here, but works with grid or manually placed objects as well
|
||||
cont1.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
|
||||
cont1.set_style_bg_color(lv.palette_lighten(lv.PALETTE.BLUE, 5), lv.STATE.FOCUSED)
|
||||
cont1.set_size(lv.pct(50), lv.pct(100))
|
||||
|
||||
# Only the container needs to be in a group
|
||||
lv.group_get_default().add_obj(cont1)
|
||||
|
||||
label = lv.label(cont1)
|
||||
label.set_text("No rollover")
|
||||
|
||||
for i in range(10):
|
||||
obj = lv.btn(cont1)
|
||||
obj.set_size(70, lv.SIZE_CONTENT)
|
||||
obj.add_flag(lv.obj.FLAG.CHECKABLE)
|
||||
lv.group_remove_obj(obj) # Not needed, we use the gridnav instead
|
||||
|
||||
label = lv.label(obj)
|
||||
label.set_text("{:d}".format(i))
|
||||
label.center()
|
||||
|
||||
# Create a second container with rollover grid nav mode.
|
||||
|
||||
cont2 = lv.obj(lv.scr_act())
|
||||
lv.gridnav_add(cont2,lv.GRIDNAV_CTRL.ROLLOVER)
|
||||
cont2.set_style_bg_color(lv.palette_lighten(lv.PALETTE.BLUE, 5), lv.STATE.FOCUSED)
|
||||
cont2.set_size(lv.pct(50), lv.pct(100))
|
||||
cont2.align(lv.ALIGN.RIGHT_MID, 0, 0)
|
||||
|
||||
label = lv.label(cont2)
|
||||
label.set_width(lv.pct(100))
|
||||
label.set_text("Rollover\nUse tab to focus the other container")
|
||||
|
||||
# Only the container needs to be in a group
|
||||
lv.group_get_default().add_obj(cont2)
|
||||
|
||||
# Add and place some children manually
|
||||
ta = lv.textarea(cont2)
|
||||
ta.set_size(lv.pct(100), 80)
|
||||
ta.set_pos(0, 80);
|
||||
lv.group_remove_obj(ta) # Not needed, we use the gridnav instead
|
||||
|
||||
cb = lv.checkbox(cont2)
|
||||
cb.set_pos(0, 170)
|
||||
lv.group_remove_obj(cb) # Not needed, we use the gridnav instead
|
||||
|
||||
sw1 = lv.switch(cont2)
|
||||
sw1.set_pos(0, 200);
|
||||
lv.group_remove_obj(sw1) # Not needed, we use the gridnav instead
|
||||
|
||||
sw2 = lv.switch(cont2)
|
||||
sw2.set_pos(lv.pct(50), 200)
|
||||
lv.group_remove_obj(sw2) # Not needed, we use the gridnav instead
|
||||
|
||||
32
examples/others/gridnav/lv_example_gridnav_2.py
Normal file
32
examples/others/gridnav/lv_example_gridnav_2.py
Normal file
@@ -0,0 +1,32 @@
|
||||
#
|
||||
# Grid navigation on a list
|
||||
#
|
||||
# It's assumed that the default group is set and
|
||||
# there is a keyboard indev
|
||||
|
||||
list1 = lv.list(lv.scr_act())
|
||||
lv.gridnav_add(list1, lv.GRIDNAV_CTRL.NONE)
|
||||
list1.set_size(lv.pct(45), lv.pct(80))
|
||||
list1.align(lv.ALIGN.LEFT_MID, 5, 0)
|
||||
list1.set_style_bg_color(lv.palette_lighten(lv.PALETTE.BLUE, 5), lv.STATE.FOCUSED)
|
||||
lv.group_get_default().add_obj(list1)
|
||||
|
||||
for i in range(15):
|
||||
item_text = "File {:d}".format(i)
|
||||
item = list1.add_btn(lv.SYMBOL.FILE, item_text)
|
||||
item.set_style_bg_opa(0, 0)
|
||||
lv.group_remove_obj(item) # Not needed, we use the gridnav instead
|
||||
|
||||
list2 = lv.list(lv.scr_act())
|
||||
lv.gridnav_add(list2, lv.GRIDNAV_CTRL.ROLLOVER)
|
||||
list2.set_size(lv.pct(45), lv.pct(80))
|
||||
list2.align(lv.ALIGN.RIGHT_MID, -5, 0)
|
||||
list2.set_style_bg_color(lv.palette_lighten(lv.PALETTE.BLUE, 5), lv.STATE.FOCUSED)
|
||||
lv.group_get_default().add_obj(list2)
|
||||
|
||||
for i in range(15):
|
||||
item_text = "Folder {:d}".format(i)
|
||||
item = list2.add_btn(lv.SYMBOL.DIRECTORY, item_text)
|
||||
item.set_style_bg_opa(0, 0)
|
||||
lv.group_remove_obj(item)
|
||||
|
||||
84
examples/others/gridnav/lv_example_gridnav_3.py
Normal file
84
examples/others/gridnav/lv_example_gridnav_3.py
Normal file
@@ -0,0 +1,84 @@
|
||||
def cont_sub_event_cb(e):
|
||||
k = e.get_key()
|
||||
obj = e.get_current_target()
|
||||
if k == lv.KEY.ENTER:
|
||||
lv.group_focus_obj(obj)
|
||||
|
||||
elif k == lv.KEY.ESC:
|
||||
obj.get_group().focus_next()
|
||||
|
||||
#
|
||||
# Nested grid navigations
|
||||
#
|
||||
# It's assumed that the default group is set and
|
||||
# there is a keyboard indev*/
|
||||
|
||||
cont_main = lv.obj(lv.scr_act())
|
||||
lv.gridnav_add(cont_main,lv.GRIDNAV_CTRL.ROLLOVER | lv.GRIDNAV_CTRL.SCROLL_FIRST)
|
||||
|
||||
# Only the container needs to be in a group
|
||||
lv.group_get_default().add_obj(cont_main)
|
||||
|
||||
# Use flex here, but works with grid or manually placed objects as well
|
||||
cont_main.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
|
||||
cont_main.set_style_bg_color(lv.palette_lighten(lv.PALETTE.BLUE, 5), lv.STATE.FOCUSED)
|
||||
cont_main.set_size(lv.pct(80), lv.SIZE_CONTENT)
|
||||
|
||||
btn = lv.btn(cont_main)
|
||||
lv.group_remove_obj(btn)
|
||||
label = lv.label(btn)
|
||||
label.set_text("Button 1")
|
||||
|
||||
btn = lv.btn(cont_main)
|
||||
lv.group_remove_obj(btn)
|
||||
label = lv.label(btn)
|
||||
label.set_text("Button 2");
|
||||
|
||||
|
||||
# Create an other container with long text to show how LV_GRIDNAV_CTRL_SCROLL_FIRST works
|
||||
cont_sub1 = lv.obj(cont_main)
|
||||
cont_sub1.set_size(lv.pct(100), 100)
|
||||
|
||||
label = lv.label(cont_sub1)
|
||||
cont_sub1.set_style_bg_color(lv.palette_lighten(lv.PALETTE.RED, 5), lv.STATE.FOCUSED)
|
||||
label.set_width(lv.pct(100));
|
||||
label.set_text(
|
||||
"""I'm a very long text which makes my container scrollable.
|
||||
As LV_GRIDNAV_FLAG_SCROLL_FIRST is enabled arrows will scroll me first
|
||||
and a new objects will be focused only when an edge is reached with the scrolling.\n
|
||||
This is only some placeholder text to be sure the parent will be scrollable. \n
|
||||
Hello world!
|
||||
Hello world!
|
||||
Hello world!
|
||||
Hello world!
|
||||
Hello world!
|
||||
Hello world!
|
||||
""")
|
||||
|
||||
# Create a third container that can be focused with ENTER and contains an other grid nav
|
||||
cont_sub2 = lv.obj(cont_main)
|
||||
lv.gridnav_add(cont_sub2, lv.GRIDNAV_CTRL.ROLLOVER)
|
||||
#Only the container needs to be in a group
|
||||
lv.group_get_default().add_obj(cont_sub2)
|
||||
|
||||
cont_sub2.add_event_cb(cont_sub_event_cb, lv.EVENT.KEY, None)
|
||||
|
||||
# Use flex here, but works with grid or manually placed objects as well
|
||||
cont_sub2.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
|
||||
cont_sub2.set_style_bg_color(lv.palette_lighten(lv.PALETTE.RED, 5), lv.STATE.FOCUSED)
|
||||
cont_sub2.set_size(lv.pct(100), lv.SIZE_CONTENT)
|
||||
|
||||
label = lv.label(cont_sub2)
|
||||
label.set_text("Use ENTER/ESC to focus/defocus this container")
|
||||
label.set_width(lv.pct(100))
|
||||
|
||||
btn = lv.btn(cont_sub2)
|
||||
lv.group_remove_obj(btn)
|
||||
label = lv.label(btn)
|
||||
label.set_text("Button 3")
|
||||
|
||||
btn = lv.btn(cont_sub2)
|
||||
lv.group_remove_obj(btn)
|
||||
label = lv.label(btn)
|
||||
label.set_text("Button 4")
|
||||
|
||||
36
examples/others/gridnav/lv_example_gridnav_4.py
Normal file
36
examples/others/gridnav/lv_example_gridnav_4.py
Normal file
@@ -0,0 +1,36 @@
|
||||
def event_handler(e):
|
||||
obj = e.get_target()
|
||||
list = obj.get_parent()
|
||||
print("Clicked: " + list.get_btn_text(obj))
|
||||
|
||||
|
||||
#
|
||||
# Simple navigation on a list widget
|
||||
#
|
||||
# It's assumed that the default group is set and
|
||||
# there is a keyboard indev
|
||||
|
||||
list = lv.list(lv.scr_act())
|
||||
lv.gridnav_add(list, lv.GRIDNAV_CTRL.ROLLOVER)
|
||||
list.align(lv.ALIGN.LEFT_MID, 0, 10)
|
||||
lv.group_get_default().add_obj(list)
|
||||
|
||||
for i in range(20):
|
||||
|
||||
# Add some separators too, they are not focusable by gridnav
|
||||
if i % 5 == 0:
|
||||
txt = "Section {:d}".format(i // 5 + 1)
|
||||
# lv_snprintf(buf, sizeof(buf), "Section %d", i / 5 + 1);
|
||||
list.add_text(txt)
|
||||
|
||||
txt = "File {:d}".format(i + 1)
|
||||
#lv_snprintf(buf, sizeof(buf), "File %d", i + 1);
|
||||
item = list.add_btn(lv.SYMBOL.FILE, txt)
|
||||
item.add_event_cb(event_handler, lv.EVENT.CLICKED, None)
|
||||
lv.group_remove_obj(item) # The default group adds it automatically
|
||||
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.align(lv.ALIGN.RIGHT_MID, 0, -10)
|
||||
label = lv.label(btn)
|
||||
label.set_text("Button")
|
||||
|
||||
50
examples/others/ime/lv_example_ime_pinyin_1.py
Normal file
50
examples/others/ime/lv_example_ime_pinyin_1.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import fs_driver
|
||||
|
||||
def ta_event_cb(e,kb):
|
||||
code = e.get_code()
|
||||
ta = e.get_target()
|
||||
|
||||
if code == lv.EVENT.FOCUSED:
|
||||
if lv.indev_get_act() != None and lv.indev_get_act().get_type() != lv.INDEV_TYPE.KEYPAD :
|
||||
kb.set_textarea(ta)
|
||||
kb.clear_flag(lv.obj.FLAG.HIDDEN)
|
||||
elif code == lv.EVENT.CANCEL:
|
||||
kb.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
ta.clear_state(ta, LV_STATE_FOCUSED);
|
||||
lv.indev_reset(None, ta) # To forget the last clicked object to make it focusable again
|
||||
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
font_simsun_16_cjk = lv.font_load("S:../../assets/font/lv_font_simsun_16_cjk.fnt")
|
||||
if font_simsun_16_cjk == None:
|
||||
print("Error when loading chinese font")
|
||||
|
||||
pinyin_ime = lv.ime_pinyin(lv.scr_act())
|
||||
pinyin_ime.set_style_text_font(font_simsun_16_cjk, 0)
|
||||
# pinyin_ime.pinyin_set_dict(your_dict) # Use a custom dictionary. If it is not set, the built-in dictionary will be used.
|
||||
|
||||
# ta1
|
||||
ta1 = lv.textarea(lv.scr_act())
|
||||
ta1.set_one_line(True)
|
||||
ta1.set_style_text_font(font_simsun_16_cjk, 0)
|
||||
ta1.align(lv.ALIGN.TOP_LEFT, 0, 0)
|
||||
|
||||
# Create a keyboard and add it to ime_pinyin
|
||||
kb = lv.keyboard(lv.scr_act())
|
||||
pinyin_ime.pinyin_set_keyboard(kb)
|
||||
kb.set_textarea(ta1)
|
||||
|
||||
ta1.add_event_cb(lambda evt: ta_event_cb(evt,kb), lv.EVENT.ALL, None)
|
||||
|
||||
# Get the cand_panel, and adjust its size and position
|
||||
cand_panel = pinyin_ime.pinyin_get_cand_panel()
|
||||
cand_panel.set_size(lv.pct(100), lv.pct(10))
|
||||
cand_panel.align_to(kb, lv.ALIGN.OUT_TOP_MID, 0, 0)
|
||||
|
||||
# Try using ime_pinyin to output the Chinese below in the ta1 above
|
||||
cz_label = lv.label(lv.scr_act())
|
||||
cz_label.set_text("嵌入式系统(Embedded System),\n是一种嵌入机械或电气系统内部、具有专一功能和实时计算性能的计算机系统。")
|
||||
cz_label.set_style_text_font(font_simsun_16_cjk, 0)
|
||||
cz_label.set_width(310)
|
||||
cz_label.align_to(ta1, lv.ALIGN.OUT_BOTTOM_LEFT, 0, 0)
|
||||
|
||||
52
examples/others/ime/lv_example_ime_pinyin_2.py
Normal file
52
examples/others/ime/lv_example_ime_pinyin_2.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import fs_driver
|
||||
|
||||
def ta_event_cb(e,kb):
|
||||
code = e.get_code()
|
||||
ta = e.get_target()
|
||||
|
||||
if code == lv.EVENT.FOCUSED:
|
||||
if lv.indev_get_act() != None and lv.indev_get_act().get_type() != lv.INDEV_TYPE.KEYPAD :
|
||||
kb.set_textarea(ta)
|
||||
kb.clear_flag(lv.obj.FLAG.HIDDEN)
|
||||
elif code == lv.EVENT.READY:
|
||||
kb.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
ta.clear_state(ta, LV_STATE_FOCUSED);
|
||||
lv.indev_reset(None, ta) # To forget the last clicked object to make it focusable again
|
||||
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
font_simsun_16_cjk = lv.font_load("S:../../assets/font/lv_font_simsun_16_cjk.fnt")
|
||||
if font_simsun_16_cjk == None:
|
||||
print("Error when loading chinese font")
|
||||
|
||||
pinyin_ime = lv.ime_pinyin(lv.scr_act())
|
||||
pinyin_ime.set_style_text_font(font_simsun_16_cjk, 0)
|
||||
# pinyin_ime.pinyin_set_dict(your_dict) # Use a custom dictionary. If it is not set, the built-in dictionary will be used.
|
||||
|
||||
# ta1
|
||||
ta1 = lv.textarea(lv.scr_act())
|
||||
ta1.set_one_line(True)
|
||||
ta1.set_style_text_font(font_simsun_16_cjk, 0)
|
||||
ta1.align(lv.ALIGN.TOP_LEFT, 0, 0)
|
||||
|
||||
# Create a keyboard and add it to ime_pinyin
|
||||
kb = lv.keyboard(lv.scr_act())
|
||||
kb.set_textarea(ta1)
|
||||
|
||||
pinyin_ime.pinyin_set_keyboard(kb)
|
||||
pinyin_ime.pinyin_set_mode(lv.ime_pinyin.PINYIN_MODE.K9) # Set to 9-key input mode. Default: 26-key input(k26) mode.
|
||||
|
||||
ta1.add_event_cb(lambda evt: ta_event_cb(evt,kb), lv.EVENT.ALL, None)
|
||||
|
||||
# Get the cand_panel, and adjust its size and position
|
||||
cand_panel = pinyin_ime.pinyin_get_cand_panel()
|
||||
cand_panel.set_size(lv.pct(100), lv.pct(10))
|
||||
cand_panel.align_to(kb, lv.ALIGN.OUT_TOP_MID, 0, 0)
|
||||
|
||||
# Try using ime_pinyin to output the Chinese below in the ta1 above
|
||||
cz_label = lv.label(lv.scr_act())
|
||||
cz_label.set_text("嵌入式系统(Embedded System),\n是一种嵌入机械或电气系统内部、具有专一功能和实时计算性能的计算机系统。")
|
||||
cz_label.set_style_text_font(font_simsun_16_cjk, 0)
|
||||
cz_label.set_width(310)
|
||||
cz_label.align_to(ta1, lv.ALIGN.OUT_BOTTOM_LEFT, 0, 0)
|
||||
|
||||
40
examples/others/imgfont/lv_example_imgfont_1.py
Normal file
40
examples/others/imgfont/lv_example_imgfont_1.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import fs_driver
|
||||
import sys
|
||||
|
||||
# set LV_USE_FFMPEG to True if it is enabled in lv_conf.h
|
||||
LV_USE_FFMPEG = True
|
||||
LV_FONT_DEFAULT = lv.font_montserrat_14
|
||||
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'A')
|
||||
|
||||
# get the directory in which the script is running
|
||||
try:
|
||||
script_path = __file__[:__file__.rfind('/')] if __file__.find('/') >= 0 else '.'
|
||||
except NameError:
|
||||
script_path = ''
|
||||
|
||||
def get_imgfont_path(font, img_src, length, unicode, unicode_next,user_data) :
|
||||
if unicode < 0xf600:
|
||||
return
|
||||
if LV_USE_FFMPEG:
|
||||
path = bytes(script_path + "/../../assets/emoji/{:04X}.png".format(unicode) + "\0","ascii")
|
||||
else:
|
||||
path = bytes("A:"+ script_path + "/../../assets/emoji/{:04X}.png".format(unicode) + "\0","ascii")
|
||||
# print("image path: ",path)
|
||||
img_src.__dereference__(length)[0:len(path)] = path
|
||||
return True
|
||||
|
||||
#
|
||||
# draw img in label or span obj
|
||||
#
|
||||
imgfont = lv.imgfont_create(80, get_imgfont_path, None)
|
||||
if imgfont == None:
|
||||
print("imgfont init error")
|
||||
|
||||
imgfont.fallback = LV_FONT_DEFAULT
|
||||
|
||||
label1 = lv.label(lv.scr_act())
|
||||
label1.set_text("12\uF600\uF617AB")
|
||||
label1.set_style_text_font(imgfont, lv.PART.MAIN)
|
||||
label1.center()
|
||||
142
examples/others/msg/lv_example_msg_2.py
Normal file
142
examples/others/msg/lv_example_msg_2.py
Normal file
@@ -0,0 +1,142 @@
|
||||
from micropython import const
|
||||
|
||||
# Define a message ID
|
||||
MSG_LOGIN_ATTEMPT = const(1)
|
||||
MSG_LOG_OUT = const(2)
|
||||
MSG_LOGIN_ERROR = const(3)
|
||||
MSG_LOGIN_OK = const(4)
|
||||
|
||||
# Define the object that will be sent as msg payload
|
||||
class Message:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
def message(self):
|
||||
return self.value
|
||||
|
||||
def auth_manager(m,passwd):
|
||||
payload = m.get_payload()
|
||||
pin_act = payload.__cast__().message()
|
||||
# print("pin act: ",pin_act,end="")
|
||||
# print(", pin axpected: ",passwd)
|
||||
pin_expected = passwd
|
||||
if pin_act == pin_expected:
|
||||
lv.msg_send(MSG_LOGIN_OK, None)
|
||||
else:
|
||||
lv.msg_send(MSG_LOGIN_ERROR, Message("Incorrect PIN"))
|
||||
|
||||
def textarea_event_cb(e):
|
||||
ta = e.get_target()
|
||||
code = e.get_code()
|
||||
if code == lv.EVENT.READY:
|
||||
passwd = Message(ta.get_text())
|
||||
lv.msg_send(MSG_LOGIN_ATTEMPT, passwd)
|
||||
elif code == lv.EVENT.MSG_RECEIVED:
|
||||
m = e.get_msg()
|
||||
id = m.get_id()
|
||||
if id == MSG_LOGIN_ERROR:
|
||||
# If there was an error, clean the text area
|
||||
msg = m.get_payload().__cast__()
|
||||
if len(msg.message()):
|
||||
ta.set_text("")
|
||||
elif id == MSG_LOGIN_OK:
|
||||
ta.add_state(lv.STATE.DISABLED)
|
||||
ta.clear_state(lv.STATE.FOCUSED | lv.STATE.FOCUS_KEY)
|
||||
elif id == MSG_LOG_OUT:
|
||||
ta.set_text("");
|
||||
ta.clear_state(lv.STATE.DISABLED)
|
||||
|
||||
def log_out_event_cb(e):
|
||||
code = e.get_code()
|
||||
if code == lv.EVENT.CLICKED:
|
||||
lv.msg_send(MSG_LOG_OUT, None)
|
||||
elif code == lv.EVENT.MSG_RECEIVED:
|
||||
m = e.get_msg()
|
||||
btn = e.get_target()
|
||||
id = m.get_id()
|
||||
if id == MSG_LOGIN_OK:
|
||||
btn.clear_state(lv.STATE.DISABLED)
|
||||
elif id == MSG_LOG_OUT:
|
||||
btn.add_state(lv.STATE.DISABLED)
|
||||
|
||||
def start_engine_msg_event_cb(e):
|
||||
|
||||
m = e.get_msg()
|
||||
btn = e.get_target()
|
||||
id = m.get_id()
|
||||
if id == MSG_LOGIN_OK:
|
||||
btn.clear_state(lv.STATE.DISABLED)
|
||||
elif id == MSG_LOG_OUT:
|
||||
btn.add_state(lv.STATE.DISABLED)
|
||||
|
||||
|
||||
def info_label_msg_event_cb(e):
|
||||
label = e.get_target()
|
||||
m = e.get_msg()
|
||||
id = m.get_id()
|
||||
if id == MSG_LOGIN_ERROR:
|
||||
payload = m.get_payload()
|
||||
label.set_text(payload.__cast__().message())
|
||||
label.set_style_text_color(lv.palette_main(lv.PALETTE.RED), 0)
|
||||
elif id == MSG_LOGIN_OK:
|
||||
label.set_text("Login successful")
|
||||
label.set_style_text_color(lv.palette_main(lv.PALETTE.GREEN), 0)
|
||||
elif id == MSG_LOG_OUT:
|
||||
label.set_text("Logged out")
|
||||
label.set_style_text_color(lv.palette_main(lv.PALETTE.GREY), 0)
|
||||
|
||||
def register_auth(msg_id,auth_msg):
|
||||
lv.msg_subscribe(MSG_LOGIN_ATTEMPT, lambda m: auth_msg(m,"hello"), None)
|
||||
#
|
||||
# Simple PIN login screen.
|
||||
# No global variables are used, all state changes are communicated via messages.
|
||||
|
||||
register_auth(MSG_LOGIN_ATTEMPT,auth_manager)
|
||||
# lv.msg_subscribe_obj(MSG_LOGIN_ATTEMPT, auth_manager, "Hello")
|
||||
|
||||
# Create a slider in the center of the display
|
||||
ta = lv.textarea(lv.scr_act())
|
||||
ta.set_pos(10, 10)
|
||||
ta.set_width(200)
|
||||
ta.set_one_line(True)
|
||||
ta.set_password_mode(True)
|
||||
ta.set_placeholder_text("The password is: hello")
|
||||
ta.add_event_cb(textarea_event_cb, lv.EVENT.ALL, None)
|
||||
lv.msg_subscribe_obj(MSG_LOGIN_ERROR, ta, None)
|
||||
lv.msg_subscribe_obj(MSG_LOGIN_OK, ta, None)
|
||||
lv.msg_subscribe_obj(MSG_LOG_OUT, ta, None)
|
||||
|
||||
kb = lv.keyboard(lv.scr_act())
|
||||
kb.set_textarea(ta)
|
||||
|
||||
# Create a log out button which will be active only when logged in
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_pos(240, 10)
|
||||
btn.add_event_cb(log_out_event_cb, lv.EVENT.ALL, None)
|
||||
lv.msg_subscribe_obj(MSG_LOGIN_OK, btn, None)
|
||||
lv.msg_subscribe_obj(MSG_LOG_OUT, btn, None)
|
||||
|
||||
label = lv.label(btn);
|
||||
label.set_text("LOG OUT")
|
||||
|
||||
# Create a label to show info
|
||||
label = lv.label(lv.scr_act());
|
||||
label.set_text("")
|
||||
label.add_event_cb(info_label_msg_event_cb, lv.EVENT.MSG_RECEIVED, None)
|
||||
label.set_pos(10, 60)
|
||||
lv.msg_subscribe_obj(MSG_LOGIN_ERROR, label, None)
|
||||
lv.msg_subscribe_obj(MSG_LOGIN_OK, label, None)
|
||||
lv.msg_subscribe_obj(MSG_LOG_OUT, label, None)
|
||||
|
||||
#Create button which will be active only when logged in
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_pos(10, 80)
|
||||
btn.add_event_cb(start_engine_msg_event_cb, lv.EVENT.MSG_RECEIVED, None)
|
||||
btn.add_flag(lv.obj.FLAG.CHECKABLE)
|
||||
lv.msg_subscribe_obj(MSG_LOGIN_OK, btn, None)
|
||||
lv.msg_subscribe_obj(MSG_LOG_OUT, btn, None)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text("START ENGINE")
|
||||
|
||||
lv.msg_send(MSG_LOG_OUT, None)
|
||||
|
||||
122
examples/others/msg/lv_example_msg_3.py
Normal file
122
examples/others/msg/lv_example_msg_3.py
Normal file
@@ -0,0 +1,122 @@
|
||||
# Define a message ID
|
||||
MSG_INC = 1
|
||||
MSG_DEC = 2
|
||||
MSG_SET = 3
|
||||
MSG_UPDATE = 4
|
||||
MSG_UPDATE_REQUEST = 5
|
||||
|
||||
# Define the object that will be sent as msg payload
|
||||
class NewValue:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
def __repr__(self):
|
||||
return f"{self.value} %"
|
||||
class LV_Example_Msg_2:
|
||||
|
||||
def __init__(self):
|
||||
self.value = 10
|
||||
lv.msg_subscribe(MSG_INC, self.value_handler, None)
|
||||
lv.msg_subscribe(MSG_DEC, self.value_handler, None)
|
||||
lv.msg_subscribe(MSG_SET, self.value_handler, None)
|
||||
lv.msg_subscribe(MSG_UPDATE, self.value_handler, None)
|
||||
lv.msg_subscribe(MSG_UPDATE_REQUEST, self.value_handler, None)
|
||||
|
||||
panel = lv.obj(lv.scr_act())
|
||||
panel.set_size(250, lv.SIZE_CONTENT)
|
||||
panel.center()
|
||||
panel.set_flex_flow(lv.FLEX_FLOW.ROW)
|
||||
panel.set_flex_align(lv.FLEX_ALIGN.SPACE_BETWEEN, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.START)
|
||||
|
||||
# Up button
|
||||
btn = lv.btn(panel)
|
||||
btn.set_flex_grow(1)
|
||||
btn.add_event_cb(self.btn_event_cb, lv.EVENT.ALL, None)
|
||||
label = lv.label(btn)
|
||||
label.set_text(lv.SYMBOL.LEFT)
|
||||
label.center()
|
||||
|
||||
# Current value
|
||||
label = lv.label(panel)
|
||||
label.set_flex_grow(2)
|
||||
label.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
|
||||
label.set_text("?")
|
||||
lv.msg_subscribe_obj(MSG_UPDATE, label, None)
|
||||
label.add_event_cb(self.label_event_cb, lv.EVENT.MSG_RECEIVED, None)
|
||||
|
||||
# Down button
|
||||
btn = lv.btn(panel)
|
||||
btn.set_flex_grow(1)
|
||||
btn.add_event_cb(self.btn_event_cb, lv.EVENT.ALL, None)
|
||||
label = lv.label(btn)
|
||||
label.set_text(lv.SYMBOL.RIGHT)
|
||||
label.center()
|
||||
|
||||
# Slider
|
||||
slider = lv.slider(panel)
|
||||
slider.set_flex_grow(1)
|
||||
slider.add_flag(lv.OBJ_FLAG_FLEX_IN_NEW_TRACK)
|
||||
slider.add_event_cb(self.slider_event_cb, lv.EVENT.ALL, None)
|
||||
lv.msg_subscribe_obj(MSG_UPDATE, slider, None)
|
||||
|
||||
|
||||
# As there are new UI elements that don't know the system's state
|
||||
# send an UPDATE REQUEST message which will trigger an UPDATE message with the current value
|
||||
lv.msg_send(MSG_UPDATE_REQUEST, None)
|
||||
|
||||
def value_handler(self,m):
|
||||
old_value = self.value
|
||||
id = m.get_id()
|
||||
if id == MSG_INC:
|
||||
if self.value < 100:
|
||||
self.value +=1
|
||||
elif id == MSG_DEC:
|
||||
if self.value > 0:
|
||||
self.value -=1
|
||||
elif id == MSG_SET:
|
||||
payload = m.get_payload()
|
||||
new_value=payload.__cast__()
|
||||
self.value = new_value.value
|
||||
# print("value_handler: new value: {:d}".format(new_value.value))
|
||||
elif id == MSG_UPDATE_REQUEST:
|
||||
lv.msg_send(MSG_UPDATE, NewValue(self.value))
|
||||
|
||||
if self.value != old_value:
|
||||
lv.msg_send(MSG_UPDATE, NewValue(self.value));
|
||||
|
||||
def btn_event_cb(self,e):
|
||||
btn = e.get_target()
|
||||
code = e.get_code()
|
||||
if code == lv.EVENT.CLICKED or code == lv.EVENT.LONG_PRESSED_REPEAT:
|
||||
if btn.get_index() == 0: # rst object is the dec. button
|
||||
lv.msg_send(MSG_DEC, None)
|
||||
else :
|
||||
lv.msg_send(MSG_INC, None)
|
||||
|
||||
def label_event_cb(self,e):
|
||||
label = e.get_target()
|
||||
code = e.get_code()
|
||||
if code == lv.EVENT.MSG_RECEIVED:
|
||||
m = e.get_msg()
|
||||
if m.get_id() == MSG_UPDATE:
|
||||
payload = m.get_payload()
|
||||
value=payload.__cast__()
|
||||
# print("label_event_cb: " + str(value))
|
||||
label.set_text(str(value))
|
||||
|
||||
def slider_event_cb(self,e):
|
||||
slider = e.get_target()
|
||||
code = e.get_code()
|
||||
if code == lv.EVENT.VALUE_CHANGED:
|
||||
v = slider.get_value()
|
||||
# print("slider_event_cb: {:d}".format(v))
|
||||
lv.msg_send(MSG_SET, NewValue(v))
|
||||
|
||||
elif code == lv.EVENT.MSG_RECEIVED:
|
||||
m = e.get_msg()
|
||||
if m.get_id() == MSG_UPDATE:
|
||||
v = m.get_payload()
|
||||
value = v.__cast__()
|
||||
slider.set_value(value.value, lv.ANIM.OFF)
|
||||
|
||||
lv_example_msg_2 = LV_Example_Msg_2()
|
||||
|
||||
Reference in New Issue
Block a user