public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
* Help regarding IO configuration of multiple lines
@ 2026-03-06 11:09 joussemetmathis
  2026-03-07  2:13 ` Kent Gibson
  0 siblings, 1 reply; 4+ messages in thread
From: joussemetmathis @ 2026-03-06 11:09 UTC (permalink / raw)
  To: linux-gpio


[-- Attachment #1.1: Type: text/plain, Size: 792 bytes --]

Hello,

I am trying to learn how to use libgpiod on a Pi 5. I created for that
goal a simple circuit with a push button and 2 LEDs, one of which turns
on on a button press.
Since I want to keep my program as modular as possible, I created a
pinconfig function that configures the pins according to the parameters
passed in main.
However, when trying to pass an enum array of gpiod_line_direction to
the function, the code no longer works and the LEDs no longer turn on.
I suspect it is due to the way the array is handled, but since this use
case isn't shown in the examples and that online ressources seems to
only talk about v1 of gpiod, I'm a bit stuck.
Could someone help me? You'll find my code attached in a zip in this
email.

Thanks in advance 

Mathis Joussemet 

[-- Attachment #1.2: Type: text/html, Size: 1126 bytes --]

[-- Attachment #2: pinconfig.zip --]
[-- Type: application/zip, Size: 7218 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help regarding IO configuration of multiple lines
  2026-03-06 11:09 Help regarding IO configuration of multiple lines joussemetmathis
@ 2026-03-07  2:13 ` Kent Gibson
  2026-03-31 13:23   ` Mathis Joussemet
  0 siblings, 1 reply; 4+ messages in thread
From: Kent Gibson @ 2026-03-07  2:13 UTC (permalink / raw)
  To: joussemetmathis; +Cc: linux-gpio

On Fri, Mar 06, 2026 at 12:09:50PM +0100, joussemetmathis@gmail.com wrote:
> Hello,
> 
> I am trying to learn how to use libgpiod on a Pi 5. I created for that
> goal a simple circuit with a push button and 2 LEDs, one of which turns
> on on a button press.
> Since I want to keep my program as modular as possible, I created a
> pinconfig function that configures the pins according to the parameters
> passed in main.
> However, when trying to pass an enum array of gpiod_line_direction to
> the function, the code no longer works and the LEDs no longer turn on.
> I suspect it is due to the way the array is handled, but since this use
> case isn't shown in the examples and that online ressources seems to
> only talk about v1 of gpiod, I'm a bit stuck.
> Could someone help me? You'll find my code attached in a zip in this
> email.
> 

Yeah, the example code doesn't provide a good example of requesting
lines with different settings.
The closest is get_multiple_line_values.c.  Though that applies settings for
each line individually when it could just use a single
gpiod_line_config_add_line_settings() call, as all the lines have the
same settings.  Not sure why.  ¯\_(ツ)_/¯

The relevant section of your code, which seems to follow that example, is:

	gpiod_line_settings_set_direction(line_settings, *direction);
    // ...
	for (int i = 0; i < line_nbr; i++) {
		gpiod_line_config_add_line_settings(line_config, &pin_list[i], 1, line_settings);	
	}


That applies the first entry in your direction array as the direction for all
lines. Definitely not what you want.

See if this works for you:

	for (int i = 0; i < line_nbr; i++) {
	    gpiod_line_settings_set_direction(line_settings, direction[i]);
		gpiod_line_config_add_line_settings(line_config, &pin_list[i], 1, line_settings);	
	}

That applies the direction for each line individually, with other line
settings being the same.

Cheers,
Kent.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help regarding IO configuration of multiple lines
  2026-03-07  2:13 ` Kent Gibson
@ 2026-03-31 13:23   ` Mathis Joussemet
  2026-03-31 14:33     ` Kent Gibson
  0 siblings, 1 reply; 4+ messages in thread
From: Mathis Joussemet @ 2026-03-31 13:23 UTC (permalink / raw)
  To: Kent Gibson; +Cc: linux-gpio


[-- Attachment #1.1: Type: text/plain, Size: 3123 bytes --]

Hi Kent,

Sorry for the late reply, was up until now in exam hell.
It indeed works! I can now drive 2 LEDs at the same time!

I just have 2 new questions:
- I am now using a button to toggle one of the two LEDs by checking the
buttons value using
gpiod_line_request_get_value(request, offset[2])!=0)
in a if statement. I've setup the correct input bias, but when I press on
it, the LED doesn't toggle. Checking with a scope still shows the voltage
jumping to 3.3v when pressed and 0v at other times. Is there something I'm
missing?
- I'm trying to switch from an snapshot based line change reporting to an
event one. however when using a watch line event before/after the request,
the program still blocks. How should i code it to prevent blocking while
still having infos like output values types when there is a change?

