example(micropython): updated MicroPython examples according to LVGL changes (#4031)
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
def event_cb(e):
|
||||
|
||||
# The original target of the event. Can be the buttons or the container
|
||||
target = e.get_target_obj()
|
||||
# print(type(target))
|
||||
|
||||
target = e.get_original_target_obj()
|
||||
|
||||
# The current target is always the container as the event is added to it
|
||||
cont = e.get_target_obj()
|
||||
|
||||
# If container was clicked do nothing
|
||||
if type(target) != type(lv.btn()):
|
||||
if target == cont:
|
||||
return
|
||||
|
||||
# Make the clicked buttons red
|
||||
@@ -16,17 +18,17 @@ def event_cb(e):
|
||||
#
|
||||
|
||||
cont = lv.obj(lv.scr_act())
|
||||
cont.set_size(320, 200)
|
||||
cont.set_size(290, 200)
|
||||
cont.center()
|
||||
cont.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
|
||||
|
||||
for i in range(30):
|
||||
btn = lv.btn(cont)
|
||||
btn.set_size(80, 50)
|
||||
btn.set_size(70, 50)
|
||||
btn.add_flag(lv.obj.FLAG.EVENT_BUBBLE)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text(str(i))
|
||||
label.set_text("{:d}".format(i))
|
||||
label.center()
|
||||
|
||||
cont.add_event(event_cb, lv.EVENT.CLICKED, None)
|
||||
|
||||
@@ -30,7 +30,7 @@ def event_cb(e, snapshot_obj):
|
||||
# no need to free the old source for snapshot_obj, gc will free it for us.
|
||||
|
||||
# take a new snapshot, overwrite the old one
|
||||
dsc = lv.snapshot_take(img.get_parent(), lv.img.CF.TRUE_COLOR_ALPHA)
|
||||
dsc = lv.snapshot_take(img.get_parent(), lv.COLOR_FORMAT.NATIVE_ALPHA)
|
||||
snapshot_obj.set_src(dsc)
|
||||
|
||||
gc.collect()
|
||||
|
||||
@@ -7,31 +7,26 @@ panel.set_size(200, 200)
|
||||
panel.center()
|
||||
|
||||
child = lv.obj(panel)
|
||||
child.set_pos(0, 0)
|
||||
child.set_pos(0, 0);
|
||||
child.set_size(70, 70)
|
||||
label = lv.label(child)
|
||||
label.set_text("Zero")
|
||||
label.center()
|
||||
|
||||
child = lv.obj(panel)
|
||||
child.set_pos(-40, 100)
|
||||
label = lv.label(child)
|
||||
label.set_text("Left")
|
||||
label.center()
|
||||
child.set_pos(160, 80)
|
||||
child.set_size(80, 80)
|
||||
|
||||
child = lv.obj(panel)
|
||||
child.set_pos(90, -30)
|
||||
label = lv.label(child)
|
||||
label.set_text("Top")
|
||||
label.center()
|
||||
child2 = lv.btn(child)
|
||||
child2.set_size(100, 50)
|
||||
|
||||
child = lv.obj(panel)
|
||||
child.set_pos(150, 80)
|
||||
label = lv.label(child)
|
||||
label = lv.label(child2)
|
||||
label.set_text("Right")
|
||||
label.center()
|
||||
|
||||
child = lv.obj(panel)
|
||||
child.set_pos(60, 170)
|
||||
child.set_pos(40, 160)
|
||||
child.set_size(100, 70)
|
||||
label = lv.label(child)
|
||||
label.set_text("Bottom")
|
||||
label.center()
|
||||
|
||||
39
examples/styles/lv_example_style_15.py
Normal file
39
examples/styles/lv_example_style_15.py
Normal file
@@ -0,0 +1,39 @@
|
||||
#
|
||||
# Opacity and Transformations
|
||||
#
|
||||
|
||||
# Normal button
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_size(100, 40)
|
||||
btn.align(lv.ALIGN.CENTER, 0, -70)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text("Normal")
|
||||
label.center()
|
||||
|
||||
# Set opacity
|
||||
# The button and the label is rendered to a layer first and that layer is blended
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_size(100, 40)
|
||||
btn.set_style_opa(lv.OPA._50, 0)
|
||||
btn.align(lv.ALIGN.CENTER, 0, 0)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text("Opa:50%")
|
||||
label.center()
|
||||
|
||||
# Set transformations
|
||||
# The button and the label is rendered to a layer first and that layer is transformed
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_size(100, 40)
|
||||
btn.set_style_transform_angle(150, 0) # 15 deg
|
||||
btn.set_style_transform_zoom(256 + 64, 0) # 1.25x
|
||||
btn.set_style_transform_pivot_x(50, 0)
|
||||
btn.set_style_transform_pivot_y(20, 0)
|
||||
btn.set_style_opa(lv.OPA._50, 0)
|
||||
btn.align(lv.ALIGN.CENTER, 0, 70)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text("Transf.")
|
||||
label.center()
|
||||
|
||||
@@ -26,7 +26,7 @@ style.set_border_color(lv.palette_main(lv.PALETTE.BLUE))
|
||||
|
||||
style.set_img_recolor(lv.palette_main(lv.PALETTE.BLUE))
|
||||
style.set_img_recolor_opa(lv.OPA._50)
|
||||
# style.set_transform_angle(300)
|
||||
style.set_transform_angle(300)
|
||||
|
||||
# Create an object with the new style
|
||||
obj = lv.img(lv.scr_act())
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
def value_changed_event_cb(e,label):
|
||||
arc = e.get_target_obj()
|
||||
|
||||
txt = "{:d}%".format(arc.get_value())
|
||||
label.set_text(txt)
|
||||
|
||||
# Rotate the label to the current position of the arc
|
||||
arc.rotate_obj_to_angle(label, 25)
|
||||
|
||||
label = lv.label(lv.scr_act())
|
||||
|
||||
# Create an Arc
|
||||
arc = lv.arc(lv.scr_act())
|
||||
arc.set_end_angle(200)
|
||||
arc.set_size(150, 150)
|
||||
arc.set_rotation(135)
|
||||
arc.set_bg_angles(0, 270)
|
||||
arc.set_value(10)
|
||||
arc.center()
|
||||
arc.add_event(lambda e: value_changed_event_cb(e,label),lv.EVENT.VALUE_CHANGED, None)
|
||||
|
||||
|
||||
# Manually update the label for the first time
|
||||
arc.send_event(lv.EVENT.VALUE_CHANGED, None)
|
||||
|
||||
|
||||
@@ -1,37 +1,24 @@
|
||||
#
|
||||
# An `lv_timer` to call periodically to set the angles of the arc
|
||||
#
|
||||
class ArcLoader():
|
||||
def __init__(self):
|
||||
self.a = 270
|
||||
|
||||
def arc_loader_cb(self,tim,arc):
|
||||
# print(tim,arc)
|
||||
self.a += 5
|
||||
|
||||
arc.set_end_angle(self.a)
|
||||
|
||||
if self.a >= 270 + 360:
|
||||
tim._del()
|
||||
|
||||
def set_angle(obj, v):
|
||||
obj.set_value(v)
|
||||
|
||||
#
|
||||
# Create an arc which acts as a loader.
|
||||
#
|
||||
|
||||
# Create an Arc
|
||||
arc = lv.arc(lv.scr_act())
|
||||
arc.set_rotation(270)
|
||||
arc.set_bg_angles(0, 360)
|
||||
arc.set_angles(270, 270)
|
||||
arc.remove_style(None, lv.PART.KNOB) # Be sure the knob is not displayed
|
||||
arc.clear_flag(lv.obj.FLAG.CLICKABLE) #To not allow adjusting by click
|
||||
arc.center()
|
||||
|
||||
# create the loader
|
||||
arc_loader = ArcLoader()
|
||||
|
||||
# Create an `lv_timer` to update the arc.
|
||||
|
||||
timer = lv.timer_create_basic()
|
||||
timer.set_period(20)
|
||||
timer.set_cb(lambda src: arc_loader.arc_loader_cb(timer,arc))
|
||||
|
||||
a = lv.anim_t()
|
||||
a.init()
|
||||
a.set_var(arc)
|
||||
a.set_time(1000)
|
||||
a.set_repeat_count(lv.ANIM_REPEAT_INFINITE) #Just for the demo
|
||||
a.set_repeat_delay(500)
|
||||
a.set_values(0, 100)
|
||||
a.set_custom_exec_cb(lambda a,val: set_angle(arc,val))
|
||||
lv.anim_t.start(a)
|
||||
|
||||
|
||||
@@ -1,30 +1,20 @@
|
||||
#
|
||||
# get an icon
|
||||
#
|
||||
def get_icon(filename,xres,yres):
|
||||
try:
|
||||
sdl_filename = "../../assets/" + filename + "_" + str(xres) + "x" + str(yres) + "_argb8888.fnt"
|
||||
print("file name: ", sdl_filename)
|
||||
with open(sdl_filename,'rb') as f:
|
||||
icon_data = f.read()
|
||||
except:
|
||||
print("Could not find image file: " + filename)
|
||||
return None
|
||||
# Create an image from the png file
|
||||
try:
|
||||
with open('../../assets/img_strip.png','rb') as f:
|
||||
png_data = f.read()
|
||||
except:
|
||||
print("Could not find img_strip.png")
|
||||
sys.exit()
|
||||
|
||||
icon_dsc = lv.img_dsc_t(
|
||||
{
|
||||
"header": {"always_zero": 0, "w": xres, "h": yres, "cf": lv.COLOR_FORMAT.NATIVE_ALPHA},
|
||||
"data": icon_data,
|
||||
"data_size": len(icon_data),
|
||||
}
|
||||
)
|
||||
return icon_dsc
|
||||
img_skew_strip_dsc = lv.img_dsc_t({
|
||||
'data_size': len(png_data),
|
||||
'data': png_data
|
||||
})
|
||||
|
||||
#
|
||||
# Bar with stripe pattern and ranged value
|
||||
#
|
||||
|
||||
img_skew_strip_dsc = get_icon("img_skew_strip",80,20)
|
||||
style_indic = lv.style_t()
|
||||
|
||||
style_indic.init()
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#!/opt/bin/lv_micropython -i
|
||||
import lvgl as lv
|
||||
|
||||
default_group = lv.group_create()
|
||||
default_group.set_default()
|
||||
|
||||
lv.sdl_window_create(480, 320)
|
||||
sdl_indev = lv.sdl_mouse_create()
|
||||
sdl_indev.set_group(default_group)
|
||||
|
||||
def set_value(bar, v):
|
||||
bar.set_value(v, lv.ANIM.OFF)
|
||||
|
||||
def event_cb(e):
|
||||
dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
|
||||
if dsc.part != lv.PART.INDICATOR:
|
||||
return
|
||||
|
||||
obj= e.get_target_obj()
|
||||
|
||||
label_dsc = lv.draw_label_dsc_t()
|
||||
label_dsc.init()
|
||||
# label_dsc.font = LV_FONT_DEFAULT;
|
||||
|
||||
value_txt = str(obj.get_value())
|
||||
txt_size = lv.point_t()
|
||||
lv.txt_get_size(txt_size, value_txt, label_dsc.font, label_dsc.letter_space, label_dsc.line_space, lv.COORD.MAX, label_dsc.flag)
|
||||
|
||||
txt_area = lv.area_t()
|
||||
# If the indicator is long enough put the text inside on the right
|
||||
if dsc.draw_area.get_width() > txt_size.x + 20:
|
||||
txt_area.x2 = dsc.draw_area.x2 - 5
|
||||
txt_area.x1 = txt_area.x2 - txt_size.x + 1
|
||||
label_dsc.color = lv.color_white()
|
||||
# If the indicator is still short put the text out of it on the right*/
|
||||
else:
|
||||
txt_area.x1 = dsc.draw_area.x2 + 5
|
||||
txt_area.x2 = txt_area.x1 + txt_size.x - 1
|
||||
label_dsc.color = lv.color_black()
|
||||
|
||||
txt_area.y1 = dsc.draw_area.y1 + (dsc.draw_area.get_height() - txt_size.y) // 2
|
||||
txt_area.y2 = txt_area.y1 + txt_size.y - 1
|
||||
|
||||
dsc.draw_ctx.label(label_dsc, txt_area, value_txt, None)
|
||||
|
||||
#
|
||||
# Custom drawer on the bar to display the current value
|
||||
#
|
||||
|
||||
bar = lv.bar(lv.scr_act())
|
||||
bar.add_event(event_cb, lv.EVENT.DRAW_PART_END, None)
|
||||
bar.set_size(200, 20)
|
||||
bar.center()
|
||||
|
||||
a = lv.anim_t()
|
||||
a.init()
|
||||
a.set_var(bar)
|
||||
a.set_values(0, 100)
|
||||
a.set_custom_exec_cb(lambda a,val: set_value(bar,val))
|
||||
a.set_time(2000)
|
||||
a.set_playback_time(2000)
|
||||
a.set_repeat_count(lv.ANIM_REPEAT_INFINITE)
|
||||
lv.anim_t.start(a)
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
_CANVAS_WIDTH = 200
|
||||
_CANVAS_HEIGHT = 150
|
||||
LV_ZOOM_NONE = 256
|
||||
LV_IMG_ZOOM_NONE = 256
|
||||
|
||||
rect_dsc = lv.draw_rect_dsc_t()
|
||||
rect_dsc.init()
|
||||
rect_dsc.radius = 10
|
||||
rect_dsc.bg_opa = lv.OPA.COVER
|
||||
rect_dsc.bg_grad.dir = lv.GRAD_DIR.HOR
|
||||
rect_dsc.bg_grad.stops[0].color = lv.palette_main(lv.PALETTE.RED)
|
||||
rect_dsc.bg_grad.stops[1].color = lv.palette_main(lv.PALETTE.BLUE)
|
||||
#rect_dsc.bg_grad.stops[0].color = lv.palette_main(lv.PALETTE.RED)
|
||||
#rect_dsc.bg_grad.stops[1].color = lv.palette_main(lv.PALETTE.BLUE)
|
||||
|
||||
rect_dsc.bg_grad.stops = [
|
||||
lv.gradient_stop_t({'color': lv.palette_main(lv.PALETTE.RED)}),
|
||||
lv.gradient_stop_t({'color': lv.palette_main(lv.PALETTE.BLUE), 'frac':0xff})
|
||||
]
|
||||
|
||||
rect_dsc.border_width = 2
|
||||
rect_dsc.border_opa = lv.OPA._90
|
||||
rect_dsc.border_color = lv.color_white()
|
||||
@@ -18,9 +24,10 @@ rect_dsc.shadow_ofs_y = 5
|
||||
|
||||
label_dsc = lv.draw_label_dsc_t()
|
||||
label_dsc.init()
|
||||
label_dsc.color = lv.palette_main(lv.PALETTE.YELLOW)
|
||||
label_dsc.color = lv.palette_main(lv.PALETTE.ORANGE)
|
||||
|
||||
cbuf = bytearray(_CANVAS_WIDTH * _CANVAS_HEIGHT * 4)
|
||||
# cbuf2 = bytearray(_CANVAS_WIDTH * _CANVAS_HEIGHT * 4)
|
||||
|
||||
canvas = lv.canvas(lv.scr_act())
|
||||
canvas.set_buffer(cbuf, _CANVAS_WIDTH, _CANVAS_HEIGHT, lv.COLOR_FORMAT.NATIVE)
|
||||
@@ -34,10 +41,14 @@ canvas.draw_text(40, 20, 100, label_dsc, "Some text on text canvas")
|
||||
# So copy the current image to buffer and rotate it to the canvas
|
||||
|
||||
img = lv.img_dsc_t()
|
||||
|
||||
img.data = cbuf[:]
|
||||
# cbuf2 = cbuf[:]
|
||||
# img.data = cbuf2
|
||||
img.header.cf = lv.COLOR_FORMAT.NATIVE
|
||||
img.header.w = _CANVAS_WIDTH
|
||||
img.header.h = _CANVAS_HEIGHT
|
||||
|
||||
canvas.fill_bg(lv.palette_lighten(lv.PALETTE.GREY, 3), lv.OPA.COVER)
|
||||
canvas.transform(img, 30, LV_ZOOM_NONE, 0, 0, _CANVAS_WIDTH // 2, _CANVAS_HEIGHT // 2, True)
|
||||
canvas.transform(img, 120, LV_IMG_ZOOM_NONE, 0, 0, _CANVAS_WIDTH // 2, _CANVAS_HEIGHT // 2, True)
|
||||
|
||||
|
||||
@@ -23,19 +23,27 @@ dsc.init()
|
||||
|
||||
dsc.color = lv.palette_main(lv.PALETTE.RED)
|
||||
|
||||
# get the directory in which the script is running
|
||||
try:
|
||||
dsc.font = lv_font_montserrat_18
|
||||
except:
|
||||
# needed for dynamic font loading
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
script_path = __file__[:__file__.rfind('/')] if __file__.find('/') >= 0 else '.'
|
||||
except NameError:
|
||||
print("Could not find script path")
|
||||
script_path = ''
|
||||
|
||||
print("Loading font montserrat_18")
|
||||
font_montserrat_18 = lv.font_load("S:../../assets/font/montserrat-18.fnt")
|
||||
if not font_montserrat_18:
|
||||
print("Font loading failed")
|
||||
else:
|
||||
dsc.font = font_montserrat_18
|
||||
if script_path != '':
|
||||
try:
|
||||
dsc.font = lv.font_montserrat_18
|
||||
except:
|
||||
# needed for dynamic font loading
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
|
||||
print("Loading font montserrat_18")
|
||||
font_montserrat_18 = lv.font_load("S:" + script_path + "/../../assets/font/montserrat-18.fnt")
|
||||
if not font_montserrat_18:
|
||||
print("Font loading failed")
|
||||
else:
|
||||
dsc.font = font_montserrat_18
|
||||
|
||||
dsc.decor = lv.TEXT_DECOR.UNDERLINE
|
||||
print('Printing "Hello"')
|
||||
|
||||
@@ -3,44 +3,78 @@ def draw_event_cb(e):
|
||||
obj = e.get_target_obj()
|
||||
|
||||
# Add the faded area before the lines are drawn
|
||||
dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
|
||||
if dsc.part != lv.PART.ITEMS:
|
||||
return
|
||||
if not dsc.p1 or not dsc.p2:
|
||||
return
|
||||
|
||||
# Add a line mask that keeps the area below the line
|
||||
line_mask_param = lv.draw_mask_line_param_t()
|
||||
line_mask_param.points_init(dsc.p1.x, dsc.p1.y, dsc.p2.x, dsc.p2.y, lv.DRAW_MASK_LINE_SIDE.BOTTOM)
|
||||
# line_mask_id = line_mask_param.draw_mask_add(None)
|
||||
line_mask_id = lv.draw_mask_add(line_mask_param, None)
|
||||
# Add a fade effect: transparent bottom covering top
|
||||
h = obj.get_height()
|
||||
fade_mask_param = lv.draw_mask_fade_param_t()
|
||||
coords = lv.area_t()
|
||||
obj.get_coords(coords)
|
||||
fade_mask_param.init(coords, lv.OPA.COVER, coords.y1 + h // 8, lv.OPA.TRANSP,coords.y2)
|
||||
fade_mask_id = lv.draw_mask_add(fade_mask_param,None)
|
||||
|
||||
# Draw a rectangle that will be affected by the mask
|
||||
draw_rect_dsc = lv.draw_rect_dsc_t()
|
||||
draw_rect_dsc.init()
|
||||
draw_rect_dsc.bg_opa = lv.OPA._20
|
||||
draw_rect_dsc.bg_color = dsc.line_dsc.color
|
||||
|
||||
a = lv.area_t()
|
||||
a.x1 = dsc.p1.x
|
||||
a.x2 = dsc.p2.x - 1
|
||||
a.y1 = min(dsc.p1.y, dsc.p2.y)
|
||||
coords = lv.area_t()
|
||||
obj.get_coords(coords)
|
||||
a.y2 = coords.y2
|
||||
dsc.draw_ctx.rect(draw_rect_dsc, a)
|
||||
|
||||
# Remove the masks
|
||||
lv.draw_mask_remove_id(line_mask_id)
|
||||
lv.draw_mask_remove_id(fade_mask_id)
|
||||
# dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
|
||||
dsc = e.get_draw_part_dsc()
|
||||
|
||||
if dsc.part == lv.PART.ITEMS:
|
||||
if not dsc.p1 or not dsc.p2:
|
||||
return
|
||||
|
||||
# Add a line mask that keeps the area below the line
|
||||
line_mask_param = lv.draw_mask_line_param_t()
|
||||
line_mask_param.points_init(dsc.p1.x, dsc.p1.y, dsc.p2.x, dsc.p2.y, lv.DRAW_MASK_LINE_SIDE.BOTTOM)
|
||||
# line_mask_id = line_mask_param.draw_mask_add(None)
|
||||
line_mask_id = lv.draw_mask_add(line_mask_param, None)
|
||||
|
||||
# Add a fade effect: transparent bottom covering top
|
||||
h = obj.get_height()
|
||||
fade_mask_param = lv.draw_mask_fade_param_t()
|
||||
coords = lv.area_t()
|
||||
obj.get_coords(coords)
|
||||
fade_mask_param.init(coords, lv.OPA.COVER, coords.y1 + h // 8, lv.OPA.TRANSP,coords.y2)
|
||||
fade_mask_id = lv.draw_mask_add(fade_mask_param,None)
|
||||
|
||||
# Draw a rectangle that will be affected by the mask
|
||||
draw_rect_dsc = lv.draw_rect_dsc_t()
|
||||
draw_rect_dsc.init()
|
||||
draw_rect_dsc.bg_opa = lv.OPA._20
|
||||
draw_rect_dsc.bg_color = dsc.line_dsc.color
|
||||
|
||||
a = lv.area_t()
|
||||
a.x1 = dsc.p1.x
|
||||
a.x2 = dsc.p2.x - 1
|
||||
a.y1 = min(dsc.p1.y, dsc.p2.y)
|
||||
coords = lv.area_t()
|
||||
obj.get_coords(coords)
|
||||
a.y2 = coords.y2
|
||||
dsc.draw_ctx.rect(draw_rect_dsc, a)
|
||||
|
||||
# Remove the masks
|
||||
lv.draw_mask_free_param(line_mask_param);
|
||||
lv.draw_mask_free_param(fade_mask_param);
|
||||
lv.draw_mask_remove_id(line_mask_id)
|
||||
lv.draw_mask_remove_id(fade_mask_id)
|
||||
|
||||
# Hook the division lines too
|
||||
elif dsc.part == lv.PART.MAIN:
|
||||
if dsc.line_dsc == None or dsc.p1 == None or dsc.p2 == None:
|
||||
return
|
||||
# Vertical line
|
||||
if dsc.p1.x == dsc.p2.x:
|
||||
dsc.line_dsc.color = lv.palette_lighten(lv.PALETTE.GREY, 1)
|
||||
if dsc.id == 3:
|
||||
dsc.line_dsc.width = 2
|
||||
dsc.line_dsc.dash_gap = 0
|
||||
dsc.line_dsc.dash_width = 0
|
||||
else :
|
||||
dsc.line_dsc.width = 1
|
||||
dsc.line_dsc.dash_gap = 6
|
||||
dsc.line_dsc.dash_width = 6
|
||||
# Horizontal line
|
||||
else :
|
||||
if dsc.id == 2:
|
||||
dsc.line_dsc.width = 2
|
||||
dsc.line_dsc.dash_gap = 0
|
||||
dsc.line_dsc.dash_width = 0
|
||||
else :
|
||||
dsc.line_dsc.width = 2
|
||||
dsc.line_dsc.dash_gap = 6
|
||||
dsc.line_dsc.dash_width = 6
|
||||
|
||||
if dsc.id == 1 or dsc.id == 3 :
|
||||
dsc.line_dsc.color = lv.palette_main(lv.PALETTE.GREEN)
|
||||
else :
|
||||
dsc.line_dsc.color = lv.palette_lighten(lv.PALETTE.GREY, 1)
|
||||
|
||||
def add_data(timer):
|
||||
# LV_UNUSED(timer);
|
||||
@@ -62,6 +96,8 @@ chart1.set_size(200, 150)
|
||||
chart1.center()
|
||||
chart1.set_type(lv.chart.TYPE.LINE) # Show lines and points too
|
||||
|
||||
chart1.set_div_line_count(5, 7)
|
||||
|
||||
chart1.add_event(draw_event_cb, lv.EVENT.DRAW_PART_BEGIN, None)
|
||||
chart1.set_update_mode(lv.chart.UPDATE_MODE.CIRCULAR)
|
||||
|
||||
|
||||
@@ -73,8 +73,7 @@ class ExampleChart_6():
|
||||
draw_rect_dsc.init()
|
||||
draw_rect_dsc.bg_color = lv.palette_main(lv.PALETTE.BLUE)
|
||||
draw_rect_dsc.radius = 3
|
||||
|
||||
lv.draw_rect(a, dsc.clip_area, draw_rect_dsc)
|
||||
dsc.draw_ctx.rect(draw_rect_dsc,a)
|
||||
|
||||
draw_label_dsc = lv.draw_label_dsc_t()
|
||||
draw_label_dsc.init()
|
||||
@@ -83,6 +82,6 @@ class ExampleChart_6():
|
||||
a.x2 -= 5
|
||||
a.y1 += 5
|
||||
a.y2 -= 5
|
||||
lv.draw_label(a, dsc.clip_area, draw_label_dsc, value_txt, None)
|
||||
dsc.draw_ctx.label(draw_label_dsc,a,value_txt,None)
|
||||
|
||||
example_chart_6 = ExampleChart_6()
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
63
examples/widgets/label/lv_example_label_4.py
Normal file
63
examples/widgets/label/lv_example_label_4.py
Normal 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)
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
#!//opt/bin/lv_micropython -i
|
||||
import utime as time
|
||||
import lvgl as lv
|
||||
import display_driver
|
||||
|
||||
def set_value(indic, v):
|
||||
meter.set_indicator_value(indic, v)
|
||||
|
||||
@@ -13,6 +9,10 @@ meter = lv.meter(lv.scr_act())
|
||||
meter.center()
|
||||
meter.set_size(200, 200)
|
||||
|
||||
#Add a scale first
|
||||
meter.set_scale_ticks(41, 2, 10, lv.palette_main(lv.PALETTE.GREY))
|
||||
meter.set_scale_major_ticks(8, 4, 15, lv.color_black(), 10)
|
||||
|
||||
indic = lv.meter_indicator_t()
|
||||
|
||||
# Add a blue arc to the start
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#!//opt/bin/lv_micropython -i
|
||||
import utime as time
|
||||
import lvgl as lv
|
||||
import display_driver
|
||||
|
||||
def set_value(indic,v):
|
||||
meter.set_indicator_end_value(indic, v)
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#!//opt/bin/lv_micropython -i
|
||||
import utime as time
|
||||
import lvgl as lv
|
||||
import display_driver
|
||||
|
||||
# Create an image from the png file
|
||||
try:
|
||||
@@ -45,7 +42,7 @@ def tick_label_event(e):
|
||||
if draw_part_dsc.id % 5: return
|
||||
|
||||
# The order of numbers on the clock is tricky: 12, 1, 2, 3...*/
|
||||
txt = ["12", "1", "2as", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
|
||||
txt = ["12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
|
||||
# dsc.text is defined char text[16], I must therefore convert the Python string to a byte_array
|
||||
|
||||
idx = int(draw_part_dsc.id / 5)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import utime as time
|
||||
|
||||
#
|
||||
# Create a pie chart
|
||||
#
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import fs_driver
|
||||
|
||||
|
||||
def event_handler(e):
|
||||
code = e.get_code()
|
||||
obj = e.get_target_obj()
|
||||
@@ -16,15 +15,30 @@ def event_handler(e):
|
||||
# A style to make the selected option larger
|
||||
style_sel = lv.style_t()
|
||||
style_sel.init()
|
||||
|
||||
|
||||
try:
|
||||
style_sel.set_text_font(lv.font_montserrat_22)
|
||||
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")
|
||||
style_sel.set_text_font(font_montserrat_22)
|
||||
|
||||
# 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_22 = lv.font_load("S:" + script_path + "/../../assets/font/montserrat-22.fnt")
|
||||
style_sel.set_text_font(font_montserrat_22)
|
||||
except:
|
||||
print("Cannot load font file montserrat-22.fnt")
|
||||
|
||||
style_sel.set_bg_color(lv.color_hex3(0xf88))
|
||||
style_sel.set_border_width(2)
|
||||
style_sel.set_border_color(lv.color_hex3(0xf00))
|
||||
|
||||
opts = "\n".join(["1","2","3","4","5","6","7","8","9","10"])
|
||||
|
||||
@@ -35,6 +49,8 @@ roller.set_visible_row_count(2)
|
||||
roller.set_width(100)
|
||||
roller.add_style(style_sel, lv.PART.SELECTED)
|
||||
roller.set_style_text_align(lv.TEXT_ALIGN.LEFT, 0)
|
||||
roller.set_style_bg_color(lv.color_hex3(0x0f0), 0)
|
||||
roller.set_style_bg_grad_color(lv.color_hex3(0xafa), 0);
|
||||
roller.align(lv.ALIGN.LEFT_MID, 10, 0)
|
||||
roller.add_event(event_handler, lv.EVENT.ALL, None)
|
||||
roller.set_selected(2, lv.ANIM.OFF)
|
||||
|
||||
@@ -32,9 +32,20 @@ class Lv_Roller_3():
|
||||
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)
|
||||
|
||||
|
||||
# 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_22 = lv.font_load("S:" + script_path + "/../../assets/font/montserrat-22.fnt")
|
||||
roller1.set_style_text_font(font_montserrat_22,lv.PART.SELECTED)
|
||||
except:
|
||||
print("Cannot load font file montserrat-22.fnt")
|
||||
|
||||
roller1.set_options("\n".join([
|
||||
"January",
|
||||
"February",
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
#!/opt/bin/lv_micropython -i
|
||||
|
||||
import lvgl as lv
|
||||
import display_driver
|
||||
|
||||
import fs_driver
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
|
||||
#
|
||||
# Create span
|
||||
#
|
||||
@@ -26,11 +35,32 @@ span.style.set_text_opa(lv.OPA._30)
|
||||
|
||||
span = spans.new_span()
|
||||
span.set_text_static("good good study, day day up.")
|
||||
|
||||
#if LV_FONT_MONTSERRAT_24
|
||||
# lv_style_set_text_font(&span->style, &lv_font_montserrat_24);
|
||||
#endif
|
||||
span.style.set_text_color(lv.palette_main(lv.PALETTE.GREEN))
|
||||
|
||||
try:
|
||||
span.style.set_text_font(ltr_label, lv.font_montserrat_24)
|
||||
except:
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
print("montserrat-24 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_24 = lv.font_load("S:" + script_path + "/../../assets/font/montserrat-24.fnt")
|
||||
span.style.set_text_font(font_montserrat_24)
|
||||
except:
|
||||
print("Cannot load font file montserrat-24.fnt")
|
||||
|
||||
span.style.set_text_color(lv.palette_main(lv.PALETTE.GREEN))
|
||||
span = spans.new_span()
|
||||
span.set_text_static("LVGL is an open-source graphics library.")
|
||||
span.style.set_text_color(lv.palette_main(lv.PALETTE.BLUE))
|
||||
@@ -41,13 +71,31 @@ span.style.set_text_color(lv.palette_main(lv.PALETTE.GREEN))
|
||||
#if LV_FONT_MONTSERRAT_20
|
||||
# lv_style_set_text_font(&span->style, &lv_font_montserrat_20);
|
||||
#endif
|
||||
try:
|
||||
span.style.set_text_font(ltr_label, lv.font_montserrat_20)
|
||||
except:
|
||||
fs_drv = lv.fs_drv_t()
|
||||
fs_driver.fs_register(fs_drv, 'S')
|
||||
print("montserrat-20 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_20 = lv.font_load("S:" + script_path + "/../../assets/font/montserrat-20.fnt")
|
||||
span.style.set_text_font(font_montserrat_20)
|
||||
except:
|
||||
print("Cannot load font file montserrat-20.fnt")
|
||||
|
||||
span.style.set_text_decor(lv.TEXT_DECOR.UNDERLINE)
|
||||
|
||||
span = spans.new_span()
|
||||
span.set_text("I have a dream that hope to come true.")
|
||||
|
||||
span.style.set_text_decor(lv.TEXT_DECOR.STRIKETHROUGH)
|
||||
spans.refr_mode()
|
||||
|
||||
# lv_span_del(spans, span);
|
||||
# lv_obj_del(spans);
|
||||
|
||||
|
||||
@@ -2,17 +2,15 @@ def event_handler(e):
|
||||
code = e.get_code()
|
||||
obj = e.get_target_obj()
|
||||
if code == lv.EVENT.VALUE_CHANGED:
|
||||
if obj.has_state(lv.STATE.CHECKED):
|
||||
print("State: on")
|
||||
else:
|
||||
print("State: off")
|
||||
print("State: ", "On" if obj.has_state(lv.STATE.CHECKED) else "Off")
|
||||
|
||||
|
||||
lv.scr_act().set_flex_flow(lv.FLEX_FLOW.COLUMN)
|
||||
lv.scr_act().set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
|
||||
|
||||
sw = lv.switch(lv.scr_act())
|
||||
sw.add_event(event_handler,lv.EVENT.ALL, None)
|
||||
sw.add_event(event_handler, lv.EVENT.ALL, None)
|
||||
sw.add_flag(lv.obj.FLAG.EVENT_BUBBLE)
|
||||
|
||||
sw = lv.switch(lv.scr_act())
|
||||
sw.add_state(lv.STATE.CHECKED)
|
||||
|
||||
Reference in New Issue
Block a user