#!/bin/sh
#
# Sets up a fresh Thruster HE card with the initial settings.
#
# Usage:
#   $ cd path/to/lrauv-application
#   $ ./Tools/configureThrusterHE <serial port> <power port>
#
if [ $# -eq 2 ]; then
  SERPORTNAME=$1 POWERPORTNAME=$2
else
  echo ""
  echo "Enter serial port on command line."
  echo ""
  echo "      usage example: ./Tools/`basename $0` '/dev/ttyA1' '/dev/loadA1'"
  echo ""
  exit
fi

echo "Configuring serial port" $SERPORTNAME
#stty -F $SERPORTNAME 9600 -parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8 -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke # set up the port

echo "Powering up" $POWERPORTNAME
echo 1 > $POWERPORTNAME

# Let's make sure the Thruster responds to power
read -d $'>' -n10 banner < $SERPORTNAME

if [[ "$banner" == *"Starting" ]]; then
    echo "Confirmed thruster has booted. Beginning configuration."
else
    echo "Did not receive boot banner from thruster. Powering down and exiting."
    echo i > $POWERPORTNAME
    exit;
fi

echo "!! run the following command in another terminal: picocom" $SERPORTNAME
echo "press any key when ready"

while [ true ] ; do
read -t 3 -n 1
if [ $? = 0 ] ; then
echo -ne "WE,08,4897\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,09,3435\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,0A,A7FE\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,0B,9C80\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,0C,2020\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,0D,8140\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,0E,0E15\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,0F,F6BD\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,10,10EC\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,11,2200\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,12,8C0D\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,13,6600\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,14,4AB4\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,15,C100\r" > $SERPORTNAME
sleep 0.5
echo -ne "WE,16,873F\r" > $SERPORTNAME
sleep 0.5
echo -ne "RA\r" > $SERPORTNAME
sleep 0.5

echo "See configuration results in picocom. Powering down."
echo i > $POWERPORTNAME
exit;
else
echo "waiting for the any key"
fi
done

sleep 1