Thanks for the help and sorry again for the late reply
Mathis

Le sam. 7 mars 2026 à 03:13, Kent Gibson <warthog618@gmail.com> a écrit :

> On Fri, Mar 06, 2026 at 12:09:50PM +0100, joussemetmathis@gmail.com wrote:
> > Hello,
> >
> > I am trying to learn how to use libgpiod on a Pi 5. I created for that
> > goal a simple circuit with a push button and 2 LEDs, one of which turns
> > on on a button press.
> > Since I want to keep my program as modular as possible, I created a
> > pinconfig function that configures the pins according to the parameters
> > passed in main.
> > However, when trying to pass an enum array of gpiod_line_direction to
> > the function, the code no longer works and the LEDs no longer turn on.
> > I suspect it is due to the way the array is handled, but since this use
> > case isn't shown in the examples and that online ressources seems to
> > only talk about v1 of gpiod, I'm a bit stuck.
> > Could someone help me? You'll find my code attached in a zip in this
> > email.
> >
>
> Yeah, the example code doesn't provide a good example of requesting
> lines with different settings.
> The closest is get_multiple_line_values.c.  Though that applies settings
> for
> each line individually when it could just use a single
> gpiod_line_config_add_line_settings() call, as all the lines have the
> same settings.  Not sure why.  ¯\_(ツ)_/¯
>
> The relevant section of your code, which seems to follow that example, is:
>
>         gpiod_line_settings_set_direction(line_settings, *direction);
>     // ...
>         for (int i = 0; i < line_nbr; i++) {
>                 gpiod_line_config_add_line_settings(line_config,
> &pin_list[i], 1, line_settings);
>         }
>
>
> That applies the first entry in your direction array as the direction for
> all
> lines. Definitely not what you want.
>
> See if this works for you:
>
>         for (int i = 0; i < line_nbr; i++) {
>             gpiod_line_settings_set_direction(line_settings, direction[i]);
>                 gpiod_line_config_add_line_settings(line_config,
> &pin_list[i], 1, line_settings);
>         }
>
> That applies the direction for each line individually, with other line
> settings being the same.
>
> Cheers,
> Kent.
>

[-- Attachment #1.2: Type: text/html, Size: 4406 bytes --]

[-- Attachment #2: pinconfig.zip --]
[-- Type: application/zip, Size: 18538 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Help regarding IO configuration of multiple lines
  2026-03-31 13:23   ` Mathis Joussemet
@ 2026-03-31 14:33     ` Kent Gibson
  0 siblings, 0 replies; 4+ messages in thread
From: Kent Gibson @ 2026-03-31 14:33 UTC (permalink / raw)
  To: Mathis Joussemet; +Cc: linux-gpio

On Tue, Mar 31, 2026 at 03:23:16PM +0200, Mathis Joussemet wrote:
> Hi Kent,
> 
> Sorry for the late reply, was up until now in exam hell.
> It indeed works! I can now drive 2 LEDs at the same time!
> 
> I just have 2 new questions:
> - I am now using a button to toggle one of the two LEDs by checking the
> buttons value using
> gpiod_line_request_get_value(request, offset[2])!=0)
> in a if statement.

I don't see any obvious reason that wouldn't read the button, assuming it is
the third line in your offset array, and the condition is tested at the time
you are pushing the button.

Have you tried the equivalent with the gpiod tools (gpioget) or some of the
example code (e.g. get_line_value) to confirm your hardware setup is working
as expected?

> I've setup the correct input bias, but when I press on
> it, the LED doesn't toggle. Checking with a scope still shows the voltage
> jumping to 3.3v when pressed and 0v at other times. Is there something I'm
> missing?

That being the input voltage?  For an input pulled down when the button is
not pressed and pulled high when it is pressed?
That is what I would expect to see.

> - I'm trying to switch from an snapshot based line change reporting to an
> event one. however when using a watch line event before/after the request,
> the program still blocks. How should i code it to prevent blocking while
> still having infos like output values types when there is a change?
> 

You can use edge events in a polling scenario by testing if the request
fd is ready to read using poll() or select() or similar.
It will become ready when an event is available.
You can then read the event as usual and the read will not block.
So similar to the async_watch_line_value example, but with a 0 timeout in
the poll() and checking the ret:

  ret = poll(&pollfd, 1, 0);
  if (ret > 0) {
    // read event...
  }

and doing whatever else you want in the polling loop.

Cheers,
Kent.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-03-31 14:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-06 11:09 Help regarding IO configuration of multiple lines joussemetmathis
2026-03-07  2:13 ` Kent Gibson
2026-03-31 13:23   ` Mathis Joussemet
2026-03-31 14:33     ` Kent Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox