Commit 3a917c01 authored by Christophe BLAESS's avatar Christophe BLAESS

Add some examples about low-level functions usage.

parent 99c71091
......@@ -3,3 +3,9 @@
monitor-gpio
read-gpio
toggle-gpio
invert-gpio
list-gpio-lines
wait-gpio-event
ctxless-toggle-gpio
low-level-toogle-gpio
......@@ -3,10 +3,15 @@ CC = $(CROSS_COMPILE)gcc
CFLAGS = -Wall -g
LDFLAGS = -lgpiod
EXE = \
read-gpio \
toggle-gpio \
monitor-gpio \
EXE = \
read-gpio \
toggle-gpio \
monitor-gpio \
list-gpio-lines \
invert-gpio \
wait-gpio-event \
ctxless-toggle-gpio \
low-level-toogle-gpio \
.PHONY: all
......
// ---------------------------------------------------------------------
// ctxless-toggle-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 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, 0, 0, argv[0], NULL, NULL);
gpiod_ctxless_set_value(argv[1], offset, 1, 0, argv[0], NULL, NULL);
}
return EXIT_SUCCESS;
}
// ---------------------------------------------------------------------
// invert-gpio.c
//
// This is an example of 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 <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct gpiod_chip *input_chip;
struct gpiod_line *input_line;
int input_offset;
struct gpiod_chip *output_chip;
struct gpiod_line *output_line;
int output_offset;
if (argc !=5 ) {
fprintf(stderr, "usage: %s <input-device> <input-offset> <output_device> <output-offset>\n",
argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[2], "%d", &input_offset) != 1) {
fprintf(stderr, "%s: invalid <input-offset> value.\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[4], "%d", &output_offset) != 1) {
fprintf(stderr, "%s: invalid <output-offset> value.\n", argv[0]);
exit(EXIT_FAILURE);
}
input_chip = gpiod_chip_open_lookup(argv[1]);
if (input_chip == NULL) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
output_chip = gpiod_chip_open_lookup(argv[3]);
if (output_chip == NULL) {
perror(argv[3]);
exit(EXIT_FAILURE);
}
input_line = gpiod_chip_get_line(input_chip, input_offset);
if (input_line == NULL) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
output_line = gpiod_chip_get_line(output_chip, output_offset);
if (output_line == NULL) {
perror(argv[4]);
exit(EXIT_FAILURE);
}
if (gpiod_line_request_input(input_line, argv[0]) < 0) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
if (gpiod_line_request_output(output_line, argv[0], 0) < 0) {
perror(argv[4]);
exit(EXIT_FAILURE);
}
for (;;) {
gpiod_line_set_value(output_line, 1 - gpiod_line_get_value(input_line));
usleep(1000);
}
gpiod_line_release(input_line);
gpiod_line_release(output_line);
return EXIT_FAILURE;
}
// ---------------------------------------------------------------------
// list-gpio-lines.c
//
// This is an example of 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 list_gpio_lines(struct gpiod_chip *chip)
{
struct gpiod_line_bulk bulk;
struct gpiod_line *line;
int offset;
const char *string;
fprintf(stdout, "%s - %s - %d lines\n",
gpiod_chip_name(chip),
gpiod_chip_label(chip),
gpiod_chip_num_lines(chip));
if (gpiod_chip_get_all_lines(chip, &bulk) != 0) {
perror("gpiod_chip_get_all_lines()");
return -1;
}
gpiod_line_bulk_foreach_line_off(&bulk, line, offset) {
fprintf(stdout, " %2d: ", offset);
string = gpiod_line_name(line);
if (string == NULL)
fprintf(stdout, "- ");
else
fprintf(stdout, "%s ", string);
string = gpiod_line_consumer(line);
if (string == NULL)
fprintf(stdout, "() ");
else
fprintf(stdout, "(%s) ", string);;
if (gpiod_line_direction(line) == GPIOD_LINE_DIRECTION_OUTPUT)
fprintf(stdout, "out ");
else
fprintf(stdout, "in ");
if (gpiod_line_active_state(line) == GPIOD_LINE_ACTIVE_STATE_LOW)
fprintf(stdout, "active-low ");
if (gpiod_line_is_open_drain(line))
fprintf(stdout, "open-drain ");
if (gpiod_line_is_open_source(line))
fprintf(stdout, "open-source ");
if (gpiod_line_is_used(line))
fprintf(stdout, "* ");
fprintf(stdout, "\n");
}
return 0;
}
int main(int argc, char *argv[])
{
struct gpiod_chip_iter *iter;
struct gpiod_chip *chip;
int i;
if (argc == 1) {
// No argument: iterate on every GPIO chip.
iter = gpiod_chip_iter_new();
if (iter == NULL) {
perror("gpiod_chip_iter");
exit(EXIT_FAILURE);
}
while ((chip = gpiod_chip_iter_next(iter)) != NULL)
list_gpio_lines(chip);
gpiod_chip_iter_free(iter);
} else {
for (i = 1; i < argc; i ++) {
chip = gpiod_chip_open_lookup(argv[i]);
if (chip == NULL)
perror(argv[i]);
else
list_gpio_lines(chip);
}
}
return EXIT_SUCCESS;
}
// ---------------------------------------------------------------------
// low-level-gpio.c
//
// This is an example of 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 <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct gpiod_chip *output_chip;
struct gpiod_line *output_line;
int output_offset;
if (argc != 3) {
fprintf(stderr, "usage: %s <output_device> <output-offset>\n",
argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[2], "%d", &output_offset) != 1) {
fprintf(stderr, "%s: invalid <output-offset> value.\n", argv[0]);
exit(EXIT_FAILURE);
}
output_chip = gpiod_chip_open_lookup(argv[1]);
if (output_chip == NULL) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
output_line = gpiod_chip_get_line(output_chip, output_offset);
if (output_line == NULL) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
if (gpiod_line_request_output(output_line, argv[0], 0) < 0) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
for (;;) {
gpiod_line_set_value(output_line, 0);
gpiod_line_set_value(output_line, 1);
}
gpiod_line_release(output_line);
return EXIT_FAILURE;
}
// ---------------------------------------------------------------------
// wait-gpio-event.c
//
// This is an example of 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 <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct gpiod_chip *input_chip;
struct gpiod_line *input_line;
int input_offset;
struct gpiod_chip *output_chip;
struct gpiod_line *output_line;
int output_offset;
struct gpiod_line_event event;
if (argc !=5 ) {
fprintf(stderr, "usage: %s <input-device> <input-offset> <output_device> <output-offset>\n",
argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[2], "%d", &input_offset) != 1) {
fprintf(stderr, "%s: invalid <input-offset> value.\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[4], "%d", &output_offset) != 1) {
fprintf(stderr, "%s: invalid <output-offset> value.\n", argv[0]);
exit(EXIT_FAILURE);
}
input_chip = gpiod_chip_open_lookup(argv[1]);
if (input_chip == NULL) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
output_chip = gpiod_chip_open_lookup(argv[3]);
if (output_chip == NULL) {
perror(argv[3]);
exit(EXIT_FAILURE);
}
input_line = gpiod_chip_get_line(input_chip, input_offset);
if (input_line == NULL) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
output_line = gpiod_chip_get_line(output_chip, output_offset);
if (output_line == NULL) {
perror(argv[4]);
exit(EXIT_FAILURE);
}
if (gpiod_line_request_both_edges_events(input_line, argv[0]) < 0) {
perror(argv[2]);
exit(EXIT_FAILURE);
}
if (gpiod_line_request_output(output_line, argv[0], 0) < 0) {
perror(argv[4]);
exit(EXIT_FAILURE);
}
gpiod_line_set_value(output_line, 1 - gpiod_line_get_value(input_line));
for (;;) {
gpiod_line_event_wait(input_line, NULL);
if (gpiod_line_event_read(input_line, &event) == 0) {
if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE) {
gpiod_line_set_value(output_line, 0);
} else {
gpiod_line_set_value(output_line, 1);
}
}
}
gpiod_line_release(input_line);
gpiod_line_release(output_line);
return EXIT_FAILURE;
}
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