RSS Git Download  Clone
Raw Blame History
print(string.format("############ CoPilotFunctions - PLANE_ICAO: %s",  PLANE_ICAO))

-- default TCAS selector mode [N]ormal
local MODE = "N"

local LandingLightsOff = false
local LandingLightsOn  = false

local SoundLevel = 0.2

local Pass10KLightsOn_Sound  = load_WAV_file(SCRIPT_DIRECTORY .. "vSpeed_Sounds/Pass10000LightsOn.wav")
local Pass10KLightsOff_Sound = load_WAV_file(SCRIPT_DIRECTORY .. "vSpeed_Sounds/Pass10000LightsOff.wav")

set_sound_gain(Pass10KLightsOn_Sound,  SoundLevel)
set_sound_gain(Pass10KLightsOff_Sound, SoundLevel)

-- ToLiss Airbus & FF A359 only // temporarily disabled A350 pending clarification of datarefs
-- if PLANE_ICAO == "A319" or PLANE_ICAO == "A20N" or PLANE_ICAO == "A321" or PLANE_ICAO == "A21N" or PLANE_ICAO == "A359" then
if PLANE_ICAO == "A319" or PLANE_ICAO == "A20N" or PLANE_ICAO == "A321" then
	dataref( "iAS",  "sim/cockpit2/gauges/indicators/airspeed_kts_pilot", "readonly")
	dataref( "iALT", "sim/cockpit2/gauges/indicators/altitude_ft_pilot",  "readonly")
	dataref( "VSI",  "sim/cockpit2/gauges/indicators/vvi_fpm_pilot",      "readonly")
	dataref( "iGS",  "sim/flightmodel/position/groundspeed",              "readonly")
	dataref( "TGT",  "sim/cockpit/autopilot/altitude",                    "readonly")
	dataref( "TCAS", "AirbusFBW/XPDRTCASMode",	    				      "readonly")

	dataref( "BEACON",   "ckpt/oh/beaconLight/anim",      	              "readonly")
	dataref( "ENG_MODE", "AirbusFBW/ENGModeSwitch",					      "readonly")
	dataref( "XPONDER",  "AirbusFBW/XPDRPower",      		              "readonly")

	if PLANE_ICAO == "A359" then
		LIGHTS = dataref_table("sim/cockpit2/switches/landing_lights_switch") -- also on A3xx
	else
		LIGHTS = dataref_table("AirbusFBW/OHPLightSwitches") -- no data entries on A359, try above
	end

	do_every_frame("main()")
end

function main()
	set_landing_lights()
	set_beacon()
	set_strobe()
	-- A359 doesn't need manual TCAS mode selector
	if PLANE_ICAO ~= "A359" then
		set_transponder()
		set_tcas_mode_selector()
	end
end

function log_msg(str) -- custom log function
	local temp = os.date("*t", os.time())
	print(string.format("CoPilotFunctions: %02d:%02d:%02d %s", temp.hour, temp.min, temp.sec, str))
end

-- set strobe & landing lights on if thrust levers in FLX or TOGA position and on the ground 
function set_strobe()
    local TLA = XPLMGetDatavf(XPLMFindDataRef("AirbusFBW/throttle_input"), 0, 1)
    local iThrustLevers = TLA[0] -- thrust levers (left lever representing both)
		--log_msg("iThrustLevers: " .. iThrustLevers)

	local sThrustLevers = "IDLE"
	if math.abs(iThrustLevers - 0.88) < 0.05
		then sThrustLevers = "FLX"
    elseif math.abs(iThrustLevers - 1.0) < 0.05
		then sThrustLevers = "TOGA"
	end
		--log_msg("sThrustLevers: " .. sThrustLevers)
	local on_ground = XPLMFindDataRef("sim/flightmodel/forces/fnrml_gear")
	if XPLMGetDataf(on_ground) > 1.0 then iOnGround = 1 else iOnGround = 0 end

	if ( sThrustLevers == "FLX" or sThrustLevers == "TOGA" ) and iOnGround == 1 then
		if ( not StrobeOn ) then
			set("ckpt/oh/strobeLight/anim", 2) -- 2=on, 1=auto, 0=off
			StrobeOn = true
		end
		-- LandingLightsOn is likely to be false if on ground
		if ( not LandingLightsOn ) then
			switch_landing_lights_on()
		end
	end
end

