// *****************************************************************************
// * Config_SIOC ver 5.0B5   -     By Manolo Vlez    -    www.opencockpits.com
// *****************************************************************************
// 
// * Editor: Hermann Hummer 
// 
// * FileName : Motorized_Throttle_2022_2.0_PMDG.txt
// * Fr Standard und PMDG jetzt (11/2022) dasselbe Script
// * Der Autopilot wird mit SIOC 7 getrennt gestartet
// * Date : 11.12.2007/24.12.2020/4.2.2021/4.2.2022, 16.8.2022, 4.11.2022
// * PM + PSS Airbus updated
// * some variables shiftet
// * manual Trim enabled when all AP of PM are disengaged
// * Subroutine for motor-lever/trim movement start every 50ms now 
// * Some improvements for AP: eg. when APP is active then V/S is set to -2000
// * PMDG variables changed esp. ALT-HOLD and SPEED-HOLD
// * Pause modus is now dedected, so levers and trim stop in pause mode
// * FeelThereAirbus with relais to connect levers to game port
// * 2nd Mastercard and some inputs added for EFIS
// * FSX Compatibility: Disconnect both LeverPotentiometers from FS when AP is on
// * Variables for AIR-File different in FS9 and FSX
// * Some improvements for LevelD FS9 & FSX
// * FeelThere Airbus and FeelThere Boeing are recognised as different planes now
// * VNAV sets IAS for LevelD
// * Standard-AP OFF switches to Trim by Hand (eg. for FeelThere Airbus)
// * Flusi 9, FSX, P3D, MSFS2020
// * Levermotoren starten nun auch in MSFS2020
// * Wenn ALT_Hold aktiv, dann VerticalSpeedAnzeige auf 0
// * LVLCHG ber Keyboard-Codes realisiert (STRG + ALT + L) (muss in FSUIPC auf virtuellen Joystick zugewiesen werden)
// * VS ber Keyboard-Codes realisiert (STRG + ALT + V) 
// * LED fr VS ber Variable 07EC ansteuern (Read-Only)
// * Variable fr PMDG gendert. PMDG fr FSX luft nun nicht mehr
// * LNAV und VNAV fr PMDG hinzugefg


 
Var 1000, Value 0     // Initialisation




{

 


&Lever_Elevator = TIMER 999 , 0,5 // Subroutine for Lever and Elevator Motors starts every 50ms
}



Var 0004, name 2020_20_PMDG // Version number 
Var 0000, name lever2_motor  // Motor control  

Var 0001, name lever1_motor, value 1 // starts program

Var 0002, name elevator_motor  // Motor control


// for FeelThereAirbus only

Var 0003, name Relais  // Relais switches from upper to lower printed board


// The following variables depend on the used Potentiometers
// You must check these values by using IOCPConsole for each Potentiometer
// 1)for 100 percent thrust: 'LeverX_upper'  2)idle: 'LeverX_idle' 3)reverser: 'LeverX_lower'.
// In my case here the potentiometer values are the following:

Var 0100, name Lever1_upper, Value 121  			// Upper potentiometer value of Lever1
Var 0101, name Lever2_upper, Value 118  			// Upper potentiometer value of Lever2
Var 0102, name Lever1_idle, Value 25  			// Idle potentiometer value of Lever1
Var 0103, name Lever2_idle, Value 24  			// Idle potentiometer value of Lever2
Var 0104, name Lever1_lower, Value 7  			// Lower potentiometer value of Lever1
Var 0105, name Lever2_lower, Value 7   			// Lower potentiometer value of Lever2

// Airbus only:
Var 0106, name Lever1_CL, Value 65  			// CL potentiometer value of Lever1
Var 0107, name Lever2_CL, Value 68   			// CL potentiometer value of Lever2
Var 0108, name Lever1_FLX, Value 90  			// FLX potentiometer value of Lever1
Var 0109, name Lever2_FLX, Value 93   			// FLX potentiometer value of Lever2

