cra*_*aft 6 python gpio raspberry-pi
测试python 2.7/RaspberryPi上的wiringPi2中断,似乎无法使其工作.
使用以下代码,中断会生成分段错误.
#!/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
Run Code Online (Sandbox Code Playgroud)
如果我回调没有"()"那么我得到另一个错误:
wpi.wiringPiISR(4, wpi.INT_EDGE_BOTH, my_int)
> TypeError: in method 'wiringPiISR', argument 3 of type 'void (*)(void)'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ???
我对 C 不太擅长,但据我从来源https://github.com/Gadgetoid/WiringPi2-Python/blob/master/wiringpi_wrap.c的了解,由于这段代码(它检查if 函数返回 void 并显示错误):
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)""'");
}
Run Code Online (Sandbox Code Playgroud)
因此,我建议在 my_int() 函数中显式返回True或 1。现在,对于已到达函数代码末尾但未返回任何值的函数,Python 将返回 None。
修改后的代码:
#!/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...')
Run Code Online (Sandbox Code Playgroud)
编辑:看来您错误地初始化了wiringpi2。请查看教程了解详细信息:http://raspi.tv/2013/how-to-use-wiringpi2-for-python-on-the-raspberry-pi-in-raspbian
| 归档时间: |
|
| 查看次数: |
3747 次 |
| 最近记录: |