繁体   English   中英

LED闪烁如何中断time.sleep RPI

[英]Led Blink how to interrupt time.sleep RPI

我在 qtdesigner 中设计了一个表单。 它有“开”和“关”按钮。 On 按钮应该开始闪烁 LED 和 off 按钮应该停止它。所以,如果 time.sleep 持续时间很短没有问题,但是当我写 10 秒睡眠时,当我点击关闭按钮时它不会立即停止。 程序等待 10 秒以停止 LED 闪烁。 那么time.sleep怎么会被打断呢?


import time
import threading
import RPi.GPIO as GPIO
import sys
from time import sleep
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel
from PyQt5 import QtCore, QtGui, QtWidgets


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

switch = True  


def blink(self):  
        def run():  
            while (switch == True):
                print('BLINK...BLINK...')
                GPIO.output(17, GPIO.HIGH)
                time.sleep(10.0)
                GPIO.output(17, GPIO.LOW)
                time.sleep(10.0)
                if switch == False:  
                    break
        thread = threading.Thread(target=run)
        thread.start()



class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)


        self.pshbttn1 = QtWidgets.QPushButton(Form)
        self.pshbttn1.setGeometry(QtCore.QRect(60, 170, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.pshbttn1.setFont(font)
        self.pshbttn1.setObjectName("pshbttn1")
        self.pshbttn1.clicked.connect(self.switchon)


        self.pshbttn2 = QtWidgets.QPushButton(Form)
        self.pshbttn2.setGeometry(QtCore.QRect(220, 170, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.pshbttn2.setFont(font)
        self.pshbttn2.setObjectName("pshbttn2")
        self.pshbttn2.clicked.connect(self.switchoff)


        self.pshbttn3 = QtWidgets.QPushButton(Form)
        self.pshbttn3.setGeometry(QtCore.QRect(140, 230, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.pshbttn3.setFont(font)
        self.pshbttn3.setObjectName("pshbttn3")
        self.pshbttn3.clicked.connect(app.exit)


        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(80, 80, 251, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "LED"))
        self.pshbttn1.setText(_translate("Form", "ON"))
        self.pshbttn2.setText(_translate("Form", "OFF"))
        self.pshbttn3.setText(_translate("Form", "EXIT"))
        self.label.setText(_translate("Form", "LED\'i açmak için butonları kullanın"))

    def switchon(self):    
        global switch  
        switch = True  
        print ('switch on')
        blink(self)

    def switchoff(self):    
        print ('switch off') 
        global switch  
        switch = False 

if __name__ == "__main__":
     import sys
     app = QtWidgets.QApplication(sys.argv)
     MainWindow = QtWidgets.QMainWindow()
     ui = Ui_Form()
     ui.setupUi(MainWindow)
     MainWindow.show()
     sys.exit(app.exec_())

在这种情况下,不需要使用睡眠,只需使用 QTimer。 为了简化任务,我创建了一个处理引脚的类。 另外,PyQt5 建议不要修改 Qt Designer 生成的类。

import sys

from PyQt5 import QtCore, QtGui, QtWidgets

import RPi.GPIO as GPIO


class Led:
    def __init__(self, pin, timeout=1000):
        self._state = False
        self._pin = pin
        self._timeout = timeout

        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.pin, GPIO.OUT)

        self.blink_timer = QtCore.QTimer(
            interval=self.timeout, timeout=self._on_blink_timeout
        )

    def _on_blink_timeout(self):
        self.state = not self.state

    def _update_internal_state(self):
        GPIO.output(self.pin, GPIO.HIGH if self._state else GPIO.LOW)

    @property
    def pin(self):
        return self._pin

    @property
    def timeout(self):
        return self._timeout

    @timeout.setter
    def timeout(self, v):
        self._timeout = v
        is_active = self.blink_timer.isActive()
        self.blink_timer.setInterval(self.timeout)
        if is_active:
            self.blink_timer.start()

    def on(self):
        self.state = True

    def off(self):
        self.state = False

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, s):
        self._state = s
        self._update_internal_state()

    def start(self):
        self.state = True
        self.blink_timer.start()

    def stop(self):
        self.state = False
        self.blink_timer.stop()


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)

        self.pshbttn1 = QtWidgets.QPushButton(Form)
        self.pshbttn1.setGeometry(QtCore.QRect(60, 170, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.pshbttn1.setFont(font)
        self.pshbttn1.setObjectName("pshbttn1")
        self.pshbttn2 = QtWidgets.QPushButton(Form)
        self.pshbttn2.setGeometry(QtCore.QRect(220, 170, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.pshbttn2.setFont(font)
        self.pshbttn2.setObjectName("pshbttn2")
        self.pshbttn3 = QtWidgets.QPushButton(Form)
        self.pshbttn3.setGeometry(QtCore.QRect(140, 230, 125, 50))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.pshbttn3.setFont(font)
        self.pshbttn3.setObjectName("pshbttn3")
        self.pshbttn3.clicked.connect(app.exit)

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(80, 80, 251, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "LED"))
        self.pshbttn1.setText(_translate("Form", "ON"))
        self.pshbttn2.setText(_translate("Form", "OFF"))
        self.pshbttn3.setText(_translate("Form", "EXIT"))
        self.label.setText(_translate("Form", "LED'i açmak için butonları kullanın"))


class Widget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.setupUi(self)
        self.led = Led(17, timeout=10000)
        self.pshbttn1.clicked.connect(self.led.start)
        self.pshbttn2.clicked.connect(self.led.stop)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM