简体   繁体   English

检测树莓派-Qt5中的GPIO引脚更改

[英]Detecting raspberry pi - GPIO pin change in Qt5

I'm fairly new to the raspberry pi. 我是树莓派的新手。

The essence of my problem is as follows: 我的问题的实质如下:

I have a GPIO pin change that must be monitored(continuously polled) to see if it changes. 我有一个GPIO引脚更改,必须对其进行监视(连续轮询)以查看其是否更改。 If it changes, I must load a new QML file in my Qt5 project using a Loader. 如果更改,则必须使用加载程序在Qt5项目中加载新的QML文件。 I know how to do the loader part, considering its fairly simple. 考虑到它相当简单,我知道如何做它。 I need a way to poll the GPIO pin and to be notified of a change. 我需要一种轮询GPIO引脚并通知更改的方法。

I've read that the QSocketNotifier class maybe helpful. 我读过QSocketNotifier类可能会有所帮助。 But I'm frankly clueless as to how to do this. 但坦率地说,我对如何做到这一点一无所知。

Any help provided towards figuring out how to do this is appreciated. 感谢您提供任何解决方法。 Thank you :) 谢谢 :)

EDIT: I'm building a sort of a Main Menu to select different options on a GUI for a raspberry pi based system, running on Debian. 编辑:我正在建立一种主菜单,以便在基于Debian的基于树莓派的系统的GUI上选择不同的选项。 It's roughly like a set top box. 大致就像一个机顶盒。 In equivalence to the GUI based buttons on screen, It should be able to acquire a hardware based interrupt from external buttons that are connected to the GPIO pins that causes a new page to load/a new activity to begin. 与屏幕上基于GUI的按钮等效,它应该能够从与GPIO引脚连接的外部按钮获取基于硬件的中断,从而导致加载新页面/开始新活动。

If I understand correctly, the c++ part of the code must capture this pin change, and relay it to the QML part. 如果我理解正确,则代码的c ++部分必须捕获此引脚更改,并将其中继到QML部分。 I need to be able to do this. 我需要能够做到这一点。

You said nothing about the environment and anything else that needs doing. 您没有提到环境以及需要做的其他事情。 If there are not other tasks nor an o/s, maybe something this is what you want?: 如果没有其他任务或操作系统,也许这就是您想要的?

int last_state = -1;   // impossible state so change is noticed the first time
for (;;)  // do forever
{
       int state = get_gpio (THE_EVENT);
       if (state == last_state)
       {
            sleep (100);
            continue;
       }
       do_loader_stuff();
       last_state = state;
}

You can configure the pin to be interrupt driven. 您可以将引脚配置为中断驱动。 The way this works is that after you perform the configuration, you can now select() on the file representing the pin, and select() will return when the pin changes state. 这种工作方式是在执行配置后,您现在可以在代表该引脚的文件上执行select() ,并且当引脚更改状态时select()将返回。

Here is the documentation on the sysfs gpio interface: https://www.kernel.org/doc/Documentation/gpio/sysfs.txt 这是sysfs gpio界面上的文档: https : //www.kernel.org/doc/Documentation/gpio/sysfs.txt

Here is what the setup for pin 19 would look like: 引脚19的设置如下所示:

QFile exportFile("/sys/class/gpio/gpio19/export");
exportFile.open(QIODevice::WriteOnly);
exportFile.write("19");

QFile directionFile("/sys/class/gpio/gpio19/direction");
directionFile.open(QIODevice::WriteOnly);
directionFile.write("in");

QFile edgeFile("/sys/class/gpio/gpio19/edge");
edgeFile.open(QIODevice::WriteOnly);
edgeFile.write("body");

Now, instead of using select() directly, you can use QSocketNotifier . 现在,可以直接使用QSocketNotifier而不是直接使用select()

QFile file("/sys/class/gpio/gpio19/value");

QSocketNotifier notifier(file.handle(), QSocketNotifier::Read);
connect(&notifier, &QSocketNotifier::activated, this, &MyClass::interruptFired);

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

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