Last active
April 28, 2018 01:55
-
-
Save blhack/d14741ab7065f3226cd46bb2eb6a3cce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
import struct | |
import time | |
import random | |
ip = "10.0.1.255" | |
pixel_channels_start = 0 | |
pixel_channels_end = 15 | |
rgb_channels_start = 31 | |
rgb_channels_end = 35 | |
max_channels = 35 | |
channels = [] | |
for i in range(0,max_channels): | |
channels.append([i, 0]) | |
class ArtNet(object): | |
PORT = 6454 # 0x1936 | |
def __init__(self, port=PORT): | |
self._socket = None | |
self.port = port | |
@property | |
def socket(self): | |
if self._socket is None: | |
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
return self._socket | |
def _make_message(self, data, port=None): | |
signature = struct.pack("!cccccccx", *list("Art-Net")) | |
opcode = struct.pack("<H", 0x5000) # low byte first | |
protocol = struct.pack("!H", 14) # high byte first | |
head = signature + opcode + protocol | |
sequence = struct.pack("!x") # 0x00 to disable | |
pyhsical = struct.pack("!x") # for information only | |
# 15 bit port address: | |
# - a universe 0-3 (16 universes in 1 subnet, 256 universes in 1 net) | |
# - b subnet 4-7 (16 subnets in 1 net) | |
# - c net 8-14 (128 nets) | |
# - d 0 15 | |
# | |
# total of 16*16*128 = 32768 universes/port addresses | |
if port is None: | |
# aaaabbbbcccccccd | |
port = struct.pack("!H", 0b0000000000000000) | |
length = struct.pack("!H", len(data)) | |
return head + sequence + pyhsical + port + length + data | |
@staticmethod | |
def encode_channels(*args): | |
channels = [0] * 512 | |
for index, value in args: | |
channels[index] = value | |
fmt = "!" + "B" * len(channels) | |
return struct.pack(fmt, *channels) | |
def sendArtDMX(self, ip, data): | |
msg = self._make_message(data) | |
return self.socket.sendto(msg, (ip, self.port)) | |
def render(): | |
a = ArtNet() | |
d = ArtNet.encode_channels(*channels) | |
c = a.sendArtDMX(ip, d) | |
print "%s bytes sent to %s" % (c, ip) | |
for x, y in channels: | |
print "channel %s: %s" % (x, y) | |
def flood(r, g, b): | |
for i in range(pixel_channels_start,pixel_channels_end,3): | |
channels[i] = [i,r] | |
channels[i+1] = [i+1,g] | |
channels[i+2] = [i+2,b] | |
def chase(r, g, b): | |
for i in range(pixel_channels_start,pixel_channels_end,3): | |
channels[i] = [i,r] | |
channels[i+1] = [i+1,g] | |
channels[i+2] = [i+2,b] | |
render() | |
time.sleep(0.1) | |
def setRGB(channelStart, r, g, b): | |
channels[channelStart] = [channelStart, r] | |
channels[channelStart+1] = [channelStart+1, g] | |
channels[channelStart+2] = [channelStart+2, b] | |
if __name__ == "__main__": | |
for i in range(0,512): | |
channels[i] = [255] | |
render() | |
time.sleep(1) | |
for i in range(0,512): | |
channels[i] = [0] | |
render() | |
time.sleep(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment