
----------------------------------------------------------------------------------------------------------
-- Special controls and custom datarefs for A200S-Ximango by BERJU83.
----------------------------------------------------------------------------------------------------------

----------------------------------- LOCATE AND/OR CREATE DATAREFS -----------------------------------
----ELEC Mgt----
Soarbus_on = find_dataref("sim/cockpit2/electrical/cross_tie")
Elec_switch = create_dataref("custom/A200S/Elec_switch","number") --(0=Soaring bus, 1=Off, 2=Main bus)
Mainbus_on = find_dataref("sim/cockpit2/electrical/battery_on[0]")
Elec_bus = find_dataref("sim/cockpit2/electrical/bus_volts[0]")

----FUEL MGT----
Fuel_tksel = find_dataref("sim/cockpit2/fuel/fuel_tank_selector")
Fuel_handle = create_dataref("custom/A200S/Fuel_handle","number") --(0=lft, 1=none, 2=rgt)

----Fuel indicator---
FuelQty_left = find_dataref("sim/cockpit2/fuel/fuel_quantity[0]")
FuelQty_right = find_dataref("sim/cockpit2/fuel/fuel_quantity[1]")
FuelN_left = create_dataref("custom/A200S/FuelN_left","number")
FuelN_right = create_dataref("custom/A200S/FuelN_right","number")
Fuel_test = find_dataref("sim/cockpit2/switches/custom_slider_on[1]")
FuelLow_left = create_dataref("custom/A200S/FuelLow_left","number")
FuelLow_right = create_dataref("custom/A200S/FuelLow_right","number")

----Magnetos----
Mag_pos = find_dataref("sim/cockpit2/engine/actuators/ignition_on[0]") -- (0=off, 1=left, 2=right, 3=both)
Mag_left = create_dataref("custom/A200S/Mag_left","number")
Mag_right = create_dataref("custom/A200S/Mag_right","number")

----ACCELEROM---
Acc_needle = find_dataref("sim/flightmodel/forces/g_nrml")
Acc_min	= create_dataref("custom/A200S/forces/Acc_min" ,"number")
Acc_max	= create_dataref("custom/A200S/forces/Acc_max", "number")

----Switches---
SW_Turnco = create_dataref("custom/A200S/SW_Turnco","number")
Fail_Turnco = find_dataref("sim/operation/failures/rel_ss_tsi")

SW_Horiz = create_dataref("custom/A200S/SW_Horiz","number")
Fail_Horiz = find_dataref("sim/operation/failures/rel_ss_ahz")

SW_Gyro = create_dataref("custom/A200S/SW_Gyro","number")
Fail_Gyro = find_dataref("sim/operation/failures/rel_ss_dgy")

SW_Vario = create_dataref("custom/A200S/SW_Vario","number")

----Prop mgt---
Startup_running = find_dataref("sim/operation/prefs/startup_running")
Prop_Clutch = find_dataref("sim/cockpit2/switches/clutch_engage")
Prop_Ratio = find_dataref("sim/cockpit2/engine/actuators/prop_ratio[0]") -- (0=min, 1= Max)
Eng_RPM = find_dataref("sim/cockpit2/engine/indicators/engine_speed_rpm[0]")
Prop_mask = find_dataref("sim/flightmodel2/engines/prop_disc/override[0]")
Prop_lever = create_dataref("custom/A200S/Prop_lever","number")  --(0=feather, 1=cruise pitch, 2=min pitch)

----MISC---
Gear_test = create_dataref("custom/A200S/Gear_test_dr","number")
Avitab_on = find_dataref("avitab/brightness","number")
Choke = find_dataref("sim/cockpit2/engine/actuators/idle_speed[0]")
Panel_light = find_dataref("sim/cockpit2/switches/panel_brightness_ratio[0]")

--------------------- VARIABLES ---------------------------------


-----------------------INIT------------------
function flight_start()
    Elec_switch = 1
    Panel_light = 0.1
    Choke = 0
    Acc_min = 1
    Acc_max = 1
    Fuel_handle = 1
    SW_Turnco = 1
    SW_Horiz = 1
    SW_Gyro = 1
    Fuel_tksel = 0
    Mag_left = 0
    Mag_right =0
    
   if Startup_running ==1 then Elec_switch =2 Mag_left =1 Mag_right =2 Prop_mod =2 Fuel_handle = 0 Fuel_tksel = 1 end