Var 0014, name dead_area1     				// to avoid jitter, we define a 'dead area' for the potentiometers
Var 0015, name break_dist1      				// how many points we need for break the motors to zero
Var 0016, name current_break1   				// we can define a break current for the motor
Var 0017, name current_run1     				// Speed for motor when running
Var 0034, name dead_area2        
Var 0035, name break_dist2     
Var 0036, name current_break2     
Var 0037, name current_run2

Var 0041, name dead_area3, Value 8     
Var 0042, name break_dist3, Value 30     
Var 0043, name current_break3, Value 40     
Var 0044, name current_run3, Value 50     		// Speed for motor when big difference



Var 0110, name Lever1_factor  				// Distance FS / PointPot
Var 0111, name Lever2_factor  				// Apply Distance_Pot to Distance FS  
Var 0112, name Pot1_factor  					// Distance Pot / PointPot
Var 0113, name Pot2_factor  					// Apply Distance_Pot to Distance Lever 

Var 0038, name Flusi, Link FSUIPC_IN, Offset $3308, Length 2	// what Flusi-version is in use?
 
{
  &dead_area1 = 5     			// to avoid jitter, we define a 'dead area' for the potentiometers
  &break_dist1 = 8      		// how many points we need for break the motors to zero
  &current_break1 = 60   		// we can define a break current for the motor
  &current_run1 = 80     		// Speed for motor when running
  &dead_area2 = 5        
  &break_dist2 = 8     
  &current_break2 = 60     
  &current_run2 = 80
}

Var 0005, name Leverdiscon, Link FSUIPC_OUT, Offset $310A, Length 1	// Disconnect both LeverPotentiometers from FS

// Var 0045, name Lever1_from_FS, Link FSUIPC_OUT, Offset $3330, Length 2
// Var 0046, name Lever2_from_FS, Link FSUIPC_OUT, Offset $3332, Length 2


Var 0060, name CL_PSS 
Var 0061, name FLX_PSS
Var 0062, name TOGA_PSS



// read 16 letters of the name of the airplane form AIR-file
// to see if it is a standard- or non-standard plane
Var 0140, name AIRFILE1, Link FSUIPC_IN, Offset $3c09, Length 1          // 1st letter of Airplane name in FS9
Var 0141, name AIRFILE2, Link FSUIPC_IN, Offset $3c0a, Length 1          // 2nd letter of Airplane name
Var 0142, name AIRFILE3, Link FSUIPC_IN, Offset $3c0b, Length 1          // 3rd letter of Airplane name
Var 0143, name AIRFILE4, Link FSUIPC_IN, Offset $3c0c, Length 1          // 4th letter of Airplane name
Var 0144, name AIRFILE5, Link FSUIPC_IN, Offset $3c0d, Length 1          // 5th letter of Airplane name
Var 0145, name AIRFILE6, Link FSUIPC_IN, Offset $3d00, Length 1          // 1st letter of Airplane name in FSX
Var 0146, name AIRFILE7, Link FSUIPC_IN, Offset $3d01, Length 1          // 2nd letter of Airplane name
Var 0147, name AIRFILE8, Link FSUIPC_IN, Offset $3d02, Length 1          // 3rd letter of Airplane name
Var 0148, name AIRFILE9, Link FSUIPC_IN, Offset $3d03, Length 1          // 4th letter of Airplane name
Var 0149, name AIRFILE10, Link FSUIPC_IN, Offset $3d04, Length 1          // 5th letter of Airplane name
Var 0150, name AIRFILE11, Link FSUIPC_IN, Offset $3d05, Length 1          // 6th letter of Airplane name
Var 0151, name AIRFILE12, Link FSUIPC_IN, Offset $3d06, Length 1          // 7th letter of Airplane name
Var 0152, name AIRFILE13, Link FSUIPC_IN, Offset $3d07, Length 1          // 8th letter of Airplane name
Var 0153, name AIRFILE14, Link FSUIPC_IN, Offset $3d08, Length 1          // 8th letter of Airplane name
Var 0154, name AIRFILE15, Link FSUIPC_IN, Offset $3d09, Length 1          // 10th letter of Airplane name
Var 0155, name AIRFILE16, Link FSUIPC_IN, Offset $3d0a, Length 1          // 11thletter of Airplane name
Var 0156, name AIRFILE17, Link FSUIPC_IN, Offset $3d0b, Length 1          // 12letter of Airplane name
Var 0157, name AIRFILE18, Link FSUIPC_IN, Offset $3d0c, Length 1          // 13thletter of Airplane name
Var 0158, name AIRFILE19, Link FSUIPC_IN, Offset $3d0d, Length 1          // 14th letter of Airplane name
Var 0159, name AIRFILE20, Link FSUIPC_IN, Offset $3d0e, Length 1          // 15thletter of Airplane name
Var 0160, name AIRFILE21, Link FSUIPC_IN, Offset $3d0f, Length 1          // 16th letter of Airplane name

