#!/bin/sh
#
# Script to configure control interfaces.
#
#
# NOTE: environment variable references:
#  If the dollar sign ('$') is followed by a left bracket ('{') then the
#  variable name is terminated with the right bracket character ('}').
#  Otherwise, the variable name goes to the end of the argument.
#

set OUTPUT_CMD pwm_out
set MIXER_AUX_FILE none
set OUTPUT_AUX_DEV /dev/pwm_output1
set OUTPUT_DEV none

#
# If mount (gimbal) control is enabled and output mode is AUX, set the aux
# mixer to mount (override the airframe-specific MIXER_AUX setting).
#
if ! param compare -s MNT_MODE_IN -1
then
        if param compare -s MNT_MODE_OUT 0
        then
                set MIXER_AUX mount
        fi
fi

# USE_IO is set to 'no' for all boards w/o px4io driver or SYS_USE_IO disabled
if [ $USE_IO = no ]
then
	set MIXER_AUX none
fi

#
# Set the default output mode if none was set.
#
if [ $OUTPUT_MODE = none ]
then
	if [ $USE_IO = yes ]
	then
		# Enable IO output only if IO is present.
		if [ $IO_PRESENT = yes ]
		then
			set OUTPUT_MODE io
			if param greater DSHOT_CONFIG 0
			then
				set OUTPUT_CMD dshot
			fi
		fi
	else
		if param greater DSHOT_CONFIG 0
		then
			set OUTPUT_MODE dshot
			set OUTPUT_CMD dshot
		else
			set OUTPUT_MODE pwm_out
		fi
	fi
fi

#
# If OUTPUT_MODE = none then something is wrong with setup and we shouldn't try to enable output.
#
if [ $OUTPUT_MODE != none ]
then
	if [ $OUTPUT_MODE = mkblctrl ]
	then
		if [ $MKBLCTRL_MODE = x ]
		then
			set MKBLCTRL_ARG "-mkmode x"
		fi
		if [ $MKBLCTRL_MODE = + ]
		then
			set MKBLCTRL_ARG "-mkmode +"
		fi

		if ! mkblctrl $MKBLCTRL_ARG
		then
			# Error tune.
			tune_control play -t 2
		fi
	fi

	if [ $OUTPUT_MODE = hil -o $OUTPUT_MODE = sim ]
	then
		if ! pwm_out_sim start -m $OUTPUT_MODE
		then
			# Error tune.
			tune_control play -t 2
		fi
	fi

	if [ $OUTPUT_MODE = $OUTPUT_CMD ]
	then
		if ! $OUTPUT_CMD mode_$FMU_MODE
		then
			echo "$OUTPUT_CMD start failed" >> $LOG_FILE
			# Error tune.
			tune_control play -t 2
		fi
	fi

	if [ $OUTPUT_MODE = uavcan_esc ]
	then
		if param compare UAVCAN_ENABLE 0
		then
			param set UAVCAN_ENABLE 3
		fi
	fi

	if [ $OUTPUT_MODE = io ]
	then
		. /etc/init.d/rc.io
	fi

	#
	# Start IO for RC input if needed.
	#
	if [ $IO_PRESENT = yes ]
	then
		if [ $OUTPUT_MODE != io ]
		then
			. /etc/init.d/rc.io
		fi
	fi
fi

if [ $MIXER != none -a $MIXER != skip ]
then
	#
	# Load main mixer.
	#
	if [ $MIXER_AUX = none -a $USE_IO = yes ]
	then
		set MIXER_AUX ${MIXER}
	fi

	if [ "$MIXER_FILE" = none ]
	then
		if [ -f ${SDCARD_MIXERS_PATH}/${MIXER}.main.mix ]
		then
			# Use the mixer file from the SD-card if it exists.
			set MIXER_FILE ${SDCARD_MIXERS_PATH}/${MIXER}.main.mix
		else
			# Try out the old convention, for backward compatibility.
			if [ -f ${SDCARD_MIXERS_PATH}/${MIXER}.mix ]
			then
				set MIXER_FILE ${SDCARD_MIXERS_PATH}/${MIXER}.mix
			else
				set MIXER_FILE /etc/mixers/${MIXER}.main.mix
			fi
		fi
	fi

	if [ $OUTPUT_MODE = mkblctrl ]
	then
		set OUTPUT_DEV /dev/mkblctrl0
	else
		set OUTPUT_DEV /dev/pwm_output0
	fi

	if [ $OUTPUT_MODE = uavcan_esc ]
	then
		set OUTPUT_DEV /dev/uavcan/esc
	fi

	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] Failed loading mixer: ${MIXER_FILE}"
		echo "ERROR  [init] Failed loading mixer: ${MIXER_FILE}" >> $LOG_FILE
		tune_control play -t 20
	fi

else
	if [ $MIXER != skip ]
	then
		echo "ERROR  [init] Mixer undefined"
		echo "ERROR  [init] Mixer undefined" >> $LOG_FILE
		tune_control play -t 20
	fi
