toggle-gpio.c 858 Bytes
Newer Older
Christophe Blaess's avatar
Christophe Blaess committed
1
// ---------------------------------------------------------------------
2
// toggle-gpio.c
Christophe Blaess's avatar
Christophe Blaess committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//
//   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;
}