
----------------------------------------------------------------------------------------------------------
-- Special controls and custom datarefs for F8E-Crusader by BERJU83.
----------------------------------------------------------------------------------------------------------

----------------------------------- LOCATE AND/OR CREATE DATAREFS AND COMMANDS -----------------------------------
----ELEC----
Elec_bus_on = find_dataref("sim/cockpit2/electrical/bus_volts[0]")
Batt_ONOFF = find_dataref("sim/cockpit2/electrical/battery_on[0]")
Gen_ONOFF = find_dataref("sim/cockpit2/electrical/generator_on[0]")
Inverter = find_dataref("sim/cockpit2/electrical/inverter_on[0]")
Avionics = find_dataref("sim/cockpit/electrical/avionics_on")

----FUEL MGT----

----STARTUP MGT----
Start_running = find_dataref("sim/operation/prefs/startup_running")
TANK_sel = find_dataref("sim/cockpit2/fuel/fuel_tank_selector")

----Speedbrakes---
SPbrk_switch = create_dataref("custom/F8E/SPbrk_switch","number") --(0=Out, 1=neutral, 2=In)

----WINGS FOLDING---
wings_fold = find_dataref("sim/cockpit2/controls/dihedral_ratio")
wings_fold_latch = find_dataref("sim/flightmodel2/misc/custom_slider_ratio[1]")
wings_fold_SW = find_dataref("sim/flightmodel2/misc/custom_slider_ratio[2]")

----WINGS INCIDENCE and FLAPS---
wing_incid_req = find_dataref("sim/multiplayer/controls/incidence_request[0]")
wing_incid_rat = find_dataref("sim/flightmodel2/controls/incidence_ratio")
wing_incid_latch = find_dataref("sim/cockpit2/switches/custom_slider_on[12]")
wing_incid_unlatch = find_dataref("sim/flightmodel2/misc/custom_slider_ratio[12]")
Flaps_handle = find_dataref("sim/cockpit2/controls/flap_handle_deploy_ratio")

----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")

----BIP---
BIP_SW = create_dataref("custom/F8E/BIP_SW","number")
BIP_red = create_dataref("custom/F8E/BIP_red","number")
BIP_green = create_dataref("custom/F8E/BIP_green","number")
BIP_amber = create_dataref("custom/F8E/BIP_amber","number")
AoA_val = find_dataref("sim/flightmodel2/misc/AoA_angle_degrees")

----BARO Setting---
Baro_inHg_pil = find_dataref("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_pilot")
Baro_inHg_stby = find_dataref("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_stby")
Baro_inHg_copil = find_dataref("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_copilot")

Baro_mBars_pil = create_dataref("custom/F8E/Baro_mBars_pil","number")
Baro_mBars_stby = create_dataref("custom/F8E/Baro_mBars_stby","number")
Baro_mBars_copil = create_dataref("custom/F8E/Baro_mBars_copil","number")

----RADIOS---
Canopy_ratio = find_dataref("sim/cockpit2/switches/canopy_open")
Canopy_handle = find_dataref("sim/cockpit2/switches/custom_slider_on[13]")
 
----MISC---
Ladder_pos = create_dataref("custom/F8E/Ladder_pos","number")
Grounded = find_dataref("sim/flightmodel2/gear/on_ground[1]")
Gspeed = find_dataref("sim/flightmodel/position/groundspeed")
Chocks_vis = create_dataref("custom/F8E/Chocks_vis","number")
Dihedral = find_dataref("sim/flightmodel2/controls/dihedral_ratio")
Avitab_on = find_dataref("avitab/brightness","number")
Deice_stat = find_dataref("sim/cockpit2/ice/ice_static_heat_on_pilot")

----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")

----Lights-----
Beacon = find_dataref("sim/cockpit2/switches/beacon_on")
Strobe = find_dataref("sim/cockpit2/switches/strobe_lights_on")
Anticoll = create_dataref("custom/F8E/Anticoll","number") 

----TRANSPONDER---
xpdr_mode = find_dataref("sim/cockpit2/radios/actuators/transponder_mode")
xpdr_code = find_dataref("sim/cockpit2/radios/actuators/transponder_code")
--------------------------------- VARIABLES ---------------------------------
AoA_amb = 5
AoA_red = 7
Aoa_greL = 4
AoA_greH = 8


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

-----------------------RUNTIME------------------
function flight_start()

    Acc_min = 1
    Acc_max = 1
    Cam_FOV = 90
    Cam_Heading = 180
    Cam_Pitch = -15
    Cam_Roll = 0
    SPbrk_switch = 1 --(init bouton en position 0)
    AP_roll_mode = 0
    AP_yaw_damp = 0
    Avionics = 0
    Anticoll = 0
    
    if Start_running == 0 then
      TANK_sel = 0					
    elseif Start_running == 1 then		
      TANK_sel = 4		
    end	
   
end

