Deutsch   English   Français   Italiano  
<lha5o3Ff6g3U1@mid.individual.net>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feeds.phibee-telecom.net!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: =?UTF-8?Q?Josef_M=C3=B6llers?= <josef@invalid.invalid>
Newsgroups: comp.sys.raspberry-pi
Subject: how to write and read back GPIO pin states with lgpio
Date: Sun, 4 Aug 2024 22:13:55 +0200
Lines: 51
Message-ID: <lha5o3Ff6g3U1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net 7lxu0EKvY1Xmpvs7Cy6x+w525/yMR1zLPziGjJb5xjsMoPUqsD
Cancel-Lock: sha1:Ols7CzKPbmqLTltXME47MnnJzP0= sha256:I4ik89nGDwHfcPcl5v4L8UguWaFk8rYfkDb5NBDcbo4=
User-Agent: Mozilla Thunderbird
Content-Language: en-US
Bytes: 2615

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