Browse Source

Added simple CPU usage visual

Thomas Buck 3 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
 # - python-pyqt5
5
 # - python-pyqt5
6
 # - python-pyserial
6
 # - python-pyserial
7
 
7
 
8
+import subprocess
8
 import sys
9
 import sys
9
 import os.path
10
 import os.path
10
 import threading
11
 import threading
34
 
35
 
35
     slowFadeUpdateFreq = 5
36
     slowFadeUpdateFreq = 5
36
     fastFadeUpdateFreq = 20
37
     fastFadeUpdateFreq = 20
38
+    cpuUsageUpdateFreq = 1
37
     fadeSaturation = 1.0
39
     fadeSaturation = 1.0
38
     fadeValue = 1.0
40
     fadeValue = 1.0
39
     fadeHueCounter = 0
41
     fadeHueCounter = 0
81
         animMenu.addAction(fastFadeAction)
83
         animMenu.addAction(fastFadeAction)
82
         self.menu.addMenu(animMenu)
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
         lightMenu = QMenu("&UV-Light")
95
         lightMenu = QMenu("&UV-Light")
85
         lightOnAction = QAction("O&n")
96
         lightOnAction = QAction("O&n")
86
         lightOnAction.triggered.connect(self.lightsOn)
97
         lightOnAction.triggered.connect(self.lightsOn)
105
             print("no icon found")
116
             print("no icon found")
106
 
117
 
107
         icon = QIcon()
118
         icon = QIcon()
108
-        if iconPathName is not "":
119
+        if iconPathName != "":
109
             pic = QPixmap(32, 32)
120
             pic = QPixmap(32, 32)
110
             pic.load(iconPathName)
121
             pic.load(iconPathName)
111
             icon = QIcon(pic)
122
             icon = QIcon(pic)
210
         (r, g, b) = colorsys.hsv_to_rgb(h, s, v)
221
         (r, g, b) = colorsys.hsv_to_rgb(h, s, v)
211
         return (round(r * 255), round(g * 255), round(b * 255))
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
     def fadeRunner(self, freq):
247
     def fadeRunner(self, freq):
214
         while self.animationRunning is True:
248
         while self.animationRunning is True:
215
             self.fadeHueCounter += 1
249
             self.fadeHueCounter += 1

Loading…
Cancel
Save