Skip to content

Instantly share code, notes, and snippets.

@jadonk
Created May 3, 2012 17:41
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save jadonk/2587524 to your computer and use it in GitHub Desktop.
Save jadonk/2587524 to your computer and use it in GitHub Desktop.
Quick test using epoll to wait on GPIO events
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/types.h>
int main(int argc, char** argv) {
int n;
int epfd = epoll_create(1);
int fd = open("/sys/class/gpio/gpio34/value", O_RDWR | O_NONBLOCK);
printf("open returned %d: %s\n", fd, strerror(errno));
if(fd > 0) {
char buf = 0;
struct epoll_event ev;
struct epoll_event events;
ev.events = EPOLLPRI;
ev.data.fd = fd;
n = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
printf("epoll_ctl returned %d: %s\n", n, strerror(errno));
while(1) {
n = epoll_wait(epfd, &events, 1, -1);
printf("epoll returned %d: %s\n", n, strerror(errno));
if(n > 0) {
n = lseek(fd, 0, SEEK_SET);
printf("seek %d bytes: %s\n", n, strerror(errno));
n = read(fd, &buf, 1);
printf("read %d bytes: %s\n", n, strerror(errno));
printf("buf = 0x%x\n", buf);
}
}
}
return(0);
}
root@beaglebone:/var/lib/cloud9# gcc -o epolltest epolltest.c && ./epolltest
open returned 4: Success
epoll_ctl returned 0: Success
epoll returned 1: Success
seek 0 bytes: Success
read 1 bytes: Success
buf = 0x31
epoll returned 1: Success
seek 0 bytes: Success
read 1 bytes: Success
buf = 0x30
epoll returned 1: Success
seek 0 bytes: Success
read 1 bytes: Success
buf = 0x31
epoll returned 1: Success
seek 0 bytes: Success
read 1 bytes: Success
buf = 0x30
epoll returned 1: Success
seek 0 bytes: Success
read 1 bytes: Success
buf = 0x31
^C
root@beaglebone:/var/lib/cloud9#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment