-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpiotest_pyqt5.py
151 lines (129 loc) · 5.3 KB
/
gpiotest_pyqt5.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
#!/usr/bin/python3
import threading
import time
import sys
import os
from libsoc import gpio
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
# define GPIO pins
gpio_input_id = int(os.environ.get('GPIO_IN', '170'))
gpio_output_id = int(os.environ.get('GPIO_OUT', '169'))
# QThread class to process GPIO interrupt
class gpioInterrupt(QThread):
# LED GPIO status signal
LedSignal = pyqtSignal(int)
# initial GPIO
gpio_in = gpio.GPIO(gpio_input_id, gpio.DIRECTION_INPUT)
gpio_out = gpio.GPIO(gpio_output_id, gpio.DIRECTION_OUTPUT)
def __init__(self, parent=None):
super(gpioInterrupt, self).__init__(parent)
self.working = True
def __del__(self):
self.working = False
self.wait()
def run(self):
# GPIO open
with gpio.request_gpios((self.gpio_in, self.gpio_out)):
# set LED GPIO out default to 1
self.gpio_out.set_high()
assert self.gpio_out.is_high()
print("The LED initial status is ON")
# loop for processing BUTTON GPIO interrupt
while self.working == True:
# start interrput
self.gpio_in.set_direction(gpio.DIRECTION_INPUT, gpio.EDGE_RISING)
self.gpio_in.wait_for_interrupt(-1)
# KEY debouncer code
debounce = 0
for x in range(3):
time.sleep(0.02)
if self.gpio_in.is_high() == 1:
debounce = debounce + 1
if debounce >= 2:
if self.gpio_out.is_high() == 1:
self.gpio_out.set_low()
LedStatus = 0
# send LED GPIO set to 0 signal
self.LedSignal.emit(LedStatus)
print("The LED turns OFF")
else:
self.gpio_out.set_high()
LedStatus = 1
# send LED GPIO set to 1 signal
self.LedSignal.emit(LedStatus)
print("The LED turns ON")
# main window QWidget class
class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# load ui file generated by Qtcreator project
loadUi('mainwindow.ui', self)
# LED GPIO current status indicator
self.LedStatus = 1
# initial relatd button and frame
self.Led_Status.setStyleSheet("background-color: rgb(115, 210, 22)")
self.pushButton_On.setEnabled(False)
# connect Buttion slot
self.pushButton_On.clicked.connect(self.Button_On_clicked)
self.pushButton_Off.clicked.connect(self.Button_Off_clicked)
self.pushButton_Exit.clicked.connect(self.Button_Exit_clicked)
# start the GPIO interrupt thread
self.thread = gpioInterrupt()
self.thread.LedSignal.connect(self.LedStatusChange)
self.thread.start()
# LED frame/label and Push Button processing according to GPIO interrupt thread feedback
def LedStatusChange(self, LedStat):
if LedStat == 1:
self.LedStatus = 1
self.Led_Status.setStyleSheet("background-color: rgb(115, 210, 22)")
self.label_On.setText("ON")
self.pushButton_On.setEnabled(False)
self.pushButton_Off.setEnabled(True)
print("button clicked for setting HIGH")
else:
self.LedStatus = 0
self.Led_Status.setStyleSheet("background-color: rgb(255, 255, 255)")
self.label_On.setText("OFF")
self.pushButton_Off.setEnabled(False)
self.pushButton_On.setEnabled(True)
print("button clicked for setting LOW")
# Push Button On processing
def Button_On_clicked(self):
self.gpio_out = self.thread.gpio_out
if self.LedStatus == 0:
self.thread.gpio_out.set_high()
self.LedStatus = 1
self.Led_Status.setStyleSheet("background-color: rgb(115, 210, 22)")
self.label_On.setText("ON")
self.pushButton_On.setEnabled(False)
self.pushButton_Off.setEnabled(True)
print("set HIGH")
# Push Button Off processing
def Button_Off_clicked(self):
self.gpio_out = self.thread.gpio_out
if self.LedStatus == 1:
self.gpio_out.set_low()
self.LedStatus = 0
self.Led_Status.setStyleSheet("background-color: rgb(255, 255, 255)")
self.label_On.setText("OFF")
self.pushButton_Off.setEnabled(False)
self.pushButton_On.setEnabled(True)
print("set LOW")
# Push Button Exit processing
def Button_Exit_clicked(self):
self.close()
# Close event processing to exit main program and GPIO interrupt thread accordingly
def closeEvent(self, event):
# stop the thread and delete
if self.thread.isRunning():
self.thread.quit()
del self.thread
super(ApplicationWindow, self).closeEvent(event)
if __name__ == '__main__':
app = QApplication(sys.argv)
appWindow = ApplicationWindow()
appWindow.show()
sys.exit(app.exec_())