Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArtNet (DMX-over-IP) support. #288

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion python/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import numpy as np
import config

if config.DEVICE == 'artnet':
import socket
_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# ESP8266 uses WiFi communication
if config.DEVICE == 'esp8266':
elif config.DEVICE == 'esp8266':
import socket
_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Raspberry Pi controls the LED strip directly
Expand Down Expand Up @@ -42,6 +45,58 @@ def signal_handler(signal, frame):

_is_python_2 = int(platform.python_version_tuple()[0]) == 2

def _make_artnet_message(data, universe=None):
start = bytearray('Art-Net'.encode('latin-1'))
start.append(0x00)

opcode = struct.pack("<H", 0x5000) # low byte first
protocol = struct.pack("!H", 14) # high byte first
head = opcode + protocol

sequence = struct.pack("!x") # 0x00 to disable
pyhsical = struct.pack("!x") # for information only

if universe is None:
universe = struct.pack("!H", 0b0000000000000000)

length = struct.pack("!H", len(data))

print(len(data))
return start + head + sequence + pyhsical + port + length + data

def _update_artnet():
global pixels, _prev_pixels
# Truncate values and cast to integer
pixels = np.clip(pixels, 0, 255).astype(int)
# Optionally apply gamma correc tio
p = _gamma[pixels] if config.SOFTWARE_GAMMA_CORRECTION else np.copy(pixels)

m = bytearray()
for i in range(0, config.N_PIXELS):
if config.ARTNET_FIXTURE == 'RGBW':
m.append(p[0][i]) # R
m.append(p[1][i]) # G
m.append(p[2][i]) # B
m.append(int((int(p[0][i]) + int(p[1][i]) + int(p[2][i]) >> 2))) # W
elif config.ARTNET_FIXTURE == 'RGB':
m.append(p[0][i]) # R
m.append(p[1][i]) # G
m.append(p[2][i]) # B
elif config.ARTNET_FIXTURE == 'GRB':
m.append(p[1][i]) # G
m.append(p[0][i]) # R
m.append(p[2][i]) # B
elif config.ARTNET_FIXTURE == 'GRBW':
m.append(p[1][i]) # G
m.append(p[0][i]) # R
m.append(p[2][i]) # B
m.append(int((int(p[0][i]) + int(p[1][i]) + int(p[2][i]) >> 2))) # W
elif config.ARTNET_FIXTURE == 'L':
m.append(int((int(p[0][i]) + int(p[1][i]) + int(p[2][i]) >> 2))) # W

data = _make_artnet_message(m)
_sock.sendto(data, (config.UDP_IP, 6454))

def _update_esp8266():
"""Sends UDP packets to ESP8266 to update LED strip values

Expand Down