From a92cb2bee89cda334c59cc53be4bea3613f7ee48 Mon Sep 17 00:00:00 2001
From: leo <Leo.Strohmidel@gmx.net>
Date: Mon, 4 Jun 2018 18:50:23 +0200
Subject: [PATCH] +global darken/lighten feature

---
 animations/random_color.py       |  5 ++--
 blinkenbase/animation_handler.py | 51 ++++++++++++++++++++++----------
 blinkenbase/blinkenfoo.py        | 21 +++++++++----
 blinkenbase/blinkenroom.py       | 12 ++++++++
 mqtt.py                          |  1 +
 5 files changed, 67 insertions(+), 23 deletions(-)

diff --git a/animations/random_color.py b/animations/random_color.py
index e7dfb81..cc87985 100644
--- a/animations/random_color.py
+++ b/animations/random_color.py
@@ -1,6 +1,7 @@
 from blinkenbase.animation import Animation
 import blinkenbase.blinkenfoo as blinkenfoo
-from blinkenbase.blinkenroom import Blinkenroom
+#from blinkenbase.blinkenroom import Blinkenroom
+import blinkenbase.blinkenroom as blinkenroom
 from blinkenbase.Color import Color
 import random
 
@@ -29,7 +30,7 @@ class Blink(object):
 class random_color(Animation):
 
     def init_animation(self):
-        self.blinkenroom = Blinkenroom(blinkenfoo.DEVICE_LIST_LOUNGE)
+        self.blinkenroom = blinkenroom.BLINKENROOM_LOUNGE
         self.set_fps(20)
         self.blink_dict = {}
         self.name = "random_color"
diff --git a/blinkenbase/animation_handler.py b/blinkenbase/animation_handler.py
index c82efaf..0896f83 100644
--- a/blinkenbase/animation_handler.py
+++ b/blinkenbase/animation_handler.py
@@ -3,6 +3,8 @@ from blinkenbase.blinkenroom import Blinkenroom
 import blinkenbase.blinkenroom
 import blinkenbase.animation
 import time
+from blinkenbase.blinkenroom import BLINKENROOM_LOUNGE
+import _thread
 #from darken import darken
 
 class AnimationHandler(object):
@@ -31,7 +33,7 @@ class AnimationHandler(object):
         for running in self.running_animations.values():
             running.stop()
         self.running_animations = {}
-        self.last_frame = blinkenroom.BLINKENROOM_LOUNGE.get_last_frame()
+        self.last_frame = BLINKENROOM_LOUNGE.get_last_frame()
 
     def pause_animation(self, p_animation_name):
         if p_animation_name in self.running_animations:
@@ -52,13 +54,8 @@ class AnimationHandler(object):
             animation.resume()
 
     def start_animation(self, p_name, p_animation_args):
-    #def start_animation(self, p_name):
         p_name = p_name.lower()
 
-        #if p_name in self.running_animations:
-        #    print("Animation already running")
-        #    return self.ERROR_ANIMATION_RUNNING
-
         animation_class = None
         try:
             exec("from animations.%s import %s" % (p_name, p_name))
@@ -95,14 +92,36 @@ class AnimationHandler(object):
     ###Light-Level###
     #################
 
+    def _thread_handler_darken(self, a, b):
+        f = 100
+        while f > 0:
+            BLINKENROOM_LOUNGE.set_brightness(f)
+            time.sleep(0.05)
+            f -= 1
+        self.pause_all_animations()
+
+    def _thread_handler_lighten(self, a, b):
+        f = 1
+        self.resume_all_animations()
+        while f <= 100:
+            BLINKENROOM_LOUNGE.set_brightness(f)
+            time.sleep(0.05)
+            f += 1
+
     def darken(self, p_args):
