Browse Source

added first test of pyqt linux client

Thomas Buck 4 years ago
parent
commit
5a3bc963a5
2 changed files with 177 additions and 0 deletions
  1. 177
    0
      linux/caselights.py
  2. BIN
      linux/icon.png

+ 177
- 0
linux/caselights.py View File

@@ -0,0 +1,177 @@
1
+# CaseLights Linux Qt System Tray client
2
+# depends on:
3
+# - python-pyqt5
4
+# - python-pyserial
5
+
6
+import sys
7
+import serial, serial.tools, serial.tools.list_ports
8
+from PyQt5 import QtWidgets, QtGui, QtCore
9
+from PyQt5.QtWidgets import QSystemTrayIcon, QAction, QMenu
10
+from PyQt5.QtGui import QIcon, QPixmap
11
+from PyQt5.QtCore import QCoreApplication, QSettings
12
+
13
+class CaseLights():
14
+    name = "CaseLights"
15
+    vendor = "xythobuz"
16
+    version = "0.1"
17
+
18
+    staticColors = [
19
+        [ "Off",     "0",   "0",   "0", None ],
20
+        [ "Red",   "255",   "0",   "0", None ],
21
+        [ "Green",   "0", "255",   "0", None ],
22
+        [ "Blue",    "0",   "0", "255", None ],
23
+        [ "White", "255", "255", "255", None ],
24
+    ]
25
+
26
+    usedPort = None
27
+    serial = None
28
+
29
+    menu = None
30
+    portMenu = None
31
+    portActions = None
32
+    refreshAction = None
33
+    quitAction = None
34
+
35
+    def __init__(self):
36
+        app = QtWidgets.QApplication(sys.argv)
37
+        QCoreApplication.setApplicationName(self.name)
38
+
39
+        if not QSystemTrayIcon.isSystemTrayAvailable():
40
+            print("System Tray is not available on this platform!")
41
+            sys.exit(0)
42
+
43
+        self.readSettings()
44
+        if self.usedPort is not None:
45
+            self.connect()
46
+
47
+        pic = QPixmap(32, 32)
48
+        pic.load("icon.png")
49
+        icon = QIcon(pic)
50
+
51
+        self.menu = QMenu()
52
+
53
+        colorMenu = QMenu("&Colors")
54
+        for color in self.staticColors:
55
+            color[4] = QAction(color[0])
56
+            colorMenu.addAction(color[4])
57
+        colorMenu.triggered.connect(self.setStaticColor)
58
+        self.menu.addMenu(colorMenu)
59
+
60
+        lightMenu = QMenu("&UV-Light")
61
+        lightOnAction = QAction("O&n")
62
+        lightOnAction.triggered.connect(self.lightsOn)
63
+        lightMenu.addAction(lightOnAction)
64
+        lightOffAction = QAction("O&ff")
65
+        lightOffAction.triggered.connect(self.lightsOff)
66
+        lightMenu.addAction(lightOffAction)
67
+        self.menu.addMenu(lightMenu)
68
+
69
+        self.refreshSerialPorts()
70
+
71
+        self.quitAction = QAction("&Quit")
72
+        self.quitAction.triggered.connect(self.exit)
73
+        self.menu.addAction(self.quitAction)
74
+
75
+        trayIcon = QSystemTrayIcon(icon)
76
+        trayIcon.setToolTip(self.name + " " + self.version)
77
+        trayIcon.setContextMenu(self.menu)
78
+        trayIcon.setVisible(True)
79
+        
80
+        sys.exit(app.exec_())
81
+
82
+    def exit(self):
83
+        if self.serial is not None:
84
+            if self.serial.is_open:
85
+                print("turning off lights")
86
+                self.serial.write(b'RGB 0 0 0\n')
87
+                self.serial.write(b'UV 0\n')
88
+                print("closing connection")
89
+                self.serial.close()
90
+        QCoreApplication.quit()
91
+
92
+    def readSettings(self):
93
+        settings = QSettings(self.vendor, self.name)
94
+        self.usedPort = settings.value("serial_port")
95
+        if self.usedPort is not None:
96
+            print("serial port stored: " + self.usedPort)
97
+        else:
98
+            print("no serial port stored")
99
+
100
+    def writeSettings(self):
101
+        settings = QSettings(self.vendor, self.name)
102
+        settings.setValue("serial_port", self.usedPort)
103
+        if self.usedPort is not None:
104
+            print("storing serial port: " + self.usedPort)
105
+        else:
106
+            print("not storing any serial port")
107
+        del settings
108
+
109
+    def refreshSerialPorts(self):
110
+        self.portMenu = QMenu("Port")
111
+        ports = serial.tools.list_ports.comports()
112
+        self.portActions = []
113
+        for port in ports:
114
+            action = QAction(port.device)
115
+            self.portActions.append(action)
116
+            self.portMenu.addAction(action)
117
+        self.portMenu.triggered.connect(self.selectSerialPort)
118
+
119
+        if self.refreshAction == None:
120
+            self.refreshAction = QAction("&Refresh")
121
+            self.refreshAction.triggered.connect(self.refreshSerialPorts)
122
+        self.portMenu.addAction(self.refreshAction)
123
+        self.menu.insertMenu(self.quitAction, self.portMenu)
124
+
125
+    def selectSerialPort(self, action):
126
+        self.usedPort = action.text()
127
+        self.writeSettings()
128
+        if self.connect():
129
+            self.portMenu.setActiveAction(action)
130
+
131
+    def connect(self):
132
+        if self.usedPort is None:
133
+            print("not connecting to any serial port")
134
+            return False
135
+
136
+        if self.serial is not None:
137
+            print("closing previous port")
138
+            self.serial.close()
139
+
140
+        self.serial = serial.Serial()
141
+        self.serial.port = self.usedPort
142
+        self.serial.baudrate = 115200
143
+        self.serial.open()
144
+        if self.serial.is_open:
145
+            print("connected to: " + self.usedPort)
146
+        else:
147
+            print("error connecting to: " + self.usedPort)
148
+        return self.serial.is_open
149
+
150
+    def setStaticColor(self, action):
151
+        for color in self.staticColors:
152
+            if color[4] is action:
153
+                if self.serial.is_open:
154
+                    r = str.encode(color[1])
155
+                    g = str.encode(color[2])
156
+                    b = str.encode(color[3])
157
+                    self.serial.write(b'RGB ' + r + b' ' + g + b' ' + b + b'\n')
158
+                else:
159
+                    print("not connected")
160
+                return True
161
+        print("color not found")
162
+        return False
163
+
164
+    def lightsOn(self):
165
+        if self.serial.is_open:
166
+            self.serial.write(b'UV 1\n')
167
+        else:
168
+            print("not connected")
169
+
170
+    def lightsOff(self):
171
+        if self.serial.is_open:
172
+            self.serial.write(b'UV 0\n')
173
+        else:
174
+            print("not connected")
175
+
176
+if __name__ == "__main__":
177
+    tray = CaseLights()

BIN
linux/icon.png View File


Loading…
Cancel
Save