// Variables for LevelD
Var 0081, name IAS_Lvld
Var 0082, name VS_Lvld
Var 0083, name ALT_Lvld 
Var 0084, name APP_Lvld 
Var 0085, name N1_Lvld
Var 0086, name SPD_Lvld
Var 0087, name VNAV_Lvld

// Variables for PM  Project Magenta
Var 0070, name IAS_PM
Var 0071, name VS_PM
Var 0072, name ALT_PM 
Var 0073, name APP_PM 
Var 0074, name N1_PM
Var 0075, name SPD_PM
Var 0076, name AT_PM
Var 0077, name AP_L_PM
Var 0078, name AP_C_PM
Var 0079, name AP_R_PM

Var 0114, name LOC_PM
Var 0115, name LNAV_PM
Var 0116, name VNAV_PM
Var 0117, name HDG_PM
Var 0118, name FLCH_PM
Var 0119, name MACH_PM



// Standard- or Non-Standard Planes?
Var 0057, name PMDG									 // PMDG
Var 0056, name LVLD									 // LevelD
Var 0058, name PSS									 // PSS Airbus
Var 0059, name PF	 									 // Project Fokker
Var 0054, name Std									 // Standard Plane
Var 0055, name PM 									 // Project Magenta
Var 0063, name FeelThere 								 // FeelThere Airbus
Var 0064, name APL_Lvld 								 // Autopilot L LevelD
Var 0065, name APC_Lvld  								 // Autopilot C LevelD
Var 0066, name APR_Lvld  								 // Autopilot R LevelD
Var 0067, name FLCH_Lvld  								 // FlightLevelChange LevelD

// LevelD: read MPC status from FSCONV 5.5. 
// Thanks to Nico Kaan for his great work!
// www.nicokaan.nl

Var 0092, name LvlD_Flg, Link FSUIPC_IN, Offset $8BAC, Length 4  		 
{
 &APL_Lvld = TESTBIT &LvlD_Flg, 2
 &APC_Lvld = TESTBIT &LvlD_Flg, 3 
 &APR_Lvld = TESTBIT &LvlD_Flg, 4
 &VNAV_Lvld = TESTBIT &LvlD_Flg, 9
 &FLCH_Lvld = TESTBIT &LvlD_Flg, 10
 &VS_Lvld = TESTBIT &LvlD_Flg, 12
 &ALT_Lvld = TESTBIT &LvlD_Flg, 13
 &APP_Lvld = TESTBIT &LvlD_Flg, 15
 &N1_Lvld = TESTBIT &LvlD_Flg, 17
 &SPD_Lvld = TESTBIT &LvlD_Flg, 18
}

Var 0095, name PMDG_Flg, Link FSUIPC_IN, Offset $62BC, Length 4  		 
{
 // &VS_PMDG = TESTBIT &PMDG_Flg, 24
 // &ALT_PMDG = TESTBIT &PMDG_Flg, 23
// &APP_PMDG = TESTBIT &PMDG_Flg, 21
 // &N1_PMDG = TESTBIT &PMDG_Flg, 15
 // &SPD_PMDG = TESTBIT &PMDG_Flg, 16
 // &CMD_A_PMDG = TESTBIT &PMDG_Flg, 8
// &CMD_B_PMDG = TESTBIT &PMDG_Flg, 9
 // &CWS_A_PMDG = TESTBIT &PMDG_Flg, 25
 // &CWS_B_PMDG = TESTBIT &PMDG_Flg, 26

}


// *** For ProjectMagenta Users:

