Last week I worked with a group of 10/11 year olds at the Raspberry Pi command line, using my speaking weather script to show them what can be done with the Raspberry Pi at the command line. This week I went a step further and got them to write a script to create a menu which incorporated the speaking weather forecast and also showed them some other cool things that the Raspberry Pi can do at the terminal.

The finished main menu looks like this:

Background

The menu uses a package called dialog, which makes creating menus really easy. I also installed a number of other packages to enable all the other goodies to run. These all run from the command line interface (CLI) and include: include:

  • fortune: a program that generates fortune cookies
  • cowsay: a program that lets ASCII animals speak words on the screen
  • sl: a steam locomotive that dashes across your screen
  • toilet: not what it seems..... a program for displaying words on the screen
  • cmatrix: similar to the matrix screen saver

Please note that if you want to use the speech mentioned below, you'll need to follow my Get the Raspberry Pi to speak the Weather tutorial first.

How to install

The commands that will install everything for you are as follows:

sudo apt-get install dialog fortune cowsay telnet sl toilet cmatrix

When you've installed all the programs, you can type in the program names just to see what they will do.

If you want to add the ASCII aquarium too, the script to install this is as follows:

sudo apt-get install libcurses-perl
cd /tmp 
wget http://search.cpan.org/CPAN/authors/id/K/KB/KBAUCOM/Term-Animation-2.4.tar.gz
tar -zxvf Term-Animation-2.4.tar.gz
cd Term-Animation-2.4/
perl Makefile.PL &&  make &&   make test
make install

cd /tmp
wget http://www.robobunny.com/projects/asciiquarium/asciiquarium.tar.gz
tar -zxvf asciiquarium.tar.gz
cd asciiquarium_1.1/
cp asciiquarium /usr/local/bin
chmod 0755 /usr/local/bin/asciiquarium

The first part of the script

To create the menu program, open up a text editor (nano) at the terminal and type in the following:

sudo nano menu.sh

Now paste the following code into it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh

# First we ask the user for their name

/home/pi/Projects/Weather/speech.sh Hello and welcome to my script. Please enter your name

DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$

trap "rm -f $tempfile" 0 1 2 5 15

$DIALOG --title "Welcome to my script" --clear \
    --inputbox "Hi, and welcome to my script\n
Please enter your name below:" 8 35 2> $tempfile
retval=$?

case $retval in
1)
# user has pressed cancel so we exit
exit 0;;
255)
# user has pressed Escape so we exit
    exit 0;;
esac

username=`cat $tempfile`

# Now we say the user's name and welcome them to the script
/home/pi/Projects/Weather/speech.sh Hello $username and welcome to my menu

Now save the contents of this file (Ctrl X and then Y). Next we need to change the permissions on it so that it will run:

sudo chmod u+x menu.sh

Now we can run it by typing:

./menu.sh

This is the first part of the script and generates the following screen:

This first part of the script speaks to the user and asks them for their name. It then creates a temporary file $tempfile to store the user's name variable. The trap command is used for if the program is cancelled and the temporary file then gets deleted. Next, the dialog menu is created and it asks for input of the user's name. If the user presses Escape or Cancel then the script exits, otherwise it speaks the user's name.

Have a go at changing some of the parameters in the $DIALOG --title line and see what happens.

The second part of the script

The second part of the script is as follows:

DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

_main () {
# This is the main menu, you can add to it or delete from it by changing it below
# If you do change it, be sure to change the program details in the case statement below 
$DIALOG --clear --title "Welcome to my menu" \
    --menu "Hi $username, Choose an option from below" 20 51 9 \
    "1"  "Read the weather local forecast" \
    "2"  "Read a fortune cookie" \
    "3"  "Get a stegosaurus to say hello" \
    "4"  "See a train speeding by" \
    "5"  "Watch a matrix screen" \
    "6"  "Show your name in fancy graphics" \
    "7"  "Watch Star Wars in ASCII" \
    "8"  "See my new aquarium" \
    "9"  "Say something" 2> $tempfile
retval=$?
choice=`cat $tempfile`

case $retval in
1)
# user has pressed cancel so we exit
exit 0;;
255)
# user has pressed Escape so we exit
    exit 0;;
esac

case $choice in
  1)
  # Weather report
dialog --title "Weather Report" --msgbox "The weather for Wawne is now being read." 8 20
/usr/bin/php /home/pi/Projects/Weather/getWeather.php
_main
;;
2)
# Fortune Cookie
clear
OUTPUT=$(/usr/games/fortune)
echo "Here is your Fortune Cookie:\n"
echo "$OUTPUT"
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
3)
# Stegosaurus
clear
cowsay -f stegosaurus hello $username
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
4)
# Speeding train
clear
sl
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
5)
# Matrix screen
clear
cmatrix
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
6)
#  Show your name in fancy graphics
clear
toilet -f mono12 -F metal $username
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
7)
# Watch Star Wars in ASCII
clear
echo -n "\nYou will now be logged onto another computer on the Internet to watch this. When you want to come back, press Ctrl ] and then type in quit
read ANSWER
telnet towel.blinkenlights.nl
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
8)
# Aquarium
/usr/local/bin/asciiquarium
echo -n "\nPress [ENTER] to continue"
read ANSWER
_main
;;
9)
# Say something
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --title "Welcome to say something" --clear \
    --inputbox "Type what you'd like me to say below" 8 35 2> $tempfile
retval=$?
case $retval in
1)
# user has pressed cancel so we exit
exit 0;;
255)
# user has pressed Escape so we exit
    exit 0;;
esac
saywhat=`cat $tempfile`
/home/pi/Projects/Weather/speech.sh $saywhat
_main
;;
esac
}
_main

This second part of the script consists of a function called _main. This function is called whenever one of the menu options has been run so that the only way to exit the script is by pressing Cancel or the Escape key.

The menu is generated at the beginning of this code and gives the user 8 options to choose from. You can modify these options to be whatever you want them to be. If you do modify the options, then you'll need to modify the corrresponding code in the case statement too.

If you are teaching children how to use this script, you could start off by adding one of the options at a time to the script and getting them to build it up slowly.


Comments

comments powered by Disqus
Gary Hall Gary Hall is based in East Yorkshire, England, and has a background in education, marketing and technology. This site is a collection of ideas and resources on these topics.