1#!//opt/bin/lv_micropython -i
2import utime as time
3import lvgl as lv
4import display_driver
5from imagetools import get_png_info, open_png
6
7# Register PNG image decoder
8decoder = lv.img.decoder_create()
9decoder.info_cb = get_png_info
10decoder.open_cb = open_png
11
12# Create an image from the png file
13try:
14    with open('../../assets/img_hand_min.png','rb') as f:
15        img_hand_min_data = f.read()
16except:
17    print("Could not find img_hand_min.png")
18    sys.exit()
19
20img_hand_min_dsc = lv.img_dsc_t({
21  'data_size': len(img_hand_min_data),
22  'data': img_hand_min_data
23})
24
25# Create an image from the png file
26try:
27    with open('../../assets/img_hand_hour.png','rb') as f:
28        img_hand_hour_data = f.read()
29except:
30    print("Could not find img_hand_hour.png")
31    sys.exit()
32
33img_hand_hour_dsc = lv.img_dsc_t({
34  'data_size': len(img_hand_hour_data),
35  'data': img_hand_hour_data
36})
37
38def set_value(indic, v):
39    meter.set_indicator_value(indic, v)
40#
41# A clock from a meter
42#
43
44meter = lv.meter(lv.scr_act())
45meter.set_size(220, 220)
46meter.center()
47
48# Create a scale for the minutes
49# 61 ticks in a 360 degrees range (the last and the first line overlaps)
50scale_min = meter.add_scale()
51meter.set_scale_ticks(scale_min, 61, 1, 10, lv.palette_main(lv.PALETTE.GREY))
52meter.set_scale_range(scale_min, 0, 60, 360, 270)
53
54# Create another scale for the hours. It's only visual and contains only major ticks
55scale_hour = meter.add_scale()
56meter.set_scale_ticks(scale_hour, 12, 0, 0, lv.palette_main(lv.PALETTE.GREY))  # 12 ticks
57meter.set_scale_major_ticks(scale_hour, 1, 2, 20, lv.color_black(), 10)         # Every tick is major
58meter.set_scale_range(scale_hour, 1, 12, 330, 300)                             # [1..12] values in an almost full circle
59
60#    LV_IMG_DECLARE(img_hand)
61
62# Add the hands from images
63indic_min = meter.add_needle_img(scale_min, img_hand_min_dsc, 5, 5)
64indic_hour = meter.add_needle_img(scale_min, img_hand_hour_dsc, 5, 5)
65
66# Create an animation to set the value
67a1 = lv.anim_t()
68a1.init()
69a1.set_values(0, 60)
70a1.set_repeat_count(lv.ANIM_REPEAT.INFINITE)
71a1.set_time(2000)        # 2 sec for 1 turn of the minute hand (1 hour)
72a1.set_var(indic_min)
73a1.set_custom_exec_cb(lambda a1,val: set_value(indic_min,val))
74lv.anim_t.start(a1)
75
76a2 = lv.anim_t()
77a2.init()
78a2.set_var(indic_hour)
79a2.set_time(24000)       # 24 sec for 1 turn of the hour hand
80a2.set_values(0, 60)
81a2.set_custom_exec_cb(lambda a2,val: set_value(indic_hour,val))
82lv.anim_t.start(a2)
83
84