-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuttonPusher_GUI.py
executable file
·424 lines (381 loc) · 16.9 KB
/
buttonPusher_GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import socket, sys, time
from multithread import Worker, WorkerSignals
class ControlClient():
def __init__(self, IP, port):
self.IP = IP
self.port = port
self.connect()
def connect(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.IP, self.port))
def disconnect(self):
self.sock.close()
def write(self, cmd):
self.sock.send(cmd.encode("utf-8"))
def read(self):
data = self.sock.recv(4096)
return data.decode("utf-8").strip()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Controller for Button Pusher"
self.left = 650
self.top = 30
self.width = 200
self.height = 200
# the default parameters
self.workMode = "2" # default workMode: short press
self.step = "5" # default steps of movement(free mode): 5
self.direct = "1" # default direction of movement(free mode): forward
self.stepDiff = 0 # step difference with the starting point
# the default operation parameters
self.hide = True
self.exit = False
# the default color setting
self.bgcolor = "#FAFAFA"
self.fgcolor = "#23373B"
self.green = "#1B813E"
self.orange = "#E98B2A"
self.red = "#AB3B3A"
# style for the state
self.off_style = """
QProgressBar{{
border-radius: 5px;
border: 1px groove silver;
color: {0:s};
text-align: center
}}
QProgressBar::chunk{{
border-radius: 5px;
background-color: #80{1:s};
}}
""".format(self.fgcolor, self.red[1:])
self.on_style = """
QProgressBar{{
border-radius: 5px;
border: 1px groove silver;
color: {0:s};
text-align: center
}}
QProgressBar::chunk{{
border-radius: 5px;
background-color: #80{1:s};
}}
""".format(self.fgcolor, self.green[1:])
self.init_style = """
QProgressBar{{
border-radius: 5px;
border: 1px groove silver;
color: {0:s};
text-align: center
}}
QProgressBar::chunk{{
border-radius: 5px;
background-color: #80{1:s};
}}
""".format(self.fgcolor, self.orange[1:])
# set the font of label
self.fontStat = QFont("RobotoCondensed", 12)
self.fontLab = QFont("RobotoCondensed", 14)
# set the style for input bar
self.stop_style = """
QLineEdit{{
color: {:s};
}}
""".format(self.fgcolor)
self.run_style = """
QLineEdit{{
color: {:s};
background-color: lightgray
}}
""".format(self.fgcolor)
# set the style for radio button
self.able_style = """
QRadioButton{{
color: {:s};
}}
""".format(self.fgcolor)
self.inable_style = """
QRadioButton{{
color: {:s};
background-color: lightgray
}}
""".format(self.fgcolor)
# set the Icon
self.iconPress = QIcon("./userManual.png")
self.initUI()
def QLineEdit_StopStyle(self, lineEdit):
lineEdit.setStyleSheet(self.stop_style)
lineEdit.setReadOnly(False)
def QLineEdit_RunStyle(self, lineEdit):
lineEdit.setStyleSheet(self.run_style)
lineEdit.setReadOnly(True)
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setStyleSheet("QLabel{{color: {0:s} }} QRadioButton{{background-color: {1:s}; color: {0:s}}} QTextEdit{{color: {0:s}}} QMainWindow{{ background-color: {1:s} }} QCentralWidget{{ background-color: {1:s} }} QGroupBox{{ background-color: {1:s} }}".format(self.fgcolor, self.bgcolor))
self.threadPool = QThreadPool()
self.setDisplayPanel()
self.buildConnection()
self.statusBar().showMessage("choose the press mode before run")
def setDisplayPanel(self):
# set the mode radio button - visiable
self.modeLongButton = QRadioButton("turn off")
self.modeLongButton.setFont(self.fontLab)
self.modeShortButton = QRadioButton("turn on ")
self.modeShortButton.setFont(self.fontLab)
self.modeShortButton.setChecked(True)
# set IQR power label & status
self.IQRstatusLab = QLabel("IQR Power")
self.IQRstatusLab.setFont(self.fontStat)
self.IQRstatus = QProgressBar()
self.IQRstatus.setFont(self.fontLab)
self.IQRstatus.setStyleSheet(self.init_style)
self.IQRstatus.setFormat("waiting")
self.IQRstatus.setValue(100)
# set the working status button
self.statusButton = QPushButton()
self.statusButton.setIcon(self.iconPress)
self.statusButton.setFixedSize(40, 40)
self.statusButton.setIconSize(QSize(25, 25))
# set the free mode buttons - invisiable
self.operatForwardButton = QRadioButton("forward")
self.operatForwardButton.setFont(self.fontLab)
self.operatForwardButton.setChecked(True)
self.operatBackwardButton = QRadioButton("backward")
self.operatBackwardButton.setFont(self.fontLab)
# set the step setting and information
self.stepLab = QLabel("steps")
self.stepLab.setFont(self.fontLab)
self.stepInput = QLineEdit(self.step)
self.stepInput.setFont(self.fontStat)
self.stepInput.setFixedWidth(55)
self.QLineEdit_RunStyle(self.stepInput)
self.stepChangeLab = QLabel("position:")
self.stepChangeLab.setFont(self.fontLab)
self.stepChange = QLabel(str(self.stepDiff))
self.stepChange.setFont(self.fontLab)
# set the normal panel
visiableGrid = QGridLayout()
visiableGrid.setSpacing(5)
visiableGrid.addWidget(self.modeShortButton, 0, 0, 1, 2, Qt.AlignLeft)
visiableGrid.addWidget(self.modeLongButton, 1, 0, 1, 2, Qt.AlignLeft)
visiableGrid.addWidget(self.IQRstatusLab, 0, 2, 1, 2, Qt.AlignHCenter)
visiableGrid.addWidget(self.IQRstatus, 1, 2, 1, 2)
visiableGrid.addWidget(self.statusButton, 0, 4, 2, 2)
self.visiablePanel = QGroupBox()
self.visiablePanel.setStyleSheet("QGroupBox{border: 1px groove silver; margin: 1px; padding-top: 0}")
self.visiablePanel.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
self.visiablePanel.setLayout(visiableGrid)
# set the hidden panel
invisiableGrid = QGridLayout()
invisiableGrid.setSpacing(5)
invisiableGrid.addWidget(self.operatForwardButton, 0, 0, 1, 2, Qt.AlignHCenter)
invisiableGrid.addWidget(self.operatBackwardButton, 0, 2, 1, 2, Qt.AlignHCenter)
invisiableGrid.addWidget(self.stepLab, 1, 0, 1, 1, Qt.AlignRight)
invisiableGrid.addWidget(self.stepInput, 1, 1, 1, 1, Qt.AlignLeft)
invisiableGrid.addWidget(self.stepChangeLab, 1, 2, 1, 1, Qt.AlignRight)
invisiableGrid.addWidget(self.stepChange, 1, 3, 1, 1, Qt.AlignHCenter)
self.invisiablePanel = QGroupBox()
self.invisiablePanel.setStyleSheet("QGroupBox{border: 1px groove silver; margin: 1px; padding-top: 0}")
self.invisiablePanel.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
self.invisiablePanel.setLayout(invisiableGrid)
self.invisiablePanel.setVisible(False)
windowLayout = QVBoxLayout()
windowLayout.setSpacing(5)
windowLayout.addWidget(self.visiablePanel)
windowLayout.addWidget(self.invisiablePanel)
wid = QWidget()
self.setCentralWidget(wid)
wid.setLayout(windowLayout)
def buildConnection(self):
# build connection with mode
def check_mode(b):
if b.text() == "turn on " and b.isChecked() == True:
self.statusBar().showMessage("turn on the IQ recorder")
self.workMode = "2"
if b.text() == "turn off" and b.isChecked() == True:
self.statusBar().showMessage("turn off the IQ recorder")
self.workMode = "1"
self.modeShortButton.toggled.connect(lambda:check_mode(self.modeShortButton))
self.modeLongButton.toggled.connect(lambda:check_mode(self.modeLongButton))
# build connection with forward or backward
def button_direction(b):
if self.invisiablePanel.isVisible():
if b.text() == "forward" and b.isChecked() == True:
self.direct = "1"
if b.text() == "backward" and b.isChecked() == True:
self.direct = "2"
else:
return
self.operatForwardButton.toggled.connect(lambda:button_direction(self.operatForwardButton))
self.operatBackwardButton.toggled.connect(lambda:button_direction(self.operatBackwardButton))
# build connection with play or pause
def button_status():
if self.statusButton.isChecked():
if not self.invisiablePanel.isVisible():
self.current_worker = Worker(Press_work, self.workMode)
self.current_worker.signals.finished.connect(ready)
self.current_worker.signals.progress.connect(process)
self.current_worker.signals.result.connect(result)
self.statusButton.setEnabled(False)
self.threadPool.start(self.current_worker)
else:
self.step = self.stepInput.text()
if int(self.step) <= 0:
self.statusBar.showMessage("step input invaild! input again...")
return
self.current_worker = Worker(Debug_work, self.direct, self.step, self.stepDiff)
self.current_worker.signals.finished.connect(ready)
self.current_worker.signals.progress.connect(process)
self.current_worker.signals.result.connect(result)
self.statusButton.setEnabled(False)
self.QLineEdit_RunStyle(self.stepInput)
self.threadPool.start(self.current_worker)
self.statusBar().showMessage("start press!")
self.statusButton.setCheckable(True)
self.statusButton.toggled.connect(button_status)
self.statusButton.setEnabled(False)
# short/long press work
def Press_work(mode, stdscr):
self.control.write(mode)
while True:
msg = self.control.read()
stdscr.emit(int(msg))
if msg == "10" or msg == "11":
break
return self.stepDiff
# free press work
def Debug_work(direction, step, stepDiff, stdscr):
self.control.write("3")
time.sleep(0.05)
self.control.write(direction + "," + step)
while True:
msg = self.control.read()
stdscr.emit(int(msg))
if msg == "10" or msg == "11":
break
if direction == "1":
return (stepDiff + int(step))
else:
return (stepDiff - int(step))
def process(percentVal):
if percentVal == 0:
self.statusButton.setChecked(False)
self.statusBar().showMessage("end press!")
elif percentVal == 10:
self.IQRstatus.setStyleSheet(self.off_style)
self.IQRstatus.setFormat("off")
elif percentVal == 11:
self.IQRstatus.setStyleSheet(self.on_style)
self.IQRstatus.setFormat("on")
else:
pass
def ready():
self.statusButton.setEnabled(True)
self.QLineEdit_StopStyle(self.stepInput)
self.statusBar().showMessage("operation ends")
if self.hide:
if self.invisiablePanel.isVisible():
self.invisiablePanel.setVisible(False)
self.modeLongButton.setEnabled(True)
self.modeShortButton.setEnabled(True)
self.modeLongButton.setStyleSheet(self.able_style)
self.modeShortButton.setStyleSheet(self.able_style)
self.statusBar().showMessage("hide the free mode")
else:
if not self.invisiablePanel.isVisible():
self.invisiablePanel.setVisible(True)
self.modeLongButton.setEnabled(False)
self.modeShortButton.setEnabled(False)
self.modeLongButton.setStyleSheet(self.inable_style)
self.modeShortButton.setStyleSheet(self.inable_style)
self.statusBar().showMessage("show the free mode")
if self.exit:
self.statusBar().showMessage("exit the controller")
self.control.disconnect()
sys.exit()
def result(stepDiff):
self.stepDiff = stepDiff
if self.stepDiff <= 0:
self.stepChange.setText(str(self.stepDiff))
else:
self.stepChange.setText("+" + str(self.stepDiff))
# intial the client
def client_init(stdscr):
self.control = ControlClient("10.10.91.96", 5052)
self.control.write("init")
msg = self.control.read()
stdscr.emit(int(msg))
client_worker = Worker(client_init)
client_worker.signals.progress.connect(process)
client_worker.signals.finished.connect(ready)
self.statusBar().showMessage("waiting for connecting...")
self.threadPool.start(client_worker)
def keyPressEvent(self, event):
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_H:
if self.statusButton.isEnabled():
if self.invisiablePanel.isVisible():
self.hide = True
self.invisiablePanel.setVisible(False)
self.modeLongButton.setEnabled(True)
self.modeShortButton.setEnabled(True)
self.modeLongButton.setStyleSheet(self.able_style)
self.modeShortButton.setStyleSheet(self.able_style)
self.statusBar().showMessage("hide the free mode")
else:
self.hide = False
self.invisiablePanel.setVisible(True)
self.modeLongButton.setEnabled(False)
self.modeShortButton.setEnabled(False)
self.modeLongButton.setStyleSheet(self.inable_style)
self.modeShortButton.setStyleSheet(self.inable_style)
self.statusBar().showMessage("show the free mode")
else:
if self.invisiablePanel.isVisible():
self.hide = True
self.statusBar().showMessage("hide the free mode after finished!")
else:
self.hide = False
self.statusBar().showMessage("show the free mode after finished!")
elif event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_W:
reply = QMessageBox.question(self, "Message", "Are you sure to quit?")
if reply == QMessageBox.Yes:
if self.statusButton.isEnabled():
self.statusBar().showMessage("exit the controller")
self.control.disconnect()
sys.exit()
else:
self.exit = True
self.statusBar().showMessage("exit after finished!")
else:
return
elif event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_X and self.statusButton.isEnabled():
reply = QMessageBox.question(self, "Message", "Are you sure to quit both controller and raspberry slave?")
if reply == QMessageBox.Yes:
self.statusBar().showMessage("exit the controller")
self.control.write("kill")
self.control.disconnect()
sys.exit()
else:
return
else:
pass
def closeEvent(self, event):
reply = QMessageBox.question(self, "Message", "Are you sure to force quit?")
if reply == QMessageBox.Yes:
self.statusBar().showMessage("exit the controller")
self.control.disconnect()
sys.exit()
else:
event.ignore()
if __name__=='__main__':
app = QApplication(sys.argv)
controller = MainWindow()
controller.show()
sys.exit(app.exec())