Two (probably) very simple questions
-
@jazzycamel Most of the resources online are only discussing pyqt and gpio output(ex: turning a lightbulb on). Has anyone gotten pyqt to work as a GPIO button signalling the computer that the button is pressed as a GPIO input (sorry messed that up previously)? The only information that I have found so far is making a thread (qthread) so looping can be processed separately, but I have not seen an example of this working for GPIO input . An example would be greatly appreciated! Thanks!
-
Something like the following should do what you want (if I'm understanding you correctly):
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) from functools import partial from PyQt5.QtCore import QObject, pyqtSignal pyqtWrapperType=type(QObject) class Singleton(pyqtWrapperType): _instances={} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls]=super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] class QGPIO(QObject, metaclass=Singleton): interrupted=pyqtSignal(int,int) @staticmethod def _callback(pin, event): QGPIO().interrupted.emit(pin,event) def register(self, pin, event, bounceTime=300): callback=partial(self._callback, pin, event) GPIO.add_event_detect(pin, event, callback=callback, bouncetime=bounceTime) if __name__=="__main__": from sys import argv, exit from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QApplication, QWidget class Widget(QWidget): def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) QGPIO().interrupted.connect(self.interrupted) QGPIO().register(17, GPIO.FALLING) @pyqtSlot(int,int) def interrupted(self, pin, event): print("Interrupted:", pin, event) a=QApplication(argv) w=Widget() w.show() exit(a.exec_())
Basically(!), I create a singleton
QObject()
that has a callback that can be registered with the RPi.GPIO interrupt system such that, when an edge change is detected, theQGPIO()
class will trap the interrupt via_callback
and then emit a signalinterrupted(int,int)
which will inform a slot of which pin has changed and by which event type.Just a warning/disclaimer: I've tested all the PyQt bits, but I had to stub the RPi.GPIO bits as I don't have a Pi handy right this second.
-
@jazzycamel What do I change to make this work for pyqt4? I am currently using a RaspberryPi 0 and it looks like pyqt5 doesnt have full support for arm v6 cpu.
-
@Stumbino
Not much I shouldn't have thought...Change the
PyQt5
imports toPyQt4
and use the Python 2 (I'm assuming you are using Python 2 with PyQt4?) metaclass syntax i.e.:class QGPIO(QObject): __metaclass__=Singleton
Should work fine. I don't have a
PyQt4
setup to test I am afraid, but if you have any issues just post 'em here :) -
@jazzycamel I got all the libraries switched over to pyqt4, but I get these two errors when I run the program.
-
@jazzycamel That worked! How would I load a UI file in the Widget class? I tried using uic.loadUI(), but that seems to override the previous gui.