mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 04:43:21 +00:00
Initial work at refactoring the edge detection code I added to get it working
This commit is contained in:
@ -63,6 +63,7 @@ struct fdx *fd_list = NULL;
|
||||
// event callbacks
|
||||
struct callback
|
||||
{
|
||||
int fde;
|
||||
int gpio;
|
||||
int edge;
|
||||
void (*func)(int gpio);
|
||||
@ -159,7 +160,6 @@ void close_value_fd(int gpio)
|
||||
}
|
||||
} /* close_value_fd */
|
||||
|
||||
|
||||
int fd_lookup(int gpio)
|
||||
{
|
||||
struct fdx *f = fd_list;
|
||||
@ -173,6 +173,18 @@ int fd_lookup(int gpio)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fde_lookup(int gpio)
|
||||
{
|
||||
struct callbacks *cb = callbacks;
|
||||
while (cb != NULL)
|
||||
{
|
||||
if (cb->gpio == gpio)
|
||||
return cb->fde;
|
||||
cb = cb->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int add_fd_list(int gpio, int fd)
|
||||
{
|
||||
@ -213,6 +225,23 @@ int open_value_file(int gpio)
|
||||
return fd;
|
||||
} /* open_value_file */
|
||||
|
||||
int open_edge_file(int gpio)
|
||||
{
|
||||
int fd;
|
||||
char filename[MAX_FILENAME];
|
||||
|
||||
// create file descriptor of value file
|
||||
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio); BUF2SMALL(filename);
|
||||
|
||||
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "open_edge_file: could not open '%s' (%s)", filename, strerror(errno));
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
} /* open_edge_file */
|
||||
|
||||
int gpio_unexport(int gpio)
|
||||
{
|
||||
@ -447,35 +476,20 @@ int gpio_set_edge(int gpio, unsigned int edge)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int open_edge_file(int gpio)
|
||||
{
|
||||
int fd;
|
||||
char filename[MAX_FILENAME];
|
||||
|
||||
// create file descriptor of value file
|
||||
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio); BUF2SMALL(filename);
|
||||
|
||||
if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "open_edge_file: could not open '%s' (%s)", filename, strerror(errno));
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
} /* open_edge_file */
|
||||
|
||||
int gpio_get_edge(int gpio)
|
||||
{
|
||||
int fd;
|
||||
int fd = fde_lookup(gpio);
|
||||
int rtnedge = -1;
|
||||
|
||||
if (!fd)
|
||||
{
|
||||
if ((fd = open_edge_file(gpio)) == -1) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d edge file", gpio);
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (lseek(fd, 0, SEEK_SET) < 0) {
|
||||
char err[256];
|
||||
@ -542,6 +556,7 @@ int add_edge_callback(int gpio, int edge, void (*func)(int gpio))
|
||||
|
||||
new_cb = malloc(sizeof(struct callback)); ASSRT(new_cb != NULL);
|
||||
|
||||
new_cb->fde = open_edge_file(gpio);
|
||||
new_cb->gpio = gpio;
|
||||
new_cb->edge = edge;
|
||||
new_cb->func = func;
|
||||
@ -605,6 +620,7 @@ void remove_callbacks(int gpio)
|
||||
{
|
||||
if (cb->gpio == gpio)
|
||||
{
|
||||
close(cb->fde);
|
||||
if (prev == NULL)
|
||||
callbacks = cb->next;
|
||||
else
|
||||
|
@ -61,14 +61,15 @@ int gpio_set_direction(int gpio, unsigned int in_flag);
|
||||
int gpio_get_direction(int gpio, unsigned int *value);
|
||||
int gpio_set_value(int gpio, unsigned int value);
|
||||
int gpio_get_value(int gpio, unsigned int *value);
|
||||
int fd_lookup(int gpio);
|
||||
int open_value_file(int gpio);
|
||||
|
||||
int gpio_set_edge(int gpio, unsigned int edge);
|
||||
int fde_lookup(int gpio);
|
||||
int open_edge_file(int gpio);
|
||||
int gpio_set_edge(int gpio, unsigned int edge);
|
||||
int gpio_get_edge(int gpio);
|
||||
|
||||
int add_edge_detect(int gpio, unsigned int edge);
|
||||
void remove_edge_detect(int gpio);
|
||||
//int add_edge_callback(int gpio, void (*func)(int gpio));
|
||||
int add_edge_callback(int gpio, int edge, void (*func)(int gpio));
|
||||
int event_detected(int gpio);
|
||||
int gpio_event_add(int gpio);
|
||||
|
Reference in New Issue
Block a user