Browse Source

Added simple CPU usage visual

Thomas Buck 4 years ago
parent
commit
152b61c211
1 changed files with 35 additions and 1 deletions
  1. 35
    1
      linux/caselights

+ 35
- 1
linux/caselights View File

@@ -5,6 +5,7 @@
5 5
 # - python-pyqt5
6 6
 # - python-pyserial
7 7
 
8
+import subprocess
8 9
 import sys
9 10
 import os.path
10 11
 import threading
@@ -34,6 +35,7 @@ class CaseLights():
34 35
 
35 36
     slowFadeUpdateFreq = 5
36 37
     fastFadeUpdateFreq = 20
38
+    cpuUsageUpdateFreq = 1
37 39
     fadeSaturation = 1.0
38 40
     fadeValue = 1.0
39 41
     fadeHueCounter = 0
@@ -81,6 +83,15 @@ class CaseLights():
81 83
         animMenu.addAction(fastFadeAction)
82 84
         self.menu.addMenu(animMenu)
83 85
 
86
+        visualMenu = QMenu("&Visualizations")
87
+        noVisualAction = QAction("Off")
88
+        noVisualAction.triggered.connect(self.animOff)
89
+        visualMenu.addAction(noVisualAction)
90
+        cpuUsageAction = QAction("CPU Usage")
91
+        cpuUsageAction.triggered.connect(self.cpuUsageOn)
92
+        visualMenu.addAction(cpuUsageAction)
93
+        self.menu.addMenu(visualMenu)
94
+
84 95
         lightMenu = QMenu("&UV-Light")
85 96
         lightOnAction = QAction("O&n")
86 97
         lightOnAction.triggered.connect(self.lightsOn)
@@ -105,7 +116,7 @@ class CaseLights():
105 116
             print("no icon found")
106 117
 
107 118
         icon = QIcon()
108
-        if iconPathName is not "":
119
+        if iconPathName != "":
109 120
             pic = QPixmap(32, 32)
110 121
             pic.load(iconPathName)
111 122
             icon = QIcon(pic)
@@ -210,6 +221,29 @@ class CaseLights():
210 221
         (r, g, b) = colorsys.hsv_to_rgb(h, s, v)
211 222
         return (round(r * 255), round(g * 255), round(b * 255))
212 223
 
224
+    def getCurrentCpuUsage(self):
225
+        # https://stackoverflow.com/a/9229692
226
+        # "top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'"
227
+        # https://stackoverflow.com/a/4760517
228
+        cmd = ["top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100 - $1}'"]
229
+        result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
230
+        num = result.stdout.decode('utf-8')
231
+        return float(num)
232
+
233
+    def cpuUsageRunner(self):
234
+        while self.animationRunning is True:
235
+            cpu = self.getCurrentCpuUsage()
236
+            color = cpu / 100.0 * 120.0
237
+            (r, g, b) = self.hsvToRgb((120.0 - color) / 360.0, self.fadeSaturation, self.fadeValue)
238
+            self.printRGBStrings(str(r), str(g), str(b))
239
+            time.sleep(1.0 / self.cpuUsageUpdateFreq)
240
+
241
+    def cpuUsageOn(self):
242
+        self.animOff()
243
+        self.animationRunning = True
244
+        self.animation = threading.Thread(target=self.cpuUsageRunner)
245
+        self.animation.start()
246
+
213 247
     def fadeRunner(self, freq):
214 248
         while self.animationRunning is True:
215 249
             self.fadeHueCounter += 1

Loading…
Cancel
Save