Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 512
DaveR Avatar asked Oct 21 '25 17:10

DaveR


1 Answers

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.

like image 64
Alexandre Belloni Avatar answered Oct 25 '25 14:10

Alexandre Belloni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!