Here is another example that sometimes works, but mostly doesn’t:
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
volatile int RUN = 1;
void inthandler(int _){RUN = 0;}
#define CONSUMER "Consumer"
int main(int argc, char **argv)
{
signal(SIGINT, inthandler);
int chipnumber = 1;
struct timespec ts = { 0, 1000000 }; // 1s Timeout for event
struct gpiod_line_event event;
#define NUMPINS (7)
unsigned int pins[NUMPINS] = {60,61,44,45,46,47,48};
unsigned int val;
struct gpiod_chip *chip;
struct gpiod_line *lines[NUMPINS] = {NULL};
int i, ret;
chip = gpiod_chip_open_by_number(chipnumber);
if (!chip) {
perror("Open chip failed\n");
goto end;
}
printf("Opened Chip %u\n", chipnumber);
usleep(10000);
for(int j = 0; j < NUMPINS; j++){
pins[j] = pins[j] % 32;
lines[j] = gpiod_chip_get_line(chip, pins[j]);
if (!lines[j]) {
printf("Get line %u failed\n", pins[j]);
goto close_chip;
}
printf("Opened Line %u\n", pins[j]);
usleep(10000);
ret = gpiod_line_request_rising_edge_events(lines[j], CONSUMER);
if (ret < 0) {
printf("Request line %u as input failed\n", pins[j]);
goto release_line;
}
printf("Requested Edges for Line %u\n", pins[j]);
usleep(10000);
}
/* Get */
printf("Starting Monitor:\n");
while(RUN) {
//printf("waiting...\n");
for(int j = 0; j < NUMPINS; j++){
ret = gpiod_line_event_wait(lines[j], &ts);
if(ret > 0){
ret = gpiod_line_event_read(lines[j], &event);
printf("ret: %d, event: %d\n", ret, event);
if (!ret){
printf("event: %d timestamp: [%8ld.%09ld]\n",
event.event_type, event.ts.tv_sec, event.ts.tv_nsec);
}
val = gpiod_line_get_value(lines[j]);
printf("%d\n", val);
usleep(1000);
}
}
}
release_line:
printf("Releasing lines\n");
for(int j = 0; j < NUMPINS; j++){
if(lines[j])
gpiod_line_release(lines[j]);
}
close_chip:
printf("Closing chip %u\n", chipnumber);
gpiod_chip_close(chip);
end:
return 0;
}
compile with gcc gpiod_tst.c -o gpiod_tst -lgpiod .
Sometimes it gets to the monitoring loop, but often it gets stuck requesting a line or edge events.