-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask_handler.py
More file actions
176 lines (132 loc) · 5.06 KB
/
task_handler.py
File metadata and controls
176 lines (132 loc) · 5.06 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# MIT license; Copyright (c) 2021 Amir Gonnen
# Copyright (c) 2024 - 2025 Kevin G. Schlosser
import lvgl as lv # NOQA
import micropython # NOQA
import sys
import time
from machine import Timer # NOQA
TASK_HANDLER_STARTED = 0x01
TASK_HANDLER_FINISHED = 0x02
_default_timer_id = 0
if sys.platform in ('pyboard', 'rp2'):
_default_timer_id = -1
class _DefaultUserData(object):
pass
def _default_exception_hook(e):
sys.print_exception(e) # NOQA
TaskHandler._current_instance.deinit() # NOQA
class TaskHandler(object):
_current_instance = None
def __init__(
self,
duration=33,
timer_id=_default_timer_id,
max_scheduled=2,
exception_hook=_default_exception_hook
):
if TaskHandler._current_instance is not None:
self.__dict__.update(TaskHandler._current_instance.__dict__)
else:
if not lv.is_initialized():
lv.init()
TaskHandler._current_instance = self
self._callbacks = []
self.duration = duration
self.exception_hook = exception_hook
self._timer = Timer(timer_id)
# Allocation occurs here
self._task_handler_ref = self._task_handler
self.max_scheduled = max_scheduled
self._start_time = time.ticks_ms() # NOQA
self._timer.init(
mode=Timer.PERIODIC,
period=self.duration,
callback=self._timer_cb
)
self._scheduled = 0
self._running = False
def add_event_cb(self, callback, event, user_data=_DefaultUserData):
for i, (cb, evt, data) in enumerate(self._callbacks):
if cb == callback:
evt = event
if user_data != _DefaultUserData:
data = user_data
self._callbacks[i] = (cb, evt, data)
break
else:
if user_data == _DefaultUserData:
user_data = None
self._callbacks.append((callback, event, user_data))
def remove_event_cb(self, callback):
for i, obj in self._callbacks:
if obj[0] == callback:
self._callbacks.remove(obj)
break
def deinit(self):
self._timer.deinit()
TaskHandler._current_instance = None
def disable(self):
self._scheduled += self.max_scheduled
def enable(self):
self._scheduled -= self.max_scheduled
@classmethod
def is_running(cls):
return cls._current_instance is not None
def _task_handler(self, _):
try:
self._scheduled -= 1
if lv._nesting.value == 0: # NOQA
self._running = True
run_update = True
for cb, evt, data in self._callbacks:
if not evt & TASK_HANDLER_STARTED:
continue
try:
if cb(TASK_HANDLER_STARTED, data) is False:
run_update = False
except Exception as err: # NOQA
if (
self.exception_hook and
self.exception_hook != _default_exception_hook
):
self.exception_hook(err)
else:
sys.print_exception(err) # NOQA
stop_time = time.ticks_ms() # NOQA
ticks_diff = time.ticks_diff(stop_time, self._start_time) # NOQA
self._start_time = stop_time
lv.tick_inc(ticks_diff)
if run_update:
lv.task_handler()
start_time = time.ticks_ms() # NOQA
for cb, evt, data in self._callbacks:
if not evt & TASK_HANDLER_FINISHED:
continue
try:
cb(TASK_HANDLER_FINISHED, data)
except Exception as err: # NOQA
if (
self.exception_hook and
self.exception_hook != _default_exception_hook
):
self.exception_hook(err)
else:
sys.print_exception(err) # NOQA
stop_time = time.ticks_ms() # NOQA
ticks_diff = time.ticks_diff(stop_time, start_time) # NOQA
lv.tick_inc(ticks_diff)
self._running = False
except Exception as e:
self._running = False
if self.exception_hook:
self.exception_hook(e)
def _timer_cb(self, _):
lv.tick_inc(self.duration)
if self._running:
return
if self._scheduled < self.max_scheduled:
try:
micropython.schedule(self._task_handler_ref, 0)
self._scheduled += 1
except: # NOQA
pass