PowerShell / PowerShell-IoT

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

Have a 'Type' for GPIO Object that can be input or output #28

Open jnury opened 6 years ago

jnury commented 6 years ago

Hi,

Using Get-GpioPin on a pin will force its level to 'low'.

To reproduce (even if the pin is not connected to anything):

Set-GpioPin -Id 0 -Value High

The pin is high.

Get-GpioPin -Id 0

Returns:

Id Value PinInfo
-- ----- -------
 0   Low Unosquare.RaspberryIO.Gpio.GpioPin

And the pin is low :-/

TylerLeonhardt commented 6 years ago

This is going to sound crazy... but you cannot set a pin and then get it's value. GPIO doesn't really work this way. A pin is either a setter or a getter.

Once you set a pin, it's marked as an "input" pin with the value of "high" (or "low").

Once you get a pin, it's marked as an "output" pin with no guarantee that it will hold the value of the previous set operation.

@anmenaga can explain this further.

jnury commented 6 years ago

Hum, my bad, Arduino (bad) habits :-D

Maybe we should have an object 'GPIOPin' with a 'type' = input|output and a Value that we can get/set if the type is Output or simply get if the type is Input.

BTW: thank you for you fast and detailed answer ;-)

TylerLeonhardt commented 6 years ago

That's a great idea :) lets change the name of this issue to support that.

jnury commented 6 years ago

After digging a bit in docs (I'm not quite familiar with Arduino GPIO), it seems that is really easy to read status of an output pin.

Example with GPIO 2:

echo "27" > /sys/class/gpio/export
cat /sys/class/gpio/gpio7/value

I'll have a look if it's possible to use existing WirePi/RaspberryIO method or PR something to these projects to support that ;-)

TylerLeonhardt commented 6 years ago

That would be awesome! Pull requests always welcome 😃

jaswalters commented 5 years ago

@TylerLeonhardt

This is going to sound crazy... but you cannot set a pin and then get it's value. GPIO doesn't really work this way. A pin is either a setter or a getter

Once you set a pin, it's marked as an "input" pin with the value of "high" (or "low").

Once you get a pin, it's marked as an "output" pin with no guarantee that it will hold the value of the previous set operation.

So I'm new to GPIO but not to PowerShell. My history on Po$h is that a Get- would get the value of whatever object that value is holding or set to.

When it comes to GPIO how is Get-GPIOPin to be used?

How would you get the value of say Pin 2 to see if it's currently "High" or "Low"?

TylerLeonhardt commented 5 years ago

You would just use Get-GpioPin but keep in mind that GPIO does not let you Set and then Get. This is a fundamental concept of GPIO.

You would only use Get-GpioPin to get High or Low FROM hardware that sets the value - like a physical button or a simple IR receiver.

You would only use Set-GpioPin to set High or Low TO hardware that uses the value - like an LED or a simple IR blaster.

TylerLeonhardt commented 5 years ago

It took me a while to understand this 😆

jaswalters commented 5 years ago

@jnury

After digging a bit in docs (I'm not quite familiar with Arduino GPIO), it seems that is really easy to read status of an output pin.

Example with GPIO 2:

echo "27" > /sys/class/gpio/export
cat /sys/class/gpio/gpio7/value

I'll have a look if it's possible to use existing WirePi/RaspberryIO method or PR something to these projects to support that ;-)

RaspberryIO can read a pin value like this. Pi.Gpio.Pin02.PinMode = GpioPinDriveMode.Input; // The below lines are roughly equivalent var isOn = Pi.Gpio.Pin02.Read(); // Reads as a boolean var pinValue = Pi.Gpio.Pin02.ReadValue(); // Reads as a GpioPinValue