[UNIX] Simple timer implementation got error in mp_stack_usage
function
#12151
-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
It seems like this could be implemented in just a few lines of Python instead of a C module. If you are really interested in sticking with C, I would suggest looking at https://github.com/micropython/micropython/blob/master/ports/unix/modtime.c#L88 for how to get more accurate timing (still not super accurate for < 100ms). In your implementation, I think the problem is because the thread is not a MicroPython thread but a regular C thread and you are trying to call MicroPython code there without doing the required thread initialization needed by MicroPython. |
Beta Was this translation helpful? Give feedback.
-
We've used unix micropython threads to simulate a Timer in the past, in case it helps: import time
import _thread
class Timer:
def __init__(self, timer_id):
self.id = timer_id
self._callback = None
self._period_ms = None
self._stop = False
_thread.start_new_thread(self._timer, ())
def init(self, freq, callback):
self._callback = callback
self._period_ms = 1000 // freq
def deinit(self):
self._stop = True
def _timer(self):
while True:
if self._stop:
break
if self._freq is None:
time.sleep_ms(100)
start = time.ticks_ms()
if self._callback:
try:
self._callback(self)
except Exception as ex:
import sys
sys.print_exception(ex)
taken = time.ticks_diff(time.ticks_ms(), start)
time.sleep_ms(min(0, self._period_ms-taken)) |
Beta Was this translation helpful? Give feedback.
-
It turns out the UNIX port already has a timer implementation in the micropython-lib |
Beta Was this translation helpful? Give feedback.
It turns out the UNIX port already has a timer implementation in the micropython-lib