#!nsh
#
# Script to configure control interface
#

set SDCARD_MIXERS_PATH /fs/microsd/etc/mixers

if [ $MIXER != none -a $MIXER != skip ]
then
	#
	# Load main mixer
	#

	# Use the mixer file
	set MIXER_FILE /etc/mixers/$MIXER.main.mix

	set OUTPUT_DEV /dev/pwm_output0

	if [ $OUTPUT_MODE == tap_esc ]
	then
		set OUTPUT_DEV /dev/tap_esc
	fi

	if mixer load $OUTPUT_DEV $MIXER_FILE
	then
		echo "INFO  [init] Mixer: $MIXER_FILE on $OUTPUT_DEV"
	else
		echo "ERROR [init] Error loading mixer: $MIXER_FILE"
		echo "ERROR:[init] Could not load mixer: $MIXER_FILE" >> $LOG_FILE
		tone_alarm $TUNE_ERR
	fi

	unset MIXER_FILE
else
	if [ $MIXER != skip ]
	then
		echo "ERROR [init] Mixer not defined"
		echo "ERROR [init] Mixer not defined" >> $LOG_FILE
		tone_alarm $TUNE_ERR
	fi
fi

if [ $OUTPUT_MODE == fmu -o $OUTPUT_MODE == io ]
then
	if [ $PWM_OUT != none ]
	then
		#
		# Set PWM output frequency
		#
		if [ $PWM_RATE != none ]
		then
			pwm rate -c $PWM_OUT -r $PWM_RATE
		fi

		#
		# Set disarmed, min and max PWM values
		#
		if [ $PWM_DISARMED != none ]
		then
			pwm disarmed -c $PWM_OUT -p $PWM_DISARMED
		fi
		if [ $PWM_MIN != none ]
		then
			pwm min -c $PWM_OUT -p $PWM_MIN
		fi
		if [ $PWM_MAX != none ]
		then
			pwm max -c $PWM_OUT -p $PWM_MAX
		fi
	fi

	if [ $FAILSAFE != none ]
	then
		pwm failsafe -d $OUTPUT_DEV $FAILSAFE
	fi
fi

unset PWM_OUT
unset PWM_RATE
unset PWM_MIN
unset PWM_MAX
unset FAILSAFE
unset OUTPUT_DEV
