Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Problem when reading the values from the pins in Background Task #529

Open
ghost opened this issue Oct 3, 2017 · 0 comments
Open

Problem when reading the values from the pins in Background Task #529

ghost opened this issue Oct 3, 2017 · 0 comments

Comments

@ghost
Copy link

ghost commented Oct 3, 2017

Hello,
when I use BackgroundApp (IoT) template I get no values from the pins.
I try it exactly as in Foreground App, but there is no event triggered when pressing the button.
Can someone tell me what I do wrong?

Here is my 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();

        // Show an error if there is no GPIO controller
        if (gpio == null)
        {
            Debug.WriteLine( "There is no GPIO controller on this device.");
            return;
        }

        buttonPin = gpio.OpenPin(BUTTON_PIN);
        ledPin = gpio.OpenPin(LED_PIN);

        // Initialize LED to the OFF state by first writing a HIGH value
        // We write HIGH because the LED is wired in a active LOW configuration
        ledPin.Write(GpioPinValue.High);
        ledPin.SetDriveMode(GpioPinDriveMode.Output);

        // Check if input pull-up resistors are supported
        if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
            buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
        else
            buttonPin.SetDriveMode(GpioPinDriveMode.Input);

        // Set a debounce timeout to filter out switch bounce noise from a button press
        buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

        // Register for the ValueChanged event so our buttonPin_ValueChanged 
        // function is called when the button is pressed
        buttonPin.ValueChanged += buttonPin_ValueChanged;

        Debug.WriteLine("GPIO pins initialized correctly.");
    }

    private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
    {  
        // toggle the state of the LED every time the button is pressed
        if (e.Edge == GpioPinEdge.FallingEdge)
        {
            ledPinValue = (ledPinValue == GpioPinValue.Low) ?
                GpioPinValue.High : GpioPinValue.Low;
            ledPin.Write(ledPinValue);
       }

   }

}

}`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants