Skip to content

Commit 5f2a135

Browse files
committed
working on macOS build
1 parent 95ef355 commit 5f2a135

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
lines changed

api_drivers/common_api_drivers/frozen/other/task_handler.py

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import lvgl as lv # NOQA
22
import micropython # NOQA
33
import sys
4+
import time
45

56
try:
67
from machine import Timer # NOQA
@@ -11,12 +12,19 @@
1112
raise RuntimeError("Missing machine.Timer implementation!")
1213

1314

15+
TASK_HANDLER_STARTED = 0x00
16+
TASK_HANDLER_FINISHED = 0x01
17+
1418
_default_timer_id = 0
1519

1620
if sys.platform in ('pyboard', 'rp2'):
1721
_default_timer_id = -1
1822

1923

24+
class _DefaultUserData(object):
25+
pass
26+
27+
2028
def _default_exception_hook(e):
2129
sys.print_exception(e)
2230
TaskHandler._current_instance.deinit() # NOQA
@@ -30,7 +38,6 @@ def __init__(
3038
duration=33,
3139
timer_id=_default_timer_id,
3240
max_scheduled=2,
33-
refresh_cb=None,
3441
exception_hook=_default_exception_hook
3542
):
3643
if TaskHandler._current_instance is not None:
@@ -41,8 +48,9 @@ def __init__(
4148

4249
TaskHandler._current_instance = self
4350

51+
self._callbacks = []
52+
4453
self.duration = duration
45-
self.refresh_cb = refresh_cb
4654
self.exception_hook = exception_hook
4755

4856
self._timer = Timer(timer_id)
@@ -58,6 +66,27 @@ def __init__(
5866
)
5967
self._scheduled = 0
6068

69+
def add_event_cb(self, callback, event, user_data=_DefaultUserData):
70+
for i, (cb, evt, data) in enumerate(self._callbacks):
71+
if cb == callback:
72+
evt = event
73+
if user_data != _DefaultUserData:
74+
data = user_data
75+
76+
self._callbacks[i] = (cb, evt, data)
77+
break
78+
else:
79+
if user_data == _DefaultUserData:
80+
user_data = None
81+
82+
self._callbacks.append((callback, event, user_data))
83+
84+
def remove_event_cb(self, callback):
85+
for i, obj in self._callbacks:
86+
if obj[0] == callback:
87+
self._callbacks.remove(obj)
88+
break
89+
6190
def deinit(self):
6291
self._timer.deinit()
6392
TaskHandler._current_instance = None
@@ -74,13 +103,57 @@ def is_running(cls):
74103

75104
def _task_handler(self, _):
76105
try:
77-
if lv._nesting.value == 0:
78-
lv.task_handler()
106+
self._scheduled -= 1
79107

80-
if self.refresh_cb:
81-
self.refresh_cb()
108+
if lv._nesting.value == 0:
109+
start_time = time.ticks_ms()
110+
111+
run_update = True
112+
for cb, evt, data in self._callbacks:
113+
if not evt ^ TASK_HANDLER_STARTED:
114+
continue
115+
116+
try:
117+
if cb(TASK_HANDLER_STARTED, data) is False:
118+
run_update = False
119+
120+
except Exception as err: # NOQA
121+
if (
122+
self.exception_hook and
123+
self.exception_hook != _default_exception_hook
124+
):
125+
self.exception_hook(err)
126+
else:
127+
sys.print_exception(err)
128+
129+
stop_time = time.ticks_ms()
130+
131+
ticks_diff = time.ticks_diff(stop_time, start_time)
132+
lv.tick_inc(ticks_diff)
133+
134+
if run_update:
135+
lv.task_handler()
136+
start_time = time.ticks_ms()
137+
138+
for cb, evt, data in self._callbacks:
139+
if not evt ^ TASK_HANDLER_FINISHED:
140+
continue
141+
142+
try:
143+
cb(TASK_HANDLER_FINISHED, data)
144+
except Exception as err: # NOQA
145+
if (
146+
self.exception_hook and
147+
self.exception_hook != _default_exception_hook
148+
):
149+
self.exception_hook(err)
150+
else:
151+
sys.print_exception(err)
152+
153+
stop_time = time.ticks_ms()
154+
ticks_diff = time.ticks_diff(stop_time, start_time)
155+
lv.tick_inc(ticks_diff)
82156

83-
self._scheduled -= 1
84157
except Exception as e:
85158
if self.exception_hook:
86159
self.exception_hook(e)

builder/macOS.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,16 @@ def compile(): # NOQA
207207
with open(mpconfigvariant_common_path, 'w') as f:
208208
f.write(mpconfigvariant_common)
209209

210-
for makefile_path in (
211-
'lib/micropython/py/mkrules.mk',
212-
):
213-
with open(makefile_path, 'rb') as f:
214-
data = f.read().decode('utf-8')
210+
mkrules_path = 'lib/micropython/py/mkrules.mk'
211+
with open(mkrules_path, 'rb') as f:
212+
data = f.read().decode('utf-8')
213+
214+
data = data.replace('QSTR_GEN_CXXFLAGS += $(QSTR_GEN_FLAGS)', 'QSTR_GEN_CXXFLAGS += $(QSTR_GEN_FLAGS)\n$(info $$QSTR_GEN_CFLAGS = $(QSTR_GEN_CFLAGS))')
215+
data = data.replace('$(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py pp $(CPP) output', '$(info makeqstrdefs.py $$QSTR_GEN_CFLAGS = $(QSTR_GEN_CFLAGS))\n $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py pp $(CPP) output')
215216

216-
data = data.replace('QSTR_GEN_CFLAGS := $(CFLAGS)', 'QSTR_GEN_CFLAGS = $(CFLAGS)')
217+
with open(mkrules_path, 'wb') as f:
218+
f.write(data.encode('utf-8'))
217219

218-
with open(makefile_path, 'wb') as f:
219-
f.write(data.encode('utf-8'))
220220

221221
build_sdl()
222222

0 commit comments

Comments
 (0)