This commit is contained in:
Stefan Bethke 2023-05-19 19:43:45 +02:00
parent f0cbb639e8
commit 2caebf320b
2 changed files with 71 additions and 39 deletions

75
dmx.py
View file

@ -13,21 +13,23 @@ def ledlog(value):
class RGB: class RGB:
def __init__(self, dmx, slot, offset=0): def __init__(self, slot, offset=0):
self.dmx = dmx
self.slot = slot self.slot = slot
self.offset = offset self.offset = offset
def rgb(self, color): def rgb(self, dmx, color):
(r, g, b) = color (r, g, b) = color
self.dmx.set(self.slot + self.offset + 0, ledlog(r)) dmx.set(self.slot + self.offset + 0, ledlog(r))
self.dmx.set(self.slot + self.offset + 1, ledlog(g)) dmx.set(self.slot + self.offset + 1, ledlog(g))
self.dmx.set(self.slot + self.offset + 2, ledlog(b)) dmx.set(self.slot + self.offset + 2, ledlog(b))
class Bar252(RGB): class Bar252(RGB):
def __init__(self, dmx, slot=1): def __init__(self, dmx, slot=1):
super(Bar252, self).__init__(dmx, slot, 2) super(Bar252, self).__init__(dmx, slot, 2)
def rgb(self, dmx, color):
super().rgb(dmx, color)
dmx.set(self.slot + 0, 81) dmx.set(self.slot + 0, 81)
dmx.set(self.slot + 1, 0) dmx.set(self.slot + 1, 0)
@ -37,6 +39,10 @@ class REDSpot18RGB(RGB):
super(REDSpot18RGB, self).__init__(dmx, slot, 1) super(REDSpot18RGB, self).__init__(dmx, slot, 1)
dmx.set(self.slot + 0, 0) dmx.set(self.slot + 0, 0)
def rgb(self, dmx, color):
super().rgb(dmx, color)
dmx.set(self.slot + 0, 81)
class StairvilleLedPar56(RGB): class StairvilleLedPar56(RGB):
def __init__(self, dmx, slot=1): def __init__(self, dmx, slot=1):
@ -46,6 +52,28 @@ class StairvilleLedPar56(RGB):
dmx.set(self.slot + 5, 0) dmx.set(self.slot + 5, 0)
dmx.set(self.slot + 6, 255) dmx.set(self.slot + 6, 255)
class Group:
def __init__(self, rgbs : list[RGB]):
self._rgbs = rgbs
self._color = (0, 0, 0)
self._animation = FadeTo((255, 255, 255))
@property
def animation(self) -> Animation:
return self._animation
@animation.setter
def animation(self, animation: Animation):
self._animation = animation
@property
def color(self) -> list[int]:
return self._color
@color.setter
def color(self, color: list[int]):
self._color = color
class DMX: class DMX:
def __init__(self, host, port=0x1936, universe=1, maxchan=512): def __init__(self, host, port=0x1936, universe=1, maxchan=512):
@ -62,12 +90,13 @@ class DMX:
packet.extend([0x00, 0x0e]) # Protocol version 14 packet.extend([0x00, 0x0e]) # Protocol version 14
self._header = packet self._header = packet
self._sequence = 1 self._sequence = 1
self._color = (0, 0, 0) self._groups = []
self._animation = FadeTo((255, 255, 255))
self._rgbs = []
self._thread = None self._thread = None
self._updating = False self._updating = False
def group(self, group: int, rgbs : list[RGB]):
self._groups[group] = Group(rgbs)
def start(self): def start(self):
if self._thread and self._thread.is_alive(): if self._thread and self._thread.is_alive():
return return
@ -84,11 +113,12 @@ class DMX:
sleep(1.0 / 30) sleep(1.0 / 30)
def update(self): def update(self):
if not self._animation: if not self._groups[0]._animation:
return return
for i in range(0, len(self._rgbs)): for g in self._groups:
self._rgbs[i].rgb(self._animation.update(i, len(self._rgbs))) for i in range(0, len(g._rgbs)):
g._rgbs[i].rgb(self, g._animation.update(i, len(g._rgbs)))
packet = self._header[:] packet = self._header[:]
packet.append(self._sequence) # Sequence, packet.append(self._sequence) # Sequence,
@ -110,7 +140,7 @@ class DMX:
@property @property
def animation(self) -> str: def animation(self) -> str:
return self._animation.name() return self._groups[0]._animation.name()
@animation.setter @animation.setter
def animation(self, animation: Union[Animation, str]): def animation(self, animation: Union[Animation, str]):
@ -118,34 +148,35 @@ class DMX:
if animation == "off": if animation == "off":
animation = Off() animation = Off()
elif animation == "chase": elif animation == "chase":
animation = Chase(self._color) animation = Chase(self._groups[0]._color)
elif animation == "fade": elif animation == "fade":
animation = FadeTo(self._color) animation = FadeTo(self._groups[0]._color)
elif animation == "rainbow": elif animation == "rainbow":
animation = RotatingRainbow() animation = RotatingRainbow()
elif animation == "steady": elif animation == "steady":
animation = Steady(self._color) animation = Steady(self._groups[0]._color)
else: else:
raise ValueError(f"No such animation {animation}") raise ValueError(f"No such animation {animation}")
self._animation = animation self._groups[0]._animation = animation
if isinstance(animation, Off): if isinstance(animation, Off):
self._updating = False self._updating = False
if self._thread and self._thread.is_alive(): if self._thread and self._thread.is_alive():
self._thread.join() self._thread.join()
# one frame black # one frame black
self._animation = Steady((0, 0, 0)) self._groups[0]._animation = Steady((0, 0, 0))
self.update() self.update()
self._animation = Off() self._groups[0]._animation = Off()
else: else:
self.start() self.start()
print(f"Animation: {animation}", file=sys.stderr) print(f"Animation: {animation}", file=sys.stderr)
@property @property
def color(self): def color(self):
return self._color return self._groups[0].color
@color.setter @color.setter
def color(self, color): def color(self, color):
if self._color != color: if self._groups[0]._color != color:
self._color = color self._groups[0].color = color
self.animation = self.animation self.animation = self.animation