-        self.stop_all_animations()
-        darken_dicts = {
-            "frame": self.last_frame,
-            "rate": 30,
-            "steps": 100
-        }
-        animation = darken()
-        animation.set_args(darken_dicts)
-        animation.init_animation()
-        animation.start()
+        _thread.start_new_thread(self._thread_handler_darken, (None, None))
+
+    def lighten(self, p_args):
+        _thread.start_new_thread(self._thread_handler_lighten, (None, None))
+
+#    def darken(self, p_args):
+#        self.stop_all_animations()
+#        darken_dicts = {
+#            "frame": self.last_frame,
+#            "rate": 30,
+#            "steps": 100
+#        }
+#        animation = darken()
+#        animation.set_args(darken_dicts)
+#        animation.init_animation()
+#        animation.start()
diff --git a/blinkenbase/blinkenfoo.py b/blinkenbase/blinkenfoo.py
index 6e5e5ba..2a1ee02 100644
--- a/blinkenbase/blinkenfoo.py
+++ b/blinkenbase/blinkenfoo.py
@@ -27,10 +27,18 @@ class Blinkenfoo(object):
         self._reset_buffer()
 
         self.current_fps = None
-        if p_brightness < 100:
-            self.brightness_factor = (100.0 / p_brightness)
-        else:
-            self.brightness_factor = 1
+        #self.brightness_factor = p_brightness / 100.0
+        super(Blinkenfoo, self).__setattr__("brightness_factor", 100.0 / p_brightness)
+
+    def __setattr__(self, p_key, p_value):
+        if p_key == "brightness_factor":
+            f = 0
+            if p_value <= 100:
+                f = (100.0 / p_value)
+            else:
+                f = 100.0
+            p_value = f
+        super(Blinkenfoo, self).__setattr__(p_key, p_value)
 
     def _split_to_byte(self, p_number):
         """
@@ -128,6 +136,9 @@ class Blinkenfoo(object):
         if p_reset:
             self._reset_buffer()
 
+    def clear(self):
+        self._reset_buffer()
+        self.flush()
 ##################
 ###End of class###
 ##################
@@ -136,7 +147,7 @@ class Blinkenfoo(object):
 SPHERES = Blinkenfoo("Spheres", "ESP_35D447.warpzone", 9, 20)
 PANEL = Blinkenfoo("Panel", "ESP_35d9E4.warpzone", 8, 12)
 WARP_SIGN = Blinkenfoo("Warp-Sign", "ESP_133C4C.warpzone", 1, 25)
-DMX = Blinkenfoo("DMX", "10.0.3.27", 5, 10, p_brightness = 50)
+DMX = Blinkenfoo("DMX", "10.0.3.27", 5, 10, p_brightness = 70)
 CUBES = Blinkenfoo("Cubes", "cubes.warpzone", 8, 5)
 TISCH = Blinkenfoo("Tisch", "tisch.warpzone", 700)
 
diff --git a/blinkenbase/blinkenroom.py b/blinkenbase/blinkenroom.py
index b06d4a1..3415fb8 100644
--- a/blinkenbase/blinkenroom.py
+++ b/blinkenbase/blinkenroom.py
@@ -43,6 +43,14 @@ class Blinkenroom:
     ###public methods###
     ####################
 
+    def set_brightness(self, p_level=100):
+        """
+        Sets the brightness of every blinkendevice in this blinkenroom.
+        Max is 100, min 0
+        """
+        for device in self.device_list:
+            device.brightness_factor = p_level
+
     def send_frame(self, p_frame, p_offset = 0):
         """
         Sends an whole frame to all blinkenfoo devices. The frames length has to
@@ -118,4 +126,8 @@ class Blinkenroom:
             frame += device.buffer
         return frame
 
+    def clear(self):
+        for device in self.device_list:
+            device.clear()
+
 BLINKENROOM_LOUNGE = Blinkenroom(blinkenfoo.DEVICE_LIST_LOUNGE)
diff --git a/mqtt.py b/mqtt.py
index d09f56b..7b0fd93 100644
--- a/mqtt.py
+++ b/mqtt.py
@@ -59,6 +59,7 @@ command_handler_dict["exit"] = handler.stop_all_animations
 command_handler_dict["resume"] = handler_resume
 command_handler_dict["pause"] = handler_pause
 command_handler_dict["darken"] = handler.darken
+command_handler_dict["lighten"] = handler.lighten
 
 client.connect("warpsrvint.warpzone", 1883, 60)
 client.loop_forever()
-- 
GitLab