-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (59 loc) · 1.74 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys, time
#from heap_mon import heap_info
# ruff: noqa: E402
sys.path.append("")
time.sleep(3) #chance to connect and hit ctrl-c if anything goes really wrong. Instead "press a key to contioue"
import tft_display
import gc
import asyncio
from ble import ble
from board import read_vbatt_loop
import tft_buttons
from alarms import alarms_loop
print('main: import done')
buttons = tft_buttons.Buttons()
initialized = False
def init():
global initialized
tft_display.init()
initialized = True
def deinit():
global initialized
initialized = False
tft_display.deinit()
ble.disconnect()
def set_global_exception():
def handle_exception(loop, context):
import sys
sys.print_exception(context["exception"])
sys.exit()
loop = asyncio.get_event_loop()
loop.set_exception_handler(handle_exception)
async def main():
set_global_exception()
#setup GUI loop
gui_task = asyncio.create_task(tft_display.gui.loop_forever())
vbatt_task = asyncio.create_task(read_vbatt_loop())
alarms_task = asyncio.create_task(alarms_loop())
#then go to bluetooth loop
while initialized:
try:
await ble.connect_and_process() # main bt reading loop inside here
except Exception as e:
print("Unhandled bluetooth exception:", e)
sys.print_exception(e)
await asyncio.sleep_ms(1000) # rest for one second and try to re-connect
gc.collect()
# stop by some exception
gui_task.cancel() # stop GUI loop
vbatt_task.cancel()
alarms_task.cancel()
try:
gc.enable()
gc.collect()
init()
asyncio.run(main())
finally:
deinit()
asyncio.new_event_loop() # Clear retained state
print('main global finally')