how to write and read back GPIO pin states with lgpio

Liste des GroupesRevenir à cs raspberry-pi 
Sujet : how to write and read back GPIO pin states with lgpio
De : josef (at) *nospam* invalid.invalid (Josef Möllers)
Groupes : comp.sys.raspberry-pi
Date : 04. Aug 2024, 21:13:55
Autres entêtes
Message-ID : <lha5o3Ff6g3U1@mid.individual.net>
User-Agent : Mozilla Thunderbird
Hi,
I used to use the sysfs interface to the GPIO pins (/sys/class/gpio) but I understand that is deprecated nowadays. So I tried to switch to lgpio which looks OK. However, I have problems writing and reading back pin states from different programs.
My setup is as follows:
I have a couple of relays (solid state and mechanical ones) that control various external devices.
I use one program to switch devices on and off and want to use another program to read back the state of the device.
Doing that with sysfs is easy:
1) export the pin:
       echo $pin > /sys/class/gpio/export
       echo $direction > /sys/class/gpio/gpio$pin/direction
    this needs to be done only once.
2) write the state of the pin, thus switching the device on/off:
       echo $newstate > /sys/class/gpio/gpio$pin/value
    this is done every time this is required
3) read back the state of the pin
       value=$(</sys/class/gpio/gpio$pin/value
    this is done every time I want to check the state of the device
Now I switch a device on/off with lgpio as follows:
1) open the GPIO chip:
       h = lgGpiochipOpen(0);
2) claim the pin as an output:
       lgGpioClaimOutput(h, LG_SET_PULL_NONE, pin, value);
    which, to my understanding, already changes the pin's state?!?
3) write the new state
       lgGpioWrite(h, pin, value);
4) close the chip
       lgGpiochipClose(h);
Reading back the state of the pin requires me to
1) open the GPIO chip:
       h = lgGpiochipOpen(0);
2) claim the pin as an input:
       lgGpioClaimInput(h, LG_SET_PULL_NONE, pin);
3) read back the pin's state
       lgGpioRead(h, pin);
4) close the chip
       lgGpiochipClose(h);
However ... When I set the pin's state to "1", I still read back "0"!
What am I doing wrong? Thanks in advance for any pointers.
Josef

Date Sujet#  Auteur
4 Aug 24 * how to write and read back GPIO pin states with lgpio4Josef Möllers
5 Aug 24 +* Re: how to write and read back GPIO pin states with lgpio2Computer Nerd Kev
8 Aug 24 i`- Re: how to write and read back GPIO pin states with lgpio1Josef Möllers
5 Aug 24 `- Re: how to write and read back GPIO pin states with lgpio1The Natural Philosopher

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal