12
12
# event_loop = lv_utils.event_loop()
13
13
#
14
14
#
15
- # uasyncio example with SDL:
15
+ # asyncio example with SDL:
16
16
#
17
17
# SDL.init(auto_refresh=False)
18
18
# # Register SDL display driver.
19
19
# # Register SDL mouse driver
20
20
# event_loop = lv_utils.event_loop(asynchronous=True)
21
- # uasyncio .Loop.run_forever()
21
+ # asyncio .Loop.run_forever()
22
22
#
23
- # uasyncio example with ili9341:
23
+ # asyncio example with ili9341:
24
24
#
25
25
# event_loop = lv_utils.event_loop(asynchronous=True) # Optional!
26
26
# self.disp = ili9341(asynchronous=True)
27
- # uasyncio .Loop.run_forever()
27
+ # asyncio .Loop.run_forever()
28
28
#
29
29
# MIT license; Copyright (c) 2021 Amir Gonnen
30
30
#
31
31
##############################################################################
32
32
33
33
import lvgl as lv
34
34
import micropython
35
- import usys
35
+ import sys
36
36
37
37
# Try standard machine.Timer, or custom timer from lv_timer, if available
38
38
42
42
try :
43
43
from lv_timer import Timer
44
44
except :
45
- raise RuntimeError ("Missing machine.Timer implementation!" )
45
+ if sys .platform != "darwin" :
46
+ raise RuntimeError ("Missing machine.Timer implementation!" )
47
+ Timer = False
46
48
47
49
# Try to determine default timer id
48
50
49
51
default_timer_id = 0
50
- if usys .platform == ' pyboard' :
52
+ if sys .platform == " pyboard" :
51
53
# stm32 only supports SW timer -1
52
54
default_timer_id = - 1
53
-
54
- if usys .platform == ' rp2' :
55
+
56
+ if sys .platform == " rp2" :
55
57
# rp2 only supports SW timer -1
56
58
default_timer_id = - 1
57
59
58
- # Try importing uasyncio , if available
60
+ # Try importing asyncio , if available
59
61
60
62
try :
61
- import uasyncio
62
- uasyncio_available = True
63
+ import asyncio
64
+
65
+ asyncio_available = True
63
66
except :
64
- uasyncio_available = False
67
+ asyncio_available = False
65
68
66
69
##############################################################################
67
70
68
- class event_loop ():
69
71
72
+ class event_loop :
70
73
_current_instance = None
71
74
72
- def __init__ (self , freq = 25 , timer_id = default_timer_id , max_scheduled = 2 , refresh_cb = None , asynchronous = False , exception_sink = None ):
75
+ def __init__ (
76
+ self ,
77
+ freq = 25 ,
78
+ timer_id = default_timer_id ,
79
+ max_scheduled = 2 ,
80
+ refresh_cb = None ,
81
+ asynchronous = False ,
82
+ exception_sink = None ,
83
+ ):
73
84
if self .is_running ():
74
85
raise RuntimeError ("Event loop is already running!" )
75
86
@@ -80,28 +91,41 @@ def __init__(self, freq=25, timer_id=default_timer_id, max_scheduled=2, refresh_
80
91
81
92
self .delay = 1000 // freq
82
93
self .refresh_cb = refresh_cb
83
- self .exception_sink = exception_sink if exception_sink else self .default_exception_sink
94
+ self .exception_sink = (
95
+ exception_sink if exception_sink else self .default_exception_sink
96
+ )
84
97
85
98
self .asynchronous = asynchronous
86
99
if self .asynchronous :
87
- if not uasyncio_available :
88
- raise RuntimeError ("Cannot run asynchronous event loop. uasyncio is not available!" )
89
- self .refresh_event = uasyncio .Event ()
90
- self .refresh_task = uasyncio .create_task (self .async_refresh ())
91
- self .timer_task = uasyncio .create_task (self .async_timer ())
100
+ if not asyncio_available :
101
+ raise RuntimeError (
102
+ "Cannot run asynchronous event loop. asyncio is not available!"
103
+ )
104
+ self .refresh_event = asyncio .Event ()
105
+ self .refresh_task = asyncio .create_task (self .async_refresh ())
106
+ self .timer_task = asyncio .create_task (self .async_timer ())
92
107
else :
93
- self .timer = Timer (timer_id )
108
+ if Timer :
109
+ self .timer = Timer (timer_id )
110
+ self .timer .init (
111
+ mode = Timer .PERIODIC , period = self .delay , callback = self .timer_cb
112
+ )
94
113
self .task_handler_ref = self .task_handler # Allocation occurs here
95
- self .timer .init (mode = Timer .PERIODIC , period = self .delay , callback = self .timer_cb )
96
114
self .max_scheduled = max_scheduled
97
115
self .scheduled = 0
98
116
117
+ def init_async (self ):
118
+ self .refresh_event = asyncio .Event ()
119
+ self .refresh_task = asyncio .create_task (self .async_refresh ())
120
+ self .timer_task = asyncio .create_task (self .async_timer ())
121
+
99
122
def deinit (self ):
100
123
if self .asynchronous :
101
124
self .refresh_task .cancel ()
102
125
self .timer_task .cancel ()
103
126
else :
104
- self .timer .deinit ()
127
+ if Timer :
128
+ self .timer .deinit ()
105
129
event_loop ._current_instance = None
106
130
107
131
def disable (self ):
@@ -122,12 +146,21 @@ def task_handler(self, _):
122
146
try :
123
147
if lv ._nesting .value == 0 :
124
148
lv .task_handler ()
125
- if self .refresh_cb : self .refresh_cb ()
149
+ if self .refresh_cb :
150
+ self .refresh_cb ()
126
151
self .scheduled -= 1
127
152
except Exception as e :
128
153
if self .exception_sink :
129
154
self .exception_sink (e )
130
155
156
+ def tick (self ):
157
+ self .timer_cb (None )
158
+
159
+ def run (self ):
160
+ if sys .platform == "darwin" :
161
+ while True :
162
+ self .tick ()
163
+
131
164
def timer_cb (self , t ):
132
165
# Can be called in Interrupt context
133
166
# Use task_handler_ref since passing self.task_handler would cause allocation.
@@ -149,15 +182,15 @@ async def async_refresh(self):
149
182
except Exception as e :
150
183
if self .exception_sink :
151
184
self .exception_sink (e )
152
- if self .refresh_cb : self .refresh_cb ()
185
+ if self .refresh_cb :
186
+ self .refresh_cb ()
153
187
154
188
async def async_timer (self ):
155
189
while True :
156
- await uasyncio .sleep_ms (self .delay )
190
+ await asyncio .sleep_ms (self .delay )
157
191
lv .tick_inc (self .delay )
158
192
self .refresh_event .set ()
159
-
160
193
161
194
def default_exception_sink (self , e ):
162
- usys .print_exception (e )
163
- event_loop .current_instance ().deinit ()
195
+ sys .print_exception (e )
196
+ # event_loop.current_instance().deinit()
0 commit comments