Skip to content

Commit 74d23ea

Browse files
committed
Changed tick to use global millis counter, added fast tick support
1 parent a0b5d42 commit 74d23ea

File tree

2 files changed

+78
-66
lines changed

2 files changed

+78
-66
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232
*.dSYM/
3333

3434
.DS_Store
35+
36+
config.json
37+
wifi.json

apps/home/main.py

+75-66
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@ def draw_battery(back_colour,percent, win_bv):
3030
y=3
3131
win_bv.area(x+35,y,40,24,back_colour)
3232
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)
3434
y += 2
3535
win_bv.area(x,y,30,11,main_c)
3636
win_bv.area(x+30,y+3,3,5,main_c)
37-
37+
3838
if percent > 120:
3939
win_bv.area(x+2,y+2,26,7,ugfx.YELLOW)
4040
elif percent > 2:
4141
win_bv.area(x+2,y+2,26,7,back_colour)
4242
win_bv.area(x+2,y+2,int(min(percent,100)*26/100),7,main_c)
4343
else:
4444
win_bv.area(x+2,y+2,26,7,ugfx.RED)
45-
45+
4646
def draw_wifi(back_colour, rssi, connected, connecting, win_wifi):
47-
47+
4848
outline = [[0,20],[25,20],[25,0]]
4949
#inline = [[3,17],[17,17],[17,3]]
50-
50+
5151
#win_wifi.fill_polygon(0, 0, outline, back_colour^0xFFFF)
5252

5353
if connected:
@@ -57,14 +57,12 @@ def draw_wifi(back_colour, rssi, connected, connecting, win_wifi):
5757
else:
5858
win_wifi.fill_polygon(0, 0, outline, ugfx.RED)
5959

60-
tick = 1
61-
pretick = 0
62-
60+
next_tick = 0
61+
tick = True
62+
6363
def tick_inc(t):
64-
global pretick
65-
pretick += 1
6664
ugfx.poll()
67-
65+
6866
def backlight_adjust():
6967
if ugfx.backlight() == 0:
7068
ugfx.power_mode(ugfx.POWER_ON)
@@ -91,7 +89,7 @@ def low_power():
9189
ugfx.backlight(0)
9290
ugfx.power_mode(ugfx.POWER_OFF)
9391

94-
92+
9593
ugfx.init()
9694
imu=IMU()
9795
ival = imu.get_acceleration()
@@ -106,7 +104,7 @@ def low_power():
106104
splashes = ["splash1.bmp"]
107105
for s in splashes:
108106
ugfx.display_image(0,0,s)
109-
delay = 5000
107+
delay = 5000
110108
while delay:
111109
delay -= 1
112110
if buttons.is_triggered("BTN_MENU"):
@@ -133,58 +131,65 @@ def low_power():
133131

134132
while True:
135133
# ugfx.init()
136-
134+
137135
ugfx.area(0,0,320,240,sty_tb.background())
138-
136+
139137
ugfx.set_default_font(ugfx.FONT_MEDIUM)
140138
win_bv = ugfx.Container(0,0,80,25, style=sty_tb)
141139
win_wifi = ugfx.Container(82,0,60,25, style=sty_tb)
142140
win_name = ugfx.Container(0,25,320,240-25-60, style=dialogs.default_style_badge)
143141
win_text = ugfx.Container(0,240-60,320,60, style=sty_tb)
144142

145143
windows = [win_bv, win_wifi, win_text]
146-
144+
147145
obj_name = apps.home.draw_name.draw(0,25,win_name)
148146

