Best practice to put GPIO logic in UWP
Asked Answered
C

1

6

If I create a app in UWP for example with Template 10. I use C#

I want to have the GpioController logic in a class or somewhere that will handle all input and output business like set all pins and events.

Like an example, when a button is pressed it will send a POST request to the server, this must work in any view.

If I go to a view and I want to get the status of a pin to show like "The door is open"

Also if for an example a sensor is triggering a pin to HIGH, if i change view it cant trigger buttonPin_ValueChanged event and or set it to LOW for any reason unless the sensor is LOW.

Even if the pins Power-on Pull is PullDown.

pin = gpio.OpenPin(12);
pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
pin.Write(GpioPinValue.Low);
pin.ValueChanged += buttonPin_ValueChanged;
Cinda answered 11/9, 2016 at 23:0 Comment(5)
I don't think you can just write such a simple class to abstract the use of the GPIO pins. The electronics is more complex than that with pins all able to do different things depending on what you connect. Would be interested to see what answers you get though.Dygert
I don't really get what your problem is? Do you simply want to be able to interact with the GPIO controller from wherever in your code you are and get notified about pin-value-changes no-matter which view is currently shown? Is that correct?Streusel
Yes @robinmanuelthiel, like a backgroundworker or something. I am logging ~8 statechanges / sec. And I can not miss one change even if I am switching view.Cinda
Is this C++ or C#? Some other language? The raspberry pi does multi-thread - so you could check atomic variables from other parts in your program, and put the gpiocontroller in a separate thread.Civilian
@Civilian I use C# if i do it in a thread would it not kill it if i change view?Cinda
C
1

You can create a GpioController object in your initial class, and pass it as an object to other classes.

Such as:

class BaseClass {
    GpioController gpio;

    void createGpioController(){
        gpio = new GpioController(/*Constuctor arguments here.*/);
    }

    void moveToNextClass(NextClass next){
        //Instantiate next class with any special constructors.
        next.gpio = this.gpio; 
        //Launch next class with same gpio member values. 
    }
}

class NextClass: BaseClass {
   GpioController gpio; //Will be assigned by last class.
}
Civilian answered 25/9, 2016 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.