@@ -30,24 +30,24 @@ def draw_battery(back_colour,percent, win_bv):
30
30
y = 3
31
31
win_bv .area (x + 35 ,y ,40 ,24 ,back_colour )
32
32
if percent <= 120 :
33
- win_bv .text (x + 35 ,y ,str (int (min (percent ,100 ))),main_c )
33
+ win_bv .text (x + 35 ,y ,str (int (min (percent ,100 ))),main_c )
34
34
y += 2
35
35
win_bv .area (x ,y ,30 ,11 ,main_c )
36
36
win_bv .area (x + 30 ,y + 3 ,3 ,5 ,main_c )
37
-
37
+
38
38
if percent > 120 :
39
39
win_bv .area (x + 2 ,y + 2 ,26 ,7 ,ugfx .YELLOW )
40
40
elif percent > 2 :
41
41
win_bv .area (x + 2 ,y + 2 ,26 ,7 ,back_colour )
42
42
win_bv .area (x + 2 ,y + 2 ,int (min (percent ,100 )* 26 / 100 ),7 ,main_c )
43
43
else :
44
44
win_bv .area (x + 2 ,y + 2 ,26 ,7 ,ugfx .RED )
45
-
45
+
46
46
def draw_wifi (back_colour , rssi , connected , connecting , win_wifi ):
47
-
47
+
48
48
outline = [[0 ,20 ],[25 ,20 ],[25 ,0 ]]
49
49
#inline = [[3,17],[17,17],[17,3]]
50
-
50
+
51
51
#win_wifi.fill_polygon(0, 0, outline, back_colour^0xFFFF)
52
52
53
53
if connected :
@@ -57,14 +57,12 @@ def draw_wifi(back_colour, rssi, connected, connecting, win_wifi):
57
57
else :
58
58
win_wifi .fill_polygon (0 , 0 , outline , ugfx .RED )
59
59
60
- tick = 1
61
- pretick = 0
62
-
60
+ next_tick = 0
61
+ tick = True
62
+
63
63
def tick_inc (t ):
64
- global pretick
65
- pretick += 1
66
64
ugfx .poll ()
67
-
65
+
68
66
def backlight_adjust ():
69
67
if ugfx .backlight () == 0 :
70
68
ugfx .power_mode (ugfx .POWER_ON )
@@ -91,7 +89,7 @@ def low_power():
91
89
ugfx .backlight (0 )
92
90
ugfx .power_mode (ugfx .POWER_OFF )
93
91
94
-
92
+
95
93
ugfx .init ()
96
94
imu = IMU ()
97
95
ival = imu .get_acceleration ()
@@ -106,7 +104,7 @@ def low_power():
106
104
splashes = ["splash1.bmp" ]
107
105
for s in splashes :
108
106
ugfx .display_image (0 ,0 ,s )
109
- delay = 5000
107
+ delay = 5000
110
108
while delay :
111
109
delay -= 1
112
110
if buttons .is_triggered ("BTN_MENU" ):
@@ -133,58 +131,65 @@ def low_power():
133
131
134
132
while True :
135
133
# ugfx.init()
136
-
134
+
137
135
ugfx .area (0 ,0 ,320 ,240 ,sty_tb .background ())
138
-
136
+
139
137
ugfx .set_default_font (ugfx .FONT_MEDIUM )
140
138
win_bv = ugfx .Container (0 ,0 ,80 ,25 , style = sty_tb )
141
139
win_wifi = ugfx .Container (82 ,0 ,60 ,25 , style = sty_tb )
142
140
win_name = ugfx .Container (0 ,25 ,320 ,240 - 25 - 60 , style = dialogs .default_style_badge )
143
141
win_text = ugfx .Container (0 ,240 - 60 ,320 ,60 , style = sty_tb )
144
142
145
143
windows = [win_bv , win_wifi , win_text ]
146
-
144
+
147
145
obj_name = apps .home .draw_name .draw (0 ,25 ,win_name )
148
146
149
147
buttons .init ()
150
-
148
+
151
149
gc .collect ()
152
150
ugfx .set_default_font (ugfx .FONT_MEDIUM_BOLD )
153
151
l_text = ugfx .List (0 ,0 ,250 ,win_text .height (),parent = win_text )
154
-
152
+
155
153
win_bv .show ()
156
154
win_text .show ()
157
- win_wifi .show ()
158
-
155
+ win_wifi .show ()
156
+
159
157
min_ctr = 28
160
-
158
+
161
159
timer = pyb .Timer (3 )
162
160
timer .init (freq = 50 )
163
- timer .callback (tick_inc )
164
-
161
+ timer .callback (tick_inc )
162
+
165
163
ext_list = get_home_screen_background_apps ()
166
164
ext_import = []
167
165
per_freq = [];
166
+ fast_tick_items = [];
168
167
for e in ext_list :
169
- ext_import .append (__import__ (e [:- 3 ]))
170
- try :
171
- per_freq .append (ext_import [- 1 ].update_rate )
172
- except AttributeError :
173
- per_freq .append (120 )
174
-
168
+ ext_object = __import__ (e [:- 3 ])
169
+ print (dir (ext_object ))
170
+ if (hasattr (ext_object , 'update_rate' )):
171
+ ext_import .append (ext_object )
172
+ per_freq .append (ext_object .update_rate )
173
+ elif (hasattr (ext_object , 'fast_tick_rate' )):
174
+ fast_tick_items .append (ext_object )
175
+ else :
176
+ raise Exception ("Background tasks must have a tick rate!" )
177
+
178
+ print (fast_tick_items , ext_import )
179
+
175
180
icons = []
176
181
x = 150
177
- for e in ext_list :
182
+ for e in ext_import :
178
183
icons .append (ugfx .Container (x ,0 ,25 ,25 ))
179
184
x += 27
180
-
185
+
181
186
per_time_since = [200 ]* len (ext_import )
182
-
187
+
183
188
backlight_adjust ()
184
-
189
+
185
190
inactivity = 0
186
-
187
-
191
+
192
+
188
193
## start connecting to wifi in the background
189
194
wifi_timeout = 60 #seconds
190
195
wifi_reconnect_timeout = 0
@@ -193,14 +198,14 @@ def low_power():
193
198
except OSError :
194
199
print ("Creating default wifi settings file" )
195
200
wifi .create_default_config ()
196
-
201
+
197
202
while True :
198
203
pyb .wfi ()
199
-
200
- if (pretick >= 50 ):
201
- pretick = 0
202
- tick += 1
203
-
204
+
205
+ if (next_tick <= pyb . millis () ):
206
+ tick = True
207
+ next_tick = pyb . millis () + 1000
208
+
204
209
#if wifi still needs poking
205
210
if (wifi_timeout > 0 ):
206
211
if wifi .nic ().is_connected ():
@@ -210,13 +215,13 @@ def low_power():
210
215
else :
211
216
wifi .nic ().update ()
212
217
213
-
214
- if tick >= 1 :
215
- tick = 0
218
+
219
+ if tick :
220
+ tick = False
221
+
216
222
if (wifi_timeout > 0 ):
217
223
wifi_timeout -= 1 ;
218
-
219
-
224
+
220
225
# change screen orientation
221
226
ival = imu .get_acceleration ()
222
227
if ival ['y' ] < - 0.6 :
@@ -233,16 +238,16 @@ def low_power():
233
238
w .hide (); w .show ()
234
239
apps .home .draw_name .draw (0 ,25 ,win_name )
235
240
236
-
241
+
237
242
#if wifi timeout has occured and wifi isnt connected in time
238
243
if (wifi_timeout == 0 ) and not (wifi .nic ().is_connected ()):
239
244
print ("Giving up on Wifi connect" )
240
245
wifi_timeout = - 1
241
246
wifi .nic ().disconnect () #give up
242
247
wifi_reconnect_timeout = 60 #try again in 60sec
243
-
248
+
244
249
wifi_connect = wifi .nic ().is_connected ()
245
-
250
+
246
251
#if not connected, see if we should try again
247
252
if not wifi_connect :
248
253
if wifi_reconnect_timeout > 0 :
@@ -252,14 +257,14 @@ def low_power():
252
257
wifi .connect (wait = False )
253
258
254
259
draw_wifi (sty_tb .background (),0 , wifi_connect ,wifi_timeout > 0 ,win_wifi )
255
-
256
-
260
+
261
+
257
262
battery_percent = onboard .get_battery_percentage ()
258
263
draw_battery (sty_tb .background (),battery_percent ,win_bv )
259
-
264
+
260
265
min_ctr += 1
261
266
inactivity += 1
262
-
267
+
263
268
if battery_percent > 120 : #if charger plugged in
264
269
if ugfx .backlight () == 0 :
265
270
ugfx .power_mode (ugfx .POWER_ON )
@@ -269,33 +274,38 @@ def low_power():
269
274
else :
270
275
backlight_adjust ()
271
276
272
-
277
+
273
278
# dont run periodic tasks if wifi is pending
274
- if (min_ctr >= 30 ) and (wifi_timeout <= 0 ):
279
+ if (min_ctr >= 30 ) and (wifi_timeout <= 0 ):
275
280
for i in range (0 , len (ext_import )):
276
281
per_time_since [i ] += min_ctr
277
282
if per_time_since [i ] >= per_freq [i ]:
278
- per_time_since [i ] = 0
279
- e = ext_import [i ]
283
+ per_time_since [i ] = 0
284
+ e = ext_import [i ]
280
285
if "periodic_home" in dir (e ):
281
286
text = e .periodic_home (icons [i ])
282
- if not (text == None ) and len (text ) > 0 :
287
+ if not (text == None ) and len (text ) > 0 :
283
288
if (l_text .count () > 10 ):
284
289
l_text .remove_item (0 )
285
290
l_text .add_item (text )
286
291
if l_text .selected_index () >= (l_text .count ()- 2 ):
287
292
l_text .selected_index (l_text .count ()- 1 )
288
- min_ctr = 0
293
+ min_ctr = 0
294
+
295
+ for item in fast_tick_items :
296
+ if not hasattr (item , 'next_tick' ) or item .next_tick < pyb .millis ():
297
+ item .next_tick = pyb .millis () + item .fast_tick_rate
298
+ item .fast_tick ()
289
299
290
300
if buttons .is_triggered ("BTN_MENU" ):
291
301
break
292
302
if buttons .is_triggered ("BTN_A" ):
293
303
inactivity = 0
294
- tick = 1
304
+ tick = True
295
305
if buttons .is_triggered ("BTN_B" ):
296
306
inactivity = 0
297
- tick = 1
298
-
307
+ tick = True
308
+
299
309
#if activity:
300
310
# inactivity = 0
301
311
@@ -312,13 +322,12 @@ def low_power():
312
322
ugfx .power_mode (ugfx .POWER_ON )
313
323
ugfx .backlight (100 )
314
324
ugfx .orientation (180 )
315
-
325
+
316
326
#if we havnt connected yet then give up since the periodic function wont be poked
317
327
if wifi_timeout >= 0 : # not (wifi.nic().is_connected()):
318
328
wifi .nic ().disconnect ()
319
-
329
+
320
330
gc .collect ()
321
-
331
+
322
332
## ToDo: Maybe boot should always chdir to the app folder?
323
333
execfile ("apps/home/quick_launch.py" )
324
-
0 commit comments