PowerShell-IoT
PowerShell-IoT copied to clipboard
Have a 'Type' for GPIO Object that can be input or output
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 :-/
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.
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 ;-)
That's a great idea :) lets change the name of this issue to support that.
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 ;-)
That would be awesome! Pull requests always welcome 😃
@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-<CMDLET> 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"?
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.
It took me a while to understand this 😆
@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