-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmicropython_timer.py
executable file
·75 lines (56 loc) · 1.85 KB
/
micropython_timer.py
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
#!/usr/bin/python3
import threading
from datetime import timedelta
'''
see:
https://docs.python.org/3/library/threading.html#timer-objects
'''
#simple python3 implementation of the micropython timer
class Timer(threading.Thread):
PERIODIC=0
ONE_SHOT=1
#CHRONO,EXTBASE
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
self._stopped = threading.Event()
self._interval=None
self._callback=None
def _run_periodically(self):
while not self._stopped.wait(self._interval.total_seconds()):
self._callback(self._stopped)
def _run_once(self):
self._stopped.wait(self._interval.total_seconds())
self._callback(self._stopped)
def init(self, period=1000, mode=PERIODIC, callback=None):
if callback is not None:
self._callback=callback
elif self._callback is None:
raise NotImplementedError('callback not initialized')
self._interval=timedelta(milliseconds=period)
if mode is self.PERIODIC:
self._target=self._run_periodically
elif mode is self.ONE_SHOT:
self._target=self._run_once
self.start()
def deinit(self):
self._stopped.set()
self.join()
def pause(self):
raise NotImplementedError('pause not implemented')
def resume(self):
raise NotImplementedError('resume not implemented')
if __name__=='__main__':
import time
#from micropython_timer import Timer
t=Timer()
def cb(event):
print('hi')
t.init(period=2000, callback=cb)
for s in 'do other things at the same time'.split():
print(s)
time.sleep(2)
t.deinit()
from micropython.cat import Cat
import simulation
cat=Cat(simulation.get_simulation_limbs(), timer=Timer())