underground-software / RPi.GPIO2

GNU General Public License v3.0
22 stars 13 forks source link

The initial arguments defined in setup function does not take effect #49

Open vava24680 opened 4 years ago

vava24680 commented 4 years ago

Hi, I had the problem that the initial argument defined in setup function does not take effect. Below is what I did.

from RPi import GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) # It does not set the initial value to low.

I ran this code under the newest version of this package and the platform is Raspberry Pi 4B+.

No matter I passed low or high, it always set the same initial value. Can somebody help me or point out what's going on?

Thank you

theyoyojo commented 4 years ago

Hi,

Thanks for raising this issue, I'll look into it and see what I can find.

vava24680 commented 4 years ago

I think I may know what's the problem. The value of GPIO.LOW is 2, so when it will be treated as the active high...

theyoyojo commented 4 years ago

I was able to reproduce this issue. Definitely worth fixing.

I extended your code slightly to create this reproducer:

#!/bin/python3

from RPi import GPIO
from RPi import GPIO_DEVEL

pin=22

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW) # It does not set the initial value to low.

input()

GPIO_DEVEL.Reset()

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH) # Same behavior as initial=GPIO.LOW

input()

Would you like to submit a fix yourself? If not, I will patch the code.

vava24680 commented 4 years ago

I think this is a bug from the python binding of the libgpiod. This link is how the GPIO.LOW is defined in the binding. As far as I know how the enum works in the C, the value of gpiod_ACTIVE_LOW will be 2.

I think I can submit a fix for this strange behavior by hardcoding the value of GPIO.LOW as 0 in the core.py. This is a temporary solution, but I think it's ok for the current using. Anyway, I will try to understand whether my guess is correct and see if I can submit a patch to the libgpiod.

theyoyojo commented 3 years ago

@vava24680 hey, are you still working on this?

vava24680 commented 3 years ago

Sorry for the delay, I think I may patch this bug on this weekend.