Var 0080, name PM_Flg, Link FSUIPC_IN, Offset $04F0, Length 2  		 
{
 &IAS_PM = TESTBIT &PM_Flg, 9			
 &VS_PM = TESTBIT &PM_Flg, 2
 &ALT_PM = TESTBIT &PM_Flg, 3
 &APP_PM = TESTBIT &PM_Flg, 4
 &N1_PM = TESTBIT &PM_Flg, 10
 &SPD_PM = TESTBIT &PM_Flg, 9
 &AT_PM = TESTBIT &PM_Flg, 11
 &AP_L_PM = TESTBIT &PM_Flg, 0
 &AP_C_PM = TESTBIT &PM_Flg, 1
 &AP_R_PM = TESTBIT &PM_Flg, 13
 &LOC_PM = TESTBIT &PM_Flg, 5
 &LNAV_PM = TESTBIT &PM_Flg, 6
 &VNAV_PM = TESTBIT &PM_Flg, 14
 &HDG_PM = TESTBIT &PM_Flg, 7
 &FLCH_PM = TESTBIT &PM_Flg, 8
 &MACH_PM = TESTBIT &PM_Flg, 15

}

// Variables for PMDG


Var 0091, name IAS_PMDG, Link FSUIPC_IN, Offset $65dc, Length 1          	// PMDG:is IAS_HOLD active? 
Var 0088, name ALT_PMDG, Link FSUIPC_IN, Offset $65e3, Length 1       		// PMDG: is ALT hold active?
Var 0089, name AP_PMDG, Link FSUIPC_IN, Offset $65e5, Length 1	 		// PMDG: is AP active 
Var 0090, name VS_PMDG, Link FSUIPC_IN, Offset $65e4, Length 1	 	 	// PMDG: is V/S active?
Var 0046, name IASMACH_PMDG, Link FSUIPC_IN, Offset $65C4, Length 4		// PMDG: Speed vom MCP
Var 0093, name APP_PMDG, Link FSUIPC_IN, Offset $65e2, Length 1

Var 0094, name CMD_B_PMDG, Link FSUIPC_IN, Offset $65e7, Length 1
Var 0045, name N1_PMDG, Link FSUIPC_IN, Offset $65DB, Length 1
Var 0098, name SPD_PMDG, Link FSUIPC_IN, Offset $65dC, Length 1
Var 0121, name LNAV_PMDG, Link FSUIPC_IN, Offset $65E0, Length 1
Var 0122, name VNAV_PMDG, Link FSUIPC_IN, Offset $65DD, Length 1

Var 0047, name IAS                           		      		 // is IAS_HOLD active?
Var 0049, name ALT_hold									 // ist ALT_HOLD active?
Var 0050, name APP									 // Autopilot APP-mode
Var 0051, name N1									 	 // Autopilot N1-mode
Var 0052, name SPD									 // Autopilot Speed-hold-mode
Var 0053, name VS										 // Autopilot V/S-mode

// Variables for Standard Planes
Var 0019, name Pause, Link FSUIPC_IN, Offset $0264, Length 2		 // is Pause mode active?
Var 0021, name ALT_STD, Link FSUIPC_IN, Offset $07D0, Length 4        	 // Standard planes: AutoPilot ALT hold active?
Var 0022, name IAS_STD, Link FSUIPC_IN, Offset $07DC, Length 4           // Standard planes: is IAS_HOLD active?
Var 0029, name APP_STD, Link FSUIPC_IN, Offset $07FC, Length 4           // is Glideslope hold active?(GS and APP exchanged!!)
Var 0030, name TOGA, Link FSUIPC_IN, Offset $080C, Length 4             // is TO/GA switch active?    
Var 0048, name MACH, Link FSUIPC_IN, Offset $07E4, Length 4              // is MACH_HOLD active?
Var 0039, name AP, Link FSUIPC_IN, Offset $07bc, Length 4            // is AutoPilot MasterSwitch active? Att: not in PSS & PM!!
// Var 0039, name AP
Var 0040, name GS, Link FSUIPC_IN, Offset $0800, Length 4                // is ApproachMode active? (GS and APP exchanged!!)

