-
Notifications
You must be signed in to change notification settings - Fork 0
/
artnet_receiver.py
177 lines (129 loc) · 4.46 KB
/
artnet_receiver.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
# The ArtNET Receiver is based upon the work of
# https://github.com/Blinkinlabs/BlinkyTape_Python/blob/master/artnet-receiver.py by Matt Mets
import sys
import time
import socket
from struct import unpack
import threading
UDP_IP = "" # listen on all sockets- INADDR_ANY
UDP_PORT = 0x1936 # Art-net is supposed to only use this address
class ArtnetPacket:
ARTNET_HEADER = b'Art-Net\x00'
OP_OUTPUT = 0x0050
def __init__(self):
self.op_code = None
self.ver = None
self.sequence = None
self.physical = None
self.universe = None
self.length = None
self.data = None
@staticmethod
def unpack_raw_artnet_packet(raw_data):
if unpack('!8s', raw_data[:8])[0] != ArtnetPacket.ARTNET_HEADER:
return None
packet = ArtnetPacket()
# We can only handle data packets
(packet.op_code,) = unpack('!H', raw_data[8:10])
if packet.op_code != ArtnetPacket.OP_OUTPUT:
return None
(packet.op_code, packet.ver, packet.sequence, packet.physical,
packet.universe, packet.length) = unpack('!HHBBHH', raw_data[8:18])
(packet.universe,) = unpack('<H', raw_data[14:16])
(packet.data,) = unpack(
'{0}s'.format(int(packet.length)),
raw_data[18:18+int(packet.length)])
return packet
class ArtNetReceiver(threading.Thread):
def __init__(self, universe, comm = None, listenStart = 0, listenStop = 512, ip = UDP_IP, port = UDP_PORT):
threading.Thread.__init__(self)
self.universe = universe
self.listenStart = listenStart
self.listenStop = listenStop
self.stopped = False
self.comm = comm
print(("Listening for ArtNet Packages in {0}:{1}").format(ip, port))
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
self.sock.bind((ip, port))
self.sock.setblocking(False)
self.actualData = [-1] * (listenStop + 1)
self.lastTime = time.time()
self.lastSequence = 0
self.callbacks = [False] * (listenStop + 1)
self.lastPacketTime = time.time()-3;
self.status = False
def watchdog(self):
if self.stopped:
print "Watchdog ended"
return
threading.Timer(0.5, self.watchdog).start();
actTime = time.time();
statusChanged = False
if actTime - self.lastPacketTime > 2:
statusChanged = self.status != False
self.status = False
else:
statusChanged = self.status != True
self.status = True
if statusChanged and self.status:
print "Now Receiving ArtNET Signal"
self.comm["signal"] = True
if statusChanged and not self.status:
print "ArtNET Signal LOST!!!"
self.comm["signal"] = False
def registerCallback(self, address, function):
'''
Function needs this footprint:
function(address, value, changeAmount, completeUniverse)
'''
if type(address) is list:
for singleAddress in address:
self.callbacks[singleAddress] = (function, True, address)
else:
self.callbacks[address] = (function, False, address)
def stop(self):
print "Stopping Artnet Receiver"
self.stopped = True
def run(self):
self.watchdog()
self.callbacksToRun = {}
while not self.stopped:
try:
data, addr = self.sock.recvfrom(1024)
packet = ArtnetPacket.unpack_raw_artnet_packet(data)
if packet != None:
self.lastPacketTime = time.time();
if packet != None and packet.universe == self.universe:
if packet.sequence != self.lastSequence:
pass
i = 0;
for newData in packet.data:
if i >= self.listenStop:
break;
oldDataValue = self.actualData[i]
newDataValue = unpack('B',newData)[0]
self.actualData[i] = newDataValue
# if newDataValue != oldDataValue:
# print "Change in Data for Channel {0} went to {1}".format(i + 1, newDataValue)
if newDataValue != oldDataValue and self.callbacks[i]:
(callback, isWide, addresses) = self.callbacks[i]
if isWide:
self.callbacksToRun[addresses[0]] = (callback, addresses, isWide)
else:
self.callbacksToRun[addresses] = (callback, addresses, isWide)
i += 1
# Do the callbacks
for cbStartAdrr in self.callbacksToRun:
(callback, addresses, isWide) = self.callbacksToRun[cbStartAdrr];
if isWide:
callback(addresses, [self.actualData[i] for i in addresses], self.actualData)
else:
callback(addresses, self.actualData[addresses], self.actualData)
self.callbacksToRun = {}
self.lastSequence = packet.sequence
except socket.error as e:
time.sleep(0.01);
except KeyboardInterrupt:
self.sock.close()
return False
print "Artnet Receiver Terminated"