Page 83
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int res;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
gpiod_line_request_both_edges_events(line4, "event test");
gpiod_line_event_wait(line4, NULL);
printf("Event on line 4");
}
Page 84
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void gpiod_line_release(struct gpiod_line *line);
int main(int argc, char **argv)
{
int res;
struct timespec delay = {0, 10 * 1000 * 1000};
struct timespec time1;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_input(line4, "RMeasure");
nanosleep(&delay, NULL);
gpiod_line_release(line4);
res = gpiod_line_request_output(line4, "RMeasure", 0);
nanosleep(&delay, NULL);
gpiod_line_release(line4);
clock_gettime(CLOCK_REALTIME, &time1);
gpiod_line_request_rising_edge_events(line4, "RMeasuret");
struct gpiod_line_event event;
gpiod_line_event_read(line4, &event);
printf("Event on line 4 %d, %d", event.event_type, (event.ts.tv_nsec - time1.tv_nsec) / 1000);
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 85
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void gpiod_line_release(struct gpiod_line *line);
int main(int argc, char **argv)
{
int res;
struct timespec time1;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_both_edges_events(line4, "Measure");
struct gpiod_line_event event;
while (true)
{
do
{
res = gpiod_line_event_read(line4, &event);
} while (event.event_type != GPIOD_LINE_EVENT_RISING_EDGE);
time1.tv_nsec = event.ts.tv_nsec;
do
{
res = gpiod_line_event_read(line4, &event);
} while (event.event_type != GPIOD_LINE_EVENT_FALLING_EDGE);
printf("Pulse Width %d \r\n",
(event.ts.tv_nsec - time1.tv_nsec) / 1000);
fflush(stdout);
}
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 86
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void gpiod_line_release(struct gpiod_line *line);
int main(int argc, char **argv)
{
int res;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_input(line4, "button");
printf("Press button \n\r");
fflush(stdout);
sleep(20);
if (gpiod_line_get_value(line4) == 1)
{
printf("button pressed");
}
else
{
printf("button not pressed");
}
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 87
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void gpiod_line_release(struct gpiod_line *line);
int main(int argc, char **argv)
{
int res;
struct timespec timeout = {0, 0};
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_both_edges_events(line4, "button");
printf("Press button \n\r");
fflush(stdout);
sleep(20);
struct gpiod_line_event event;
res = gpiod_line_event_wait(line4, &timeout);
if (res > 0)
{
res = gpiod_line_event_read(line4, &event);
printf("button pressed %d", event.ts.tv_nsec);
}
else
{
printf("button not pressed");
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 88
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int gpiod_line_event_poll(struct gpiod_line *line, struct gpiod_line_event *event);
int main(int argc, char **argv)
{
int res;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_both_edges_events(line4, "button");
struct gpiod_line_event event;
while (true)
{
res = gpiod_line_event_poll(line4, &event);
if (res > 0)
{
gpiod_line_event_read(line4, &event);
printf("Event on line 4 %d,%d \n\r ", event.event_type, event.ts);
fflush(stdout);
}
}
}
int gpiod_line_event_poll(struct gpiod_line *line, struct gpiod_line_event *event)
{
struct timespec timeout = {0, 0};
int res = gpiod_line_event_wait(line, &timeout);
if (res > 0)
res = gpiod_line_event_read(line, event);
if (res == 0)
res = 1;
return res;
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 92
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
#include <sys/epoll.h>
int main(int argc, char **argv)
{
int fd, ret;
struct gpioevent_request req;
req.lineoffset = 4;
req.handleflags = GPIOHANDLE_REQUEST_INPUT;
req.eventflags = GPIOEVENT_REQUEST_RISING_EDGE;
strcpy(req.consumer_label, "Event test");
fd = open("/dev/gpiochip0", O_RDONLY);
ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
close(fd);
static struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = req.fd;
int epfd = epoll_create(1);
int res = epoll_ctl(epfd, EPOLL_CTL_ADD, req.fd, &ev);
int nfds = epoll_wait(epfd, &ev, 1, 20000);
if (nfds != 0)
{
struct gpioevent_data edata;
read(req.fd, &edata, sizeof edata);
printf("%u,%llu", edata.id, edata.timestamp);
}
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 93
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
#include <sys/epoll.h>
#include <gpiod.h>
int main(int argc, char **argv)
{
int res;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_rising_edge_events(line4, "button");
int fd = gpiod_line_event_get_fd(line4);
static struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
int epfd = epoll_create(1);
res = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
int nfds = epoll_wait(epfd, &ev, 1, 20000);
if (nfds != 0)
{
struct gpioevent_data edata;
read(fd, &edata, sizeof edata);
printf("%u,%llu", edata.id, edata.timestamp);
}
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 97
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <gpiod.h>
#include <sys/epoll.h>
#include <pthread.h>
typedef void (*eventHandler)(struct gpiod_line_event *);
typedef struct
{
struct gpiod_line *line;
eventHandler func;
} intVec;
void myHandler(struct gpiod_line_event *event)
{
printf("%d,%d \n\r", event->event_type,
event->ts.tv_nsec / 1000);
fflush(stdout);
}
void *waitInterrupt(void *arg)
{
intVec *intData = (intVec *)arg;
struct gpiod_line_event event;
while (true)
{
int res = gpiod_line_event_read(intData->line, &event);
if (res == 0)
intData->func(&event);
}
}
int main(int argc, char **argv)
{
int fd, ret, res;
struct gpiod_chip *chip = gpiod_chip_open_by_number(0);
struct gpiod_line *line4 = gpiod_chip_get_line(chip, 4);
res = gpiod_line_request_rising_edge_events(line4, "button");
intVec intData;
intData.line = line4;
intData.func = &myHandler;
pthread_t intThread;
if (pthread_create(&intThread, NULL, waitInterrupt, (void *)&intData))
{
fprintf(stderr, "Error creating thread\n");
return 1;
}
for (;;)
{
printf("Working\n\r");
fflush(stdout);
sleep(2);
}
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 100
#define _GNU_SOURCE
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int event_cb(int event, unsigned int offset, const struct timespec *timestamp, void *unused)
{
printf("[%ld.%09ld] %s\n", timestamp->tv_sec, timestamp->tv_nsec,
(event == GPIOD_CTXLESS_EVENT_CB_RISING_EDGE)? "rising" :
(event == GPIOD_CTXLESS_EVENT_CB_FALLING_EDGE)? "falling" :
(event == GPIOD_CTXLESS_EVENT_CB_TIMEOUT) ? "timeout" : "??");
fflush(stdout);
return GPIOD_CTXLESS_EVENT_CB_RET_OK;
}
int main(int argc, char **argv) {
int res;
struct timespec time;
time.tv_nsec=0;
time.tv_sec=20;
res = gpiod_ctxless_event_monitor("0",
GPIOD_CTXLESS_EVENT_BOTH_EDGES, 4, 0, "monitor test",
&time, NULL, event_cb, NULL);
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.