/
timer.py
48 lines (37 loc) · 1.25 KB
/
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
from component import Component
class Timer(Component):
"""
Timer class
"""
def __init__(self, name, timer, debug):
Component.__init__(self)
self.name = name
self.timer = timer
self.repeat = True
self.cnt = 1
self.curr_state = 'stop'
self.debug = debug
def start(self):
self.curr_state = 'run'
def set_repeat(self,repeat):
self.repeat = repeat
def pre_run(self, cmd):
self.next_state = self.curr_state #default
if (self.curr_state == 'stop'):
return
if (self.curr_state == 'run'):
if (cmd == 'tick'):
self.cnt = (self.cnt+1) % (self.timer+1)
self.next_state = 'executeed' if (self.cnt == self.timer) else self.curr_state
if (self.curr_state == 'executeed'):
if (self.repeat):
self.next_state = 'run'
self.cnt = 1
else:
self.next_state = 'stop'
def __repr__(self):
return 'name=%s, curr_state=%s timer=%s cnt=%s' % (self.name, self.curr_state, self.timer, self.cnt)
def execute(self,cmd):
if (self.debug):
print self
Component.execute(self, cmd)