function after_physics()

        --(Canopy open-close)--
        if Canopy_handle == 1 then Canopy_ratio = 1
        else Canopy_ratio = 0
        end      
       
        --(Master Elec Power)--
        if Batt_ONOFF == 1 then 
            Gen_ONOFF = 1 Inverter = 1 Avionics = 1
        elseif Batt_ONOFF == 0 then 
            Gen_ONOFF = 0 Inverter = 0 Avionics = 0
        end
        
        --(accelerometre)--
        Acc_negat()
        Acc_posit() 
        
        --(WINGS FOLD)--
        if wings_fold_SW == 1 then wings_fold = 1 else wings_fold = 0
        end
        
        ---(wing incidence and flaps)---
        if Flaps_handle == 1 then wing_incid_latch = 1 
            if wing_incid_unlatch == 1 then wing_incid_req = 1 end
        else wing_incid_latch = 0 wing_incid_req = 0
        end
        
        --(voyants BIP)--
      if Elec_bus_on > 15 then
          if BIP_SW == 1 then BIP_amber = 1 BIP_green = 1 BIP_red = 1
          else BIP_amber = 0 BIP_green = 0 BIP_red = 0
          end
          if BIP_SW == 2 then
              if AoA_amb > AoA_val then BIP_amber = 1 
              else BIP_amber = 0
              end
              if AoA_val > AoA_red then BIP_red = 1
              else BIP_red = 0
              end
              if AoA_val > Aoa_greL and AoA_val < AoA_greH then BIP_green = 1 
              else BIP_green = 0
              end
          end
      else BIP_amber = 0 BIP_green = 0 BIP_red = 0
      end 

         ---(Conversion InHg to mBars)---
        Baro_mBars_pil = Baro_inHg_pil * 33.8639
        Baro_mBars_stby = Baro_inHg_stby * 33.8639
        Baro_mBars_copil = Baro_inHg_copil * 33.8639
        
        --(Canopy Open/Close)--  
      if Canopy_handle > 0.5 then Canopy_Toggle = 1
      else Canopy_Toggle = 0
      end

        --(cales et accessoires)--
      if Elec_bus_on == 0 and Grounded == 1 and Gspeed < 1 and Dihedral == 1 then
        Chocks_vis = 1
      else
        Chocks_vis = 0
      end
      
          --(Transponder)--
      if xpdr_mode == 1 then xpdr_code = 1200
      elseif xpdr_mode == 2 then xpdr_code = xpdr_code
      elseif xpdr_mode > 2 then xpdr_code = 7700
      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

    ---BIP---
function cmd_BIP_on(phase, duration)
	if phase == 0 then 
    BIP_SW = BIP_SW + 1
  end
  if BIP_SW > 2 then BIP_SW = 2
  end
end

function cmd_BIP_off(phase, duration)
	if phase == 0 then 
    BIP_SW = BIP_SW - 1
  end
  if BIP_SW < 0 then BIP_SW = 0
  end
end

----Speedbrakes---
function cmd_SPbrk_out(phase, duration)
	if phase == 0 then 
    SPbrk_switch = 2
    cmdSPbrkmoveout:start()
  end
 	if phase == 2 then 
    SPbrk_switch = 1
    cmdSPbrkmoveout:stop()
  end
end
 
function cmd_SPbrk_In(phase, duration)
	if phase == 0 then 
    SPbrk_switch = 0
    cmdSPbrkmoveIn:start()
  end
 	if phase == 2 then 
    SPbrk_switch = 1
    cmdSPbrkmoveIn:stop()
  end 
end

function cmd_Anticoll_SW_on(phase, duration)
	if phase == 0 then
    Anticoll = 1
    Strobe = 1
    Beacon = 1
	end
end
function cmd_Anticoll_SW_off(phase, duration)
	if phase == 0 then
    Anticoll = 0
    Strobe = 0
    Beacon = 0
	end
end  
	  
------------------------------- LOCATE AND/OR CREATE COMMANDS -------------------------------



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

---COMMANDS BIP---
cmdBIPon = create_command("custom/F8E/BIP_on"," ",cmd_BIP_on)
cmdBIPoff = create_command("custom/F8E/BIP_off"," ",cmd_BIP_off)

---COMMANDS Speedbrakes-------
cmdSPbrksout = create_command("custom/F8E/SPbrk_out","SPbrk increase",cmd_SPbrk_out)
cmdSPbrkIn = create_command("custom/F8E/SPbrk_In","SPbrk decrease",cmd_SPbrk_In)
cmdSPbrkmoveout = find_command("sim/flight_controls/speed_brakes_down_one")
cmdSPbrkmoveIn = find_command("sim/flight_controls/speed_brakes_up_one")

cmdAnticollSWon = create_command("custom/F8E/Anticoll_SW_on","Anticollision",cmd_Anticoll_SW_on)
cmdAnticollSWoff = create_command("custom/F8E/Anticoll_SW_off","Anticollision",cmd_Anticoll_SW_off)