我想让我的RainbowHAT的红色发光二极管发光。这要求我向BCM 6引脚发送一个高信号。
(短程序)源
类常量:
private static readonly int GPIO_NUMBER_RED = 22;在构造函数中调用:
var gpioController = GpioController.GetDefault();
redPin = gpioController.OpenPin(GPIO_NUMBER_RED);
redPin.Write(GpioPinValue.High);
redPin.SetDriveMode(GpioPinDriveMode.Output);我尝试了类全局常量GPIO_NUMBER_RED的值6 (BCM)、31 (针号)和22 (配线引脚号),但我总是得到以下错误:
WinRT information: Pin ' is not available. It is reserved by the system or in use for another function.插针布局
全源
包信息
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="lowLevel"/>
</Capabilities>我还重新启动Pi,以“重置”可能是其他访问这个Pin的应用程序--但没有成功。
发布于 2019-05-29 18:51:57
解决方案#2
在@michaelxu指出,我错过了使用Singelton模式之后,我删除了它,瞧,现在我可以从构造器中调用Init了。我得研究原因。
发布于 2019-05-26 19:09:44
溶液
如果不调用构造函数中的gpioController,而是在Init之后调用的另一个方法中,则一切都正常。
现在的问题是,为什么不可能在init中这样做呢?
发布于 2019-05-29 09:27:24
我已经下载了回购和查看代码。我不知道为什么需要在MainPageViewModel中的构造函数中添加Init方法。但是不建议在模型的构造函数方法中添加单例实例,因为模型用于绑定视图以显示数据。当您重用GPIO引脚资源时,您应该在另一个页面中释放并释放它。此外,当您不需要使用时,应该手动处理GpioPin。您可以尝试在RainbowHAT的构造函数中移动Init方法,如下所示。
RainbowHAT.cs(Update)
/// <summary>
/// Private singelton instance.
/// </summary>
private static RainbowHAT instance;
/// <summary>
/// Default instance of RainbowHAT.
/// </summary>
public static RainbowHAT Default
{
get
{
if (instance == null)
{
instance = new RainbowHAT();
}
return instance;
}
}
private RainbowHAT()
{
gpioController = GpioController.GetDefault();
// Ensure that we have a valid gpio connection
if (gpioController == null)
{
throw new OperationCanceledException("Operation canceled due missing GPIO controller");
}
Init();
}
public void PerformAction(RainbowHATAction action)
{
switch(action)
{
case RainbowHATAction.TurnOnRed:
redPin.Write(GpioPinValue.High);
break;
case RainbowHATAction.TurnOffRed:
redPin.Write(GpioPinValue.Low);
break;
case RainbowHATAction.LEDsOn:
apa102.TurnOn();
break;
case RainbowHATAction.LEDsOff:
apa102.TurnOff();
break;
default:
break;
}
}MainPageViewModel.cs
public MainPageViewModel()
{
// Setup timer.
ThreadPoolTimer.CreatePeriodicTimer
(ClockTimer_Tick,
TimeSpan.FromSeconds(1)
);
// Setup callback
rainbowHAT.CaptiveButtonPressed += CaptiveButtonPressed;
}https://stackoverflow.com/questions/56315973
复制相似问题