0

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:

  1. ./sw-state.sh <<< "$(sudo pin-control poll 25)"

  2. sudo pin-control poll 25 | ./sw-state.sh

Q: What am I doing wrong??

5
  • Maybe it writes to stderr? Have you tried adding 2>&1 after pin-control poll 25?
    – Arfrever
    Commented Mar 26 at 5:21
  • Shouldn't you be using something like unix.stackexchange.com/a/785259/70524 instead of either of these?
    – muru
    Commented Mar 26 at 5:25
  • @Arfrever: Thanks; that seems unlikely, but I tried it anyway... it did not work either
    – Seamus
    Commented Mar 26 at 5:27
  • @muru: great suggestion! :) I had completely forgotten about that... Anyway - I cobbled something together that was close enough, and it worked. Thanks again, and if you'd like to answer this question, please do. I don't think I will as it's so close to the other answer. ... and I must remember the while read
    – Seamus
    Commented Mar 26 at 6:26
  • @EdMorton - Based on my comment above, I clearly agree. I'd delete it, but there's an answer now... please feel free to close the Q.
    – Seamus
    Commented Mar 26 at 20:23

2 Answers 2

0

Try:

nohup sudo pin-control poll 25 > nohup.out &
tail -f nohup.out | ./sw-state.sh

Here nohup is the UNIX no hang up command.

This approach is similar to your second "didn't work" solution.

1
  • I did try this, but unfortunately it doesn't work. The problem is with the first line... nohup and sudo don't seem to "get along together". I fooled around with similar attempts before submitting my question, and some advise using sudo -b... but that didn't help me either.
    – Seamus
    Commented Mar 26 at 21:59
0

Once again, credit to @muru for his comment. Here's what worked for me:

#!/usr/bin/env bash

tcnt=0
# note: pinctrl requires root; use sudo, or run script as root
# also: note 'pinctrl' is 'piped' to the 'while read'
# sudo /usr/bin/pinctrl poll 25 |
/usr/bin/pinctrl poll 25 |
while read toggles; do
    state=$(echo "$toggles" | grep -Eo 'hi|lo')
    case "$state" in
        hi)
            echo -e "$state\t\tGPIO25 is "hi" - nothing to do"
            ;;
        lo)
            tcnt=$((tcnt+1))
            echo -e "$state\t$tcnt\tGPIO25 is "lo" - get busy!"
            ;;
    esac
done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.