[英]Raspberry 3 push button in c#
I'm trying out C# for Raspberry through Visual Studio and windows 10 IoT. 我正在通过Visual Studio和Windows 10 IoT试用Raspberry的C#。 Just took me an eternity to make everything work, but I slowly got there. 只是花了我一个永恒的时间才能使一切正常,但是我慢慢地到达了那里。 I got my leds workings following their outdated 2 years old tutorials. 按照他们过时的2年制教程,我了解了led的工作原理。 I'm now trying to add a button to my breadboard to turn the leds on and off... Sadly, This doesn't work at all. 我现在正尝试在面包板上添加一个按钮来打开和关闭LED……可惜的是,这根本不起作用。 It's like ValueChanged is never triggered. 就像ValueChanged永远不会被触发。
I followed this guide: https://developer.microsoft.com/en-us/windows/iot/samples/pushbutton 我遵循了该指南: https : //developer.microsoft.com/en-us/windows/iot/samples/pushbutton
My button is wired like: 1 pin directly to ground and the other one to GPIO18 (Pin 12) 我的按钮的接线方式是:1个引脚直接接地,另一个引脚连接GPIO18(引脚12)
Before thequestion comes, yes I did try using GPIO5 too. 在问题出现之前,是的,我也尝试过使用GPIO5。 I just went back to GPIO18 as it was working on my python script before. 我刚刚回到GPIO18,因为它之前在我的python脚本上工作。
This is the code I'm trying to run, but having button problems (leds are fine): 这是我要运行的代码,但是有按钮问题(指示灯很好):
public sealed class StartupTask : IBackgroundTask
{
private GpioController gpio = GpioController.GetDefault();
private GpioPin pinRed;
private GpioPin pinBlue;
private GpioPin pinButton;
private GpioPinValue pinValue;
private const int BLUE_PIN = 19;
private const int RED_PIN = 26;
private const int BUTTON_PIN = 18;
public void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine("initialising");
InitGpio();
if (pinRed != null)
{
pinValue = GpioPinValue.High;
pinRed.Write(pinValue);
pinBlue.Write(pinValue);
}
}
private void InitGpio()
{
gpio = GpioController.GetDefault();
if (gpio == null)
{
pinRed = null;
pinBlue = null;
pinButton = null;
Debug.WriteLine("Failed starting GPIO");
return;
}
pinValue = GpioPinValue.Low;
pinRed = gpio.OpenPin(RED_PIN);
pinRed.Write(pinValue);
pinRed.SetDriveMode(GpioPinDriveMode.Output);
pinBlue = gpio.OpenPin(BLUE_PIN);
pinBlue.Write(pinValue);
pinBlue.SetDriveMode(GpioPinDriveMode.Output);
pinButton = gpio.OpenPin(BUTTON_PIN);
if (pinButton.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
{
Debug.WriteLine("Is supported");
pinButton.SetDriveMode(GpioPinDriveMode.InputPullUp);
}
else
{
Debug.WriteLine("Not supported");
pinButton.SetDriveMode(GpioPinDriveMode.Input);
}
pinButton.DebounceTimeout = TimeSpan.FromMilliseconds(50);
pinButton.ValueChanged += buttonValueChange;
Debug.WriteLine("GPIO initialised");
}
private void buttonValueChange(GpioPin sender, GpioPinValueChangedEventArgs e)
{
Debug.WriteLine("here");
if (e.Edge == GpioPinEdge.FallingEdge)
{
Debug.WriteLine("Button push");
pinValue = (pinValue == GpioPinValue.Low) ? GpioPinValue.High : GpioPinValue.Low;
pinRed.Write(pinValue);
pinBlue.Write(pinValue);
}
else
{
Debug.WriteLine("Button release");
}
}
}
Yes, lots of debug lines, as I said, trying out C# on Raspberry (InputPullUp is supported). 是的,正如我所说,许多调试行都在Raspberry上试用C#(支持InputPullUp)。 The "here" in buttonValueChange is never triggered. buttonValueChange中的“这里”从不触发。 I did the exact same wiring setup on Python and it worked flawlessly on the same pins. 我在Python上进行了完全相同的接线设置,并且在相同的引脚上可以完美地工作。
It is suggested here ValueChanged not firing with C# Win10 Iot that "When the Run method ends, unless a deferral object is created, the Background Application ends." 建议此处ValueChanged不使用C#Win10 Iot触发,即“当Run方法结束时,除非创建了延迟对象,否则后台应用程序结束。”
As such you could try to add this line to your Run
code: 因此,您可以尝试将此行添加到Run
代码中:
var deferral = taskInstance.GetDeferral();
And see if it helps. 看看是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.