example(micropython): updated MicroPython examples according to LVGL changes (#4031)

This commit is contained in:
Uli Raich
2023-03-10 21:44:12 +01:00
committed by GitHub
parent 55c9a9220b
commit d1c6c61780
24 changed files with 394 additions and 225 deletions

View File

@@ -1,13 +1,18 @@
#!/opt/bin/lv_micropython -i
import lvgl as lv
import display_driver
#
# Show line wrap, re-color, line align and text scrolling.
#
label1 = lv.label(lv.scr_act())
label1.set_long_mode(lv.label.LONG.WRAP) # Break the long lines*/
label1.set_recolor(True) # Enable re-coloring by commands in the text
label1.set_text("#0000ff Re-color# #ff00ff words# #ff0000 of a# label, align the lines to the center"
label1.set_text("#0000ff Re-color# #ff00ff words# #ff0000 of a# label, align the lines to the center "
"and wrap long text automatically.")
label1.set_width(150) # Set smaller width to make the lines wrap
label1.set_style_text_align(lv.ALIGN.CENTER, 0)
label1.set_style_text_align(lv.TEXT_ALIGN.CENTER, 0)
label1.align(lv.ALIGN.CENTER, 0, -40)

View File

@@ -11,10 +11,24 @@ fs_drv = lv.fs_drv_t()
fs_driver.fs_register(fs_drv, 'S')
try:
ltr_label.set_style_text_font(ltr_label, lv.font_montserrat_16, 0)
ltr_label.set_style_text_font(ltr_label, lv.font_montserrat_16,0)
except:
font_montserrat_16 = lv.font_load("S:../../assets/font/montserrat-16.fnt")
ltr_label.set_style_text_font(font_montserrat_16, 0)
fs_drv = lv.fs_drv_t()
fs_driver.fs_register(fs_drv, 'S')
print("montserrat-16 not enabled in lv_conf.h, dynamically loading the font")
# get the directory in which the script is running
try:
script_path = __file__[:__file__.rfind('/')] if __file__.find('/') >= 0 else '.'
except NameError:
print("Could not find script path")
script_path = ''
if script_path != '':
try:
font_montserrat_16 = lv.font_load("S:" + script_path + "/../../assets/font/montserrat-16.fnt")
ltr_label.set_style_text_font(font_montserrat_16,0)
except:
print("Cannot load font file montserrat-16.fnt")
ltr_label.set_width(310)
ltr_label.align(lv.ALIGN.TOP_LEFT, 5, 5)

View File

@@ -0,0 +1,63 @@
MASK_WIDTH = 100
MASK_HEIGHT = 45
def add_mask_event_cb(e,mask_map):
code = e.get_code()
obj = e.get_target_obj()
if code == lv.EVENT.COVER_CHECK :
e.set_cover_res(lv.COVER_RES.MASKED)
elif code == lv.EVENT.DRAW_MAIN_BEGIN:
m = lv.draw_mask_map_param_t()
obj_coords = lv.area_t()
obj.get_coords(obj_coords)
m.init(obj_coords, mask_map)
mask_id = lv.draw_mask_add(m,None)
elif code == lv.EVENT.DRAW_MAIN_END:
try:
m.free_param()
mask_id.remove_id()
except:
pass
#
# Draw label with gradient color
#
# Create the mask of a text by drawing it to a canvas
mask_map = bytearray(MASK_WIDTH * MASK_HEIGHT * 4)
# Create a "8 bit alpha" canvas and clear it
canvas = lv.canvas(lv.scr_act())
canvas.set_buffer(mask_map, MASK_WIDTH, MASK_HEIGHT, lv.COLOR_FORMAT.NATIVE)
canvas.fill_bg(lv.color_black(), lv.OPA.TRANSP)
# Draw a label to the canvas. The result "image" will be used as mask
label_dsc = lv.draw_label_dsc_t()
label_dsc.init()
label_dsc.color = lv.color_white()
label_dsc.align = lv.TEXT_ALIGN.CENTER
canvas.draw_text(5, 5, MASK_WIDTH, label_dsc, "Text with gradient")
# The mask is reads the canvas is not required anymore
canvas.delete()
# Convert the mask to A8
# This is just a work around and will be changed later
mask8 = bytearray(MASK_WIDTH * MASK_HEIGHT)
for i in range(MASK_WIDTH * MASK_HEIGHT):
#mask8[i] = lv.color_brightness(mask_c[i]);
mask8[i] = mask_map[4*i]
# Create an object from where the text will be masked out.
# Now it's a rectangle with a gradient but it could be an image too
grad = lv.obj(lv.scr_act())
grad.set_size( MASK_WIDTH, MASK_HEIGHT)
grad.center()
grad.set_style_bg_color(lv.color_hex(0xff0000), 0)
grad.set_style_bg_grad_color(lv.color_hex(0x0000ff), 0)
grad.set_style_bg_grad_dir(lv.GRAD_DIR.HOR, 0)
grad.add_event(lambda e: add_mask_event_cb(e,mask8), lv.EVENT.ALL, None)