#!/bin/bash

# This script can be used to set the mixer bass/treble/channel volume
# and line-out
#
# Usage:
# 
# mixer_setup [channel] [master_volume] [bass] [treble] [line_out]
#   channel: 1 - 4
#   master_volume: 	0(mute) - 100
#   bass:		0 - 100
#   treble:		0 - 100
#   line_out:		0 (do not send channel input to line-out)
# 			1 (send channel input to line-out)
#   mode:		1 (line-in-analog), 2 (line-in-digital), 3(mic)
#   power:		0 (phantom power off), 1 (phantom power on)
#   gain:		256 (min) - 0 (max)
#     

CHANNEL="$1"
MASTER_VOL="$2"
BASS_LEVEL="$3"
TREBLE_LEVEL="$4"
LINE_OUT="$5"
MODE="$6"
PWR="$7"
GAIN="$8"
paramnum="$#"
gpio_path=/sys/class/gpio
chanpwr_gpio=(CHANPWR 22 26 65 87)
chanmode_gpio=(CHANMODE 64 67 68 69)
gain_path=/sys/bus/iio/devices/iio\:device
changain_path=(CHANGAIN 0/out_resistance0_raw 1/out_resistance0_raw 2/out_resistance0_raw 3/out_resistance0_raw)
NUM_CHANNELS=4
MAX_BASS=100
MAX_TREBLE=100
MAX_VOL=100

MASTER_STR="In"
BASS_STR="EQ BASS"
TREBLE_STR="EQ TREBLE"

show_usage()
{
	echo "Usage:"
	echo ""
	echo " mixer_setup [channel] [master_volume] [bass] [treble] [line_out]"
	echo "   channel:               1 - 4"
	echo "   master_volume:         0(mute) - 100"
	echo "   bass:                  0 - 100"
	echo "   treble:                0 - 100"
	echo "   line_out:              0 (do not send channel input to line-out)"
	echo "                          1 (send channel input to line-out)"
	echo "   mode:                  1 (line-in-analog), 2 (line-in-digital), 3(mic)"
	echo "   power:                 0 (phantom power off), 1 (phantom power on)"
	echo "   gain:                  256 (min) - 0 (max)"
	echo ""
}

check_num_parameters()
{
        # Exit if number of parameters are incorrect
        expectedparamnum=$1
        if [ "$paramnum" -ne $expectedparamnum ]; then
		echo "Expected $expectedparamnum arguments - exiting"
		show_usage
                exit 1
        fi
}

check_num_parameters 8

if [ "$CHANNEL" -gt "$NUM_CHANNELS" ] || [ "$CHANNEL" -lt 1 ]; then
  echo "ERROR: Channel $CHANNEL is an invalid argument (must be 1-$NUM_CHANNELS)"
  show_usage
else
  logger -t $0 "Mixer parameters $*"
  if [ "$LINE_OUT" == 0 ]; then
    sudo amixer set -M "Router L-$CHANNEL" 0% > /dev/null
  elif [ "$LINE_OUT" == 1 ]; then
    sudo amixer set -M "Router L-$CHANNEL" 100% > /dev/null
  else
    echo "ERROR: Line-out argument $LINE_OUT is invalid (must be 0 or 1)"
    show_usage
  fi

  if [ "$BASS_LEVEL" -gt "$MAX_BASS" ] || [ "$BASS_LEVEL" -lt 0 ]; then
    echo "ERROR: Bass level $BASS_LEVEL is an invalid argument (must be 0-$MAX_BASS)"
    show_usage
  else
    sudo amixer set -M "CH$CHANNEL $BASS_STR" $BASS_LEVEL% > /dev/null 
  fi

  if [ "$TREBLE_LEVEL" -gt "$MAX_TREBLE" ] || [ "$TREBLE_LEVEL" -lt 0 ]; then
    echo "ERROR: Treble level $TREBLE_LEVEL is an invalid argument (must be 0-$MAX_TREBLE)"
    show_usage
  else
    sudo amixer set -M "CH$CHANNEL $TREBLE_STR" $TREBLE_LEVEL% > /dev/null
  fi

  if [ "$MASTER_VOL" -gt "$MAX_VOL" ] || [ "$MASTER_VOL" -lt 0 ]; then
    echo "ERROR: Master volume level $MASTER_VOL is an invalid argument (must be 0-$MAX_VOL)"
    show_usage
  else
    sudo amixer set -M "CH$CHANNEL $MASTER_STR" $MASTER_VOL% > /dev/null
  fi

  [ "$MODE" -eq 3 ] && echo 0 > ${gpio_path}/gpio${chanmode_gpio[$CHANNEL]}/value || echo 1 > ${gpio_path}/gpio${chanmode_gpio[$CHANNEL]}/value
  [ "$PWR" -eq 0 ] && echo 0 > ${gpio_path}/gpio${chanpwr_gpio[$CHANNEL]}/value || echo 1 > ${gpio_path}/gpio${chanpwr_gpio[$CHANNEL]}/value
  echo $GAIN > ${gain_path}${changain_path[$CHANNEL]}
fi

