From 38f3030b93b3aca9fc3d6bbc69bde16f9bb7579b Mon Sep 17 00:00:00 2001 From: Stefan Bethke Date: Wed, 20 Jul 2022 22:55:37 +0200 Subject: [PATCH] Kleine Verbesserungen --- dmx.py | 39 +++++++++++++++++++++++++++++---------- foobaz.py | 2 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/dmx.py b/dmx.py index 5723a62..892143e 100644 --- a/dmx.py +++ b/dmx.py @@ -5,6 +5,12 @@ from threading import Thread 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: def __init__(self, dmx, slot, offset=0): self.dmx = dmx @@ -45,6 +51,22 @@ class Steady: def __str__(self): 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: def __init__(self): pass @@ -57,13 +79,9 @@ class RotatingRainbow: :return: """ 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 - 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): return "RotatingRainbow" @@ -81,25 +99,26 @@ class DMX: packet.extend([0x00, 0x50]) # Opcode ArtDMX 0x5000 (Little endian) packet.extend([0x00, 0x0e]) # Protocol version 14 self.header = packet - self.animation = Steady(64, 64, 64) + self.animation = FadeTo(255, 255, 255) self.rgbs = [] + self.thread = None def start(self): + if self.thread and self.thread.is_alive(): + return self.thread = Thread(daemon=True, target=self.background) self.thread.start() def background(self): - print("background starts") while True: animation = self.animation for i in range(0, len(self.rgbs)): self.rgbs[i].rgb(animation.update(i, len(self.rgbs))) self.update() # print("updating") - print(self.data) - break + # print(self.data) + # break sleep(1.0/30) - print("background ends") def update(self): packet = self.header[:] diff --git a/foobaz.py b/foobaz.py index 19782ea..37c480f 100644 --- a/foobaz.py +++ b/foobaz.py @@ -45,5 +45,5 @@ dmx.rgbs = [ dmx.update() dmx.start() -run(host='localhost', port=8080, reloader=True) +run(host='localhost', port=8080, server='wsgiref', reloader=True)