-
Notifications
You must be signed in to change notification settings - Fork 2
/
artnet_to_serial.py
186 lines (142 loc) · 5.13 KB
/
artnet_to_serial.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import socket
import sys
from artnet import packet
import serial
from serial.tools import list_ports
import struct
import threading
import queue
import itertools
import time
UDP_IP = "0.0.0.0"
UDP_PORT = 6454
try:
SERIAL_DEVICE = next(serial.tools.list_ports.grep("")).device
except StopIteration:
print("No serial devices found.")
sys.exit(1)
BAUD_RATE = 500000
DEBUG = True
SKIP_BYTE = 254
RESET_BYTE = 255
START_UNIVERSE = 0
MAX_UNIVERSES = 17
IS_MATRIX = False
SEND_INCOMPLETE_FRAMES = False
if IS_MATRIX:
# Matrix Screen (All strips equal)
UNIVERSE_SIZE_LED = [150] * MAX_UNIVERSES
else:
UNIVERSE_SIZE_LED = [169, 160, 166, 120, 156, 140, 110, 118, 120, 120, 123, 122, 122, 121, 125, 167, 52]
UNIVERSE_SIZE_CHAN = [x*3 for x in UNIVERSE_SIZE_LED]
NUM_PIXELS = sum(UNIVERSE_SIZE_LED)
PLAY_TEST_PATTERN = True
TEST_RED = [30,0,0] * NUM_PIXELS
TEST_GREEN = [0,30,0] * NUM_PIXELS
TEST_BLUE = [0,0,30] * NUM_PIXELS
class Listener(threading.Thread):
def __init__(self, output_queue, address=UDP_IP, port=UDP_PORT):
super(Listener, self).__init__()
self.queue = output_queue
self.sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((address, port))
self.sock.settimeout(0.0)
self.running = True
self.clear_universes()
print("Listening on %s:%s" % (address, port))
def clear_universes(self):
self.universes = [None,] * MAX_UNIVERSES
self.universes_received = 0
self.universes_present = 0
def run(self):
while(self.running):
data, addr = self.read_artnet()
if data is not None:
pkt = packet.ArtNetPacket.decode(addr, data)
if type(pkt) is packet.DmxPacket:
self.handle_artnet(pkt)
def read_artnet(self):
try:
data, addr = self.sock.recvfrom(1024) # buffer size is 1024 bytes
return data, addr
except socket.error as e:
return None, None
def handle_artnet(self, pkt):
self.universes[pkt.universe] = pkt.framedata[:UNIVERSE_SIZE_CHAN[pkt.universe]]
self.universes_received += 1
self.universes_present |= (1 << pkt.universe)
# Do it every N universes received
#if self.universes_received == MAX_UNIVERSES:
# Do it once all universes are received
if self.universes_present == (2**MAX_UNIVERSES - 1):
self.send_current_frame()
self.clear_universes()
def send_current_frame(self):
missed_universes = ['.'] * MAX_UNIVERSES
incomplete_frame = False
# Get rid of any values of 254 or 255, as they have special meaning
for index, universe in enumerate(self.universes):
if universe is None:
self.universes[index] = [SKIP_BYTE] * UNIVERSE_SIZE_CHAN[index]
missed_universes[index] = 'X'
incomplete_frame = True
#print("Missed universe %d" % (index,))
else:
for n, c in enumerate(universe):
if c > 253:
self.universes[index][n] = 253
if DEBUG:
print("Channel Maxxed.")
if not incomplete_frame and not SEND_INCOMPLETE_FRAMES:
frame = bytearray(itertools.chain.from_iterable(self.universes))
self.queue.put_nowait(frame)
if DEBUG:
print("Sent frame. (q: %d)\t%s\t(%f)" % (self.queue.qsize(),"".join(missed_universes), (self.universes_received/MAX_UNIVERSES)))
else:
if DEBUG:
print("Skipped frame. (q: %d)\t%s" % (self.queue.qsize(),"".join(missed_universes)))
class Writer(threading.Thread):
def __init__(self, input_queue, serial_device=SERIAL_DEVICE):
super(Writer, self).__init__()
self.queue = input_queue
self.ser = serial.Serial(serial_device, BAUD_RATE, write_timeout=1)
self.running = True
print("Sending out to: %s" % (self.ser.name,))
def run(self):
while self.running:
frame = self.queue.get()
self.ser.write(frame)
self.write_reset()
self.queue.task_done()
#print("QSize: %d" % (self.queue.qsize(),))
def write_reset(self):
self.ser.write(struct.pack("B", 255))
self.ser.flushOutput()
def write_test(self, test_frame=TEST_BLUE):
self.ser.write(test_frame)
self.write_reset()
if __name__ == '__main__':
q = queue.Queue()
w = Writer(q)
w.daemon = True
if PLAY_TEST_PATTERN:
w.write_test(TEST_RED)
time.sleep(0.5)
w.write_test(TEST_GREEN)
time.sleep(0.5)
w.write_test(TEST_BLUE)
time.sleep(0.5)
l = Listener(q)
l.daemon = True
try:
l.start()
w.start()
while True:
time.sleep(1)
except KeyboardInterrupt:
l.running = False
w.running = False
print("KeyboardInterrupt: Shutting down.")
sys.exit(0)