Last active
February 8, 2022 23:00
-
-
Save tsuriga/5085070 to your computer and use it in GitHub Desktop.
EffectServer example in English. The actual server with a mockup option can be forked at https://github.com/epeli/effectserver/. I did not test this myself, but the same code (originally in Finnish) has been used at a LAN party for a few years now
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/python | |
# -*- coding: utf-8 -*- | |
import socket | |
import time | |
class LightServer(object): | |
""" | |
Sends light commands to DMX light server. Can be used to envelope | |
multiple commands to a single packet. | |
""" | |
def __init__(self, nick, ip, port): | |
self.ip = ip | |
self.port = port | |
self.nick = nick | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
self.reset() | |
def reset(self): | |
"""Resets UDP packet""" | |
self.packet = [ 1 ] # Spec version, always 1 | |
self.packet.append(0) # Begin tag part | |
for char in self.nick: | |
# Convert nick to ASCII codes | |
self.packet.append(ord(char)) | |
self.packet.append(0) # End tag part | |
def set(self, i, r, g, b): | |
"""Sets light i to an RGB value""" | |
self.packet += [ | |
1, # Effect type 1 (light) | |
i, # Light index | |
0, # Extension byte. Always 0 | |
r, # Red | |
g, # Green | |
b, # Blue | |
] | |
def send(self): | |
"""Sends set bytes and resets effect list""" | |
bytes = bytearray(self.packet) | |
self.socket.sendto(bytes, (self.ip, self.port)) | |
self.reset() | |
lights = LightServer("myLights", "example.com", 9909) | |
# Set all lights blue | |
for i in range(0, 5): | |
lights.set(i, 0, 0, 255) | |
# Send all "set light to blue" commands at once | |
lights.send() | |
# Looping green colour | |
i = 0 | |
while True: | |
lights.set(i, 0, 255, 0) | |
lights.send() | |
time.sleep(0.3) | |
lights.set(i, 0, 0, 255) | |
lights.send() | |
i += 1 | |
i = i % 36 | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment