我正在尝试从simplepio实现按钮示例。我已经按照原理图所示进行了连接。按下按钮后,我没有收到 GPIO 回调。
我使用的代码与示例代码相同。没有例外,只有“开始活动”在日志中打印
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting ButtonActivity");
PeripheralManagerService service = new PeripheralManagerService();
try {
String pinName = BoardDefaults.getGPIOForButton();
mButtonGpio = service.openGpio(pinName);
mButtonGpio.setDirection(Gpio.DIRECTION_IN);
mButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
mButtonGpio.registerGpioCallback(new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
Log.i(TAG, "GPIO changed, button pressed");
// Return true to continue listening to events
return true;
}
});
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
到目前为止我已经尝试过:
通过使用以下代码运行
python
按钮程序来验证电路和按钮是否正常工作raspbian jessie
#!/usr/bin/env python import os from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP) while True: if (GPIO.input(21) == False): print("Button Clicked") sleep(0.1)
上面的代码在按下按钮时打印“Button Clicked”。所以我确信我的 PI 上的按钮和 GPIO 引脚不是问题。
- 为了确保日志记录没有问题,我还尝试修改原始程序以包含 a
TextView
和一个计数器,以便当单击按钮时计数器值增加并显示在TextView
但再次未收到回调TextView
且未更新. - 尝试了不同的边沿触发类型,但从未调用 onGpioEdge。
以下是我的设置图片