Var 0020, name Trim_to_FS, Link FSUIPC_OUT, Offset $0BC0, Length 2       // Send value of PitchTrim to FS
Var 0023, name Lever2_to_FS, Link FSUIPC_OUT, Offset $0932, Length 2     // Send value of Lever2 to FS
Var 0024, name Lever1_to_FS, Link FSUIPC_OUT, Offset $089A, Length 2     // Send value of Lever1 to FS
Var 0031, name EngineNumbers, Link FSUIPC_IN, Offset $0AEC, Length 2     // how many engines has the plane?
Var 0032, name Lever3_to_FS, Link FSUIPC_OUT, Offset $09CA, Length 2     // Send value of Lever3 to FS
Var 0033, name Lever4_to_FS, Link FSUIPC_OUT, Offset $0A62, Length 2     // Send value of Lever4 to FS
Var 0097, name LEVER_START								 // Motor for levers should start
Var 0096, name ELEV_START								 // Motor for elevator trim should start
 

Var 0068, name lever2_diff     // difference where the Lever2 is and where it should be
Var 0069, name lever1_diff     // difference where the Lever1 is and where it should be
Var 0027, name corr_Trim_pos   // Where ElevTrim should be (corrected to potentiometer values)
Var 0028, name trim_diff       // difference where the Trim is and where it should be

Var 0013, Link FSUIPC_IN, name Lever1_target, Offset $088c, Length 2     // 88c  Where Lever1 should be


{

// becouse the potentiometer differ in value, we align the potentiometer value to the leverlength
  L0 = &Lever1_upper - &Lever1_idle      // Upper Potentiomer Value - Idle Potentiomer Value
  L1 = 12800 / L0                        // Distance Pot = 128 * 100 becouse in SIOC are no decimal values possible
  
  &Pot1_factor = L1

  L0 = &Lever1_Pot - &Lever1_idle        // Corrected value from Pot to FS
  L1 = L0 * &Pot1_factor                 // apply length lever to pot
  L1 = L1 / 100                          // same as above, becouse of no decimal value possible
  L2 = &Lever1_target / 128              // value from FSUIPC must be adjusted to pot value
  &Lever1_diff = L1 - L2                 // where the lever is and where it should be
   
IF &LEVER_START = 1                      // A/T is on
  {
//   IF &VNAV_PMDG = 0
//    {
     &Leverdiscon = 192				// Disconnect both LeverPotentiometers from FS
//    }
   CALL &MOTOR_LEVER                      // subroutine for motor motion

  }
}

Var 0012, Link FSUIPC_IN, name Lever2_target, Offset $0924, Length 2     // 924   Where Lever2 should be

{
  L0 = &Lever2_upper - &Lever2_idle      // Upper Potentiomer Value - Idle Potentiomer Value
  L1 = 12800 / L0                        // Distance Pot = 128
  
  &Pot2_factor = L1

  L0 = &Lever2_Pot - &Lever2_idle        // Corrected value from Pot to FS
  L1 = L0 * &Pot2_factor
  L1 = L1 / 100
  L2 = &Lever1_target / 128
  &Lever2_diff = L1 - L2
 
IF &LEVER_START = 1                      // A/T is on
  {
  // &Leverdiscon = 192
   CALL &MOTOR_LEVER                      // subroutine for motor2 motion
  }
}

Var 0018, Link FSUIPC_IN, name Trim_target, Offset $0BC2, Length 2     // Where Elevator Trim should be
{
  IF &Trim_target > 33000
  {
    L0 = 81918 - &Trim_target     		// Trim down  (49153 .. 65535)
    L1 = L0 / 128     				// 65535 + 16383 = 81918
    L1 = TRUNC L1
  }
  ELSE
  {
    L0 = 16383 - &Trim_target     		// Trim up  (0 .. 16383)
    L1 = L0 / 128
    L1 = TRUNC L1
  }
  &corr_Trim_pos = L1    
  &trim_diff = &corr_Trim_pos - &Trim_Pot


}


