-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_machine.py
67 lines (52 loc) · 1.67 KB
/
state_machine.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
#!/usr/bin/env python3
"""
@Filename: state_machine.py
@Author: dulanj
@Time: 2021-09-23 18.51
"""
import enum
class State(enum.Enum):
off = 0
on = 1
none = 2
transit = 3
class StateMachine:
def __init__(self, on_thresh, off_thresh):
self._on_thresh = on_thresh
self._off_thresh = off_thresh
self._on_count = 0
self._off_count = 0
self._temp_state = State.none
self._previous_state = State.none
self._state = State.none
def __str__(self):
return f"ON:{self._on_count} OFF:{self._off_count} PRE:{self._previous_state} STATE:{self._state}"
def get_state(self):
return self._state
def get_previous_state(self):
return self._previous_state
def update(self, switch):
if switch:
self._on_count += 1
self._off_count = 0
else:
self._off_count += 1
self._on_count = 0
if self._state == State.transit:
self._state = self._temp_state
if self._temp_state != State.on and self._on_count > self._on_thresh:
self._previous_state = self._state
self._state = State.transit
self._temp_state = State.on
self._on_count = 0
if self._temp_state != State.off and self._off_count > self._off_thresh:
self._previous_state = self._state
self._state = State.transit
self._temp_state = State.off
self._off_count = 0
if __name__ == '__main__':
sm1 = StateMachine(on_thresh=3, off_thresh=3)
for i in range(20):
sm1.update(i < 10)
print(sm1)
# print(sm1.get_state())