PowerShell / PowerShell-IoT

Interact with I2C, SPI & GPIO devices using PowerShell Core!
https://www.powershellgallery.com/packages/Microsoft.PowerShell.IoT
MIT License
130 stars 28 forks source link

GpioPin changes #50

Closed DanielSSilva closed 4 years ago

DanielSSilva commented 4 years ago

On the new library, the GPIO is controlled by a GpioController instance (here's the class). As far as I've understood, there should be only 1 controller "active" (1 instance at the time). For that reason, I've decided that it was a better approach to have the *-GpioPin to receive the controller, similar to what's being done with the I2C device. This avoids the overload of creating a controller per command execution, and allows us to keep context of the pins through the controller. This also requires that we have a new cmdlet to create a Controller (Get-GpioController ?)

Another thing to keep in mind is that we should have a way do properly dispose the controller, if the user doesn't do so (see here).

Another breaking change is that this library does not use the PullMode like we currently have. Instead, they have a PinMode (here's the source). This is used when opening a pin/setting its mode

_controller.OpenPin(_echo, PinMode.Input);
_controller.OpenPin(_trigger, PinMode.Output);

(source)

As a final note, we now have to pay special attention to opening/closing pins while developing our cmdlets, in order to avoid potential bugs where pins are left open, or are wrongly closed :)

Here's a snippet with an example on how to get a pin info

$controller = Get-GpioController #if no scheme is specified, use the default one

$pin = Get-GpioPin -Controller $controller -Id 5 -PinMode Input
TylerLeonhardt commented 4 years ago

I think we can simply hold a singleton/static field inside of the module.

inside of Get-GpioPin:

if(s_gpioController == null)
{
    s_gpioController = new GpioController(...);
}

// do logic

It seems to me that GpioController is an implementation detail that the user doesn't need to see.

DanielSSilva commented 4 years ago

Assuming that we can do that (the static field) inside the module without any problem, I can agree with that. This will have to work for any GpioPin cmdlet, that being get, set or anything else we might have later ( like this RegisterCallbackForPinValueChangedEvent which I think that can be a game changer 😃 )