Notify when gpio value is changed
I am currently trying to poll the gpio value with a shell script only.
Basically I developed a script with a test file before using / sys / class / gpio / gpioxx / value
This is the solution I found:
#!/bin/bash
SCRIPT_DIR=$(dirname $(readlink -f $0))
FILE_NAME=$SCRIPT_DIR"/fileTest"
while true
do
inotifywait -qq -e modify $FILE_NAME
read val < $FILE_NAME
echo $val
### do something here ###
done
This works with the base file, but I have two problems with this solution.
1 - The "modify" event is fired when the file is saved, not when the contents of the file have changed. So if I write the same value in the file, the event is triggered, but it shouldn't.
2 - I repeated that this solution does not work for gpios if I use a simple ascii file it works, but when I use inotifywait on / sys / class / gpio / gpioxx / value it depends.
If I use echo value> / sys / class / gpio / gpioxx / value the event is detected, but if I configure the pin as input and connect it to 3v3 or 0V nothing is triggered.
Does anyone know how I can trigger this change using only scripts?
source to share
This is a tight loop solution (which is more resource intensive), but will do the trick if you don't get anything better:
gpio_value=$(cat /sys/class/gpio/gpio82/value)
while true; do
value=$(cat /sys/class/gpio/gpio82/value)
if [[ $gpio_value != $value ]]; then
gpio_value=$value
echo "$(date +'%T.%N') value changed to $gpio_value"
fi
done
Output example:
13:09:52.527811324 value changed to 1
13:09:52.775153524 value changed to 0
13:09:55.439330380 value changed to 1
13:09:55.711569164 value changed to 0
13:09:56.211028463 value changed to 1
13:09:57.082968491 value changed to 0
I am using it for debugging.
Actually, I usually use this one-liner more often:
printf " Press any key to stop...\n GPIO value: " ; until $(read -r -t 0 -n 1 -s key); do printf "\033[2D$(cat /sys/class/gpio/gpio82/value) " ; done ; echo
Again, for debugging purposes.
source to share
From linux/Documentation/gpio/gpio-legacy.txt
:
"/sys/class/gpio/gpioN/edge"
... reads as either "none", "rising", "falling", or
"both". Write these strings to select the signal edge(s)
that will make poll(2) on the "value" file return.
So you can do:
echo input > /sys/class/gpio/gpioN/direction
echo both > /sys/class/gpio/gpioN/edge
Now you need to find the command that calls poll
(or pselect
) on /sys/class/gpio/gpioN/value
. (I will update my answer if I find it)
source to share