Var 0006, name Lever1_Pot, Link USB_ANALOGIC, Input 1, PosL 0, PosC 127, PosR 254     // Lever1 Pot position 
{
  L0 = &Lever1_upper - &Lever1_idle 	// Upper Potentiomer Value - Idle Potentiomer Value
  L1 = 16383 / L0   				// Distance FS = 16383
  L1 = TRUNC L1
  &Lever1_factor = L1

  L0 = &Lever1_Pot - &Lever1_idle    	// Corrected value from Pot to FS
  L1 = L0 * &Lever1_factor
  L1 = TRUNC L1
  IF &LEVER_START = 0     			// A/T is OFF
    {
     &lever1_motor = 0     			// motor stops
     &Leverdiscon = 0				// Connect both LeverPotentiometers to FS
    }      
    IF &EngineNumbers <= 2			// here we look how many engines the plane has
    {
     &Lever1_to_FS = L1         		// Send Lever1 Position to FS
    }
    IF &EngineNumbers = 4
    {
     &Lever1_to_FS = L1         		// Send Lever1 Position to FS
     &Lever2_to_FS = L1         		// Send Lever2 Position to FS
    }
     
}

Var 0007, name Lever2_Pot, Link USB_ANALOGIC, Input 2, PosL 0, PosC 127, PosR 254     // Lever2 Pot position 
{
  L0 = &Lever2_upper - &Lever2_idle // Upper Potentiomer Value - Idle Potentiomer Value
  L1 = 16383 / L0   // Distance FS = 16383
  L1 = TRUNC L1
  &Lever2_factor = L1

  L0 = &Lever2_Pot - &Lever2_idle     	// Corrected value from Pot to FS
  L1 = L0 * &Lever2_factor
  L1 = TRUNC L1
  IF &LEVER_START = 0     			// A/T is OFF
   {
    &lever2_motor = 0    			// motor stops
    &Leverdiscon = 0  				// Connect both LeverPotentiometers to FS      
    }
IF &EngineNumbers <= 2 			// here we look how many engines the plane has
    {
    &Lever2_to_FS = L1         		// Send Lever2 Position to FS
    }
     IF &EngineNumbers = 4
    {
     &Lever3_to_FS = L1         		// Send Lever3 Position to FS
     &Lever4_to_FS = L1         		// Send Lever4 Position to FS
    }
   
}

Var 0008, name Trim_Pot, Link USB_ANALOGIC, Input 3, PosL 0, PosC 128, PosR 254     // Trim Potentiometer position
{
 L0 = &Trim_Pot * 128
 L1 = 16383 - L0

 IF &ELEV_START = 0		// Trimwheel steered by hand                      
  {
   &Trim_to_FS = L1    		// send Trimwheel to FS
   &elevator_motor = 0   	// Elevator motor stops        
  }
}

Var 0120, name Lever_Elevator, Link SUBRUTINE // Subroutine for Lever and Trim Motor movement

