-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.py
54 lines (43 loc) · 1.98 KB
/
fsm.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
class FSM:
"""This class implements a deterministic finite state machine working with Raspberry Pico W interrupts as events
"""
def __init__(self, initial_state: int = 0) -> None:
""" Initializes the finite state machine
Args:
initial_state (int): the code of the initial state (default = 0)
"""
self.__current_state = initial_state
self.__transitions : dict[tuple[int, int], int] = {}
def get_current_state(self) -> int:
""" Returns the current state of the FSM
Returns:
int: the code of the current state
"""
return self.__current_state
def set_transition_rule(self, current_state: int, event_number: int, next_state: int) -> None:
"""Set the transition rules the FSM obeys
Args:
current_state (int): the code of the current state
event (str): the name of the interrupt that yields the state change
next_state (int): the code of the next state
"""
self.__transitions[(current_state, event_number)] = next_state
def get_next_state(self, current_state: int, event_number: int) -> int:
""" Returns the code of the next state
Args:
current_state (int): the code of the current state
event (str): the kind of interrupt producing the change of state
Returns:
int: The code of the next state
"""
return self.__transitions[(current_state, event_number)]
def compute_next_state(self, event_number: int) -> None:
""" Updates the code of the current state according to the code of the next state
Args:
event (str): the kind of interrupt producing the change of state
"""
try:
self.__current_state = self.__transitions[(self.__current_state, event_number)]
except KeyError:
print(f'Damn it! [{self.__current_state}, {event_number}]')
self.__current_state = -1