This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
67 lines (52 loc) · 1.73 KB
/
state.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
class State(object):
def __init__(self, name):
self.is_starting_state = False
self._name = name
self._logic = []
def add_logic(self, log):
""" Add state logic forwarding
log should be a list of tuples, or just a tuple where
the first element is the "output" of the state and
the second element is the "bit read", and the third example
a string stating what state it forwards to.
Example: log = [(0b00, 0, "00"), (0b11, 1, "10")]
"""
if isinstance(log, list):
for val in log:
self._logic.append(val)
else:
self._logic.append(log)
# TODO
self.refresh()
def process(self, rcvd):
"""Process input depending on logic
Arguments:
rcvd: binary
Return [ list of new paths and new vals ]
or `None` if rcvd does not exist in this State
"""
pvs = []
for logic in self._logic:
pvs.append((logic[2], logic[0]^rcvd))
return pvs
def return_bit(self, next_state):
""" Return the bit that is received
in the state machine to transition
to the next state
"""
for logic in self._logic:
if next_state == logic[2]:
return logic[1]
def set_starting_state(self):
self.is_starting_state = True
def set_state_logic(self, f):
self._logic = f
def next_state(self, arg):
return self._logic(arg)
@property
def name(self):
return self._name
def __repr__(self):
rs = "State %s -- " % self._name
rs += "Starting state: %s" % self.is_starting_state
return rs