{
// Dedection if Standard- or Non-Standard-Plane

&PM = 0
IF &PM_Flg >= 1
  {
   &PM = 1			// PM started
  }

IF &PM = 0
{
  &PMDG = 0
  IF &AIRFILE6 = 80
   {
    IF &AIRFILE7 = 77
    {
     IF &AIRFILE8 = 68
      {
       IF &AIRFILE9 = 71						
       {
         &PMDG = 1					// Plane is PMDG
       }
      }
     }
   }

 &lvld = 0
    IF &Flusi = 7					// FS2004
     {	
     IF &AIRFILE1 = 76				// l
      {
      IF &AIRFILE2 = 86				// v
       {
        IF &AIRFILE3 = 76				// l
        {
         IF &AIRFILE4 = 68				// d						
          {
           &lvld = 1				      // Plane is LevelD767
          }						
        }
       }
      }
     }
   
    IF &Flusi >= 8					// FSX, P3D, 2020
     {
     IF &AIRFILE7 = 101				// E
      {
      IF &AIRFILE8 = 118				// V
       {
        IF &AIRFILE9 = 101				// E
        {
         IF &AIRFILE10 = 108			// L						
         {
           &lvld = 1					// Plane is LevelD767
         }
        }
       }
      }
     }



  &PSS = 0
  IF &AIRFILE1 = 80						// P
   {
    IF &AIRFILE2 = 115					// S
    {
     IF &AIRFILE3 = 115					// S
     {
      IF &AIRFILE4 = 65					// A						
      {
       &PSS = 1							// Plane is PSS Airbus (PSSA)
      }
     }
    }
   }

  &FeelThere = 0
    IF &Flusi = 7						// FS2004
     {
     IF &AIRFILE13 = 114					// r
      {
      IF &AIRFILE14 = 101					// e
       {
        IF &AIRFILE15 = 32
        {
         IF &AIRFILE16 = 65					// A						
          {
           &FeelThere = 1				     // Plane is FeelThere Airbus
          }						
        }
       }
      }
     }
   
    IF &Flusi >= 8					// FSX, P3D, 2020
     {
     IF &AIRFILE13 = 114					// r
      {
      IF &AIRFILE14 = 101					// e
       {
        IF &AIRFILE15 = 32
        {
         IF &AIRFILE16 = 65					// A						
         {
           &FeelThere = 1				// Plane is FeelThere Airbus
         }
        }
       }
      }
     }


  IF &PMDG = 1 						// PMDG Airplane  
   {

    &IAS = &IAS_PMDG
    &N1  = &N1_PMDG
    &ALT_hold = &ALT_PMDG				// AP of PMDG often switches off ALT_HOLD so we use STD instead

    IF &IAS_PMDG >= 1
     {
     &IAS = 1
     }
     ELSE
     {
     &IAS = 0
     }   		 
 				 
    &APP = &APP_PMDG					// ist APP_PMDG APP 
    &VS = &VS_PMDG
    // &AP = &AP_PMDG
    &SPD = &SPD_PMDG
   
    IF &AP_PMDG >= 1
     {
     &AP = 1
     }
     ELSE
     {
     &AP = 0
     }  

    IF &ALT_PMDG >= 1
     {
     &ALT_hold = 1
     }
     ELSE
     {
     &ALT_hold = 0
     }   		 
   }

  IF &LVLD = 1 							// LevelD Airplane  
   {
    &IAS = &IAS_Lvld 				 
    &ALT_hold = &ALT_Lvld
    &APP = &APP_Lvld
    &N1 = &N1_Lvld
    &SPD = &SPD_Lvld
    &VS = &VS_Lvld
    &IAS = &VNAV_Lvld
   }

  IF &PSS = 1 							// PSS Aibus
   {
    &IAS = &IAS_STD 						// In this time all are Standard			 
    &ALT_hold = &ALT_STD					// but shortly we will find adresses for PSS
    &APP = &GS							// PSS uses GS instead of APP (or vice versa?)		 
   }

  IF &Std = 1           					 // Standard Airplane
    {
     &IAS = &IAS_STD  				 
     &ALT_hold = &ALT_STD
     &APP = &APP_STD 			 
    }
}

IF &PM = 1           					 	// Project Magenta
  {
   &IAS = &IAS_PM 									 
   &N1 = &N1_PM
   &SPD = &SPD_PM
   &ALT_hold = &ALT_STD		// MCP often switches here to 0 but it should remain 1 !! So we use STD and not PM
   &APP = &APP_PM
   &VS = &VS_PM
  }

&Std = 0
IF &PMDG = 0
 { 
 IF &LVLD = 0
  {
  IF &PSS = 0
   {
   IF &PF = 0
    {
    IF &PM = 0
       {
        &Std = 1			// FeelThere in the moment is standard plane
       }
    }
   }
  }
 }
 									 
    


// Start Relais for FeelThere

IF &FeelThere = 1
 {
  &Relais = 120
 }
ELSE
 {
  &Relais = 0
 }



// now we look if the levermotors should  start or stop
  
    &LEVER_START = 0
    IF &Pause = 0 
     {
      IF &PSS = 0			// Airbus has no Motorized Throttles  !!
       {
        IF &FeelThere = 0
         {    
          &LEVER_START = 1
         }
       }
     }
  
 
IF &IAS = 0
  {
   IF &IAS_PMDG = 0
    {
     IF &MACH = 0
      {
       IF &TOGA = 0
      
	 {
	  IF &FLCH_Lvld = 0	// When FLCH is set, LevelD switches SPD to 0, so Throttles would stop
          {
           IF &N1 = 0
            {
             IF &SPD = 0
              {
   	       IF &LNAV_PMDG = 0
	        {
		 IF &VNAV_PMDG = 0
		  {
		   &LEVER_START = 0
                 }
               }
             }  
	   }
         }
	 
       }
     }
    }
   }      

// now we look if the elevator motor should  start or stop

  IF &Pause = 0 
    {
     IF &AP = 1
      {
       &ELEV_START = 1
      }
     ELSE
      {
       &ELEV_START = 0
      }
 
    }
  ELSE
    {
     &ELEV_START = 0
    }


   
   

    IF &ALT_hold = 0				// ALT hold is on
     {
     IF &APP = 0					// APP hold is on
      {
       IF &VS = 0					// V/S mode is on
        {
         IF &AP_L_PM = 0       		// all AP of PM are off
          {
           IF &AP_C_PM = 0       		
            {
             IF &AP_R_PM = 0       		
              {
   	         IF &AP_PMDG = 0 		// Autopilot PMDG is off      		  
                {
		      IF &APL_LvlD = 0       // Autopilot LevlD is off       		
                    {
                     IF &APC_LvlD = 0       		
                      {
                       IF &APR_LvlD = 0       		  
//                        {
//				 IF &AP = 0		// Autopilot Standard is off
				  {
                          &ELEV_START = 0		// Elevator Motor stop
			        }
			      }
//			    }
                    }
			}
		    
              }
            } 
          }
        }
      }
    }      
     

  IF &ELEV_START = 1                       // Elevator Motor should start
  {
   CALL &Motor_Elevator				// subroutine for motor motion
  }
}



