DaveR
DaveR

Reputation: 1435

Are ctxless functions worse or better to use in libgpiod

Is there anyone out there familiar with libgpiod who could answer this question:

Are there any draw backs to using ctxless function rather than calling individual function to manipulate chip lines (or in general)? For example, to turn on a line, I might write something like:

struct gpiod_chip* chip = gpiod_chip_open_by_name("gpiochip2");
struct gpiod_line* line = gpiod_chip_get_line(chip, 10);
gpiod_line_request_output(line, "foo", 0);
gpiod_chip_close(chip);

or I could simply use a single call:

gpiod_ctxless_set_value("gpiochip2", 10, 1, false, "foo", NULL, NULL);

When would you use one over the other?

Upvotes: 4

Views: 1578

Answers (1)

Alexandre Belloni
Alexandre Belloni

Reputation: 2304

The ctxless function are great to quickly set or get the value of a GPIO. However, I would keep that for one time use over the life cycle of your program.

The reason is that using the ctxless functions, libgpiod will have to always redo the same setup (opening the gpiochip, requesting the line, setting its direction) and then get or set the value.

If you are reading or setting the value multiple times in your program, you should probably not use the ctxless functions.

Moreover, keeping the line requested for the life of your program is definitively a good thing at this will prevent any other program to use that GPIO.

Upvotes: 3

Related Questions