我刚刚开始为Linux编程,现在我希望在连接到Beaglebone的外部按钮被按下时触发某些事件。 Here我发现了一个基于Glib的很好的解决方案,并试图实现它。但不幸的是,事件一开始只触发一次,然后无论我按下按钮多少次都没有反应。GIocondition G_IO_PRI用于按Beaglebone上的按钮不被满足
下面是本教程究竟采取代码:
#include<iostream>
#include<unistd.h>
#include<fstream>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<glib-2.0/glib.h>
using namespace std;
static gboolean onButtonEvent(GIOChannel *channel, GIOCondition condition, gpointer user_data)
{
GError *error = 0;
gsize bytes_read = 0;
const int buf_sz = 1024;
gchar buf[buf_sz] = {};
g_io_channel_seek_position(channel, 0, G_SEEK_SET, 0);
GIOStatus rc = g_io_channel_read_chars(channel,
buf,buf_sz - 1,
&bytes_read,
&error);
cerr << "rc:" << rc << " data:" << buf << endl;
return 1;
}
int main(int argc, char** argv)
{
GMainLoop* loop = g_main_loop_new(0, 0);
int fd = open("/sys/class/gpio/gpio49/value", O_RDONLY | O_NONBLOCK);
GIOChannel* channel = g_io_channel_unix_new(fd);
GIOCondition cond = GIOCondition(G_IO_PRI);
guint id = g_io_add_watch(channel, cond, onButtonEvent, 0);
g_main_loop_run(loop);
}
当我已经改变了GIOCondition到G_IO_IN的onButtonEvent运行所有的时间给我的GPIO正确的值,但无穷无尽,因为(如写作here)g_io_add_watch在这种情况下总会在有数据读取时读取文件。但我只想在文件被更改时读取数据。
是否真的有可能通过G_IO_PRI在“有紧急数据要读取”时调用事件?当这个条件满足时?什么是我的错误?