Var 0125, name Motor_Lever, Link SUBRUTINE     // USB_DC controls motors for Lever1&2

// Motor control  0-127 = left; 129-255 =Right; 0 stops 
{
  L0 = &lever1_diff    
  L1 = 0    
  IF L0 < 0
   {
    L1 = 128 				// Motor run direction   
   }
  L0 = ABS L0
  L2 = &current_run1 + L1		// Lever1 Motor runs with full current
  IF L0 <= &break_dist1
   {
    L2 = &current_break1 + L1		// Lever1 Motor runs with break current
   }
  L0 = ABS L0
  IF L0 <= &dead_area1
   {
    L2 = 0    
   }
  IF &LEVER_START = 0
   {
    L2 = 0  		   		// Lever1 Motor stops
   }

&lever1_motor = L2 
  
  L0 = &lever2_diff    
  L1 = 128    
  IF L0 < 0
   {
    L1 = 0    
   }
  L0 = ABS L0
  L2 = &current_run2 + L1		// Lever2 Motor runs with full current
  IF L0 <= &break_dist2
   {
    L2 = &current_break2 + L1       // Lever2 Motor runs with break current
   }
  L0 = ABS L0
  IF L0 <= &dead_area2
   {
    L2 = 0    
   }
  IF &LEVER_START = 0 
   {
    L2 = 0    				// Lever2 Motor stops                       
   }
  
&lever2_motor = L2    
}


Var 0130, name Motor_Elevator, Link SUBRUTINE     // USB_DC controls motor for Elevator
// Motor control  0-127=left, 128 stops, 129-255=right
{
  L0 = &trim_diff    
  IF &Flusi = 7						  // FS2004
   {
    IF L0 < 0
     {
      L1 = 128
     } 
     ELSE
     {
      L1 = 0
     }
   }
  IF &Flusi >= 8						  // FSX, P3D, 2020
   {
    IF L0 > 0
     {
      L1 = 0
     }   
    ELSE
     {
      L1 = 128
     } 
   }
 
 
  L0 = ABS L0
  L2 = &current_break3 + L1
  IF L0 <= &break_dist3
  {
    L2 = &current_run3 + L1
  }
  L0 = ABS L0
  IF L0 <= &dead_area3
  {
    L2 = 0    
  }
   

  IF &ELEV_START = 0     // Motor Elevator trim stop
      {
        L2 = 0 
      }   
  &elevator_motor = L2    
}


// *** End of TQ



