test.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/opt/bin/lv_micropython -i
  2. import lvgl as lv
  3. import display_driver
  4. def set_value(bar, v):
  5. bar.set_value(v, lv.ANIM.OFF)
  6. def event_cb(e):
  7. dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
  8. if dsc.part != lv.PART.INDICATOR:
  9. return
  10. obj= e.get_target()
  11. label_dsc = lv.draw_label_dsc_t()
  12. label_dsc.init()
  13. # label_dsc.font = LV_FONT_DEFAULT;
  14. value_txt = str(obj.get_value())
  15. txt_size = lv.point_t()
  16. 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)
  17. txt_area = lv.area_t()
  18. # If the indicator is long enough put the text inside on the right
  19. if dsc.draw_area.get_width() > txt_size.x + 20:
  20. txt_area.x2 = dsc.draw_area.x2 - 5
  21. txt_area.x1 = txt_area.x2 - txt_size.x + 1
  22. label_dsc.color = lv.color_white()
  23. # If the indicator is still short put the text out of it on the right*/
  24. else:
  25. txt_area.x1 = dsc.draw_area.x2 + 5
  26. txt_area.x2 = txt_area.x1 + txt_size.x - 1
  27. label_dsc.color = lv.color_black()
  28. txt_area.y1 = dsc.draw_area.y1 + (dsc.draw_area.get_height() - txt_size.y) // 2
  29. txt_area.y2 = txt_area.y1 + txt_size.y - 1
  30. dsc.draw_ctx.label(label_dsc, txt_area, value_txt, None)
  31. #
  32. # Custom drawer on the bar to display the current value
  33. #
  34. bar = lv.bar(lv.scr_act())
  35. bar.add_event_cb(event_cb, lv.EVENT.DRAW_PART_END, None)
  36. bar.set_size(200, 20)
  37. bar.center()
  38. a = lv.anim_t()
  39. a.init()
  40. a.set_var(bar)
  41. a.set_values(0, 100)
  42. a.set_custom_exec_cb(lambda a,val: set_value(bar,val))
  43. a.set_time(2000)
  44. a.set_playback_time(2000)
  45. a.set_repeat_count(lv.ANIM_REPEAT.INFINITE)
  46. lv.anim_t.start(a)