adding micropython examples (#2286)
* adding micropython examples * adding micropython examples
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
bar1 = lv.bar(lv.scr_act())
|
||||
bar1.set_size(200, 30)
|
||||
bar1.align(None, lv.ALIGN.CENTER, 0, 0)
|
||||
bar1.set_anim_time(1000)
|
||||
bar1.set_value(100, lv.ANIM.ON)
|
||||
bar1.set_size(200, 20)
|
||||
bar1.center()
|
||||
bar1.set_value(70, lv.ANIM.OFF)
|
||||
|
||||
|
||||
27
examples/widgets/bar/lv_example_bar_2.py
Normal file
27
examples/widgets/bar/lv_example_bar_2.py
Normal file
@@ -0,0 +1,27 @@
|
||||
#
|
||||
# Example of styling the bar
|
||||
#
|
||||
style_bg = lv.style_t()
|
||||
style_indic = lv.style_t()
|
||||
|
||||
style_bg.init()
|
||||
style_bg.set_border_color(lv.palette_main(lv.PALETTE.BLUE))
|
||||
style_bg.set_border_width(2)
|
||||
style_bg.set_pad_all(6) # To make the indicator smaller
|
||||
style_bg.set_radius(6)
|
||||
style_bg.set_anim_time(1000)
|
||||
|
||||
style_indic.init()
|
||||
style_indic.set_bg_opa(lv.OPA.COVER)
|
||||
style_indic.set_bg_color(lv.palette_main(lv.PALETTE.BLUE))
|
||||
style_indic.set_radius(3)
|
||||
|
||||
bar = lv.bar(lv.scr_act())
|
||||
bar.remove_style_all() # To have a clean start
|
||||
bar.add_style(style_bg, 0)
|
||||
bar.add_style(style_indic, lv.PART.INDICATOR)
|
||||
|
||||
bar.set_size(200, 20)
|
||||
bar.center()
|
||||
bar.set_value(100, lv.ANIM.ON)
|
||||
|
||||
32
examples/widgets/bar/lv_example_bar_3.py
Normal file
32
examples/widgets/bar/lv_example_bar_3.py
Normal file
@@ -0,0 +1,32 @@
|
||||
def set_temp(bar, temp):
|
||||
bar.set_value(temp, lv.ANIM.ON)
|
||||
|
||||
#
|
||||
# A temperature meter example
|
||||
#
|
||||
|
||||
|
||||
style_indic = lv.style_t()
|
||||
|
||||
style_indic.init()
|
||||
style_indic.set_bg_opa(lv.OPA.COVER)
|
||||
style_indic.set_bg_color(lv.palette_main(lv.PALETTE.RED))
|
||||
style_indic.set_bg_grad_color(lv.palette_main(lv.PALETTE.BLUE))
|
||||
style_indic.set_bg_grad_dir(lv.GRAD_DIR.VER)
|
||||
|
||||
bar = lv.bar(lv.scr_act())
|
||||
bar.add_style(style_indic, lv.PART.INDICATOR)
|
||||
bar.set_size(20, 200)
|
||||
bar.center()
|
||||
bar.set_range(-20, 40)
|
||||
|
||||
a = lv.anim_t()
|
||||
a.init()
|
||||
a.set_time(3000)
|
||||
a.set_playback_time(3000)
|
||||
a.set_var(bar)
|
||||
a.set_values(-20, 40)
|
||||
a.set_repeat_count(lv.ANIM_REPEAT.INFINITE)
|
||||
a.set_custom_exec_cb(lambda a, val: set_temp(bar,val))
|
||||
lv.anim_t.start(a)
|
||||
|
||||
45
examples/widgets/bar/lv_example_bar_4.py
Normal file
45
examples/widgets/bar/lv_example_bar_4.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#
|
||||
# get an icon
|
||||
#
|
||||
def get_icon(filename,xres,yres):
|
||||
try:
|
||||
sdl_filename = "../../assets/" + filename + "_" + str(xres) + "x" + str(yres) + "_argb8888.bin"
|
||||
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
|
||||
|
||||
icon_dsc = lv.img_dsc_t(
|
||||
{
|
||||
"header": {"always_zero": 0, "w": xres, "h": yres, "cf": lv.img.CF.TRUE_COLOR_ALPHA},
|
||||
"data": icon_data,
|
||||
"data_size": len(icon_data),
|
||||
}
|
||||
)
|
||||
return icon_dsc
|
||||
|
||||
#
|
||||
# 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()
|
||||
style_indic.set_bg_img_src(img_skew_strip_dsc)
|
||||
style_indic.set_bg_img_tiled(True);
|
||||
style_indic.set_bg_img_opa(lv.OPA._30)
|
||||
|
||||
bar = lv.bar(lv.scr_act())
|
||||
bar.add_style(style_indic, lv.PART.INDICATOR)
|
||||
|
||||
bar.set_size(260, 20)
|
||||
bar.center()
|
||||
bar.set_mode(lv.bar.MODE.RANGE)
|
||||
bar.set_value(90, lv.ANIM.OFF)
|
||||
bar.set_start_value(20, lv.ANIM.OFF)
|
||||
|
||||
|
||||
|
||||
22
examples/widgets/bar/lv_example_bar_5.py
Normal file
22
examples/widgets/bar/lv_example_bar_5.py
Normal file
@@ -0,0 +1,22 @@
|
||||
#
|
||||
# Bar with LTR and RTL base direction
|
||||
#
|
||||
|
||||
bar_ltr = lv.bar(lv.scr_act())
|
||||
bar_ltr.set_size(200, 20)
|
||||
bar_ltr.set_value(70, lv.ANIM.OFF)
|
||||
bar_ltr.align(lv.ALIGN.CENTER, 0, -30)
|
||||
|
||||
label = lv.label(lv.scr_act())
|
||||
label.set_text("Left to Right base direction")
|
||||
label.align_to(bar_ltr, lv.ALIGN.OUT_TOP_MID, 0, -5)
|
||||
|
||||
bar_rtl = lv.bar(lv.scr_act())
|
||||
bar_rtl.set_style_base_dir(lv.BASE_DIR.RTL,0)
|
||||
bar_rtl.set_size(200, 20)
|
||||
bar_rtl.set_value(70, lv.ANIM.OFF)
|
||||
bar_rtl.align(lv.ALIGN.CENTER, 0, 30)
|
||||
|
||||
label = lv.label(lv.scr_act())
|
||||
label.set_text("Right to Left base direction")
|
||||
label.align_to(bar_rtl, lv.ALIGN.OUT_TOP_MID, 0, -5)
|
||||
54
examples/widgets/bar/lv_example_bar_6.py
Normal file
54
examples/widgets/bar/lv_example_bar_6.py
Normal file
@@ -0,0 +1,54 @@
|
||||
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= lv.bar.__cast__(e.get_target())
|
||||
|
||||
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
|
||||
|
||||
lv.draw_label(txt_area, dsc.clip_area, label_dsc, value_txt, None)
|
||||
|
||||
#
|
||||
# Custom drawer on the bar to display the current value
|
||||
#
|
||||
|
||||
bar = lv.bar(lv.scr_act())
|
||||
bar.add_event_cb(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)
|
||||
|
||||
57
examples/widgets/bar/test.py
Normal file
57
examples/widgets/bar/test.py
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/opt/bin/lv_micropython -i
|
||||
import lvgl as lv
|
||||
import display_driver
|
||||
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= lv.bar.__cast__(e.get_target())
|
||||
|
||||
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
|
||||
|
||||
lv.draw_label(txt_area, dsc.clip_area, label_dsc, value_txt, None)
|
||||
|
||||
#
|
||||
# Custom drawer on the bar to display the current value
|
||||
#
|
||||
|
||||
bar = lv.bar(lv.scr_act())
|
||||
bar.add_event_cb(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)
|
||||
|
||||
Reference in New Issue
Block a user