Browse Source

added slow and fast fade animations

Thomas Buck 5 years ago
parent
commit
a55fb34608
1 changed files with 68 additions and 7 deletions
  1. 68
    7
      linux/caselights

+ 68
- 7
linux/caselights View File

@@ -7,6 +7,9 @@
7 7
 
8 8
 import sys
9 9
 import os.path
10
+import threading
11
+import time
12
+import colorsys
10 13
 import serial, serial.tools, serial.tools.list_ports
11 14
 from PyQt5 import QtWidgets, QtGui, QtCore
12 15
 from PyQt5.QtWidgets import QSystemTrayIcon, QAction, QMenu
@@ -29,8 +32,16 @@ class CaseLights():
29 32
         [ "White", "255", "255", "255", None ],
30 33
     ]
31 34
 
35
+    slowFadeUpdateFreq = 5
36
+    fastFadeUpdateFreq = 20
37
+    fadeSaturation = 1.0
38
+    fadeValue = 1.0
39
+    fadeHueCounter = 0
40
+
32 41
     usedPort = None
33 42
     serial = None
43
+    animation = None
44
+    animationRunning = False
34 45
     menu = None
35 46
     portMenu = None
36 47
     portActions = None
@@ -58,6 +69,18 @@ class CaseLights():
58 69
         colorMenu.triggered.connect(self.setStaticColor)
59 70
         self.menu.addMenu(colorMenu)
60 71
 
72
+        animMenu = QMenu("&Animations")
73
+        noFadeAction = QAction("Off")
74
+        noFadeAction.triggered.connect(self.animOff)
75
+        animMenu.addAction(noFadeAction)
76
+        slowFadeAction = QAction("Slow Fade")
77
+        slowFadeAction.triggered.connect(self.slowFadeOn)
78
+        animMenu.addAction(slowFadeAction)
79
+        fastFadeAction = QAction("Fast Fade")
80
+        fastFadeAction.triggered.connect(self.fastFadeOn)
81
+        animMenu.addAction(fastFadeAction)
82
+        self.menu.addMenu(animMenu)
83
+
61 84
         lightMenu = QMenu("&UV-Light")
62 85
         lightOnAction = QAction("O&n")
63 86
         lightOnAction.triggered.connect(self.lightsOn)
@@ -97,6 +120,8 @@ class CaseLights():
97 120
     def exit(self):
98 121
         if self.serial is not None:
99 122
             if self.serial.is_open:
123
+                print("stopping animations")
124
+                self.animOff()
100 125
                 print("turning off lights")
101 126
                 self.serial.write(b'RGB 0 0 0\n')
102 127
                 self.serial.write(b'UV 0\n')
@@ -162,20 +187,56 @@ class CaseLights():
162 187
             print("error connecting to: " + self.usedPort)
163 188
         return self.serial.is_open
164 189
 
190
+    def printRGBStrings(self, rs, gs, bs):
191
+        if self.serial.is_open:
192
+            r = str.encode(rs)
193
+            g = str.encode(gs)
194
+            b = str.encode(bs)
195
+            rgb = b'RGB ' + r + b' ' + g + b' ' + b + b'\n'
196
+            self.serial.write(rgb)
197
+        else:
198
+            print("not connected")
199
+
165 200
     def setStaticColor(self, action):
201
+        self.animOff()
166 202
         for color in self.staticColors:
167 203
             if color[4] is action:
168
-                if self.serial.is_open:
169
-                    r = str.encode(color[1])
170
-                    g = str.encode(color[2])
171
-                    b = str.encode(color[3])
172
-                    self.serial.write(b'RGB ' + r + b' ' + g + b' ' + b + b'\n')
173
-                else:
174
-                    print("not connected")
204
+                self.printRGBStrings(color[1], color[2], color[3])
175 205
                 return True
176 206
         print("color not found")
177 207
         return False
178 208
 
209
+    def hsvToRgb(self, h, s, v):
210
+        (r, g, b) = colorsys.hsv_to_rgb(h, s, v)
211
+        return (round(r * 255), round(g * 255), round(b * 255))
212
+
213
+    def fadeRunner(self, freq):
214
+        while self.animationRunning is True:
215
+            self.fadeHueCounter += 1
216
+            if self.fadeHueCounter >= 360:
217
+                self.fadeHueCounter = 0
218
+            (r, g, b) = self.hsvToRgb(self.fadeHueCounter / 360.0, self.fadeSaturation, self.fadeValue)
219
+            self.printRGBStrings(str(r), str(g), str(b))
220
+            time.sleep(1.0 / freq)
221
+
222
+    def slowFadeOn(self):
223
+        self.animOff()
224
+        self.animationRunning = True
225
+        self.animation = threading.Thread(target=self.fadeRunner, args=[self.slowFadeUpdateFreq])
226
+        self.animation.start()
227
+
228
+    def fastFadeOn(self):
229
+        self.animOff()
230
+        self.animationRunning = True
231
+        self.animation = threading.Thread(target=self.fadeRunner, args=[self.fastFadeUpdateFreq])
232
+        self.animation.start()
233
+
234
+    def animOff(self):
235
+        self.animationRunning = False
236
+        self.animation = None
237
+        self.printRGBStrings("0", "0", "0")
238
+        time.sleep(0.1)
239
+
179 240
     def lightsOn(self):
180 241
         if self.serial.is_open:
181 242
             self.serial.write(b'UV 1\n')

Loading…
Cancel
Save