gpt4 book ai didi

raspberry-pi - 为什么按下一个按钮会同时触发处理程序

转载 作者:行者123 更新时间:2023-12-04 04:07:54 27 4
gpt4 key购买 nike

我在 Raspberry Pi 上运行 Android Things 0.4。我一直在关注本教程:

https://developer.android.com/things/training/first-device/peripherals.html

在第一个按钮开始工作后,我决定在继续本教程的引导部分之前添加第二个按钮。我知道第一个按钮的硬件设置是正确的,所以我复制了第二个按钮,但出于某种原因,我无法理解按钮的行为不符合预期。第一个按钮触发两个按钮的事件监听器。第二个按钮将触发一个方向,直到按下第二个按钮后按下第一个按钮才会再次触发。

我是一名经验丰富的 Android 开发人员,但对物联网和事物非常陌生。这是我的代码:

public class MainActivity extends Activity {
private static final String TAG = "ButtonActivity";
private static final String INC_BUTTON_PIN_NAME = "BCM4"; // GPIO port wired to the button
private static final String DEC_BUTTON_PIN_NAME = "BCM17"; // GPIO port wired to the button

private Gpio mIncButtonGpio;
private Gpio mDecButtonGpio;

Handler mHandler = new Handler(Looper.getMainLooper());

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManagerService service = new PeripheralManagerService();
try {
// Step 1. Create GPIO connection.
mIncButtonGpio = service.openGpio(INC_BUTTON_PIN_NAME);
mDecButtonGpio = service.openGpio(DEC_BUTTON_PIN_NAME);
// Step 2. Configure as an input.
mIncButtonGpio.setDirection(Gpio.DIRECTION_IN);
mDecButtonGpio.setDirection(Gpio.DIRECTION_IN);
// Step 3. Enable edge trigger events.
mIncButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
mDecButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING);
// Step 4. Register an event callback.
mIncButtonGpio.registerGpioCallback(mIncCallback);
mDecButtonGpio.registerGpioCallback(mDecCallback);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}

// Step 4. Register an event callback.
private GpioCallback mIncCallback = new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
Log.i(TAG, "GPIO changed, INC button pressed");

// Step 5. Return true to keep callback active.
return true;
}
};

private GpioCallback mDecCallback = new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
Log.i(TAG, "GPIO changed, DEC button pressed");

// Step 5. Return true to keep callback active.
return true;
}
};

@Override
protected void onDestroy() {
super.onDestroy();

// Step 6. Close the resource
if (mIncButtonGpio != null) {
mIncButtonGpio.unregisterGpioCallback(mIncCallback);
try {
mIncButtonGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (mDecButtonGpio != null) {
mDecButtonGpio.unregisterGpioCallback(mDecCallback);
try {
mDecButtonGpio.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
}
}

Hardware Config
这是我按第一个按钮1次后的logcat:
06-09 14:33:21.717 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:33:21.718 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed

这是在按下第一个按钮之后的第二个按钮之后:
06-09 14:33:21.717 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:33:21.718 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:33:58.047 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed

如果我按下第一个按钮,然后按下第二个按钮 4 次,然后再次按下第一个按钮,效果如下:
06-09 14:39:06.804 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:39:06.804 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:39:08.846 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:39:11.377 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:39:11.377 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed
06-09 14:39:11.510 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, INC button pressed
06-09 14:39:11.510 1393-1393/com.maddesa.iottest I/ButtonActivity: GPIO changed, DEC button pressed

就像我说的,我对 IoT 和事物很陌生,但我只想有两个单独的按钮,可以始终触发单独的处理程序。

谢谢。

最佳答案

您需要在接地的电线上放置一些二极管,以防止信号向后传播。当您按下其中一个按钮时,接地会使另一个按钮短路。

See this diagram

当您按下顶部按钮时,电流将通过接地线(红线)向下到达向下按钮的白线。从白色开始,它穿过你的电阻,然后穿过你的橙色线回到你的 gpio 7。

关于raspberry-pi - 为什么按下一个按钮会同时触发处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44460700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com