fi

if [ $MIXER_AUX != none -a $AUX_MODE != none ]
then
	#
	# Load aux mixer.
	#
	if [ -f ${SDCARD_MIXERS_PATH}/${MIXER_AUX}.aux.mix ]
	then
		set MIXER_AUX_FILE ${SDCARD_MIXERS_PATH}/${MIXER_AUX}.aux.mix
	else

		if [ -f /etc/mixers/${MIXER_AUX}.aux.mix ]
		then
			set MIXER_AUX_FILE /etc/mixers/${MIXER_AUX}.aux.mix
		fi
	fi

	if [ $MIXER_AUX_FILE != none ]
	then
		if $OUTPUT_CMD mode_${AUX_MODE}
		then
			# Append aux mixer to main device.
			if param greater SYS_HITL 0
			then
				if mixer append ${OUTPUT_DEV} ${MIXER_AUX_FILE}
				then
					echo "INFO  [init] Mixer: ${MIXER_AUX_FILE} appended to ${OUTPUT_DEV}"
				else
					echo "ERROR  [init] Failed appending mixer: ${MIXER_AUX_FILE}"
					echo "ERROR  [init] Failed appending mixer: ${MIXER_AUX_FILE}" >> $LOG_FILE
				fi
			fi
			if [ -e $OUTPUT_AUX_DEV -a $OUTPUT_MODE != hil ]
			then
				if mixer load ${OUTPUT_AUX_DEV} ${MIXER_AUX_FILE}
				then
					echo "INFO  [init] Mixer: ${MIXER_AUX_FILE} on ${OUTPUT_AUX_DEV}"
				else
					echo "ERROR  [init] Failed loading mixer: ${MIXER_AUX_FILE}"
					echo "ERROR  [init] Failed loading mixer: ${MIXER_AUX_FILE}" >> $LOG_FILE
				fi
			else
				set PWM_AUX_OUT none
				set FAILSAFE_AUX none
			fi
		else
			echo "ERROR: Could not start: pwm_out mode_pwm" >> $LOG_FILE
			tune_control play -t 20
			set PWM_AUX_OUT none
			set FAILSAFE_AUX none
		fi

		# for DShot do not configure pwm values
		if [ $OUTPUT_CMD != dshot ]
		then
			# Set min / max for aux out and rates.
			if [ $PWM_AUX_OUT != none ]
			then
				# Set PWM_AUX output frequency.
				if [ $PWM_AUX_RATE != none ]
				then
					pwm rate -c ${PWM_AUX_OUT} -r ${PWM_AUX_RATE} -d ${OUTPUT_AUX_DEV}
				fi

				# Set disarmed, min and max PWM_AUX values.
				if [ $PWM_AUX_DISARMED != none ]
				then
					pwm disarmed -c ${PWM_AUX_OUT} -p ${PWM_AUX_DISARMED} -d ${OUTPUT_AUX_DEV}
				fi
				if [ $PWM_AUX_MIN != none ]
				then
					pwm min -c ${PWM_AUX_OUT} -p ${PWM_AUX_MIN} -d ${OUTPUT_AUX_DEV}
				fi
				if [ $PWM_AUX_MAX != none ]
				then
					pwm max -c ${PWM_AUX_OUT} -p ${PWM_AUX_MAX} -d ${OUTPUT_AUX_DEV}
				fi
			fi

			#
			# Per channel disarmed settings.
			#
			pwm disarmed -c 1 -p p:PWM_AUX_DIS1 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 2 -p p:PWM_AUX_DIS2 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 3 -p p:PWM_AUX_DIS3 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 4 -p p:PWM_AUX_DIS4 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 5 -p p:PWM_AUX_DIS5 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 6 -p p:PWM_AUX_DIS6 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 7 -p p:PWM_AUX_DIS7 -d ${OUTPUT_AUX_DEV}
			pwm disarmed -c 8 -p p:PWM_AUX_DIS8 -d ${OUTPUT_AUX_DEV}

			#
			# Per channel min settings.
			#
			pwm min -c 1 -p p:PWM_AUX_MIN1 -d ${OUTPUT_AUX_DEV}
			pwm min -c 2 -p p:PWM_AUX_MIN2 -d ${OUTPUT_AUX_DEV}
			pwm min -c 3 -p p:PWM_AUX_MIN3 -d ${OUTPUT_AUX_DEV}
			pwm min -c 4 -p p:PWM_AUX_MIN4 -d ${OUTPUT_AUX_DEV}
			pwm min -c 5 -p p:PWM_AUX_MIN5 -d ${OUTPUT_AUX_DEV}
			pwm min -c 6 -p p:PWM_AUX_MIN6 -d ${OUTPUT_AUX_DEV}
			pwm min -c 7 -p p:PWM_AUX_MIN7 -d ${OUTPUT_AUX_DEV}
			pwm min -c 8 -p p:PWM_AUX_MIN8 -d ${OUTPUT_AUX_DEV}

			#
			# Per channel max settings.
			#
			pwm max -c 1 -p p:PWM_AUX_MAX1 -d ${OUTPUT_AUX_DEV}
			pwm max -c 2 -p p:PWM_AUX_MAX2 -d ${OUTPUT_AUX_DEV}
			pwm max -c 3 -p p:PWM_AUX_MAX3 -d ${OUTPUT_AUX_DEV}
			pwm max -c 4 -p p:PWM_AUX_MAX4 -d ${OUTPUT_AUX_DEV}
			pwm max -c 5 -p p:PWM_AUX_MAX5 -d ${OUTPUT_AUX_DEV}
			pwm max -c 6 -p p:PWM_AUX_MAX6 -d ${OUTPUT_AUX_DEV}
			pwm max -c 7 -p p:PWM_AUX_MAX7 -d ${OUTPUT_AUX_DEV}
			pwm max -c 8 -p p:PWM_AUX_MAX8 -d ${OUTPUT_AUX_DEV}

			if [ $FAILSAFE_AUX != none ]
			then
				pwm failsafe -c ${PWM_AUX_OUT} -p ${FAILSAFE} -d ${OUTPUT_AUX_DEV}
			fi

			#
			# Per channel failsafe settings.
			#
			pwm failsafe -c 1 -p p:PWM_AUX_FAIL1 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 2 -p p:PWM_AUX_FAIL2 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 3 -p p:PWM_AUX_FAIL3 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 4 -p p:PWM_AUX_FAIL4 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 5 -p p:PWM_AUX_FAIL5 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 6 -p p:PWM_AUX_FAIL6 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 7 -p p:PWM_AUX_FAIL7 -d ${OUTPUT_AUX_DEV}
			pwm failsafe -c 8 -p p:PWM_AUX_FAIL8 -d ${OUTPUT_AUX_DEV}
		fi
	fi
