RaspberryPI Python WiringPi2 Interrupt Syntax

Testing wiringPi2 interrupts on python 2.7 / RaspberryPi and doesn't seem to make it work.

In the following code, the interrupt generates a segmentation error.

#!/usr/bin/env python2 import wiringpi2 import time def my_int(): print('Interrupt') wpi = wiringpi2.GPIO(wiringpi2.GPIO.WPI_MODE_PINS) wpi.pullUpDnControl(4,wpi.PUD_UP) wpi.wiringPiISR(4, wpi.INT_EDGE_BOTH, my_int()) while True: time.sleep(1) print('Waiting...') Waiting... Waiting... Waiting... Waiting... Segmentation fault 

If I callback without "()", then I get another error:

 wpi.wiringPiISR(4, wpi.INT_EDGE_BOTH, my_int) > TypeError: in method 'wiringPiISR', argument 3 of type 'void (*)(void)' 

What am I doing wrong?

+6
source share
1 answer

I'm not too good with C, but as far as I understood from the sources https://github.com/Gadgetoid/WiringPi2-Python/blob/master/wiringpi_wrap.c , you got this error because of this code (it checks, returns whether the function is void and displays an error):

 int res = SWIG_ConvertFunctionPtr(obj2, (void**)(&arg3), SWIGTYPE_p_f_void__void); if (!SWIG_IsOK(res)) { SWIG_exception_fail(SWIG_ArgError(res), "in method '" "wiringPiISR" "', argument " "3"" of type '" "void (*)(void)""'"); } 

So, I recommend returning True or 1 in the my_int () function explicitly. Now python returns None for a function that reached the end of the function code but did not return a value.

Modified Code:

 #!/usr/bin/env python2 import wiringpi2 import time def my_int(): print('Interrupt') return True # setup wiringpi2.wiringPiSetupGpio() # set up pin 4 as input wiringpi2.pinMode(4, 0) # enable pull up down for pin 4 wiringpi2.pullUpDnControl(4, 1) # attaching function to interrupt wiringpi2.wiringPiISR(4, wiringpi2.INT_EDGE_BOTH, my_int) while True: time.sleep(1) print('Waiting...') 

EDIT . It looks like you incorrectly initialized wirepi2. See the manual for more details: http://raspi.tv/2013/how-to-use-wiringpi2-for-python-on-the-raspberry-pi-in-raspbian

+3
source

Source: https://habr.com/ru/post/957565/


All Articles