#! /bin/sh -
# interface.sh - Wifi Radio User Interface Script
# 01/29/09	Jeff Keyzer	http://mightyohm.com
#
# This shell script sets up a playlist in mpd and changes playlist entries
# based on the position of a tuner knob connected to an AVR on the router's
# serial port.
#
# The script expects the AVR to send data at 9600 baud (8N1) to the router,
# in the format "tuner: value", one line at a time.
#
# This script also launches display.sh, the LCD display script.
#
# For more information, visit
# http://mightyohm.com/blog/tag/wifiradio/
#
# This work is protected by the
# Creative Commons Attribution-Share Alike 3.0 United States License.
# http://creativecommons.org/licenses/by-sa/3.0/us/ 

# Some configuration settings
VOLUME=100

trap 'kill $! ; exit 1' SIGINT	# exit on ctrl-c, useful for debugging
				# kills the display.sh process before exiting

stty -echo < /dev/tts/0		# set serial port to 9600 baud
				# so we can talk to the AVR
				# turn off local echo to make TX/RX directions
				# completely separate from each other

# mpd setup
mpc volume $VOLUME	# adjust this to suit your speakers/amplifier
mpc clear	# clear current playlist

# build a playlist, substitute your favorite radio stations here
# the first line becomes station #1, and so on.
mpc add http://radio.bigblueswing.com:8002/
mpc add http://stream.dradio.de/7/530/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dkultur_m 
mpc add http://stream.dradio.de/7/249/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dlf_m 
mpc add http://stream.dradio.de/7/728/142684/v1/gnl.akacast.akamaistream.net/dradio_mp3_dwissen_m 
mpc add http://live.slovakradio.sk:8000/Litera_256.ogg
mpc add http://193.219.175.105:8000/
mpc add http://sc-ajazz-64kaac.1.fm:80/
mpc add http://91.121.92.167:8700
mpc add http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio1_p
mpc add http://icecast7.play.cz:8000/cro1-128.mp3
mpc add http://icecast2.play.cz/crojazz128aac

mpc playlist	# show the resulting playlist
mpc play

oldstation=1	# var to keep track of what station we're playing

# Tell the AVR we're ready to start doing stuff
echo "AVR Start" > /dev/tts/0

# launch LCD display routines in the background
/root/display2.sh &

while true	# loop forever
do
   inputline="" # clear input
  
   # Loop until we get a valid tuner position from the AVR 
   until inputline=$(echo $inputline | grep -e "^tuner: ")
   do
      inputline=$(head -n 1 < /dev/tts/0)
   done
   value=$(echo $inputline | sed 's/tuner: //')	# strip out the tuner: part
   echo "$value"
  
   # compute playlist positon based on tuner value
   # the tuner range is 0 to MAXADC
   # the ADC range is separated into equally sized bins, each bin
   # is one station
   # station ends up being an integer from 1 to STATIONS
   # that determines what station to play 
   # station=$(expr $value / $ADCBIN + 1)
   station=$value
   # if station has changed, we need to change position in the playlist 
   if [ "$station" -ne "$oldstation" ]
   then
   	echo "Tuner Position: " $value
   	echo "New station..."
   	mpc play $station
    fi
    
   oldstation=$station	# remember what station we're on so we know
   			# if we need to change stations next time around
done

