Kleine Verbesserungen

This commit is contained in:
Stefan Bethke 2022-07-20 22:55:37 +02:00
parent b7586bed26
commit 38f3030b93
2 changed files with 30 additions and 11 deletions

39
dmx.py
View file

@ -5,6 +5,12 @@ from threading import Thread
from time import sleep, time from time import sleep, time
def hsv_to_rgb(h, s, v):
(r, g, b) = colorsys.hsv_to_rgb(h, s, v)
return [int(r * 255), int(g * 255), int(b * 255)]
class RGB: class RGB:
def __init__(self, dmx, slot, offset=0): def __init__(self, dmx, slot, offset=0):
self.dmx = dmx self.dmx = dmx
@ -45,6 +51,22 @@ class Steady:
def __str__(self): def __str__(self):
return f"steady({self.r}, {self.g}, {self.b})" return f"steady({self.r}, {self.g}, {self.b})"
class FadeTo(Steady):
def __init__(self, r, g, b, t=2.0):
super(FadeTo, self).__init__(r, g, b)
self.t = t
self.start = time()
def update(self, index, count):
h = (time() - self.start) / self.t
h = min(h, 1.0)
return (int(self.r * h), int(self.g * h), int(self.b * h))
def __str__(self):
return f"fadeTo({self.r}, {self.g}, {self.b})"
class RotatingRainbow: class RotatingRainbow:
def __init__(self): def __init__(self):
pass pass
@ -57,13 +79,9 @@ class RotatingRainbow:
:return: :return:
""" """
hue = (time() / 10.0 + (index + 0.0) / count) % 1.0 hue = (time() / 10.0 + (index + 0.0) / count) % 1.0
rgb = self.hsv_to_rgb(hue, 1, 1) rgb = hsv_to_rgb(hue, 1, 1)
return rgb return rgb
def hsv_to_rgb(self, h, s, v):
(r, g, b) = colorsys.hsv_to_rgb(h, s, v)
return [int(r * 255), int(g * 255), int(b * 255)]
def __str__(self): def __str__(self):
return "RotatingRainbow" return "RotatingRainbow"
@ -81,25 +99,26 @@ class DMX:
packet.extend([0x00, 0x50]) # Opcode ArtDMX 0x5000 (Little endian) packet.extend([0x00, 0x50]) # Opcode ArtDMX 0x5000 (Little endian)
packet.extend([0x00, 0x0e]) # Protocol version 14 packet.extend([0x00, 0x0e]) # Protocol version 14
self.header = packet self.header = packet
self.animation = Steady(64, 64, 64) self.animation = FadeTo(255, 255, 255)
self.rgbs = [] self.rgbs = []
self.thread = None
def start(self): def start(self):
if self.thread and self.thread.is_alive():
return
self.thread = Thread(daemon=True, target=self.background) self.thread = Thread(daemon=True, target=self.background)
self.thread.start() self.thread.start()
def background(self): def background(self):
print("background starts")
while True: while True:
animation = self.animation animation = self.animation
for i in range(0, len(self.rgbs)): for i in range(0, len(self.rgbs)):
self.rgbs[i].rgb(animation.update(i, len(self.rgbs))) self.rgbs[i].rgb(animation.update(i, len(self.rgbs)))
self.update() self.update()
# print("updating") # print("updating")
print(self.data) # print(self.data)
break # break
sleep(1.0/30) sleep(1.0/30)
print("background ends")
def update(self): def update(self):
packet = self.header[:] packet = self.header[:]

View file

@ -45,5 +45,5 @@ dmx.rgbs = [
dmx.update() dmx.update()
dmx.start() dmx.start()
run(host='localhost', port=8080, reloader=True) run(host='localhost', port=8080, server='wsgiref', reloader=True)