Skip to content

Commit f47c1fa

Browse files
committed
lib: Allow using lv_utils in unix-macos port.
Fix to allow using lv_utils with asyncio in unix-macos.
1 parent b51bf02 commit f47c1fa

File tree

1 file changed

+63
-30
lines changed

1 file changed

+63
-30
lines changed

lib/lv_utils.py

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
1212
# event_loop = lv_utils.event_loop()
1313
#
1414
#
15-
# uasyncio example with SDL:
15+
# asyncio example with SDL:
1616
#
1717
# SDL.init(auto_refresh=False)
1818
# # Register SDL display driver.
1919
# # Register SDL mouse driver
2020
# event_loop = lv_utils.event_loop(asynchronous=True)
21-
# uasyncio.Loop.run_forever()
21+
# asyncio.Loop.run_forever()
2222
#
23-
# uasyncio example with ili9341:
23+
# asyncio example with ili9341:
2424
#
2525
# event_loop = lv_utils.event_loop(asynchronous=True) # Optional!
2626
# self.disp = ili9341(asynchronous=True)
27-
# uasyncio.Loop.run_forever()
27+
# asyncio.Loop.run_forever()
2828
#
2929
# MIT license; Copyright (c) 2021 Amir Gonnen
3030
#
3131
##############################################################################
3232

3333
import lvgl as lv
3434
import micropython
35-
import usys
35+
import sys
3636

3737
# Try standard machine.Timer, or custom timer from lv_timer, if available
3838

@@ -42,34 +42,45 @@
4242
try:
4343
from lv_timer import Timer
4444
except:
45-
raise RuntimeError("Missing machine.Timer implementation!")
45+
if sys.platform != "darwin":
46+
raise RuntimeError("Missing machine.Timer implementation!")
47+
Timer = False
4648

4749
# Try to determine default timer id
4850

4951
default_timer_id = 0
50-
if usys.platform == 'pyboard':
52+
if sys.platform == "pyboard":
5153
# stm32 only supports SW timer -1
5254
default_timer_id = -1
53-
54-
if usys.platform == 'rp2':
55+
56+
if sys.platform == "rp2":
5557
# rp2 only supports SW timer -1
5658
default_timer_id = -1
5759

58-
# Try importing uasyncio, if available
60+
# Try importing asyncio, if available
5961

6062
try:
61-
import uasyncio
62-
uasyncio_available = True
63+
import asyncio
64+
65+
asyncio_available = True
6366
except:
64-
uasyncio_available = False
67+
asyncio_available = False
6568

6669
##############################################################################
6770

68-
class event_loop():
6971

72+
class event_loop:
7073
_current_instance = None
7174

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+
):
7384
if self.is_running():
7485
raise RuntimeError("Event loop is already running!")
7586

@@ -80,28 +91,41 @@ def __init__(self, freq=25, timer_id=default_timer_id, max_scheduled=2, refresh_
8091

8192
self.delay = 1000 // freq
8293
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+
)
8497

8598
self.asynchronous = asynchronous
8699
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())
92107
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+
)
94113
self.task_handler_ref = self.task_handler # Allocation occurs here
95-
self.timer.init(mode=Timer.PERIODIC, period=self.delay, callback=self.timer_cb)
96114
self.max_scheduled = max_scheduled
97115
self.scheduled = 0
98116

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+
99122
def deinit(self):
100123
if self.asynchronous:
101124
self.refresh_task.cancel()
102125
self.timer_task.cancel()
103126
else:
104-
self.timer.deinit()
127+
if Timer:
128+
self.timer.deinit()
105129
event_loop._current_instance = None
106130

107131
def disable(self):
@@ -122,12 +146,21 @@ def task_handler(self, _):
122146
try:
123147
if lv._nesting.value == 0:
124148
lv.task_handler()
125-
if self.refresh_cb: self.refresh_cb()
149+
if self.refresh_cb:
150+
self.refresh_cb()
126151
self.scheduled -= 1
127152
except Exception as e:
128153
if self.exception_sink:
129154
self.exception_sink(e)
130155

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+
131164
def timer_cb(self, t):
132165
# Can be called in Interrupt context
133166
# Use task_handler_ref since passing self.task_handler would cause allocation.
@@ -149,15 +182,15 @@ async def async_refresh(self):
149182
except Exception as e:
150183
if self.exception_sink:
151184
self.exception_sink(e)
152-
if self.refresh_cb: self.refresh_cb()
185+
if self.refresh_cb:
186+
self.refresh_cb()
153187

154188
async def async_timer(self):
155189
while True:
156-
await uasyncio.sleep_ms(self.delay)
190+
await asyncio.sleep_ms(self.delay)
157191
lv.tick_inc(self.delay)
158192
self.refresh_event.set()
159-
160193

161194
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

Comments
 (0)