emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

GPIO Bash Scripts

Thu Sep 06, 2012 4:14 pm

I was working with the wiringPi library, and wrote the following scripts for debugging purpose. I thought somebody might like to use them (bash is hard with no experience or reference books :roll: ), so here goes!

poll-gpio.sh

Code: Select all

#!/bin/bash

clear

while test 1 -eq 1
do
    ./read-gpio-all.sh
    for i in {1..5000}
    do
        echo -n
    done
    clear
done
read-gpio-all.sh

Code: Select all

#!/bin/bash

for counter in {0..7}
do
    echo -n "pin "
    echo -n $((counter))
    echo -n " is "
    gpio read $((counter))
done
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

User avatar
jojopi
Posts: 3107
Joined: Tue Oct 11, 2011 8:38 pm

Re: GPIO Bash Scripts

Thu Sep 06, 2012 5:34 pm

emcsquirreled wrote:

Code: Select all

    for i in {1..5000}
    do
        echo -n
    done
I assume this is to introduce a delay. It would be better to use "sleep 1" instead, because it does not waste cpu time, and will not vary with overclocking. On some systems sleep can only wait for an integer number of seconds, but on Linux you can use fractions like "sleep 0.2" as well.

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: GPIO Bash Scripts

Thu Sep 06, 2012 7:12 pm

jojopi wrote:

Code: Select all

    for i in {1..5000}
    do
        echo -n
    done
I assume this is to introduce a delay. It would be better to use "sleep 1" instead, because it does not waste cpu time, and will not vary with overclocking. On some systems sleep can only wait for an integer number of seconds, but on Linux you can use fractions like "sleep 0.2" as well.
That was designed to delay the code, and prevented all the reads and clears from creating a rapidly flashing screen, which was very hard to read. I am fairly new to shell scripting, so sorry for the dumb mistake.

I cannot find a way to edit my original post, but the script should read like so:

poll-gpio.sh

Code: Select all

#!/bin/bash

clear

while test 1 -eq 1
do
    ./read-gpio-all.sh
    sleep 1
    clear
done
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: GPIO Bash Scripts

Fri Sep 07, 2012 6:32 am

@emcsquirreled: Both scripts could be expressed more concise:

Code: Select all

#!/bin/bash

while true
do
    clear
    ./read-gpio-all.sh
    sleep 1
done

Code: Select all

#!/bin/bash

for counter in {0..7}
do
    echo "pin $counter is $(gpio read $counter)"
done
Edit: And instead of the first script you could use ``watch``:

Code: Select all

$ watch --help
Usage: watch [-bcdhnptvx] [--beep] [--color] [--differences[=cumulative]] [--exec] [--help] [--interval=<n>] [--no-title] [--version] <command>
  -b, --beep                            beep if the command has a non-zero exit
  -d, --differences[=cumulative]        highlight changes between updates
                (cumulative means highlighting is cumulative)
  -e, --errexit                         exit watch if the command has a non-zero exit
  -h, --help                            print a summary of the options
  -n, --interval=<seconds>              seconds to wait between updates
  -p, --precise                         precise timing, ignore command run time
  -v, --version                         print the version number
  -t, --no-title                        turns off showing the header
  -x, --exec                            pass command to exec instead of sh

Code: Select all

while not self.asleep():
    sheep += 1

emcsquirreled
Posts: 90
Joined: Sat Aug 18, 2012 1:26 am
Location: Portland, OR USA
Contact: Website

Re: GPIO Bash Scripts

Fri Sep 07, 2012 4:36 pm

BlackJack wrote:@emcsquirreled: Both scripts could be expressed more concise.
Edit: And instead of the first script you could use ``watch``:
Thank you! As I said in the first post, I have self-taught myself bash from a single reference book and trial-and-error. This is my first published script, so all the input and corrections are greatly appreciated. Apparently I had some bad habits forming, so it is good they got killed before they grew too far.

The new script should read like so (poll-gpio.sh is no longer necessary):

read-gpio-all.sh

Code: Select all

#!/bin/bash
for $counter in {0..7}
do
    echo "pin $counter is $(gpio read $counter)"
done
And then you run it like so:

Command Line

Code: Select all

watch -n 1 ./read-gpio-all.sh
Linux -- The operating system with a CLUE (Command Line User Environment)
--
FTC Team 4508
E=MC Squirreled
emcsquirreled.github.com

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: GPIO Bash Scripts

Fri Sep 07, 2012 6:08 pm

There are also some examples online which I put together for the various projects - e.g. the Tuxx Traffic light crossing thing is:

https://git.drogon.net/?p=gpioExamples; ... xx/tuxx.sh

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “Other programming languages”