View file

@ -53,27 +53,28 @@ def main(args):
artnet = args.artnet artnet = args.artnet
print(f"Starting DMX via Art-Net to {artnet}", file=sys.stderr) print(f"Starting DMX via Art-Net to {artnet}", file=sys.stderr)
# dmx = DMX("10.31.242.35")
dmx = DMX(artnet, maxchan=128) dmx = DMX(artnet, maxchan=128)
if args.room == "shop": if args.room == "shop":
dmx._rgbs = [ dmx.group(0, [
REDSpot18RGB(dmx, 1), REDSpot18RGB(1),
REDSpot18RGB(dmx, 5), REDSpot18RGB(5),
REDSpot18RGB(dmx, 9), REDSpot18RGB(9),
REDSpot18RGB(dmx, 13), REDSpot18RGB(13),
] ])
elif args.room == "big": elif args.room == "big":
dmx._rgbs = [ dmx.group(0, [
StairvilleLedPar56(dmx, 1), StairvilleLedPar56(1), # Window/Kitchen
StairvilleLedPar56(dmx, 8), StairvilleLedPar56(15), # Hallway/Kitchen
StairvilleLedPar56(dmx, 15), Bar252(29), # Window/Blackboard
StairvilleLedPar56(dmx, 22), Bar252(40), # Hallway/Blackboard
Bar252(dmx, 29), Bar252(51), # Window/Kitchen
Bar252(dmx, 40), Bar252(62), # Hallway/Kitchen
Bar252(dmx, 51), ])
Bar252(dmx, 62), dmx.group(0, [
] StairvilleLedPar56(8), # Window/Blackboard
StairvilleLedPar56(22), # Hallway/Blackboard
])
else: else:
print(f"Unknown room {args.room}", file=sys.stderr) print(f"Unknown room {args.room}", file=sys.stderr)
sys.exit(64) sys.exit(64)