Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RaspberryPI Python WiringPi2 Interrupt Syntax

Testing out the wiringPi2 interrupts on python 2.7 / RaspberryPi and can't seem to get it to work.

With the following code, the interrupt generates a segmentation fault.

#!/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 the "()" 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 ???

like image 608
crankshaft Avatar asked Nov 07 '13 08:11

crankshaft


1 Answers

I am not too good with C, but as far as I understood from sources https://github.com/Gadgetoid/WiringPi2-Python/blob/master/wiringpi_wrap.c you've got this error because of this code (it checks if function returns void and displays 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 to return True or 1 in my_int() function explicitly. Now python returns None for the function that have reached the end of function code but returned no 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 seems that you've initialize wiringpi2 wrongly. Please check tutorial for details: http://raspi.tv/2013/how-to-use-wiringpi2-for-python-on-the-raspberry-pi-in-raspbian

like image 160
nickzam Avatar answered Oct 19 '22 16:10

nickzam