76 lines
1.7 KiB
C
76 lines
1.7 KiB
C
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <sys/epoll.h>
|
|
|
|
#define GPIO_EXPORT "/sys/class/gpio/export"
|
|
#define GPIO_UNEXPORT "/sys/class/gpio/unexport"
|
|
|
|
static int open_led(char* gpio_path, char* pin) {
|
|
|
|
// unexport pin out of sysfs (reinitialization)
|
|
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
|
write(f, pin, strlen(pin));
|
|
close(f);
|
|
|
|
// export pin to sysfs
|
|
f = open(GPIO_EXPORT, O_WRONLY);
|
|
write(f, pin, strlen(pin));
|
|
close(f);
|
|
|
|
// config pin
|
|
char direction_path[100];
|
|
strcpy(direction_path, gpio_path);
|
|
strcat(direction_path, "/direction");
|
|
|
|
f = open(direction_path, O_WRONLY);
|
|
write(f, "out", 3);
|
|
close(f);
|
|
|
|
// open gpio value attribute
|
|
char value_path[100];
|
|
strcpy(value_path, gpio_path);
|
|
strcat(value_path, "/value");
|
|
|
|
f = open(value_path, O_RDWR);
|
|
return f;
|
|
}
|
|
|
|
static int open_btn(char* gpio_path, char* pin) {
|
|
int f = open(GPIO_UNEXPORT, O_WRONLY);
|
|
write(f, pin, strlen(pin));
|
|
close(f);
|
|
|
|
f = open(GPIO_EXPORT, O_WRONLY);
|
|
write(f, pin, strlen(pin));
|
|
close(f);
|
|
|
|
char direction_path[100];
|
|
strcpy(direction_path, gpio_path);
|
|
strcat(direction_path, "/direction");
|
|
|
|
f = open(direction_path, O_WRONLY);
|
|
write(f, "in", 2);
|
|
close(f);
|
|
|
|
char edge_path[100];
|
|
strcpy(edge_path, gpio_path);
|
|
strcat(edge_path, "/edge");
|
|
|
|
f = open(edge_path, O_WRONLY);
|
|
write(f, "both", 4); // "both" means it triggers on press AND release
|
|
close(f);
|
|
|
|
char value_path[100];
|
|
strcpy(value_path, gpio_path);
|
|
strcat(value_path, "/value");
|
|
|
|
f = open(value_path, O_RDONLY);
|
|
return f;
|
|
}
|