-- beacon on if engine mode selector to ign/start and beacon not already on
function set_beacon()
	--log_msg("ENG_MODE" .. ENG_MODE)
	--log_msg("BEACON" .. BEACON)
	if ENG_MODE == 2 and BEACON ~= 1 then
		set("ckpt/oh/beaconLight/anim", 1)
	end
end

-- set transponder to mode C if moving > 5kts
function set_transponder()
	gs = iGS * 1.9438445 -- ground speed [kts]

	if gs > 5 then
		if XPONDER ~= 4 then
			-- set("ckpt/transponder/mode/anim", 0) -- sets switch but not ECAM display	
			set("AirbusFBW/XPDRPower", 4)
		end
		if TCAS ~= 1 then
			set("AirbusFBW/XPDRTCASMode", 1)
		end
	end
end

function set_taxi_lights(val) -- 0=off, 1=on, 2=take-off
	set("ckpt/oh/taxiLight/anim", val)
end

function set_landing_lights()
	-- switch lights off if passing 10K ft and climbing and not already off
	if ( math.floor(iALT) >= 10001 ) and ( math.floor(iALT) <= 10010 ) and ( math.floor(VSI) ) > 0 and ( not LandingLightsOff ) then
		switch_landing_lights_off()
		set_taxi_lights(0)
		play_sound(Pass10KLightsOff_Sound)
	end

	-- switch lights on if passing 10K ft and descending and not already on
	if ( math.floor(iALT) <= 9999 ) and ( math.floor(iALT) >= 9990 ) and ( math.floor(VSI) < 0 ) and ( not LandingLightsOn ) then
		switch_landing_lights_on()
		set_taxi_lights(1)
		play_sound(Pass10KLightsOn_Sound)
	end
end

function switch_landing_lights_off()
	if PLANE_ICAO == "A319" or PLANE_ICAO == "A20N" or PLANE_ICAO == "A321" or PLANE_ICAO == "A21N" then
		LIGHTS[4] = 1   -- Left landing off
		LIGHTS[5] = 1   -- Right landing off
	elseif PLANE_ICAO == "A359" then
		LIGHTS[0] = 0   -- Left landing off
		LIGHTS[1] = 0   -- Right landing off
	end

	LandingLightsOff = true
	LandingLightsOn  = false
	log_msg("switched landing lights off")
end

function switch_landing_lights_on()
	if PLANE_ICAO == "A319" or PLANE_ICAO == "A20N" or PLANE_ICAO == "A321" or PLANE_ICAO == "A21N" then
		LIGHTS[4] = 2   -- Left landing on
		LIGHTS[5] = 2   -- Right landing on
	elseif PLANE_ICAO == "A359" then
		LIGHTS[0] = 1   -- Left landing on
		LIGHTS[1] = 1   -- Right landing on
	end
	
	LandingLightsOn  = true
	LandingLightsOff = false
	log_msg("switched landing lights on")
end

function set_tcas_mode_selector()
	-- if we are moving >50kts
	if math.floor(iAS) > 50 then
		-- if below TGT altitude by more than 100 feet and climbing with vertical rate > +100fpm, set mode to ABV if not already:
		if math.floor(iALT) < ( TGT - 100 ) and math.floor(VSI) > 100 then
			if MODE ~= "ABV" then
				MODE = "ABV"
				set("AirbusFBW/XPDRTCASAltSelect", 0)
				log_msg( string.format("%.0f", iALT) .. "ft set TCAS mode ABV")
			end
		-- if above TGT altitude and decending with vertical rate > -100fpm, set mode to BLW if not already
		elseif math.floor(iALT) > ( TGT + 100 ) and math.floor(VSI) < -100 then
			if MODE ~= "BLW" then
				MODE = "BLW"
				set("AirbusFBW/XPDRTCASAltSelect", 2)
				log_msg( string.format("%.0f", iALT) .. "ft set TCAS mode BLW")
			end
		-- else vertical speed between +100 & -100 fpm, set mode to position 'N'
		elseif ( MODE ~= "N" ) then
			MODE = "N"
			set("AirbusFBW/XPDRTCASAltSelect", 1)
			log_msg( string.format("%.0f", iALT) .. "ft set TCAS mode N")
		end
	end
end

--[[ todo:
	set taxi & r/w turnoff lights on if >500ft and gear down [AirbusFBW/GearLever = ?]
-]]