Wenn ich BackgroundApp (IoT) Vorlage verwende bekomme ich keine Werte von den Pins. Ich versuche es genau wie in Foreground App, aber beim Drücken der Taste wird kein Ereignis ausgelöst. Kann mir jemand sagen, was ich falsch mache?Wie lese ich Value von gpio im Hintergrund Task? C# Win IOT RP3
Hier ist mein Code
namespace BackgroundApplication2
{
public sealed class StartupTask : IBackgroundTask
{
private const int LED_PIN = 6;
private const int BUTTON_PIN = 5;
private GpioPin ledPin;
private GpioPin buttonPin;
private GpioPinValue ledPinValue = GpioPinValue.High;
public void Run(IBackgroundTaskInstance taskInstance)
{
InitGPIO();
}
private void InitGPIO()
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
Debug.WriteLine("There is no GPIO controller on this device.");
return;
}
buttonPin = gpio.OpenPin(BUTTON_PIN);
ledPin = gpio.OpenPin(LED_PIN);
ledPin.Write(GpioPinValue.High);
ledPin.SetDriveMode(GpioPinDriveMode.Output);
if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
else
buttonPin.SetDriveMode(GpioPinDriveMode.Input);
buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);
buttonPin.ValueChanged += buttonPin_ValueChanged;
Debug.WriteLine("GPIO pins initialized correctly.");
}
private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
ledPinValue = (ledPinValue == GpioPinValue.Low) ?
GpioPinValue.High : GpioPinValue.Low;
ledPin.Write(ledPinValue);
}
}
}
}
Dieser Code funktioniert gut in einer UWP-App