#! /bin/sh
# Simple tool for controlling the FM radio on the command line.
# (c) 2009 Martin Grimme  <martin.grimme@gmail.com>


FREQ=$1
MODE=$2


send_request() {

  dbus-send --system --print-reply \
            --dest=de.pycage.FMRXEnabler \
            /de/pycage/FMRXEnabler\
            de.pycage.FMRXEnabler.request >/dev/null

}


require_root() {

  MSG=$1
  if [ "`id -u`" != "0" ]; then
    echo ${MSG}
    exit 1
  fi

}


find_sysfs_path() {

  F=`find /sys/class/i2c-adapter -name "fm_frequency"`
  echo `dirname ${F}`

}


mixer_off() {

  # for headphone output
  amixer -qc0 cset iface=MIXER,name='Left Line2 Bypass Mixer Line Sw' off
  amixer -qc0 cset iface=MIXER,name='Right Line2 Bypass Mixer Line S' off
  
  # for speaker output
  amixer -qc0 cset iface=MIXER,name='Left Line2 Bypass Mixer HP Swit' off
  amixer -qc0 cset iface=MIXER,name='Right Line2 Bypass Mixer HP Swi' off

}


speaker_on() {

  # for headphone output
  amixer -qc0 cset iface=MIXER,name='Left Line2 Bypass Mixer Line Sw' off
  amixer -qc0 cset iface=MIXER,name='Right Line2 Bypass Mixer Line S' off
  amixer -qc0 cset iface=MIXER,name='Line Line2 Bypass Playback Volume' 100,100
  
  # for speaker output
  amixer -qc0 cset iface=MIXER,name='Left Line2 Bypass Mixer HP Swit' on
  amixer -qc0 cset iface=MIXER,name='Right Line2 Bypass Mixer HP Swi' on
  amixer -qc0 cset iface=MIXER,name='HP Line2 Bypass Playback Volume' 100,100

  amixer -qc0 cset iface=MIXER,name='Speaker Function' 1

}


speaker_off() {

  # for headphone output
  amixer -qc0 cset iface=MIXER,name='Left Line2 Bypass Mixer Line Sw' on
  amixer -qc0 cset iface=MIXER,name='Right Line2 Bypass Mixer Line S' on
  amixer -qc0 cset iface=MIXER,name='Line Line2 Bypass Playback Volume' 100,100
  
  # for speaker output
  amixer -qc0 cset iface=MIXER,name='Left Line2 Bypass Mixer HP Swit' off
  amixer -qc0 cset iface=MIXER,name='Right Line2 Bypass Mixer HP Swi' off
  amixer -qc0 cset iface=MIXER,name='HP Line2 Bypass Playback Volume' 100,100

  amixer -qc0 cset iface=MIXER,name='Speaker Function' 0

}


power_on() {

  # switch on
  echo 1 >${SYSFS_PATH}/power_state
  # set DAC audio route 
  echo 16 >${SYSFS_PATH}/audio_route
  # set stereo
  echo 12 >${SYSFS_PATH}/dac_output
  # set preset tune mode
  echo 1 >${SYSFS_PATH}/fm_search_tune_mode
  # unmute
  echo 0 >${SYSFS_PATH}/mute

}


power_off() {

  echo "powering off"
  echo ""
  
  # mute
  echo 1 >${SYSFS_PATH}/mute
  # switch off
  echo 0 >${SYSFS_PATH}/power_state
  mixer_off

}


tune_in() {

  FREQ=$1
  echo "Frequency: $1 KHz"
  echo ${FREQ} >${SYSFS_PATH}/fm_frequency

}


if [ -z "${FREQ}" ]; then
  echo "Command line tool for controlling the FM radio."
  echo "Copyright (c) 2009 Martin Grimme"
  echo ""
  echo "  Usage: $0 <Frequency in KHz> [speaker|headphones]"
  echo ""
  echo "  Examples:"
  echo "         $0 87900"
  echo "         $0 87900 speaker"
  exit 1
fi


require_root "You need to be root to control the FM radio. Aborting."
send_request
SYSFS_PATH=`find_sysfs_path`
power_on
tune_in ${FREQ}

case "${MODE}" in
  headphones)
    speaker_off
    ;;
  speaker)
    speaker_on
    ;;
  *)
    speaker_off
    ;;
esac

trap power_off EXIT
while true; do
  sleep 60
  send_request
done