end
----------------------------------------------------
function after_physics()

    ---main switch init---
    if Elec_switch == 1 then Mainbus_on = 0 Soarbus_on = 0 end
    
	  --(accelerometre)--
    Acc_negat()
    Acc_posit()
    
	 ---Magnetos---
	 Mag_pos = Mag_left + Mag_right
	 
	 ---Fuel indicator needles---
	if Elec_bus > 0.0 and Fuel_test == 0 then
          FuelN_left = FuelQty_left
          FuelN_right = FuelQty_right
  elseif Elec_bus > 0.0 and Fuel_test == 1 then
          FuelN_left = 40     
          FuelN_right = 40
	else
		FuelN_left = 0
		FuelN_right = 0
	end
	
	---Fuel indicator lights---
	if Elec_bus > 0.0 then
        if FuelQty_left < 10 or Fuel_test == 1 then FuelLow_left = 1 else FuelLow_left = 0 end
        if FuelQty_right < 10 or Fuel_test == 1 then FuelLow_right = 1 else FuelLow_right = 0 end
  end	
  
   ---Prop mgt---
    if Prop_mod == 0 and Eng_RPM < 1 then Prop_Clutch = 0 Prop_Ratio = 0 Prop_mask = 1 Prop_lever = 0 end  ---(feather)
    if Prop_mod == 2 then Prop_Clutch = 1 Prop_Ratio = 0.2 Prop_mask = 0 Prop_lever = 2 end   ---(Min pitch)
    if Prop_mod == 1 then Prop_Clutch = 1 Prop_Ratio = 1 Prop_mask = 0 Prop_lever = 1 end ---(Cruise pitch)
  	
end

------------------------------- FUNCTIONS: COMMANDS CALLBACK -------------------------------
----Elec Buses---
function cmd_Main_SW(phase, duration)
	if phase == 0 then 
    Elec_switch = Elec_switch + 1
    Mainbus_on = 1
    Soarbus_on = 1 
  end
  if Elec_switch > 2 then Elec_switch = 2
  end

end

function cmd_Soar_SW(phase, duration)
	if phase == 0 then 
    Elec_switch = Elec_switch - 1
    Mainbus_on = 1 
    Soarbus_on = 0
  end
  if Elec_switch < 0 then Elec_switch = 0
  end

end  

     ---FUEL MGT--- 
function cmd_Fuel_lft(phase, duration)
	if phase == 0 then 
    Fuel_handle = 0 Fuel_tksel = 1
  end
end 
function cmd_Fuel_none(phase, duration)
	if phase == 0 then 
    Fuel_handle = 1 Fuel_tksel = 0
  end
end
function cmd_Fuel_rgt(phase, duration)
	if phase == 0 then 
    Fuel_handle = 2 Fuel_tksel = 3
  end
end


  ----Magnetos---
function cmd_Mag_lft_off(phase, duration)
	if phase == 0 then 
    Mag_left = 0
  end
end      
function cmd_Mag_lft_on(phase, duration)
	if phase == 0 then 
    Mag_left = 1
  end
end

function cmd_Mag_rgt_off(phase, duration)
	if phase == 0 then 
    Mag_right = 0
  end
end      
function cmd_Mag_rgt_on(phase, duration)
	if phase == 0 then 
    Mag_right = 2
  end
end         
        
  ----ACCELEROM---

function Acc_negat()
	if Acc_needle < Acc_min and Acc_needle ~= 1 then
		Acc_min = Acc_needle
	end
end 

function Acc_posit()
	if Acc_needle > Acc_max then
		Acc_max = Acc_needle
	end
end

function cmd_Needle_RAZ(phase, duration)
	if phase == 0 then
		Acc_min = 1
		Acc_max = 1
	end		
end

---Switches---
function cmd_Turnco_off(phase, duration)
	if phase == 0 then 
    SW_Turnco = 0
    Fail_Turnco = 6
  end
end      
function cmd_Turnco_on(phase, duration)
	if phase == 0 then 
    SW_Turnco = 1
    Fail_Turnco = 0
  end
end    

function cmd_Horiz_off(phase, duration)
	if phase == 0 then 
    SW_Horiz = 0
    Fail_Horiz = 6
  end
