Created
September 11, 2014 01:38
-
-
Save maxp/4f11d072cd8d65bfaf87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// - http://habrahabr.ru/post/236251/ | |
// set edge event on specific gpio_line | |
int gpio_edge_set(int n, const char *edge_str) | |
{ | |
char filename[PATH_MAX]; | |
FILE *file; | |
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", n); | |
file = fopen(filename, "w"); | |
if (file == NULL) return -1; | |
fprintf(file, "%s\n", edge_str); | |
fclose(file); | |
return 0; | |
} | |
// set GPIO line polling mode | |
int gpio_poll(int n) | |
{ | |
char filename[PATH_MAX]; | |
int fd; | |
char c; | |
int err; | |
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", n); | |
fd = open(filename, O_RDONLY); | |
if (fd < 0) return -1; | |
read(fd, &c, sizeof(c)); | |
return fd; | |
} | |
// get GPIO line value | |
int gpio_get(int fd, int timeout) | |
{ | |
struct pollfd pollfd[1]; | |
char c; | |
int err; | |
pollfd[0].fd = fd; | |
pollfd[0].events = POLLPRI | POLLERR; | |
pollfd[0].revents = 0; | |
err = poll(pollfd, 1, timeout); | |
if(err != 1) return -1; | |
lseek(fd, 0, SEEK_SET); | |
err = read(fd, &c, sizeof(c)); | |
if(err != 1) return -1; | |
return c - '0'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment