gpt4 book ai didi

c# - 带有 Xamarin 的 Android Things IGpioCallback 问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:39 25 4
gpt4 key购买 nike

我刚开始使用带有 Xamarin 的 Android Things,我已经成功打开了 LED,但我无法检测到按钮输入。我认为问题出在下面代码中的 "RegisterGpioCallback",但我不确定,也不知道如何解决。有人可以帮帮我吗??这是我正在使用的代码:

public class BlinkActivity : Activity
{
private IGpio gpio;
private IGpio button;
private IGpioCallback mButtonCallback;
protected override void OnCreate(Bundle savedInstanceState)
{
this.mButtonCallback = mButtonCallback;
PeripheralManager peripheralManager = PeripheralManager.Instance;
gpio = peripheralManager.OpenGpio("BCM17");
gpio.SetDirection(Gpio.DirectionOutInitiallyLow);
gpio.Value = false;
button = peripheralManager.OpenGpio("BCM4");
button.SetDirection(Gpio.DirectionIn);
button.SetEdgeTriggerType(Gpio.EdgeNone);
button.RegisterGpioCallback(new Handler(), mButtonCallback);
base.OnCreate(savedInstanceState);
Task.Run(() =>
{
if (mButtonCallback.OnGpioEdge(button) == true)
{
gpio.Value = !gpio.Value;
}
});
}
}

最佳答案

您需要实际实现 IGpioCallback 接口(interface),以便 com.google.android.things.pio 库可以在您的应用程序中进行“回调” GPIO 的变化。

RegisterGpioCallback 分配给已实现该接口(interface)的实际对象实例,在以下示例中,它将位于 Activity 上。

public class BlinkActivity : Activity, IGpioCallback
{
~~~~
button.RegisterGpioCallback(new Handler(), this);
~~~~

// remove the Task.Run block

public OnGpioEdge(Gpio gpio)
{
Log.Debug("SO", gpio.Value.ToString());
}

~~~~
}

关于c# - 带有 Xamarin 的 Android Things IGpioCallback 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50827387/

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