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