11"""Some commonly used functions, like helpers"""
22import lvgl as lv
3- import qrcode
3+ # import qrcode
44import math
55from micropython import const
66import gc
1414
1515
1616def init_styles (dark = True ):
17+ # LVGL 9.x theme and style initialization
18+ disp = lv .display_get_default ()
19+
1720 if dark :
18- # Set theme
19- th = lv .theme_night_init (210 , lv .font_roboto_22 )
20- # adjusting theme
21- # background color
2221 cbg = lv .color_hex (0x192432 )
23- # ctxt = lv.color_hex(0x7f8fa4)
2422 ctxt = lv .color_hex (0xFFFFFF )
2523 cbtnrel = lv .color_hex (0x506072 )
2624 cbtnpr = lv .color_hex (0x405062 )
2725 chl = lv .color_hex (0x313E50 )
26+ cprimary = lv .palette_main (lv .PALETTE .BLUE )
27+ csecondary = lv .palette_main (lv .PALETTE .GREY )
2828 else :
29- # Set theme to light
30- # TODO: work in progress...
31- th = lv .theme_material_init (210 , lv .font_roboto_22 )
32- # adjusting theme
33- # background color
3429 cbg = lv .color_hex (0xEEEEEE )
35- # ctxt = lv.color_hex(0x7f8fa4)
36- ctxt = lv .color_hex (0 )
30+ ctxt = lv .color_hex (0x000000 )
3731 cbtnrel = lv .color_hex (0x506072 )
3832 cbtnpr = lv .color_hex (0x405062 )
3933 chl = lv .color_hex (0x313E50 )
40- th .style .label .sec .text .color = cbtnrel
41- th .style .scr .body .main_color = cbg
42- th .style .scr .body .grad_color = cbg
43- # text color
44- th .style .scr .text .color = ctxt
45- # buttons
46- # btn released
47- th .style .btn .rel .body .main_color = cbtnrel
48- th .style .btn .rel .body .grad_color = cbtnrel
49- th .style .btn .rel .body .shadow .width = 0
50- th .style .btn .rel .body .border .width = 0
51- th .style .btn .rel .body .radius = 10
52- # btn pressed
53- lv .style_copy (th .style .btn .pr , th .style .btn .rel )
54- th .style .btn .pr .body .main_color = cbtnpr
55- th .style .btn .pr .body .grad_color = cbtnpr
56- # button map released
57- th .style .btnm .btn .rel .body .main_color = cbg
58- th .style .btnm .btn .rel .body .grad_color = cbg
59- th .style .btnm .btn .rel .body .radius = 0
60- th .style .btnm .btn .rel .body .border .width = 0
61- th .style .btnm .btn .rel .body .shadow .width = 0
62- th .style .btnm .btn .rel .text .color = ctxt
63- # button map pressed
64- lv .style_copy (th .style .btnm .btn .pr , th .style .btnm .btn .rel )
65- th .style .btnm .btn .pr .body .main_color = chl
66- th .style .btnm .btn .pr .body .grad_color = chl
67- # button map toggled
68- lv .style_copy (th .style .btnm .btn .tgl_pr , th .style .btnm .btn .pr )
69- lv .style_copy (th .style .btnm .btn .tgl_rel , th .style .btnm .btn .pr )
70- # button map inactive
71- lv .style_copy (th .style .btnm .btn .ina , th .style .btnm .btn .rel )
72- th .style .btnm .btn .ina .text .opa = 80
73- # button map background
74- th .style .btnm .bg .body .opa = 0
75- th .style .btnm .bg .body .border .width = 0
76- th .style .btnm .bg .body .shadow .width = 0
77- # textarea
78- th .style .ta .oneline .body .opa = 0
79- th .style .ta .oneline .body .border .width = 0
80- th .style .ta .oneline .text .font = lv .font_roboto_28
81- th .style .ta .oneline .text .color = ctxt
82- # slider
83- th .style .slider .knob .body .main_color = cbtnrel
84- th .style .slider .knob .body .grad_color = cbtnrel
85- th .style .slider .knob .body .radius = 5
86- th .style .slider .knob .body .border .width = 0
87- # page
88- th .style .page .bg .body .opa = 0
89- th .style .page .scrl .body .opa = 0
90- th .style .page .bg .body .border .width = 0
91- th .style .page .bg .body .padding .left = 0
92- th .style .page .bg .body .padding .right = 0
93- th .style .page .bg .body .padding .top = 0
94- th .style .page .bg .body .padding .bottom = 0
95- th .style .page .scrl .body .border .width = 0
96- th .style .page .scrl .body .padding .left = 0
97- th .style .page .scrl .body .padding .right = 0
98- th .style .page .scrl .body .padding .top = 0
99- th .style .page .scrl .body .padding .bottom = 0
100-
101- lv .theme_set_current (th )
34+ cprimary = lv .palette_main (lv .PALETTE .BLUE )
35+ csecondary = lv .palette_main (lv .PALETTE .GREY )
36+
37+ # Initialize default theme
38+ th = lv .theme_default_init (disp , cprimary , csecondary , dark , lv .font_montserrat_22 )
39+ disp .set_theme (th )
40+
41+ # Store colors for later use
42+ styles ["cbg" ] = cbg
43+ styles ["ctxt" ] = ctxt
44+ styles ["cbtnrel" ] = cbtnrel
45+ styles ["cbtnpr" ] = cbtnpr
46+ styles ["chl" ] = chl
47+
48+ # Screen style
49+ styles ["scr" ] = lv .style_t ()
50+ styles ["scr" ].init ()
51+ styles ["scr" ].set_bg_color (cbg )
52+ styles ["scr" ].set_text_color (ctxt )
53+
54+ # Button style
55+ styles ["btn" ] = lv .style_t ()
56+ styles ["btn" ].init ()
57+ styles ["btn" ].set_bg_color (cbtnrel )
58+ styles ["btn" ].set_shadow_width (0 )
59+ styles ["btn" ].set_border_width (0 )
60+ styles ["btn" ].set_radius (10 )
61+
62+ # Button pressed style
63+ styles ["btn_pressed" ] = lv .style_t ()
64+ styles ["btn_pressed" ].init ()
65+ styles ["btn_pressed" ].set_bg_color (cbtnpr )
66+
67+ # Button matrix styles
68+ styles ["btnm" ] = lv .style_t ()
69+ styles ["btnm" ].init ()
70+ styles ["btnm" ].set_bg_color (cbg )
71+ styles ["btnm" ].set_radius (0 )
72+ styles ["btnm" ].set_border_width (0 )
73+ styles ["btnm" ].set_shadow_width (0 )
74+ styles ["btnm" ].set_text_color (ctxt )
75+
76+ styles ["btnm_pressed" ] = lv .style_t ()
77+ styles ["btnm_pressed" ].init ()
78+ styles ["btnm_pressed" ].set_bg_color (chl )
79+
80+ styles ["btnm_bg" ] = lv .style_t ()
81+ styles ["btnm_bg" ].init ()
82+ styles ["btnm_bg" ].set_bg_opa (0 )
83+ styles ["btnm_bg" ].set_border_width (0 )
84+ styles ["btnm_bg" ].set_shadow_width (0 )
85+
86+ # Textarea style
87+ styles ["ta" ] = lv .style_t ()
88+ styles ["ta" ].init ()
89+ styles ["ta" ].set_bg_opa (0 )
90+ styles ["ta" ].set_border_width (0 )
91+ styles ["ta" ].set_text_font (lv .font_montserrat_28 )
92+ styles ["ta" ].set_text_color (ctxt )
93+
94+ # Slider knob style
95+ styles ["slider_knob" ] = lv .style_t ()
96+ styles ["slider_knob" ].init ()
97+ styles ["slider_knob" ].set_bg_color (cbtnrel )
98+ styles ["slider_knob" ].set_radius (5 )
99+ styles ["slider_knob" ].set_border_width (0 )
102100
103101 styles ["theme" ] = th
104- # Title style - just a default style with larger font
102+
103+ # Title style
105104 styles ["title" ] = lv .style_t ()
106- lv . style_copy ( styles ["title" ], th . style . label . prim )
107- styles ["title" ].text . font = lv .font_roboto_28
108- styles ["title" ].text . color = ctxt
105+ styles ["title" ]. init ( )
106+ styles ["title" ].set_text_font ( lv .font_montserrat_28 )
107+ styles ["title" ].set_text_color ( ctxt )
109108
109+ # Hint style
110110 styles ["hint" ] = lv .style_t ()
111- lv .style_copy (styles ["hint" ], th .style .label .sec )
112- styles ["hint" ].text .font = lv .font_roboto_16
111+ styles ["hint" ].init ()
112+ styles ["hint" ].set_text_font (lv .font_montserrat_16 )
113+ styles ["hint" ].set_text_color (csecondary )
113114
115+ # Small style
114116 styles ["small" ] = lv .style_t ()
115- lv .style_copy (styles ["small" ], styles ["hint" ])
116- styles ["small" ].text .color = ctxt
117+ styles ["small" ].init ()
118+ styles ["small" ].set_text_font (lv .font_montserrat_16 )
119+ styles ["small" ].set_text_color (ctxt )
117120
121+ # Warning style
118122 styles ["warning" ] = lv .style_t ()
119- lv . style_copy ( styles ["warning" ], th . style . label . prim )
120- styles ["warning" ].text . color = lv .color_hex (0xFF9A00 )
123+ styles ["warning" ]. init ( )
124+ styles ["warning" ].set_text_color ( lv .color_hex (0xFF9A00 ) )
121125
122126def add_label (text , y = PADDING , scr = None , style = None , width = None ):
123127 """Helper functions that creates a title-styled label"""
124128 if width is None :
125129 width = HOR_RES - 2 * PADDING
126130 if scr is None :
127- scr = lv .scr_act ()
131+ scr = lv .screen_active ()
128132 lbl = lv .label (scr )
129133 lbl .set_text (text )
130134 if style in styles :
131- lbl .set_style ( 0 , styles [style ])
132- lbl .set_long_mode (lv .label .LONG . BREAK )
135+ lbl .add_style ( styles [style ], 0 )
136+ lbl .set_long_mode (lv .label .LONG_MODE . WRAP )
133137 lbl .set_width (width )
134138 lbl .set_x ((HOR_RES - width ) // 2 )
135- lbl .set_align (lv .label . ALIGN . CENTER )
139+ lbl .set_style_text_align (lv .TEXT_ALIGN . CENTER , 0 )
136140 lbl .set_y (y )
137141 return lbl
138142
139143
140144def add_button (text = None , callback = None , scr = None , y = 700 ):
141145 """Helper function that creates a button with a text label"""
142146 if scr is None :
143- scr = lv .scr_act ()
144- btn = lv .btn (scr )
147+ scr = lv .screen_active ()
148+ btn = lv .button (scr )
145149 btn .set_width (HOR_RES - 2 * PADDING )
146150 btn .set_height (BTN_HEIGHT )
147151
148152 if text is not None :
149153 lbl = lv .label (btn )
150154 lbl .set_text (text )
151- lbl .set_align ( lv . label . ALIGN . CENTER )
155+ lbl .center ( )
152156
153- btn .align (scr , lv .ALIGN .IN_TOP_MID , 0 , 0 )
157+ btn .align (lv .ALIGN .TOP_MID , 0 , 0 )
154158 btn .set_y (y )
155159
156160 if callback is not None :
157- btn .set_event_cb (callback )
161+ btn .add_event_cb (callback , lv . EVENT . CLICKED , None )
158162
159163 return btn
160164
@@ -172,14 +176,17 @@ def align_button_pair(btn1, btn2):
172176 w = (HOR_RES - 3 * PADDING ) // 2
173177 btn1 .set_width (w )
174178 btn2 .set_width (w )
179+ # Clear alignment and set explicit x positions
180+ btn1 .set_align (lv .ALIGN .DEFAULT )
181+ btn2 .set_align (lv .ALIGN .DEFAULT )
175182 btn1 .set_x (PADDING )
176- btn2 .set_x (HOR_RES // 2 + PADDING // 2 )
183+ btn2 .set_x (PADDING + w + PADDING )
177184
178185
179186def add_qrcode (text , y = QR_PADDING , scr = None , style = None , width = None ):
180187 """Helper functions that creates a title-styled label"""
181188 if scr is None :
182- scr = lv .scr_act ()
189+ scr = lv .screen_active ()
183190
184191 if width is None :
185192 width = 350
@@ -188,7 +195,7 @@ def add_qrcode(text, y=QR_PADDING, scr=None, style=None, width=None):
188195 qr .set_text (text )
189196 qr .set_size (width )
190197 qr .set_text (text )
191- qr .align (scr , lv .ALIGN .IN_TOP_MID , 0 , y )
198+ qr .align_to (scr , lv .ALIGN .TOP_MID , 0 , y )
192199 return qr
193200
194201
0 commit comments