2022-07-20 22:27:40 +02:00
|
|
|
import socket
|
|
|
|
import struct
|
2022-07-22 17:51:29 +02:00
|
|
|
import sys
|
2022-07-20 22:27:40 +02:00
|
|
|
from threading import Thread
|
2022-07-24 11:43:00 +02:00
|
|
|
from time import sleep
|
|
|
|
from typing import Union
|
2022-07-20 22:27:40 +02:00
|
|
|
|
2022-08-06 19:42:40 +02:00
|
|
|
from animation import Animation, Off, RandomSingle, Steady, FadeTo, RotatingRainbow, Chase, TwoColor, Caramelldansen
|
2022-07-20 22:55:37 +02:00
|
|
|
|
|
|
|
|
2022-07-21 18:45:46 +02:00
|
|
|
def ledlog(value):
|
2022-07-23 17:47:19 +02:00
|
|
|
return int(pow(float(value) / 255.0, 2) * 255)
|
2022-07-21 18:45:46 +02:00
|
|
|
|
2022-07-20 22:55:37 +02:00
|
|
|
|
2022-07-20 22:27:40 +02:00
|
|
|
class RGB:
|
|
|
|
def __init__(self, dmx, slot, offset=0):
|
|
|
|
self.dmx = dmx
|
|
|
|
self.slot = slot
|
|
|
|
self.offset = offset
|
|
|
|
|
|
|
|
def rgb(self, color):
|
|
|
|
(r, g, b) = color
|
2022-07-23 17:47:19 +02:00
|
|
|
self.dmx.set(self.slot + self.offset + 0, ledlog(r))
|
|
|
|
self.dmx.set(self.slot + self.offset + 1, ledlog(g))
|
|
|
|
self.dmx.set(self.slot + self.offset + 2, ledlog(b))
|
|
|
|
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
class Bar252(RGB):
|
|
|
|
def __init__(self, dmx, slot=1):
|
|
|
|
super(Bar252, self).__init__(dmx, slot, 2)
|
2022-07-23 17:47:19 +02:00
|
|
|
dmx.set(self.slot + 0, 81)
|
|
|
|
dmx.set(self.slot + 1, 0)
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
|
2022-07-21 18:45:46 +02:00
|
|
|
class REDSpot18RGB(RGB):
|
|
|
|
def __init__(self, dmx, slot=1):
|
|
|
|
super(REDSpot18RGB, self).__init__(dmx, slot, 1)
|
2022-07-23 17:47:19 +02:00
|
|
|
dmx.set(self.slot + 0, 0)
|
2022-07-21 18:45:46 +02:00
|
|
|
|
|
|
|
|
2022-07-20 22:27:40 +02:00
|
|
|
class StairvilleLedPar56(RGB):
|
|
|
|
def __init__(self, dmx, slot=1):
|
|
|
|
super(StairvilleLedPar56, self).__init__(dmx, slot, 0)
|
2022-07-23 17:47:19 +02:00
|
|
|
dmx.set(self.slot + 3, 0)
|
|
|
|
dmx.set(self.slot + 4, 0)
|
|
|
|
dmx.set(self.slot + 5, 0)
|
|
|
|
dmx.set(self.slot + 6, 255)
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
|
2024-12-16 16:15:56 +01:00
|
|
|
class ZhennbyPar(RGB):
|
|
|
|
def __init__(self, dmx, slot=1):
|
|
|
|
super(ZhennbyPar, self).__init__(dmx, slot, 1)
|
2024-12-16 16:31:26 +01:00
|
|
|
dmx.set(self.slot + 0, 255)
|
2024-12-16 16:15:56 +01:00
|
|
|
dmx.set(self.slot + 4, 0)
|
|
|
|
dmx.set(self.slot + 5, 0)
|
|
|
|
dmx.set(self.slot + 6, 0)
|
|
|
|
|
|
|
|
|
2022-07-20 22:27:40 +02:00
|
|
|
class DMX:
|
2022-07-21 18:45:46 +02:00
|
|
|
def __init__(self, host, port=0x1936, universe=1, maxchan=512):
|
2022-07-24 11:43:00 +02:00
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
self._universe = universe
|
|
|
|
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
|
|
|
|
self._socket.setblocking(False)
|
|
|
|
self._data = bytearray(maxchan)
|
2022-07-20 22:27:40 +02:00
|
|
|
packet = bytearray()
|
|
|
|
packet.extend(map(ord, "Art-Net"))
|
2022-07-23 17:47:19 +02:00
|
|
|
packet.append(0x00) # Null terminate Art-Net
|
2022-07-20 22:27:40 +02:00
|
|
|
packet.extend([0x00, 0x50]) # Opcode ArtDMX 0x5000 (Little endian)
|
|
|
|
packet.extend([0x00, 0x0e]) # Protocol version 14
|
2022-07-24 11:43:00 +02:00
|
|
|
self._header = packet
|
|
|
|
self._sequence = 1
|
|
|
|
self._color = (0, 0, 0)
|
|
|
|
self._animation = FadeTo((255, 255, 255))
|
|
|
|
self._rgbs = []
|
|
|
|
self._thread = None
|
|
|
|
self._updating = False
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
def start(self):
|
2022-07-24 11:43:00 +02:00
|
|
|
if self._thread and self._thread.is_alive():
|
2022-07-20 22:55:37 +02:00
|
|
|
return
|
2022-07-24 11:43:00 +02:00
|
|
|
self._thread = Thread(daemon=True, target=self.background)
|
|
|
|
self._updating = True
|
|
|
|
self._thread.start()
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
def background(self):
|
2022-07-24 11:43:00 +02:00
|
|
|
while self._updating:
|
2022-07-20 22:27:40 +02:00
|
|
|
self.update()
|
|
|
|
# print("updating")
|
2022-07-20 22:55:37 +02:00
|
|
|
# print(self.data)
|
|
|
|
# break
|
2022-07-23 17:47:19 +02:00
|
|
|
sleep(1.0 / 30)
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
def update(self):
|
2022-07-24 11:43:00 +02:00
|
|
|
if not self._animation:
|
2022-07-23 17:47:19 +02:00
|
|
|
return
|
|
|
|
|
2022-07-24 11:43:00 +02:00
|
|
|
for i in range(0, len(self._rgbs)):
|
|
|
|
self._rgbs[i].rgb(self._animation.update(i, len(self._rgbs)))
|
2022-07-23 17:47:19 +02:00
|
|
|
|
2022-07-24 11:43:00 +02:00
|
|
|
packet = self._header[:]
|
|
|
|
packet.append(self._sequence) # Sequence,
|
2022-07-23 17:47:19 +02:00
|
|
|
packet.append(0x00) # Physical
|
2022-07-24 11:43:00 +02:00
|
|
|
packet.append(self._universe & 0xFF) # Universe LowByte
|
|
|
|
packet.append(self._universe >> 8 & 0xFF) # Universe HighByte
|
2022-07-20 22:27:40 +02:00
|
|
|
|
2022-07-24 11:43:00 +02:00
|
|
|
packet.extend(struct.pack('>h', len(self._data))) # Pack the number of channels Big endian
|
|
|
|
packet.extend(self._data)
|
|
|
|
self._socket.sendto(packet, (self._host, self._port))
|
2022-07-21 18:45:46 +02:00
|
|
|
# print(f"sent {len(packet)} bytes, {threading.get_native_id()}")
|
|
|
|
|
2022-07-24 11:43:00 +02:00
|
|
|
self._sequence += 1
|
|
|
|
if self._sequence > 255:
|
|
|
|
self._sequence = 1
|
2022-07-20 22:27:40 +02:00
|
|
|
|
|
|
|
def set(self, slot, value):
|
2022-07-24 11:43:00 +02:00
|
|
|
self._data[slot - 1] = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def animation(self) -> str:
|
|
|
|
return self._animation.name()
|
|
|
|
|
|
|
|
@animation.setter
|
|
|
|
def animation(self, animation: Union[Animation, str]):
|
|
|
|
if isinstance(animation, str):
|
|
|
|
if animation == "off":
|
|
|
|
animation = Off()
|
|
|
|
elif animation == "chase":
|
|
|
|
animation = Chase(self._color)
|
|
|
|
elif animation == "fade":
|
|
|
|
animation = FadeTo(self._color)
|
|
|
|
elif animation == "rainbow":
|
|
|
|
animation = RotatingRainbow()
|
|
|
|
elif animation == "steady":
|
|
|
|
animation = Steady(self._color)
|
2022-08-06 19:42:40 +02:00
|
|
|
elif animation == "twocolor":
|
|
|
|
animation = TwoColor(self._color)
|
|
|
|
elif animation == "caramelldansen":
|
|
|
|
animation = Caramelldansen(self._color)
|
|
|
|
elif animation == "randomsingle":
|
|
|
|
animation = RandomSingle(self._color)
|
2022-07-24 11:43:00 +02:00
|
|
|
else:
|
|
|
|
raise ValueError(f"No such animation {animation}")
|
|
|
|
self._animation = animation
|
|
|
|
if isinstance(animation, Off):
|
|
|
|
self._updating = False
|
|
|
|
if self._thread and self._thread.is_alive():
|
|
|
|
self._thread.join()
|
2022-07-23 17:47:19 +02:00
|
|
|
# one frame black
|
2022-07-24 11:43:00 +02:00
|
|
|
self._animation = Steady((0, 0, 0))
|
2022-07-23 17:47:19 +02:00
|
|
|
self.update()
|
2022-07-26 12:27:50 +02:00
|
|
|
self._animation = Off()
|
2022-07-23 17:47:19 +02:00
|
|
|
else:
|
|
|
|
self.start()
|
|
|
|
print(f"Animation: {animation}", file=sys.stderr)
|
2022-07-24 11:43:00 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def color(self):
|
|
|
|
return self._color
|
|
|
|
|
|
|
|
@color.setter
|
|
|
|
def color(self, color):
|
|
|
|
if self._color != color:
|
|
|
|
self._color = color
|
|
|
|
self.animation = self.animation
|