I'm struggling with the vocabulary here, so I'll ask this way:
On an embedded system (Raspberry Pi) I use, there is a command invoked as follows:
$ pin-control poll 25
This command "polls" GPIO25, and reports its state. When run from the CLI, pin-control
does not "return" to the bash
prompt. It continues to run, and outputs a couple of lines each time the polled GPIO input changes state (i.e. the GPIO pin is connected to a switch through a pullup Resistor, and the switch is toggled). It runs fine when started from the command line. For example, here are 4 "toggles":
$ sudo pin-control poll 25 # requires root privs
25: hi // GPIO25 # initial output gives state of GPIO25 input
+17852310us # subsequent outputs appear each time
25: lo // GPIO25 # the input is "toggled"
+2288490us
25: hi // GPIO25
+2201396us
25: lo // GPIO25
+1904080us
25: hi // GPIO25
The time figures are irrelevant (for now).
pin-control
outputs a single line when invoked giving the current state; it outputs nothing further until the GPIO input state changes (which could be a long time!).
I need to "capture" each of these pin-control
outputs in a script (the hi-lo transitions). If the current status becomes "lo", I need to take some action; let's assume that action is to increment a counter; as in this script:
#!/usr/bin/env bash
# $0 (name): sw-state.sh
tcnt=0 # toggle counter
pcs="hi" # pin control status (current)
read pcoi # pin control "raw" input from 'sudo pin-control poll 25'
pc0=$(echo -e "$pcoi" | grep -Eo 'hi|lo') # pco is pin control output ("hi" or "lo")
if [ "$pco" != "$pcs" ]; then
tcnt=$((tcnt+1))
echo "$tcnt" > /home/pi/togctr.log
echo "value is: $tcnt"
fi
I've tried a couple of things that didn't work:
./sw-state.sh <<< "$(sudo pin-control poll 25)"
sudo pin-control poll 25 | ./sw-state.sh
2>&1
afterpin-control poll 25
?while read