fi

if [ $OUTPUT_MODE = pwm_out -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

	#
	# Per channel disarmed settings.
	#
	pwm disarmed -c 1 -p p:PWM_MAIN_DIS1
	pwm disarmed -c 2 -p p:PWM_MAIN_DIS2
	pwm disarmed -c 3 -p p:PWM_MAIN_DIS3
	pwm disarmed -c 4 -p p:PWM_MAIN_DIS4
	pwm disarmed -c 5 -p p:PWM_MAIN_DIS5
	pwm disarmed -c 6 -p p:PWM_MAIN_DIS6
	pwm disarmed -c 7 -p p:PWM_MAIN_DIS7
	pwm disarmed -c 8 -p p:PWM_MAIN_DIS8

	#
	# Per channel min settings.
	#
	pwm min -c 1 -p p:PWM_MAIN_MIN1
	pwm min -c 2 -p p:PWM_MAIN_MIN2
	pwm min -c 3 -p p:PWM_MAIN_MIN3
	pwm min -c 4 -p p:PWM_MAIN_MIN4
	pwm min -c 5 -p p:PWM_MAIN_MIN5
	pwm min -c 6 -p p:PWM_MAIN_MIN6
	pwm min -c 7 -p p:PWM_MAIN_MIN7
	pwm min -c 8 -p p:PWM_MAIN_MIN8

	#
	# Per channel max settings.
	#
	pwm max -c 1 -p p:PWM_MAIN_MAX1
	pwm max -c 2 -p p:PWM_MAIN_MAX2
	pwm max -c 3 -p p:PWM_MAIN_MAX3
	pwm max -c 4 -p p:PWM_MAIN_MAX4
	pwm max -c 5 -p p:PWM_MAIN_MAX5
	pwm max -c 6 -p p:PWM_MAIN_MAX6
	pwm max -c 7 -p p:PWM_MAIN_MAX7
	pwm max -c 8 -p p:PWM_MAIN_MAX8

	if [ $FAILSAFE != none ]
	then
		pwm failsafe -c ${PWM_OUT} -p ${FAILSAFE} -d ${OUTPUT_DEV}
	fi

	#
	# Per channel failsafe settings.
	#
	pwm failsafe -c 1 -p p:PWM_MAIN_FAIL1
	pwm failsafe -c 2 -p p:PWM_MAIN_FAIL2
	pwm failsafe -c 3 -p p:PWM_MAIN_FAIL3
	pwm failsafe -c 4 -p p:PWM_MAIN_FAIL4
	pwm failsafe -c 5 -p p:PWM_MAIN_FAIL5
	pwm failsafe -c 6 -p p:PWM_MAIN_FAIL6
	pwm failsafe -c 7 -p p:PWM_MAIN_FAIL7
	pwm failsafe -c 8 -p p:PWM_MAIN_FAIL8
fi

unset OUTPUT_CMD
unset MIXER_AUX_FILE
unset OUTPUT_AUX_DEV
unset OUTPUT_DEV
