Get output state
doraess opened this issue · 3 comments
How can I check the actual status of an output pin?
I am using your library (btw great work), and I can control successfully the gpio pins as outputs.
I would like to know every time if there is any output activated.
The value property reports the state of an output pin.
if output_pin.value == 1:
print("output is high")
else:
print("output is low")
But it's more efficient to keep track of the state of the pins in your code by clearing all the pins to a known state at the start of the program and holding the current pin state in variables.
Thank you for your quick answer.
I have already tried this method, but not in the same context where I activated the output.
I mean, I use a procedure to activate a pin, it works, and after I unexport the pin. In another procedure (a different command call), I activate the pin as input and I get the value (activated, as expected), and aIso unexport the pin. In a second call to this last procedure to check the status, I get 0.
My two files are the following:
<gpio.py> ---> example: gpio.py on 7
import sys
from quick2wire.gpio import Pin
if (len(sys.argv) > 1):
out_pin = Pin(int(sys.argv[2]), Pin.Out)
if (sys.argv[1] == 'on'):
out_pin.value = 1
else:
out_pin.value = 0
else:
out_pin = Pin(7, Pin.Out)
out_pin.value = 1
print ("Ok")
out_pin.unexport()
<gpio_status.py>
import sys
from quick2wire.gpio import Pin
pin_list = [3, 5, 7, 8, 10, 11, 12, 13, 15, 16, 18, 19, 21, 22, 23, 26]
pin_status = []
for gpio_pin in pin_list:
input_pin = Pin(gpio_pin, Pin.In)
pin_status.append(':'.join([str(gpio_pin).zfill(2), str(input_pin.value)]))
print(pin_status)
input_pin.unexport()
I am building a web server to control the GPIOs, so I could save the status of the pins in the database. But I was looking for a more direct way to get the status.
If you are going to read the value that you previously set, you need to open it as 'Pin.Out'. Reading a GPIO port opened with Pin.In will read the state of the hardware connected to the pin.