
----------------------------------------------------------------------------------------------------------
-- Special controls and custom datarefs for Nord_3202 by Restauravia.
----------------------------------------------------------------------------------------------------------

----------------------------------- LOCATE AND/OR CREATE DATAREFS AND COMMANDS -----------------------------------
----INIT----
battery_on = find_dataref("sim/cockpit2/electrical/battery_on[0]")
generator_on = find_dataref("sim/cockpit2/electrical/generator_on[0]")
avionics_power_on = find_dataref("sim/cockpit2/switches/avionics_power_on")
prop_pitch_cde = find_dataref("sim/cockpit2/switches/custom_slider_on[7]")
ENG_speed = find_dataref("sim/cockpit2/engine/indicators/engine_speed_rpm[0]")

Instr_lighting = find_dataref("sim/cockpit2/switches/panel_brightness_ratio[0]")
Suction_Turn = find_dataref("sim/cockpit2/switches/custom_slider_on[11]")
Suction_Horizon = find_dataref("sim/cockpit2/switches/custom_slider_on[12]")
Suction_Gyro = find_dataref("sim/cockpit2/switches/custom_slider_on[13]")

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

----FUEL----
mixt_selector = find_dataref("sim/cockpit2/engine/actuators/mixture_ratio_all","number") --(0 for cutoff, 1 max)
mixt_override = find_dataref("sim/operation/override/override_mixture")
mixt_cutoff = create_dataref("custom/N1101/mixt_cutoff_selector","number") -- (0=none,1=mixture cutoff)
fuel_pump_fuse = find_dataref("sim/cockpit2/switches/custom_slider_on[8]")
fuel_pump_sw = find_dataref("sim/cockpit2/switches/custom_slider_on[23]")
fuel_pump_run = find_dataref("sim/cockpit/engine/fuel_pump_on[0]")

----Turn coord----
Low_vac = find_dataref("sim/cockpit2/annunciators/low_vacuum","number")
Turn_suct = find_dataref("sim/cockpit2/switches/custom_slider_on[13]")
Turn_needle_off = create_dataref("custom/N3202/Turn_needle_off","number")
Turn_flag = create_dataref("custom/N3202/Turn_flag","number")

---ADF----
ADF_ON = find_dataref("sim/cockpit2/radios/actuators/adf1_power","number")
ADF1_flip = find_dataref("sim/cockpit2/radios/actuators/adf1_right_is_selected")
ADF2_flip = find_dataref("sim/cockpit2/radios/actuators/adf2_right_is_selected")
ADF_channel1 = create_dataref("custom/N3202/ADF_channel1","number")
ADF_channel2 = create_dataref("custom/N3202/ADF_channel2","number")
ADF_channel3 = create_dataref("custom/N3202/ADF_channel3","number")
ADF_channel4 = create_dataref("custom/N3202/ADF_channel4","number")

----MISC---
Elec_bus_on = find_dataref("sim/cockpit2/electrical/bus_volts[0]")
Grounded = find_dataref("sim/flightmodel2/gear/on_ground[1]")
Gspeed = find_dataref("sim/flightmodel/position/groundspeed")
Cowls = find_dataref("sim/cockpit2/switches/custom_slider_on[5]")

----Retro camera---
Cam_FOV = find_dataref("sim/cockpit2/camera/camera_field_of_view")
Cam_Heading = find_dataref("sim/cockpit2/camera/camera_offset_heading")
Cam_Pitch = find_dataref("sim/cockpit2/camera/camera_offset_pitch")
Cam_Roll = find_dataref("sim/cockpit2/camera/camera_offset_roll")
--------------------------------- DO THIS EACH FLIGHT START ---------------------------------


------------------------------- FUNCTIONS: COMMANDS CALLBACK -------------------------------

-------------RUNTIME--------------

---ADF init----
ADF_channel1 = 1

function flight_start()
    ADF_ON = 0
    Cam_FOV = 160
    Cam_Heading = 180
    Cam_Pitch = 10
    Cam_Roll = 0
end

function after_physics()

        --(accelerometre)--
        Acc_negat()
        Acc_posit() 
        
        ----Turn coord----
        if Low_vac == 1 or Turn_suct == 0 then 
            Turn_flag = 1 
        else Turn_flag = 0
        end
 
         --(capots)--
      if Elec_bus_on == 0 and Grounded == 1 and Gspeed < 1 and ENG_speed < 5 then
        Cowls = 1
      else
        Cowls = 0
      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

-----CDE MANETTE ETOUFFOIR--------------
function cmd_mixt_cutoff(phase, duration)
	if phase == 1 then
      mixt_override = 1
			mixt_cutoff = 1
      mixt_selector = 0
  end
	if phase == 2 then
      mixt_override = 0
			mixt_cutoff = 0
      mixt_selector = mixt_selector
	end	
end

---ADF----
function CMD_ADF1(phase, duration)
	if phase == 0 then
		ADF_channel1 = 1 ADF1_flip = 0
		ADF_channel2 = 0
		ADF_channel3 = 0
		ADF_channel4 = 0
	end		
end
function CMD_ADF2(phase, duration)
	if phase == 0 then
		ADF_channel1 = 0
		ADF_channel2 = 1 ADF1_flip = 1
		ADF_channel3 = 0
		ADF_channel4 = 0
	end		
end
function CMD_ADF3(phase, duration)
	if phase == 0 then
		ADF_channel1 = 0
		ADF_channel2 = 0
		ADF_channel3 = 1 ADF2_flip = 0
		ADF_channel4 = 0
	end		
end
function CMD_ADF4(phase, duration)
	if phase == 0 then
		ADF_channel1 = 0
		ADF_channel2 = 0
		ADF_channel3 = 0
		ADF_channel4 = 1 ADF2_flip = 1
	end		
end
------------------------------- LOCATE AND/OR CREATE COMMANDS -------------------------------

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

---COMMANDE ETOUFFOIR--------------
cmdmixtcutoff = create_command("custom/N1101/mixture_cutoff","Mixture cutoff",cmd_mixt_cutoff)

---ADF----
CMD_ADF1 = create_command("custom/N3202/ADF1","ADF Channel1",CMD_ADF1)
CMD_ADF2 = create_command("custom/N3202/ADF2","ADF Channel2",CMD_ADF2)
CMD_ADF3 = create_command("custom/N3202/ADF3","ADF Channel3",CMD_ADF3)
CMD_ADF4 = create_command("custom/N3202/ADF4","ADF Channel4",CMD_ADF4)