end      
function cmd_Horiz_on(phase, duration)
	if phase == 0 then 
    SW_Horiz = 1
    Fail_Horiz = 0
  end
end   

function cmd_Gyro_off(phase, duration)
	if phase == 0 then 
    SW_Gyro = 0
    Fail_Gyro = 6
  end
end      
function cmd_Gyro_on(phase, duration)
	if phase == 0 then 
    SW_Gyro = 1
    Fail_Gyro = 0
  end
end

function cmd_Vario_off(phase, duration)
	if phase == 0 then 
    SW_Vario = 0
  end
end      
function cmd_Vario_on(phase, duration)
	if phase == 0 then 
    SW_Vario = 1
  end
end     

    ---MISC---
function cmd_Gear_test(phase, duration)
	if phase == 1 and Elec_Mainbus_on > 15 then 
    Gear_test = 1
    else Gear_test = 0
  end
end 

    ---PROP MGT---
function cmd_Feather(phase, duration)
	if phase == 0 then 
    Prop_mod = 0
  end
end      
function cmd_Unfeather(phase, duration)
	if phase == 0 then 
    Prop_mod = 2
  end
end
function cmd_MaxPitch(phase, duration)
	if phase == 0 then 
    Prop_mod = 1
  end
end  

------------------------------- LOCATE AND/OR CREATE COMMANDS -------------------------------
---bus elec-------
cmdMainSW = create_command("custom/A200S/MainSW","Select Electrical bus",cmd_Main_SW)
cmdSoarSW = create_command("custom/A200S/SoarSW","Select Electrical bus",cmd_Soar_SW)

---Magnetos---
cmdMafLefton = create_command("custom/A200S/cmd_Mag_lft_on","Right magneto",cmd_Mag_lft_on)
cmdMafLeftoff = create_command("custom/A200S/cmd_Mag_lft_off","Right magneto",cmd_Mag_lft_off)
cmdMafRighton = create_command("custom/A200S/cmd_Mag_rgt_on","Right magneto",cmd_Mag_rgt_on)
cmdMafRightoff = create_command("custom/A200S/cmd_Mag_rgt_off","Right magneto",cmd_Mag_rgt_off)

---FUEL MGT---
cmdFuelrgt = create_command("custom/A200S/cmd_Fuel_rgt","Right tank",cmd_Fuel_rgt)
cmdFuelnone = create_command("custom/A200S/cmd_Fuel_none","Fuel cutoff",cmd_Fuel_none)
cmdFuellft = create_command("custom/A200S/cmd_Fuel_lft","Left tank",cmd_Fuel_lft)


 ----ACCELEROM---
CMDNeedleRAZ	= create_command("custom/A200S/cmd_Needle_RAZ","RAZ Accelerometre",cmd_Needle_RAZ)

---Switches---
cmdTurncoff = create_command("custom/A200S/cmd_Turnco_off", "Turn coordinator off",cmd_Turnco_off)
cmdTurncon = create_command("custom/A200S/cmd_Turnco_on", "Turn coordinator on",cmd_Turnco_on)

cmdHorizoff = create_command("custom/A200S/cmd_Horiz_off", "Horizon off",cmd_Horiz_off)
cmdHorizon = create_command("custom/A200S/cmd_Horiz_on", "Horizon on",cmd_Horiz_on)

cmdGyroff = create_command("custom/A200S/cmd_Gyro_off", "Directional off",cmd_Gyro_off)
cmdGyron = create_command("custom/A200S/cmd_Gyro_on", "Directional on",cmd_Gyro_on)

cmdVarioff = create_command("custom/A200S/cmd_Vario_off", "Directional off",cmd_Vario_off)
cmdVarion = create_command("custom/A200S/cmd_Vario_on", "Directional on",cmd_Vario_on)

 ---MISC---
cmdGeartest = create_command("custom/A200S/Gear_test","Gear test",cmd_Gear_test)

----Prop mgt---
cmdFeather = create_command("custom/A200S/cmd_Feather", "Prop Feather",cmd_Feather)
cmdUnfeather = create_command("custom/A200S/cmd_Unfeather", "Prop UnFeather",cmd_Unfeather)
cmdMaxPitch = create_command("custom/A200S/cmd_MaxPitch", "Prop Cruise pitch",cmd_MaxPitch)