-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateMachine.py
74 lines (58 loc) · 1.46 KB
/
StateMachine.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
69
70
71
72
73
74
import threading
import time
import pysimpledmx
import numpy as np
import random
ROWS = 5
COLS = 6
IDLE = "IDLE"
ANOTHER_STATE = "ANOTHER_STATE"
class StateMachine(threading.Thread):
"""docstring for StateMachine"""
def __init__(self, period, address):
super(StateMachine, self).__init__()
self.cur_state = IDLE
self.period = period
self.time = time.time()
self.next_execute_time = self.time + self.period
self.dmx = pysimpledmx.DMXConnectionEthernet(address)
grid = []
for i in xrange(ROWS):
row = [(0,0,0)]*COLS
grid.append(row)
self.grid = np.array(grid)
print self.grid
def run(self):
while True:
if self.time > self.next_execute_time:
self.execute()
self.next_execute_time += self.period
else:
time.sleep(0.001)
self.time = time.time()
def execute(self):
next_state = self.cur_state
if self.cur_state == IDLE:
self.idle()
next_state = ANOTHER_STATE
elif self.cur_state == ANOTHER_STATE:
self.another_state()
if(next_state != self.cur_state):
print "%s -> %s"%(self.cur_state, next_state)
self.cur_state = next_state
self.renderDMX()
print "Executing"
def idle(self):
pass
def another_state(self):
#print self.grid
pass
def renderDMX(self):
for r in xrange(ROWS):
for c in xrange(COLS):
val = self.grid[r, c]
for i in xrange(3):
chan = r*COLS*3 + c*3 + i + 1
#print chan
self.dmx.setChannel(chan, random.randint(1,255))
self.dmx.render()