149147
buttons.init()
150-
148+
151149
gc.collect()
152150
ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD)
153151
l_text = ugfx.List(0,0,250,win_text.height(),parent=win_text)
154-
152+
155153
win_bv.show()
156154
win_text.show()
157-
win_wifi.show()
158-
155+
win_wifi.show()
156+
159157
min_ctr = 28
160-
158+
161159
timer = pyb.Timer(3)
162160
timer.init(freq=50)
163-
timer.callback(tick_inc)
164-
161+
timer.callback(tick_inc)
162+
165163
ext_list = get_home_screen_background_apps()
166164
ext_import = []
167165
per_freq=[];
166+
fast_tick_items=[];
168167
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+
175180
icons = []
176181
x = 150
177-
for e in ext_list:
182+
for e in ext_import:
178183
icons.append(ugfx.Container(x,0,25,25))
179184
x += 27
180-
185+
181186
per_time_since = [200]*len(ext_import)
182-
187+
183188
backlight_adjust()
184-
189+
185190
inactivity = 0
186-
187-
191+
192+
188193
## start connecting to wifi in the background
189194
wifi_timeout = 60 #seconds
190195
wifi_reconnect_timeout = 0
@@ -193,14 +198,14 @@ def low_power():
193198
except OSError:
194199
print("Creating default wifi settings file")
195200
wifi.create_default_config()
196-
201+
197202
while True:
198203
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+
204209
#if wifi still needs poking
205210
if (wifi_timeout > 0):
206211
if wifi.nic().is_connected():
@@ -210,13 +215,13 @@ def low_power():
210215
else:
211216
wifi.nic().update()
212217

213-
214-
if tick >= 1:
215-
tick = 0
218+
219+
if tick:
220+
tick = False
221+
216222
if (wifi_timeout > 0):
217223
wifi_timeout -= 1;
218-
219-
224+
220225
# change screen orientation
221226
ival = imu.get_acceleration()
222227
if ival['y'] < -0.6:
@@ -233,16 +238,16 @@ def low_power():
233238
w.hide(); w.show()
234239
apps.home.draw_name.draw(0,25,win_name)
235240

236-
241+
237242
#if wifi timeout has occured and wifi isnt connected in time
238243
if (wifi_timeout == 0) and not (wifi.nic().is_connected()):
239244
print("Giving up on Wifi connect")
240245
wifi_timeout = -1
241246
wifi.nic().disconnect() #give up
242247
wifi_reconnect_timeout = 60 #try again in 60sec
243-
248+
244249
wifi_connect = wifi.nic().is_connected()
245-
250+
246251
#if not connected, see if we should try again
247252
if not wifi_connect:
248253
if wifi_reconnect_timeout>0:
@@ -252,14 +257,14 @@ def low_power():
252257
wifi.connect(wait = False)
253258

254259
draw_wifi(sty_tb.background(),0, wifi_connect,wifi_timeout>0,win_wifi)
255-
256-
260+
261+
257262
battery_percent = onboard.get_battery_percentage()
258263
draw_battery(sty_tb.background(),battery_percent,win_bv)
259-
264+
260265
min_ctr += 1
261266
inactivity += 1
262-
267+
263268
if battery_percent > 120: #if charger plugged in
264269
if ugfx.backlight() == 0:
265270
ugfx.power_mode(ugfx.POWER_ON)
@@ -269,33 +274,38 @@ def low_power():
269274
else:
270275
backlight_adjust()
271276

272-
277+
273278
# 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):
275280
for i in range(0, len(ext_import)):
276281
per_time_since[i] += min_ctr
277282
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]
280285
if "periodic_home" in dir(e):
281286
text = e.periodic_home(icons[i])
282-
if not (text == None) and len(text) > 0:
287+
if not (text == None) and len(text) > 0:
283288
if (l_text.count() > 10):
284289
l_text.remove_item(0)
285290
l_text.add_item(text)
286291
if l_text.selected_index() >= (l_text.count()-2):
287292
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()
289299

290300
if buttons.is_triggered("BTN_MENU"):
291301
break
292302
if buttons.is_triggered("BTN_A"):
293303
inactivity = 0
294-
tick = 1
304+
tick = True
295305
if buttons.is_triggered("BTN_B"):
296306
inactivity = 0
297-
tick = 1
298-
307+
tick = True
308+
299309
#if activity:
300310
# inactivity = 0
301311

@@ -312,13 +322,12 @@ def low_power():
312322
ugfx.power_mode(ugfx.POWER_ON)
313323
ugfx.backlight(100)
314324
ugfx.orientation(180)
315-
325+
316326
#if we havnt connected yet then give up since the periodic function wont be poked
317327
if wifi_timeout >= 0: # not (wifi.nic().is_connected()):
318328
wifi.nic().disconnect()
319-
329+
320330
gc.collect()
321-
331+
322332
## ToDo: Maybe boot should always chdir to the app folder?
323333
execfile("apps/home/quick_launch.py")
324-

0 commit comments

Comments
 (0)