#!/bin/bash

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

#Frequency Steering Master file
#32 Channel Radius/Maxtrac/GM300 Radio with LCD backlight
#See other script for 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=42622
SPEED=.025
#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 for the duration of the script
asterisk -rx "rpt cop $NODE cop 3 1"

gpio write $UP 0
gpio write $DOWN 0

SEG=$(gpio read $IN)

asterisk -rx "rpt playback 42622 wait-moment"

sleep 1.5

while [ "$SEG" != "1" ]
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 > 18)) && (($CHAN <= 32))  #In this case, it is quicker by using down arrows
then
PMARK="$(( 16 -($CHAN -18)))" #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 <= 18)) && (($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 1

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

sleep 2
#Re-enable the transmitter
asterisk -rx  "rpt cmd $NODE cop 2 1"
