出自flip the world 檢視Application - Monitor GPIO interrupt in userspace的原始碼 頁面討論 閱讀檢視原始碼檢視歷史 ←Application - Monitor GPIO interrupt in userspace 您沒有權限執行編輯此頁面,由於以下原因: 您請求的操作只有這個群組的使用者能使用:使用者 您可以檢視並複製此頁面的原始碼。 Watching a '''GPIO interrupt from userspace''' on a Raspberry Pi with a small GLib program, rather than polling the pin. == Monitor GPIO 4 interrupt == === watch-button-glib.cpp === <syntaxhighlight lang="c"> #include <glib.h> #include <fcntl.h> #include <iostream> using namespace std; #define buf_sz 10 static int onButtonEvent(GIOChannel *channel, GIOCondition condition, gpointer user_data) { static char buf[buf_sz]; cerr << "onButtonEvent" <<endl; GError *error = 0; gsize bytes_read = 0; 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/gpio4/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); } </syntaxhighlight> === Makefile === <pre> watch-button-glib: watch-button-glib.cpp Makefile g++ watch-button-glib.cpp -o watch-button-glib -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include -lglib-2.0 </pre> [[Category:Projects]] 返回到「Application - Monitor GPIO interrupt in userspace」。