检测树莓派-Qt5中的GPIO引脚更改 [英] Detecting raspberry pi - GPIO pin change in Qt5

查看:410
本文介绍了检测树莓派-Qt5中的GPIO引脚更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对树莓派还很陌生.

我的问题的实质如下:

我有一个GPIO引脚更改,必须对其进行监视(连续轮询)以查看其是否更改.如果更改,则必须使用加载程序在我的Qt5项目中加载新的QML文件.考虑到它相当简单,我知道如何做加载器部分.我需要一种轮询GPIO引脚并通知更改的方法.

I have a GPIO pin change that must be monitored(continuously polled) to see if it changes. If it changes, I must load a new QML file in my Qt5 project using a Loader. 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.

我已经读过QSocketNotifier类可能会有所帮助.但坦率地说,我对如何做到这一点一无所知.

I've read that the QSocketNotifier class maybe helpful. 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 :)

我正在构建一种主菜单,以便为在Debian上运行的基于raspberry pi的系统的GUI选择不同的选项.它大致像一个机顶盒.与屏幕上基于GUI的按钮等效,它应该能够从与GPIO引脚连接的外部按钮获取基于硬件的中断,从而导致加载新页面/开始新活动.

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. 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.

如果我理解正确,则代码的c ++部分必须捕获此引脚更改,并将其中继到QML部分.我需要能够做到这一点.

If I understand correctly, the c++ part of the code must capture this pin change, and relay it to the QML part. I need to be able to do this.

推荐答案

您可以将引脚配置为中断驱动.起作用的方式是,在执行配置后,您现在可以在代表该引脚的文件上select(),并且当引脚更改状态时,select()将返回.

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.

以下是sysfs gpio界面上的文档: https://www .kernel.org/doc/Documentation/gpio/sysfs.txt

Here is the documentation on the sysfs gpio interface: https://www.kernel.org/doc/Documentation/gpio/sysfs.txt

这是针脚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");

现在,可以直接使用QSocketNotifier,而不是直接使用select().

Now, instead of using select() directly, you can use QSocketNotifier.

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

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

这篇关于检测树莓派-Qt5中的GPIO引脚更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