-
Notifications
You must be signed in to change notification settings - Fork 8
/
statemachine_test.py
69 lines (63 loc) · 1.69 KB
/
statemachine_test.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
from statemachine import StateMachine
def ones_counter(val):
print("ONES State: ", end=' ')
while 1:
if val <= 0 or val >= 30:
newState = "Out_of_Range"
break
elif 20 <= val < 30:
newState = "TWENTIES"
break
elif 10 <= val < 20:
newState = "TENS"
break
else:
print(" @ %2.1f+" % val, end=' ')
val = math_func(val)
print(" >>")
return newState, val
def tens_counter(val):
print("TENS State: ", end=' ')
while 1:
if val <= 0 or val >= 30:
newState = "Out_of_Range"
break
elif 1 <= val < 10:
newState = "ONES"
break
elif 20 <= val < 30:
newState = "TWENTIES"
break
else:
print(" #%2.1f+" % val, end=' ')
val = math_func(val)
print(" >>")
return (newState, val)
def twenties_counter(val):
print("TWENTIES State:", end=' ')
while 1:
if val <= 0 or val >= 30:
newState = "Out_of_Range"
break
elif 1 <= val < 10:
newState = "ONES"
break
elif 10 <= val < 20:
newState = "TENS"
break
else:
print(" *%2.1f+" % val, end=' ')
val = math_func(val)
print(" >>")
return (newState, val)
def math_func(n):
from math import sin
return abs(sin(n))*31
if __name__== "__main__":
m = StateMachine()
m.add_state("ONES", ones_counter)
m.add_state("TENS", tens_counter)
m.add_state("TWENTIES", twenties_counter)
m.add_state("OUT_OF_RANGE", None, end_state=1)
m.set_start("ONES")
m.run(1)