#!/bin/bash

#VHF Blue Mountain Remote Base Implementation

#Written by KD0WHB. Send improvements / modifications to electricity440@gmail.com
#Version 1.0

#Frequency Steering Master file
#40 Channel GM300 Radio with LED backlight
#See other script for 32 channels or 16 channels

#Prerequisites:
#1. Program Channel 12's name to something other than a number with 2 as the second digit. Ex. 99
#2. Feed the Bottom Right segment of the right 8-segment display to a gpio pin on the pi.
#3. Feed two GPIO pins to channel UP and Down on the radio
#4. Change permissions of this script so it is executable by running "chmod +x steer.sh"
#4. Syntax:
#steer.sh <Channel to go to 1-16> <GPIO LCD bottom right segment IN pin (Type gpio readall for pinouts)> <GPIO DOWN pin> <GPIO UP pin>
#5. Find wiringpi GPIO pinout by executing the command "gpio readall"

#Change this to the remote base node number (Needed for audio playback to node)
NODE=40764
SPEED=.1
#SPEED=.3 #How fast does it switch channel?
#Change to constant numbers as needed :

CHAN=$1
UP=$2
DOWN=$3
IN=$4


#Set the pinmodes for the inputs
gpio mode $IN in
gpio mode $IN up
gpio mode $UP out
gpio mode $DOWN out

#Disable the transmitter before we do anything else, VERY IMPORTANT!
asterisk -rx "rpt fun $NODE  *903" 
sleep .5
asterisk -rx "rpt playback $NODE /etc/asterisk/scripts/steer/uhfdis"

gpio write $UP 0
gpio write $DOWN 0

SEG=$(gpio read $IN)

#asterisk -rx "rpt playback $NODE  wait-moment"



while [ "$SEG" != "0" ]
do

gpio write $UP 1
sleep $SPEED
gpio write $UP 0
sleep $SPEED
echo "$SEG"
SEG=$(gpio read $IN)
done

echo "passed while loop!"
#If the desired channel is equal to two, no further action is required

if [ "$CHAN" = "2" ]
then
    #nothing more to do required
    z=1 #I guess the if statement has to have some code in it
elif [ "$CHAN" = "1" ]
then
#If the channel is equal to one, simply pulse down once.
	gpio write $DOWN 1
	sleep $SPEED
	gpio write $DOWN 0
	sleep $SPEED
elif (($CHAN > 12)) && (($CHAN <= 17))  #In this case, it is quicker by using down arrows
then
PMARK="$(( 2 +(17 - $CHAN)))" #Placemark for channel steering
echo $PMARK
for (( a=1; a<=$PMARK ; a++ )) #This determines how many DOWN presses
do
	gpio write $DOWN 1
        sleep $SPEED
        gpio write $DOWN 0
        sleep $SPEED
done
elif (($CHAN <= 12)) && (($CHAN > 2)) #In this case it is quicker to go up
then
PMARK="$(($CHAN -2))" #Placemark for channel steering
for (( a=1; a<=$PMARK ; a++ )) #How many pulses to get to desired channel?
do
	gpio write $UP 1
	sleep $SPEED
	gpio write $UP 0
	sleep $SPEED
done
fi

#rm -Rf /tmp/temp.ulaw
sleep 3

if (($CHAN <= 20)) && (($CHAN  >= 1))
then
	if (($CHAN <=20))
	then
	cat /etc/asterisk/sounds/channel.ulaw > /tmp/temp.ulaw
	cat /etc/asterisk/sounds/digits/$CHAN.ulaw >> /tmp/temp.ulaw
	asterisk -rx  "rpt playback $NODE /tmp/temp"
	echo "Channel succesfully steered to $CHAN !"
	echo "channel is 20 or below"
	elif (($CHAN <=29))
	then
	cat /etc/asterisk/sounds/channel.ulaw > /tmp/temp.ulaw
	cat /etc/asterisk/sounds/digits/20.ulaw >> /tmp/temp.ulaw
	cat /etc/asterisk/sounds/digits/$(($CHAN -20)).ulaw >> /tmp/temp.ulaw
	asterisk -rx  "rpt playback $NODE /tmp/temp"
	elif [ "$CHAN" = "30" ]
	then
	cat /etc/asterisk/sounds/channel.ulaw > /tmp/temp.ulaw
        cat /etc/asterisk/sounds/digits/30.ulaw >> /tmp/temp.ulaw
	asterisk -rx  "rpt playback $NODE /tmp/temp"
	elif (($CHAN <=39))
	then
	cat /etc/asterisk/sounds/channel.ulaw > /tmp/temp.ulaw
        cat /etc/asterisk/sounds/digits/30.ulaw >> /tmp/temp.ulaw
        cat /etc/asterisk/sounds/digits/$(($CHAN -30)).ulaw >> /tmp/temp.ulaw
	asterisk -rx  "rpt playback $NODE /tmp/temp"
	elif [ "$CHAN" = "40" ]
	then
	cat /etc/asterisk/sounds/channel.ulaw > /tmp/temp.ulaw
        cat /etc/asterisk/sounds/digits/40.ulaw >> /tmp/temp.ulaw
        asterisk -rx  "rpt playback $NODE /tmp/temp"
fi
else
        asterisk -rx  "rpt playback $NODE option-is-invalid"
fi

echo $CHAN>/var/www/html/allmon2/uhfstatus.php
sleep 2
if ![ -e /tmp/uhflinked ]
then
#Re-enable the transmitter if the VHF node is not linked to anything. User has to manualy do this if linked.
asterisk -rx  "rpt cmd $NODE cop 2 xxx" 
fi


