#!/bin/sh
#
# Robinson R22 low rotor-RPM warning horn simulation.
#
# The purpose of this exercise is to train the pilot to 
# react IMMEDIATELY to a low rotor-RPM condition.
#
# ------------------------------------------------------
# Version   Date     Author             Remarks
# 0.1.0     20090122 gavron@wetwork.net Initial version
# ------------------------------------------------------
#
# Methodology:
# while (we haven't been stopped) {
#    get a random integer between 0-255
#    sleep that number of minutes
#    play the horn!!!
# }
#
# Note: Ideally we'd have a remote mouse or remote 
#       bluetooth adapter so that the user could 
#       indicate the response to the horn, and we
#       could time it.  If it occurs prior to 1.6
#       seconds (plus 1 sec per 1000ft AGL) then
#       it is a timely response.  
#
# -----------------------------------------------------
# TEST CODE:
#      If a parameter is passed as a value then it
#      will be used as the "seconds to minutes"
#      multiplier (default = 60).
#
#      If specified as "1" then it will cause this
#      thing to go off every 0-255 seconds ;)
#
#
# -----------------------------------------------------
#
SOUNDFILE=low-rotor-rpm-warning-horn.wav
PLAYCMD=play
TRUE=/bin/true
SECSPERMIN=60
#
### TEST CODE FOLLOWS
if [ ! -z "$1" ]; then
   SECSPERMIN="$1"
fi
### TEST CODE ENDS
#
while ( $TRUE ); do
#
# First get a random number 1-255
   DEC=`dd if=/dev/urandom bs=1 ibs=1 count=1 2>/dev/null | hexdump -d| awk '{print $2}'`
#
# now let's strip leading zeroes...
   STRIP=`echo $DEC | sed -e "s/\(^0*\)\(.*\)/\2/g"`
#
# Now multiply that number of seconds (1-255) and make it minutes...
   SLEEP=`echo "$STRIP * $SECSPERMIN" | bc`
#
# And now wait that number of seconds...
### TEST
if [ ! -z "$1" ]; then
   echo "Sleeping $SLEEP"
fi
### TEST END
   sleep $SLEEP
#
#  Display time for logging purposes and sound the horn
   date
   $PLAYCMD $SOUNDFILE
#
# Rinse, repeat
done 
