Commit d3047efd authored by Christophe Blaess's avatar Christophe Blaess

First release.

parents
*~
*.o
monitor-gpio
read-gpio
toggle-gpio
CROSS_COMPILE ?=
CC = $(CROSS_COMPILE)gcc
CFLAGS = -Wall -g
LDFLAGS = -lgpiod
EXE = \
read-gpio \
toggle-gpio \
monitor-gpio \
.PHONY: all
all:$(EXE)
.PHONY: clean
clean:
rm -f $(EXE) *.o
# Example Programs using libGPIOd
Christophe BLAESS 2018
This project is a set of examples programs to access the GPIO lines using the new Linux API with the libGPIOd library.
The explaining (french) article will be released on October 22, 2018 at <https://www.blaess.fr/christophe/>.
// ---------------------------------------------------------------------
// monitor-gpio.c
//
// This is an example of GPIO monitoring with the libGPIOd library.
//
// Copyright 2018 Christophe BLAESS (christophe@blaess.fr)
// License GPL v2.
// ---------------------------------------------------------------------
#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>
int event_cb(int event, unsigned int offset, const struct timespec *timestamp, void *unused)
{
fprintf(stderr, "[%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" : "??");
return GPIOD_CTXLESS_EVENT_CB_RET_OK;
}
int main(int argc, char *argv[])
{
int offset;
if (argc != 3) {
fprintf(stderr, "usage: %s <controler> <offset>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[2], "%d", &offset) != 1) {
fprintf(stderr, "invalid offset: %s\n", argv[2]);
exit(EXIT_FAILURE);
}
gpiod_ctxless_event_monitor(argv[1], GPIOD_CTXLESS_EVENT_BOTH_EDGES,
offset, 0, argv[0], NULL, NULL, event_cb, NULL);
return EXIT_SUCCESS;
}
// ---------------------------------------------------------------------
// monitor-gpio.c
//
// This is an example of simple GPIO access with the libGPIOd library.
//
// Copyright 2018 Christophe BLAESS (christophe@blaess.fr)
// License GPL v2.
// ---------------------------------------------------------------------
#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int value;
int offset;
if (argc != 3) {
fprintf(stderr, "usage: %s <controler> <offset>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[2], "%d", &offset) != 1) {
fprintf(stderr, "invalid offset: %s\n", argv[2]);
exit(EXIT_FAILURE);
}
value = gpiod_ctxless_get_value(argv[1], offset, 0, argv[0]);
if (value < 0) {
perror("gpiod_ctxless_get_value");
exit(EXIT_FAILURE);
}
fprintf(stdout, "%d\n", value);
return EXIT_SUCCESS;
}
// ---------------------------------------------------------------------
// monitor-gpio.c
//
// This is an example of simple GPIO access with the libGPIOd library.
//
// Copyright 2018 Christophe BLAESS (christophe@blaess.fr)
// License GPL v2.
// ---------------------------------------------------------------------
#include <gpiod.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int value = 1;
int offset;
if (argc != 3) {
fprintf(stderr, "usage: %s <controler> <offset>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[2], "%d", &offset) != 1) {
fprintf(stderr, "invalid offset: %s\n", argv[2]);
exit(EXIT_FAILURE);
}
for (;;) {
gpiod_ctxless_set_value(argv[1], offset, value, 0, argv[0], (void (*)(void *))usleep, (void *)500000);
value = 1 - value;
}
return EXIT_SUCCESS;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment