rm-hull/OPi.GPIO

cannot detect inputs from within event detection

Fionnoch opened this issue · 1 comments

I have in issue trying to detect any gpio input changes from within an event detection callback.

My goal is to read an ir remote with the GPIO's, when an input is detected (a falling edge) I want to enter a function where i measure the number and duration of pulses of the remote for a fixed time. so far I can detect the initial pulse and enter the callback function however once inside the input seems to be fixed to high (1) and does not change. I know this is not correct as if i just print the inputs in the main loop then I can detect both 0's and 1's.

Any idea or guidance on this would be hugely appreciated!

import OPi.GPIO as GPIO
from time import sleep
from time import perf_counter

LED_gpio = 7 # pin name = PA6 wPi = 2 GPIO = 6 
ir_gpio = 5 # pin name = SCL.0 wPi = 1 GPIO = 11

GPIO.setboard(GPIO.PCPCPLUS)
GPIO.setmode(GPIO.BOARD)  # set up BOARD BCM SUNXI numbering
GPIO.setup(LED_gpio, GPIO.OUT)    # set pin as an output (LED)
GPIO.setup(ir_gpio, GPIO.IN)

def inputChange(channel):
    print("input interupt detected") 
    timeout = perf_counter()+3 #stay in this loop for 3 seconds
    counter_1 = 0
    counter_0 = 0
        
    while perf_counter()<timeout:
        if GPIO.input(ir_gpio)==1: # 1 if high 0 if low tells you input value
            counter_1 = counter_1 +1   
        else: 
            counter_0 = counter_0 +1 
        
    print("callback ended")
    print ("number of 1's detected = ", counter_1)
    print ("number of 0's detected = ", counter_0)
                          
GPIO.add_event_detect(ir_gpio, GPIO.FALLING, callback = inputChange, bouncetime = 3200) #bouncetime stops the callback from being run again for a set time in miliseconds. needs to be length of ir command

try:
    GPIO.output(LED_gpio, 0) 
    print("running program press ctrl+c to stop")
    while 1:
        sleep(4) #comment out when measuring all values
        #print("mainloop pin value = ", GPIO.input(ir_gpio)) #display all values
        
except KeyboardInterrupt:
    print("")
    print("program stopping") 
    GPIO.cleanup()

Apologies this is was meant for the OrangePi.GPIO library. This issue wasn't present on this library, even when running on a OrangePi PC+.