public void Run(IBackgroundTaskInstance taskInstance) { myDeferral = taskInstance.GetDeferral(); var gpioController = Windows.Devices.Gpio.GpioController.GetDefault(); if (gpioController != null) { pinBlink = gpioController.OpenPin(pinLED); pinBlink.SetDriveMode(GpioPinDriveMode.Output); BlinkMe(pinBlink, 1); int pinCount = gpioController.PinCount; System.Diagnostics.Debug.WriteLine("Board has " + pinCount + " pins"); BlinkMe(pinBlink, 2); if (pinPIR <= pinCount) { System.Diagnostics.Debug.WriteLine("Assigning pin " + pinPIR + " for tests"); BlinkMe(pinBlink, 4); pinUnderTest = gpioController.OpenPin(pinPIR); if (pinUnderTest != null) { pinUnderTest.DebounceTimeout = TimeSpan.FromMilliseconds(100); // 100 ms, nominal value pinUnderTest.SetDriveMode(pinMode); // pin data to be read by app pinUnderTest.ValueChanged += pinUnderTest_ValueChanged; // event handler for rising/falling signal System.Diagnostics.Debug.WriteLine("Awaiting state change for pin " + pinPIR + " in mode " + pinMode.ToString()); BlinkMe(pinBlink, 8); } else { System.Diagnostics.Debug.WriteLine("Unable to initialize GPIO pin " + pinPIR); } } else { System.Diagnostics.Debug.WriteLine("Pin number under test " + pinPIR + " beyond upper limit of " + pinCount); } } else { System.Diagnostics.Debug.WriteLine("Unable to access GPIO controller"); } }
private void ThrowExceptionIfDriveModeIsInput() { if (_pinMode == GpioPinDriveMode.Input || _pinMode == GpioPinDriveMode.InputPullUp || _pinMode == GpioPinDriveMode.InputPullDown) { throw new InvalidOperationException($"Can't set state on pin {PinNumber} since it is configured as an input pin ({_pinMode.ToString()})"); } }