From 10efd065c442cf714c5693e58b08546efe5c4ab6 Mon Sep 17 00:00:00 2001
From: mfranz <mfranz>
Date: Sun, 4 Feb 2007 12:55:28 +0000
Subject: [PATCH] Dave Perry & Roy Vegard Ovesen:

"This patch allows all the AC using the kap140 to use one kap140.nas
which includes Roy's move to "bool" values as well as the check for
power used by the pa24-250." (dp)
---
 Aircraft/Generic/kap140.nas               | 1695 +++++++++++++++++++++
 Aircraft/Generic/limits.nas               |    2 +-
 Aircraft/Instruments/KAP140TwoAxis.xml    |   48 +-
 Aircraft/Instruments/KAP140TwoAxisAlt.xml |   86 +-
 Aircraft/Instruments/fuel-c172s.xml       |    2 +-
 Aircraft/c172p/Systems/KAP140.xml         |   22 +-
 Aircraft/c172p/c172p-set.xml              |    5 +-
 Aircraft/c172p/kap140.nas                 | 1520 ------------------
 8 files changed, 1776 insertions(+), 1604 deletions(-)
 create mode 100644 Aircraft/Generic/kap140.nas
 delete mode 100644 Aircraft/c172p/kap140.nas

diff --git a/Aircraft/Generic/kap140.nas b/Aircraft/Generic/kap140.nas
new file mode 100644
index 000000000..ea068d9e7
--- /dev/null
+++ b/Aircraft/Generic/kap140.nas
@@ -0,0 +1,1695 @@
+##
+# Bendix/King KAP140 Autopilot System
+# Tries to behave like the Bendix/King KAP140 autopilot
+# two axis w/altitude preselect.
+#
+# One would also need the autopilot configuration file
+# KAP140.xml and the panel instrument configuration file
+#
+# Written by Roy Vegard Ovesen
+# "Power-check" edits by Dave Perry
+##
+
+# Properties
+
+locks = "/autopilot/KAP140/locks";
+settings = "/autopilot/KAP140/settings";
+annunciators = "/autopilot/KAP140/annunciators";
+internal = "/autopilot/internal";
+power="/systems/electrical/outputs/autopilot";
+
+# locks
+propLocks = props.globals.getNode(locks, 1);
+
+lockAltHold   = propLocks.getNode("alt-hold", 1);
+lockAprHold   = propLocks.getNode("apr-hold", 1);
+lockGsHold    = propLocks.getNode("gs-hold", 1);
+lockHdgHold   = propLocks.getNode("hdg-hold", 1);
+lockNavHold   = propLocks.getNode("nav-hold", 1);
+lockRevHold   = propLocks.getNode("rev-hold", 1);
+lockRollAxis  = propLocks.getNode("roll-axis", 1);
+lockRollMode  = propLocks.getNode("roll-mode", 1);
+lockPitchAxis = propLocks.getNode("pitch-axis", 1);
+lockPitchMode = propLocks.getNode("pitch-mode", 1);
+lockRollArm   = propLocks.getNode("roll-arm", 1);
+lockPitchArm  = propLocks.getNode("pitch-arm", 1);
+
+
+rollModes     = { "OFF" : 0, "ROL" : 1, "HDG" : 2, "NAV" : 3, "REV" : 4, "APR" : 5 };
+pitchModes    = { "OFF" : 0, "VS" : 1, "ALT" : 2, "GS" : 3 };
+rollArmModes  = { "OFF" : 0, "NAV" : 1, "APR" : 2, "REV" : 3 };
+pitchArmModes = { "OFF" : 0, "ALT" : 1, "GS" : 2 };
+
+# settings
+propSettings = props.globals.getNode(settings, 1);
+
+settingTargetAltPressure    = propSettings.getNode("target-alt-pressure", 1);
+settingTargetInterceptAngle = propSettings.getNode("target-intercept-angle", 1);
+settingTargetPressureRate   = propSettings.getNode("target-pressure-rate", 1);
+settingTargetTurnRate       = propSettings.getNode("target-turn-rate", 1);
+settingTargetAltFt          = propSettings.getNode("target-alt-ft", 1);
+settingBaroSettingInhg      = propSettings.getNode("baro-setting-inhg", 1);
+settingBaroSettingHpa       = propSettings.getNode("baro-setting-hpa", 1);
+
+#annunciators
+propAnnunciators = props.globals.getNode(annunciators, 1);
+
+annunciatorRol          = propAnnunciators.getNode("rol", 1);
+annunciatorHdg          = propAnnunciators.getNode("hdg", 1);
+annunciatorNav          = propAnnunciators.getNode("nav", 1);
+annunciatorNavArm       = propAnnunciators.getNode("nav-arm", 1);
+annunciatorApr          = propAnnunciators.getNode("apr", 1);
+annunciatorAprArm       = propAnnunciators.getNode("apr-arm", 1);
+annunciatorRev          = propAnnunciators.getNode("rev", 1);
+annunciatorRevArm       = propAnnunciators.getNode("rev-arm", 1);
+annunciatorVs           = propAnnunciators.getNode("vs", 1);
+annunciatorVsNumber     = propAnnunciators.getNode("vs-number", 1);
+annunciatorFpm          = propAnnunciators.getNode("fpm", 1);
+annunciatorAlt          = propAnnunciators.getNode("alt", 1);
+annunciatorAltArm       = propAnnunciators.getNode("alt-arm", 1);
+annunciatorAltNumber    = propAnnunciators.getNode("alt-number", 1);
+annunciatorAltAlert     = propAnnunciators.getNode("alt-alert", 1);
+annunciatorApr          = propAnnunciators.getNode("apr", 1);
+annunciatorGs           = propAnnunciators.getNode("gs", 1);
+annunciatorGsArm        = propAnnunciators.getNode("gs-arm", 1);
+annunciatorPtUp         = propAnnunciators.getNode("pt-up", 1);
+annunciatorPtDn         = propAnnunciators.getNode("pt-dn", 1);
+annunciatorBsHpaNumber  = propAnnunciators.getNode("bs-hpa-number", 1);
+annunciatorBsInhgNumber = propAnnunciators.getNode("bs-inhg-number", 1);
+annunciatorAp           = propAnnunciators.getNode("ap", 1);
+annunciatorBeep         = propAnnunciators.getNode("beep", 1);
+
+navRadio = "/instrumentation/nav";
+encoder =  "/instrumentation/encoder";
+staticPort = "/systems/static";
+
+annunciator = annunciatorAp;
+annunciatorState = 0;
+flashInterval = 0.0;
+flashCount = 0.0;
+flashTimer = -1.0;
+
+pressureUnits = { "inHg" : 0, "hPa" : 1 };
+baroSettingUnit = pressureUnits["inHg"];
+baroSettingInhg = 29.92;
+baroSettingHpa = baroSettingInhg * 0.03386389;
+baroSettingAdjusting = 0;
+baroButtonDown = 0;
+baroTimerRunning = 0;
+
+altPreselect = 0;
+altButtonTimerRunning = 0;
+altButtonTimerIgnore = 0;
+altAlertOn = 0;
+altCaptured = 0;
+
+valueTest = 0;
+lastValue = 0;
+newValue = 0;
+minVoltageLimit = 8.0;
+
+flasher = func {
+  flashTimer = -1.0;
+  annunciator = arg[0];
+  flashInterval = arg[1];
+  flashCount = arg[2] + 1;
+  annunciatorState = arg[3];
+
+  flashTimer = 0.0;
+
+  flashAnnunciator();
+}
+
+flashAnnunciator = func {
+  #print(annunciator.getName());
+  #print("FI:", flashInterval);
+  #print("FC:", flashCount);
+  #print("FT:", flashTimer);
+
+  ##
+  # If flashTimer is set to -1 then flashing is aborted
+  if (flashTimer < -0.5)
+  {
+    ##print ("flash abort ", annunciator);
+    annunciator.setBoolValue(0);
+    return;
+  }
+
+  if (flashTimer < flashCount)
+  {
+    #flashTimer = flashTimer + 1.0;
+    if (annunciator.getValue() == 1)
+    {
+      annunciator.setBoolValue(0);
+      settimer(flashAnnunciator, flashInterval / 2.0);
+    }
+    else
+    {
+      flashTimer = flashTimer + 1.0;
+      annunciator.setBoolValue(1);
+      settimer(flashAnnunciator, flashInterval);
+    }
+  }
+  else
+  {
+    flashTimer = -1.0;
+    annunciator.setBoolValue(annunciatorState);
+  }
+}
+
+
+ptCheck = func {
+  ##print("pitch trim check");
+
+  if (lockPitchMode.getValue() == pitchModes["OFF"])
+  {
+    annunciatorPtUp.setBoolValue(0);
+    annunciatorPtDn.setBoolValue(0);
+    return;
+  }
+
+  else
+  {
+    elevatorControl = getprop("/controls/flight/elevator");
+    ##print(elevatorControl);
+
+    # Flash the pitch trim up annunciator
+    if (elevatorControl < -0.01)
+    {
+      if (annunciatorPtUp.getValue() == 0)
+      {
+        annunciatorPtUp.setBoolValue(1);
+      }
+      elsif (annunciatorPtUp.getValue() == 1)
+      {
+        annunciatorPtUp.setBoolValue(0);
+      }
+    }
+    # Flash the pitch trim down annunciator
+    elsif (elevatorControl > 0.01)
+    {
+      if (annunciatorPtDn.getValue() == 0)
+      {
+        annunciatorPtDn.setBoolValue(1);
+      }
+      elsif (annunciatorPtDn.getValue() == 1)
+      {
+        annunciatorPtDn.setBoolValue(0);
+      }
+    }
+
+    else
+    {
+      annunciatorPtUp.setBoolValue(0);
+      annunciatorPtDn.setBoolValue(0);
+    }
+  }
+
+  settimer(ptCheck, 0.5);
+}
+
+
+apInit = func {
+  ##print("ap init");
+
+  ##
+  # Initialises the autopilot.
+  ##
+
+  lockAltHold.setBoolValue(0);
+  lockAprHold.setBoolValue(0);
+  lockGsHold.setBoolValue(0);
+  lockHdgHold.setBoolValue(0);
+  lockNavHold.setBoolValue(0);
+  lockRevHold.setBoolValue(0);
+  lockRollAxis.setBoolValue(0);
+  lockRollMode.setIntValue(rollModes["OFF"]);
+  lockPitchAxis.setBoolValue(0);
+  lockPitchMode.setIntValue(pitchModes["OFF"]);
+  lockRollArm.setIntValue(rollArmModes["OFF"]);
+  lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+#  Reset the memory for power down or power up
+  altPreselect = 0;
+  baroSettingInhg = 29.92;
+  settingBaroSettingInhg.setDoubleValue(baroSettingInhg);
+  settingBaroSettingHpa.setDoubleValue(baroSettingInhg * 0.03386389);
+  settingTargetAltFt.setDoubleValue(altPreselect);
+  settingTargetAltPressure.setDoubleValue(0.0);
+  settingTargetInterceptAngle.setDoubleValue(0.0);
+  settingTargetPressureRate.setDoubleValue(0.0);
+  settingTargetTurnRate.setDoubleValue(0.0);
+
+  annunciatorRol.setBoolValue(0);
+  annunciatorHdg.setBoolValue(0);
+  annunciatorNav.setBoolValue(0);
+  annunciatorNavArm.setBoolValue(0);
+  annunciatorApr.setBoolValue(0);
+  annunciatorAprArm.setBoolValue(0);
+  annunciatorRev.setBoolValue(0);
+  annunciatorRevArm.setBoolValue(0);
+  annunciatorVs.setBoolValue(0);
+  annunciatorVsNumber.setBoolValue(0);
+  annunciatorFpm.setBoolValue(0);
+  annunciatorAlt.setBoolValue(0);
+  annunciatorAltArm.setBoolValue(0);
+  annunciatorAltNumber.setBoolValue(0);
+  annunciatorGs.setBoolValue(0);
+  annunciatorGsArm.setBoolValue(0);
+  annunciatorPtUp.setBoolValue(0);
+  annunciatorPtDn.setBoolValue(0);
+  annunciatorBsHpaNumber.setBoolValue(0);
+  annunciatorBsInhgNumber.setBoolValue(0);
+  annunciatorAp.setBoolValue(0);
+  annunciatorBeep.setBoolValue(0);
+
+#  settimer(altAlert, 5.0);
+}
+
+
+apPower = func {
+
+## Monitor autopilot power
+## Call apInit if the power is too low
+
+  if (getprop(power) < minVoltageLimit) {
+    newValue = 0;
+  } else {
+    newValue = 1;
+  }
+
+  valueTest = newValue - lastValue;
+#  print("v_test = ", v_test);
+  if (valueTest > 0.5){
+    # autopilot just powered up
+    print("power up");
+    apInit();
+    altAlert();
+  } elsif (valueTest < -0.5) {
+    # autopilot just lost power
+    print("power lost");
+    apInit();
+    annunciatorAltAlert.setBoolValue(0);
+    # note: all button and knobs disabled in functions below
+  }
+  lastValue = newValue;
+  settimer(apPower, 0.5);
+}
+
+apButton = func {
+  ##print("apButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  ##
+  # Engages the autopilot in Wings level mode (ROL) and Vertical speed hold
+  # mode (VS).
+  ##
+  if (lockRollMode.getValue() == rollModes["OFF"] and
+      lockPitchMode.getValue() == pitchModes["OFF"])
+  {
+    flashTimer = -1.0;
+
+    lockAltHold.setBoolValue(0);
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(0);
+    lockNavHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["ROL"]);
+    lockPitchAxis.setBoolValue(1);
+    lockPitchMode.setIntValue(pitchModes["VS"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+    lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+
+    annunciatorRol.setBoolValue(1);
+    annunciatorVs.setBoolValue(1);
+    annunciatorVsNumber.setBoolValue(1);
+
+    settingTargetTurnRate.setDoubleValue(0.0);
+
+    ptCheck();
+
+    pressureRate = getprop(internal, "pressure-rate");
+    #print(pressureRate);
+    fpm = -pressureRate * 58000;
+    #print(fpm);
+    if (fpm > 0.0)
+    {
+      fpm = int(fpm/100 + 0.5) * 100;
+    }
+    else
+    {
+      fpm = int(fpm/100 - 0.5) * 100;
+    }
+    #print(fpm);
+
+    settingTargetPressureRate.setDoubleValue(-fpm / 58000);
+
+    if (altButtonTimerRunning == 0)
+    {
+      settimer(altButtonTimer, 3.0);
+      altButtonTimerRunning = 1;
+      altButtonTimerIgnore = 0;
+      annunciatorAltNumber.setBoolValue(0);
+    }
+  }
+  ##
+  # Disengages all modes.
+  ##
+  elsif (lockRollMode.getValue() != rollModes["OFF"] and
+         lockPitchMode.getValue() != pitchModes["OFF"])
+  {
+    flashTimer = -1.0;
+
+    lockAltHold.setBoolValue(0);
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(0);
+    lockNavHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(0);
+    lockRollMode.setIntValue(rollModes["OFF"]);
+    lockPitchAxis.setBoolValue(0);
+    lockPitchMode.setIntValue(pitchModes["OFF"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+    lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+
+    settingTargetAltPressure.setDoubleValue(0.0);
+    settingTargetInterceptAngle.setDoubleValue(0.0);
+    settingTargetPressureRate.setDoubleValue(0.0);
+    settingTargetTurnRate.setDoubleValue(0.0);
+
+    annunciatorRol.setBoolValue(0);
+    annunciatorHdg.setBoolValue(0);
+    annunciatorNav.setBoolValue(0);
+    annunciatorNavArm.setBoolValue(0);
+    annunciatorApr.setBoolValue(0);
+    annunciatorAprArm.setBoolValue(0);
+    annunciatorRev.setBoolValue(0);
+    annunciatorRevArm.setBoolValue(0);
+    annunciatorVs.setBoolValue(0);
+    annunciatorVsNumber.setBoolValue(0);
+    annunciatorAlt.setBoolValue(0);
+    annunciatorAltArm.setBoolValue(0);
+    annunciatorAltNumber.setBoolValue(0);
+    annunciatorApr.setBoolValue(0);
+    annunciatorGs.setBoolValue(0);
+    annunciatorGsArm.setBoolValue(0);
+    annunciatorPtUp.setBoolValue(0);
+    annunciatorPtDn.setBoolValue(0);
+
+    flasher(annunciatorAp, 1.0, 5, 0);
+  }
+}
+
+
+hdgButton = func {
+  ##print("hdgButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  ##
+  # Engages the heading mode (HDG) and vertical speed hold mode (VS). The
+  # commanded vertical speed is set to the vertical speed present at button
+  # press.
+  ##
+  if (lockRollMode.getValue() == rollModes["OFF"] and
+      lockPitchMode.getValue() == pitchModes["OFF"])
+  {
+    flashTimer = -1.0;
+
+    lockAltHold.setBoolValue(0);
+    lockAprHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["HDG"]);
+    lockPitchAxis.setBoolValue(1);
+    lockPitchMode.setIntValue(pitchModes["VS"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+    lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+
+    annunciatorHdg.setBoolValue(1);
+    annunciatorAlt.setBoolValue(0);
+    annunciatorApr.setBoolValue(0);
+    annunciatorGs.setBoolValue(0);
+    annunciatorNav.setBoolValue(0);
+    annunciatorVs.setBoolValue(1);
+    annunciatorVsNumber.setBoolValue(1);
+
+    settingTargetInterceptAngle.setDoubleValue(0.0);
+
+    ptCheck();
+
+    pressureRate = getprop(internal, "pressure-rate");
+    fpm = -pressureRate * 58000;
+    #print(fpm);
+    if (fpm > 0.0)
+    {
+      fpm = int(fpm/100 + 0.5) * 100;
+    }
+    else
+    {
+      fpm = int(fpm/100 - 0.5) * 100;
+    }
+    #print(fpm);
+
+    settingTargetPressureRate.setDoubleValue(-fpm / 58000);
+
+    if (altButtonTimerRunning == 0)
+    {
+      settimer(altButtonTimer, 3.0);
+      altButtonTimerRunning = 1;
+      altButtonTimerIgnore = 0;
+      annunciatorAltNumber.setBoolValue(0);
+    }
+  }
+  ##
+  # Switch from ROL to HDG mode, but don't change pitch mode.
+  ##
+  elsif (lockRollMode.getValue() == rollModes["ROL"])
+  {
+    lockAprHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["HDG"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+
+    annunciatorApr.setBoolValue(0);
+    annunciatorGs.setBoolValue(0);
+    annunciatorHdg.setBoolValue(1);
+    annunciatorNav.setBoolValue(0);
+    annunciatorRol.setBoolValue(0);
+    annunciatorRev.setBoolValue(0);
+
+    settingTargetInterceptAngle.setDoubleValue(0.0);
+  }
+  ##
+  # Switch to HDG mode, but don't change pitch mode.
+  ##
+  elsif ( (lockRollMode.getValue() == rollModes["NAV"] or
+         lockRollArm.getValue() == rollArmModes["NAV"] or
+         lockRollMode.getValue() == rollModes["REV"] or
+         lockRollArm.getValue() == rollArmModes["REV"]) and
+         flashTimer < -0.5)
+  {
+    lockAprHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["HDG"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+
+    annunciatorApr.setBoolValue(0);
+    annunciatorGs.setBoolValue(0);
+    annunciatorHdg.setBoolValue(1);
+    annunciatorNav.setBoolValue(0);
+    annunciatorRol.setBoolValue(0);
+    annunciatorRev.setBoolValue(0);
+    annunciatorNavArm.setBoolValue(0);
+
+    settingTargetInterceptAngle.setDoubleValue(0.0);
+  }
+  ##
+  # If we already are in HDG mode switch to ROL mode. Again don't touch pitch
+  # mode.
+  ##
+  elsif (lockRollMode.getValue() == rollModes["HDG"])
+  {
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["ROL"]);
+
+    annunciatorApr.setBoolValue(0);
+    annunciatorGs.setBoolValue(0);
+    annunciatorHdg.setBoolValue(0);
+    annunciatorNav.setBoolValue(0);
+    annunciatorRol.setBoolValue(1);
+
+    settingTargetTurnRate.setDoubleValue(0.0);
+  }
+  ##
+  # If we are in APR mode we also have to change pitch mode.
+  # TODO: Should we switch to VS or ALT mode? (currently VS)
+  ##
+  elsif ( (lockRollMode.getValue() == rollModes["APR"] or
+         lockRollArm.getValue() == rollArmModes["APR"] or
+         lockPitchMode.getValue() == pitchModes["GS"] or
+         lockPitchArm.getValue() == pitchArmModes["GS"]) and
+         flashTimer < -0.5)
+  {
+    lockAltHold.setBoolValue(0);
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["HDG"]);
+    lockPitchAxis.setBoolValue(1);
+    lockPitchMode.setIntValue(pitchModes["VS"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+    lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+
+    annunciatorAlt.setBoolValue(0);
+    annunciatorAltArm.setBoolValue(0);
+    annunciatorHdg.setBoolValue(1);
+    annunciatorRol.setBoolValue(0);
+    annunciatorNav.setBoolValue(0);
+    annunciatorApr.setBoolValue(0);
+    annunciatorAprArm.setBoolValue(0);
+    annunciatorGs.setBoolValue(0);
+    annunciatorGsArm.setBoolValue(0);
+    annunciatorVs.setBoolValue(1);
+    annunciatorVsNumber.setBoolValue(1);
+
+    settingTargetInterceptAngle.setDoubleValue(0.0);
+
+    pressureRate = getprop(internal, "pressure-rate");
+    #print(pressureRate);
+    fpm = -pressureRate * 58000;
+    #print(fpm);
+    if (fpm > 0.0)
+    {
+      fpm = int(fpm/100 + 0.5) * 100;
+    }
+    else
+    {
+      fpm = int(fpm/100 - 0.5) * 100;
+    }
+    #print(fpm);
+
+    settingTargetPressureRate.setDoubleValue(-fpm / 58000);
+
+    if (altButtonTimerRunning == 0)
+    {
+      settimer(altButtonTimer, 3.0);
+      altButtonTimerRunning = 1;
+      altButtonTimerIgnore = 0;
+      annunciatorAltNumber.setBoolValue(0);
+    }
+  }
+}
+
+
+navButton = func {
+  ##print("navButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  ##
+  # If we are in HDG mode we switch to the 45 degree angle intercept NAV mode
+  ##
+  if (lockRollMode.getValue() == rollModes["HDG"])
+  {
+    flasher(annunciatorHdg, 0.5, 8, 0);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["NAV"]);
+    lockRollMode.setIntValue(rollModes["NAV"]);
+
+    annunciatorNavArm.setBoolValue(1);
+
+    navArmFromHdg();
+  }
+  ##
+  # If we are in ROL mode we switch to the all angle intercept NAV mode.
+  ##
+  elsif (lockRollMode.getValue() == rollModes["ROL"])
+  {
+    flasher(annunciatorHdg, 0.5, 8, 0);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(0);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["NAV"]);
+    lockRollMode.setIntValue(rollModes["NAV"]);
+
+    annunciatorNavArm.setBoolValue(1);
+
+    navArmFromRol();
+  }
+  ##
+  # TODO:
+  # NAV mode can only be armed if we are in HDG or ROL mode.
+  # Can anyone verify that this is correct?
+  ##
+}
+
+navArmFromHdg = func
+{
+  ##
+  # Abort the NAV-ARM mode if something has changed the arm mode to something
+  # else than NAV-ARM.
+  ##
+  if (lockRollArm.getValue() != rollArmModes["NAV"])
+  {
+    annunciatorNavArm.setBoolValue(0);
+    return;
+  }
+
+  #annunciatorNavArm.setBoolValue(1);
+  ##
+  # Wait for the HDG annunciator flashing to finish.
+  ##
+  if (flashTimer > -0.5)
+  {
+    #print("flashing...");
+    settimer(navArmFromHdg, 2.5);
+    return;
+  }
+  ##
+  # Activate the nav-hold controller and check the needle deviation.
+  ##
+  lockNavHold.setBoolValue(1);
+  deviation = getprop(navRadio, "heading-needle-deflection");
+  ##
+  # If the deflection is more than 3 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 3.0)
+  {
+    #print("deviation");
+    settimer(navArmFromHdg, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 3 degrees turn of the NAV-ARM annunciator
+  # and show the NAV annunciator. End of NAV-ARM sequence.
+  ##
+  elsif (abs(deviation) < 3.1)
+  {
+    #print("capture");
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+    annunciatorNavArm.setBoolValue(0);
+    annunciatorNav.setBoolValue(1);
+  }
+}
+
+navArmFromRol = func
+{
+  ##
+  # Abort the NAV-ARM mode if something has changed the arm mode to something
+  # else than NAV-ARM.
+  ##
+  if (lockRollArm.getValue() != rollArmModes["NAV"])
+  {
+    annunciatorNavArm.setBoolValue(0);
+    return;
+  }
+  ##
+  # Wait for the HDG annunciator flashing to finish.
+  ##
+  #annunciatorNavArm.setBoolValue(1);
+  if (flashTimer > -0.5)
+  {
+    #print("flashing...");
+    annunciatorRol.setBoolValue(0);
+    settimer(navArmFromRol, 2.5);
+    return;
+  }
+  ##
+  # Turn the ROL annunciator back on and activate the ROL mode.
+  ##
+  annunciatorRol.setBoolValue(1);
+  lockRollAxis.setBoolValue(1);
+  settingTargetTurnRate.setDoubleValue(0.0);
+  deviation = getprop(navRadio, "heading-needle-deflection");
+  ##
+  # If the deflection is more than 3 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 3.0)
+  {
+    #print("deviation");
+    settimer(navArmFromRol, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 3 degrees turn of the NAV-ARM annunciator
+  # and show the NAV annunciator. End of NAV-ARM sequence.
+  ##
+  elsif (abs(deviation) < 3.1)
+  {
+    #print("capture");
+    annunciatorRol.setBoolValue(0);
+    annunciatorNavArm.setBoolValue(0);
+    annunciatorNav.setBoolValue(1);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(1);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["NAV"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+  }
+}
+
+aprButton = func {
+  ##print("aprButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  ##
+  # If we are in HDG mode we switch to the 45 degree intercept angle APR mode
+  ##
+  if (lockRollMode.getValue() == rollModes["HDG"])
+  {
+    flasher(annunciatorHdg, 0.5, 8, 0);
+
+    lockAprHold.setBoolValue(1);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["APR"]);
+    lockRollMode.setIntValue(rollModes["APR"]);
+
+    annunciatorAprArm.setBoolValue(1);
+
+    aprArmFromHdg();
+  }
+  elsif (lockRollMode.getValue() == rollModes["ROL"])
+  {
+    flasher(annunciatorHdg, 0.5, 8, 0);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(0);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["APR"]);
+    lockRollMode.setIntValue(rollModes["APR"]);
+
+    annunciatorAprArm.setBoolValue(1);
+
+    aprArmFromRol();
+  }
+}
+
+aprArmFromHdg = func
+{
+  ##
+  # Abort the APR-ARM mode if something has changed the arm mode to something
+  # else than APR-ARM.
+  ##
+  if (lockRollArm.getValue() != rollArmModes["APR"])
+  {
+    annunciatorAprArm.setBoolValue(0);
+    return;
+  }
+
+  #annunciatorAprArm.setBoolValue(1);
+  ##
+  # Wait for the HDG annunciator flashing to finish.
+  ##
+  if (flashTimer > -0.5)
+  {
+    #print("flashing...");
+    settimer(aprArmFromHdg, 2.5);
+    return;
+  }
+  ##
+  # Activate the apr-hold controller and check the needle deviation.
+  ##
+  lockAprHold.setBoolValue(1);
+  deviation = getprop(navRadio, "heading-needle-deflection");
+  ##
+  # If the deflection is more than 3 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 3.0)
+  {
+    #print("deviation");
+    settimer(aprArmFromHdg, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 3 degrees turn of the APR-ARM annunciator
+  # and show the APR annunciator. End of APR-ARM sequence. Start the GS-ARM
+  # sequence.
+  ##
+  elsif (abs(deviation) < 3.1)
+  {
+    #print("capture");
+    annunciatorAprArm.setBoolValue(0);
+    annunciatorApr.setBoolValue(1);
+    lockPitchArm.setIntValue(pitchArmModes["GS"]);
+
+    gsArm();
+  }
+}
+
+aprArmFromRol = func
+{
+  ##
+  # Abort the APR-ARM mode if something has changed the roll mode to something
+  # else than APR-ARM.
+  ##
+  if (lockRollArm.getValue() != rollArmModes["APR"])
+  {
+    annunciatorAprArm.setBoolValue(0);
+    return;
+  }
+
+  #annunciatorAprArm.setBoolValue(1);
+  ##
+  # Wait for the HDG annunciator flashing to finish.
+  ##
+  if (flashTimer > -0.5)
+  {
+    #print("flashing...");
+    annunciatorRol.setBoolValue(0);
+    settimer(aprArmFromRol, 2.5);
+    return;
+  }
+  ##
+  # Turn the ROL annunciator back on and activate the ROL mode.
+  ##
+  annunciatorRol.setBoolValue(1);
+  lockRollAxis.setBoolValue(1);
+  settingTargetTurnRate.setDoubleValue(0.0);
+  deviation = getprop(navRadio, "heading-needle-deflection");
+  ##
+  # If the deflection is more than 3 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 3.0)
+  {
+    #print("deviation");
+    settimer(aprArmFromRol, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 3 degrees turn of the APR-ARM annunciator
+  # and show the APR annunciator. End of APR-ARM sequence. Start the GS-ARM
+  # sequence.
+  ##
+  elsif (abs(deviation) < 3.1)
+  {
+    #print("capture");
+    annunciatorRol.setBoolValue(0);
+    annunciatorAprArm.setBoolValue(0);
+    annunciatorApr.setBoolValue(1);
+
+    lockAprHold.setBoolValue(1);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["APR"]);
+    lockPitchArm.setIntValue(pitchArmModes["GS"]);
+
+    gsArm();
+  }
+}
+
+
+gsArm = func {
+  ##
+  # Abort the GS-ARM mode if something has changed the arm mode to something
+  # else than GS-ARM.
+  ##
+  if (lockPitchArm.getValue() != pitchArmModes["GS"])
+  {
+    annunciatorGsArm.setBoolValue(0);
+    return;
+  }
+
+  annunciatorGsArm.setBoolValue(1);
+
+  deviation = getprop(navRadio, "gs-needle-deflection");
+  ##
+  # If the deflection is more than 1 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 1.0)
+  {
+    #print("deviation");
+    settimer(gsArm, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 1 degrees turn off the GS-ARM annunciator
+  # and show the GS annunciator. Activate the GS pitch mode.
+  ##
+  elsif (abs(deviation) < 1.1)
+  {
+    #print("capture");
+    annunciatorAlt.setBoolValue(0);
+    annunciatorVs.setBoolValue(0);
+    annunciatorVsNumber.setBoolValue(0);
+    annunciatorGsArm.setBoolValue(0);
+    annunciatorGs.setBoolValue(1);
+
+    lockAltHold.setBoolValue(0);
+    lockGsHold.setBoolValue(1);
+    lockPitchMode.setIntValue(pitchModes["GS"]);
+    lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+  }
+
+}
+
+
+revButton = func {
+  ##print("revButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  ##
+  # If we are in HDG mode we switch to the 45 degree intercept angle REV mode
+  ##
+  if (lockRollMode.getValue() == rollModes["HDG"])
+  {
+    flasher(annunciatorHdg, 0.5, 8, 0);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["REV"]);
+
+    annunciatorRevArm.setBoolValue(1);
+
+    revArmFromHdg();
+  }
+  elsif (lockRollMode.getValue() == rollModes["ROL"])
+  {
+    flasher(annunciatorHdg, 0.5, 8, 0);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(0);
+    lockHdgHold.setBoolValue(0);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["REV"]);
+
+    annunciatorRevArm.setBoolValue(1);
+
+    revArmFromRol();
+  }
+}
+
+
+revArmFromHdg = func
+{
+  ##
+  # Abort the REV-ARM mode if something has changed the arm mode to something
+  # else than REV-ARM.
+  ##
+  if (lockRollArm.getValue() != rollArmModes["REV"])
+  {
+    annunciatorRevArm.setBoolValue(0);
+    return;
+  }
+
+  #annunciatorRevArm.setBoolValue(1);
+  ##
+  # Wait for the HDG annunciator flashing to finish.
+  ##
+  if (flashTimer > -0.5)
+  {
+    #print("flashing...");
+    settimer(revArmFromHdg, 2.5);
+    return;
+  }
+  ##
+  # Activate the rev-hold controller and check the needle deviation.
+  ##
+  lockRevHold.setBoolValue(1);
+  deviation = getprop(navRadio, "heading-needle-deflection");
+  ##
+  # If the deflection is more than 3 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 3.0)
+  {
+    #print("deviation");
+    settimer(revArmFromHdg, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 3 degrees turn of the REV-ARM annunciator
+  # and show the REV annunciator. End of REV-ARM sequence.
+  ##
+  elsif (abs(deviation) < 3.1)
+  {
+    #print("capture");
+    annunciatorRevArm.setBoolValue(0);
+    annunciatorRev.setBoolValue(1);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+    annunciatorRol.setBoolValue(0);
+    annunciatorRevArm.setBoolValue(0);
+    annunciatorRev.setBoolValue(1);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(1);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["REV"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+  }
+}
+
+
+revArmFromRol = func
+{
+  ##
+  # Abort the REV-ARM mode if something has changed the arm mode to something
+  # else than REV-ARM.
+  ##
+  if (lockRollArm.getValue() != rollArmModes["REV"])
+  {
+    annunciatorRevArm.setBoolValue(0);
+    return;
+  }
+
+  #annunciatorRevArm.setBoolValue(1);
+  ##
+  # Wait for the HDG annunciator flashing to finish.
+  ##
+  if (flashTimer > -0.5)
+  {
+    #print("flashing...");
+    annunciatorRol.setBoolValue(0);
+    settimer(revArmFromRol, 2.5);
+    return;
+  }
+  ##
+  # Turn the ROL annunciator back on and activate the ROL mode.
+  ##
+  annunciatorRol.setBoolValue(1);
+  lockRollAxis.setBoolValue(1);
+  settingTargetTurnRate.setDoubleValue(0.0);
+  deviation = getprop(navRadio, "heading-needle-deflection");
+  ##
+  # If the deflection is more than 3 degrees wait 5 seconds and check again.
+  ##
+  if (abs(deviation) > 3.0)
+  {
+    #print("deviation");
+    settimer(revArmFromRol, 5);
+    return;
+  }
+  ##
+  # If the deviation is less than 3 degrees turn of the REV-ARM annunciator
+  # and show the REV annunciator. End of REV-ARM sequence.
+  ##
+  elsif (abs(deviation) < 3.1)
+  {
+    #print("capture");
+    annunciatorRol.setBoolValue(0);
+    annunciatorRevArm.setBoolValue(0);
+    annunciatorRev.setBoolValue(1);
+
+    lockAprHold.setBoolValue(0);
+    lockGsHold.setBoolValue(0);
+    lockRevHold.setBoolValue(1);
+    lockHdgHold.setBoolValue(1);
+    lockNavHold.setBoolValue(0);
+    lockRollAxis.setBoolValue(1);
+    lockRollMode.setIntValue(rollModes["REV"]);
+    lockRollArm.setIntValue(rollArmModes["OFF"]);
+  }
+}
+
+
+altButtonTimer = func {
+  #print("alt button timer");
+  #print(altButtonTimerIgnore);
+
+  if (altButtonTimerIgnore == 0)
+  {
+      annunciatorVsNumber.setBoolValue(0);
+      annunciatorAltNumber.setBoolValue(1);
+
+      altButtonTimerRunning = 0;
+  }
+  elsif (altButtonTimerIgnore > 0)
+  {
+      altButtonTimerIgnore = altButtonTimerIgnore - 1;
+  }
+}
+
+
+altButton = func {
+  ##print("altButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+
+  if (lockPitchMode.getValue() == pitchModes["ALT"])
+  {
+    if (altButtonTimerRunning == 0)
+    {
+      settimer(altButtonTimer, 3.0);
+      altButtonTimerRunning = 1;
+      altButtonTimerIgnore = 0;
+    }
+    lockAltHold.setBoolValue(0);
+
+    lockPitchAxis.setBoolValue(1);
+    lockPitchMode.setIntValue(pitchModes["VS"]);
+
+    annunciatorAlt.setBoolValue(0);
+    annunciatorAltNumber.setBoolValue(0);
+    annunciatorVs.setBoolValue(1);
+    annunciatorVsNumber.setBoolValue(1);
+
+    pressureRate = getprop(internal, "pressure-rate");
+    fpm = -pressureRate * 58000;
+    #print(fpm);
+    if (fpm > 0.0)
+    {
+      fpm = int(fpm/100 + 0.5) * 100;
+    }
+    else
+    {
+      fpm = int(fpm/100 - 0.5) * 100;
+    }
+    #print(fpm);
+
+    settingTargetPressureRate.setDoubleValue(-fpm / 58000);
+
+  }
+  elsif (lockPitchMode.getValue() == pitchModes["VS"])
+  {
+    lockAltHold.setBoolValue(1);
+    lockPitchAxis.setBoolValue(1);
+    lockPitchMode.setIntValue(pitchModes["ALT"]);
+
+    annunciatorAlt.setBoolValue(1);
+    annunciatorVs.setBoolValue(0);
+    annunciatorVsNumber.setBoolValue(0);
+    annunciatorAltNumber.setBoolValue(1);
+
+    altPressure = getprop(staticPort, "pressure-inhg");
+    altFt = (baroSettingInhg - altPressure) / 0.00103;
+    if (altFt > 0.0)
+    {
+      altFt = int(altFt/20 + 0.5) * 20;
+    }
+    else
+    {
+      altFt = int(altFt/20 - 0.5) * 20;
+    }
+    #print(altFt);
+
+    altPressure = baroSettingInhg - altFt * 0.00103;
+    settingTargetAltPressure.setDoubleValue(altPressure);
+
+  }
+}
+
+
+downButton = func {
+  ##print("downButton");#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroTimerRunning == 0)
+  {
+    if (lockPitchMode.getValue() == pitchModes["VS"])
+    {
+      if (altButtonTimerRunning == 0)
+      {
+        settimer(altButtonTimer, 3.0);
+        altButtonTimerRunning = 1;
+        altButtonTimerIgnore = 0;
+      }
+      elsif (altButtonTimerRunning == 1)
+      {
+          settimer(altButtonTimer, 3.0);
+          altButtonTimerIgnore = altButtonTimerIgnore + 1;
+      }
+      targetVS = getprop(settings, "target-pressure-rate");
+      settingTargetPressureRate.setDoubleValue(targetVS +
+                                               0.0017241379310345);
+      annunciatorAltNumber.setBoolValue(0);
+      annunciatorVsNumber.setBoolValue(1);
+    }
+    elsif (lockPitchMode.getValue() == pitchModes["ALT"])
+    {
+      targetPressure = getprop(settings, "target-alt-pressure");
+      settingTargetAltPressure.setDoubleValue(targetPressure + 0.0206);
+    }
+  }
+}
+
+upButton = func {
+  ##print("upButton");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroTimerRunning == 0)
+  {
+    if (lockPitchMode.getValue() == pitchModes["VS"])
+    {
+      if (altButtonTimerRunning == 0)
+      {
+        settimer(altButtonTimer, 3.0);
+        altButtonTimerRunning = 1;
+        altButtonTimerIgnore = 0;
+      }
+      elsif (altButtonTimerRunning == 1)
+      {
+          settimer(altButtonTimer, 3.0);
+          altButtonTimerIgnore = altButtonTimerIgnore + 1;
+      }
+      targetVS = getprop(settings, "target-pressure-rate");
+      settingTargetPressureRate.setDoubleValue(targetVS -
+                                               0.0017241379310345);
+      annunciatorAltNumber.setBoolValue(0);
+      annunciatorVsNumber.setBoolValue(1);
+    }
+    elsif (lockPitchMode.getValue() == pitchModes["ALT"])
+    {
+      targetPressure = getprop(settings, "target-alt-pressure");
+      settingTargetAltPressure.setDoubleValue(targetPressure - 0.0206);
+    }
+  }
+}
+
+armButton = func {
+  #print("arm button");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  pitchArm = lockPitchArm.getValue();
+
+  if (pitchArm == pitchArmModes["OFF"])
+  {
+    lockPitchArm.setIntValue(pitchArmModes["ALT"]);
+
+    annunciatorAltArm.setBoolValue(1);
+  }
+  elsif (pitchArm == pitchArmModes["ALT"])
+  {
+    lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+
+    annunciatorAltArm.setBoolValue(0);
+  }
+}
+
+
+baroButtonTimer = func {
+  #print("baro button timer");
+
+  baroTimerRunning = 0;
+  if (baroButtonDown == 1)
+  {
+    baroSettingUnit = !baroSettingUnit;
+    baroButtonDown = 0;
+    baroButtonPress();
+  }
+  elsif (baroButtonDown == 0 and
+         baroSettingAdjusting == 0)
+  {
+    annunciatorBsHpaNumber.setBoolValue(0);
+    annunciatorBsInhgNumber.setBoolValue(0);
+    annunciatorAltNumber.setBoolValue(1);
+  }
+  elsif (baroSettingAdjusting == 1)
+  {
+    baroTimerRunning = 1;
+    baroSettingAdjusting = 0;
+    settimer(baroButtonTimer, 3.0);
+  }
+}
+
+baroButtonPress = func {
+  #print("baro putton press");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroButtonDown == 0 and
+      baroTimerRunning == 0 and
+      altButtonTimerRunning == 0)
+  {
+    baroButtonDown = 1;
+    baroTimerRunning = 1;
+    settimer(baroButtonTimer, 3.0);
+    annunciatorAltNumber.setBoolValue(0);
+
+    if (baroSettingUnit == pressureUnits["inHg"])
+    {
+      settingBaroSettingInhg.setDoubleValue(baroSettingInhg);
+
+      annunciatorBsInhgNumber.setBoolValue(1);
+      annunciatorBsHpaNumber.setBoolValue(0);
+    }
+    elsif (baroSettingUnit == pressureUnits["hPa"])
+    {
+      settingBaroSettingHpa.setDoubleValue(
+              baroSettingInhg * 0.03386389);
+
+      annunciatorBsHpaNumber.setBoolValue(1);
+      annunciatorBsInhgNumber.setBoolValue(0);
+    }
+  }
+}
+
+
+baroButtonRelease = func {
+  #print("baro button release");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  baroButtonDown = 0;
+}
+
+
+pow = func {
+  #print(arg[0],arg[1]);
+  return math.exp(arg[1]*math.ln(arg[0]));
+}
+
+
+pressureToHeight = func {
+  p0 = arg[1];    # [Pa]
+  p = arg[0];     # [Pa]
+  t0 = 288.15;    # [K]
+  LR = -0.0065;    # [K/m]
+  g = -9.80665;    # [m/s²]
+  Rd = 287.05307; # [J/kg K]
+
+  z = -(t0/LR) * (1.0-pow((p/p0),((Rd*LR)/g)));
+  return z;
+}
+
+
+heightToPressure = func {
+  p0 = arg[1];    # [Pa]
+  z = arg[0];     # [m]
+  t0 = 288.15;    # [K]
+  LR = -0.0065;    # [K/m]
+  g = -9.80665;    # [m/s²]
+  Rd = 287.05307; # [J/kg K]
+
+  p = p0 * pow(((t0+LR*z)/t0),(g/(Rd*LR)));
+  return p;
+}
+
+hPartial = func {
+  p0 = arg[1];    # Units of p0 must match units of delta p
+  p = arg[0];     # Units of p must match units of delta p
+  t0 = 288.15;    # [K]
+  LR = -0.0065;    # [K/m]
+  g = -9.80665;    # [m/s²]
+  Rd = 287.05307; # [J/kg K]
+  gamma = (Rd*LR)/g;
+
+  z = -(t0/LR)*gamma*pow((p/p0),gamma)/p0;
+  return z;
+}
+
+altAlert = func {
+  #print("alt alert");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  pressureAltitude = getprop(encoder, "pressure-alt-ft");
+  altPressure = getprop(staticPort, "pressure-inhg");
+  hPartStat = hPartial(altPressure, 29.92) / 0.3048006;
+  altFt = pressureAltitude + hPartStat * (baroSettingInhg - 29.92);
+  altDifference = abs(altPreselect - altFt);
+  #print(altDifference);
+
+  if (altDifference > 1000)
+  {
+    annunciatorAltAlert.setBoolValue(0);
+  }
+  elsif (altDifference < 1000 and
+         altCaptured == 0)
+  {
+    if (flashTimer < -0.5) {
+      annunciatorAltAlert.setBoolValue(1); }
+    if (altDifference < 200)
+    {
+      if (flashTimer < -0.5) {
+        annunciatorAltAlert.setBoolValue(0); }
+      if (altDifference < 20)
+      {
+        #print("altCapture()");
+        altCaptured = 1;
+
+        if (lockPitchArm.getValue() == pitchArmModes["ALT"])
+        {
+          lockAltHold.setBoolValue(1);
+          lockPitchAxis.setBoolValue(1);
+          lockPitchMode.setIntValue(pitchModes["ALT"]);
+          lockPitchArm.setIntValue(pitchArmModes["OFF"]);
+
+          annunciatorAlt.setBoolValue(1);
+          annunciatorAltArm.setBoolValue(0);
+          annunciatorVs.setBoolValue(0);
+          annunciatorVsNumber.setBoolValue(0);
+          annunciatorAltNumber.setBoolValue(1);
+
+          #altPressure = baroSettingInhg - altPreselect * 0.00103;
+          #altPressure = heightToPressure(altPreselect*0.3048006,
+          #                                baroSettingInhg*3386.389)/3386.389;
+          altPressure = getprop(staticPort, "pressure-inhg");
+          settingTargetAltPressure.setDoubleValue(altPressure);
+        }
+
+        flasher(annunciatorAltAlert, 1.0, 0, 0);
+      }
+    }
+  }
+  elsif (altDifference < 1000 and
+         altCaptured == 1)
+  {
+    if (altDifference > 200)
+    {
+      flasher(annunciatorAltAlert, 1.0, 5, 1);
+      altCaptured = 0;
+    }
+  }
+  settimer(altAlert, 2.0);
+}
+
+
+knobSmallUp = func {
+  #print("knob small up");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroTimerRunning == 1)
+  {
+    baroSettingAdjusting = 1;
+    if (baroSettingUnit == pressureUnits["inHg"])
+    {
+      baroSettingInhg = baroSettingInhg + 0.01;
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+
+      settingBaroSettingInhg.setDoubleValue(baroSettingInhg);
+    }
+    elsif (baroSettingUnit == pressureUnits["hPa"])
+    {
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+      baroSettingHpa = baroSettingHpa + 0.001;
+      baroSettingInhg = baroSettingHpa / 0.03386389;
+
+      settingBaroSettingHpa.setDoubleValue(baroSettingHpa);
+    }
+  }
+  elsif (baroTimerRunning == 0 and
+         altButtonTimerRunning == 0)
+  {
+    altCaptured = 0;
+    altPreselect = altPreselect + 20;
+    settingTargetAltFt.setDoubleValue(altPreselect);
+
+    if (lockRollMode.getValue() == rollModes["OFF"] and
+        lockPitchMode.getValue() == pitchModes["OFF"])
+    {
+      annunciatorAltNumber.setBoolValue(1);
+      if (altAlertOn == 0)
+      {
+        altAlertOn = 1;
+      }
+    }
+    elsif (lockPitchArm.getValue() == pitchArmModes["OFF"])
+    {
+      lockPitchArm.setIntValue(pitchArmModes["ALT"]);
+      annunciatorAltArm.setBoolValue(1);
+    }
+  }
+}
+
+
+knobLargeUp = func {
+  #print("knob large up");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroTimerRunning == 1)
+  {
+    baroSettingAdjusting = 1;
+    if (baroSettingUnit == pressureUnits["inHg"])
+    {
+      baroSettingInhg = baroSettingInhg + 1.0;
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+
+      settingBaroSettingInhg.setDoubleValue(baroSettingInhg);
+    }
+    elsif (baroSettingUnit == pressureUnits["hPa"])
+    {
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+      baroSettingHpa = baroSettingHpa + 0.1;
+      baroSettingInhg = baroSettingHpa / 0.03386389;
+
+      settingBaroSettingHpa.setDoubleValue(baroSettingHpa);
+    }
+  }
+  elsif (baroTimerRunning == 0 and
+         altButtonTimerRunning == 0)
+  {
+    altCaptured = 0;
+    altPreselect = altPreselect + 100;
+    settingTargetAltFt.setDoubleValue(altPreselect);
+
+    if (lockRollMode.getValue() == rollModes["OFF"] and
+        lockPitchMode.getValue() == pitchModes["OFF"])
+    {
+      annunciatorAltNumber.setBoolValue(1);
+      if (altAlertOn == 0)
+      {
+        altAlertOn = 1;
+      }
+    }
+    elsif (lockPitchArm.getValue() == pitchArmModes["OFF"])
+    {
+      lockPitchArm.setIntValue(pitchArmModes["ALT"]);
+      annunciatorAltArm.setBoolValue(1);
+    }
+  }
+}
+
+
+knobSmallDown = func {
+  #print("knob small down");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroTimerRunning == 1)
+  {
+    baroSettingAdjusting = 1;
+    if (baroSettingUnit == pressureUnits["inHg"])
+    {
+      baroSettingInhg = baroSettingInhg - 0.01;
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+
+      settingBaroSettingInhg.setDoubleValue(baroSettingInhg);
+    }
+    elsif (baroSettingUnit == pressureUnits["hPa"])
+    {
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+      baroSettingHpa = baroSettingHpa - 0.001;
+      baroSettingInhg = baroSettingHpa / 0.03386389;
+
+      settingBaroSettingHpa.setDoubleValue(baroSettingHpa);
+    }
+  }
+  elsif (baroTimerRunning == 0 and
+         altButtonTimerRunning == 0)
+  {
+    altCaptured = 0;
+    altPreselect = altPreselect - 20;
+    settingTargetAltFt.setDoubleValue(altPreselect);
+
+    if (lockRollMode.getValue() == rollModes["OFF"] and
+        lockPitchMode.getValue() == pitchModes["OFF"])
+    {
+      annunciatorAltNumber.setBoolValue(1);
+      if (altAlertOn == 0)
+      {
+        altAlertOn = 1;
+      }
+    }
+    elsif (lockPitchArm.getValue() == pitchArmModes["OFF"])
+    {
+      lockPitchArm.setIntValue(pitchArmModes["ALT"]);
+      annunciatorAltArm.setBoolValue(1);
+    }
+  }
+}
+
+
+knobLargeDown = func {
+  #print("knob large down");
+#  Disable button if too little power
+  if (getprop(power) < minVoltageLimit) { return; }
+
+  if (baroTimerRunning == 1)
+  {
+    baroSettingAdjusting = 1;
+    if (baroSettingUnit == pressureUnits["inHg"])
+    {
+      baroSettingInhg = baroSettingInhg - 1.0;
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+
+      settingBaroSettingInhg.setDoubleValue(baroSettingInhg);
+    }
+    elsif (baroSettingUnit == pressureUnits["hPa"])
+    {
+      baroSettingHpa = baroSettingInhg * 0.03386389;
+      baroSettingHpa = baroSettingHpa - 0.1;
+      baroSettingInhg = baroSettingHpa / 0.03386389;
+
+      settingBaroSettingHpa.setDoubleValue(baroSettingHpa);
+    }
+  }
+  elsif (baroTimerRunning == 0 and
+         altButtonTimerRunning == 0)
+  {
+    altCaptured = 0;
+    altPreselect = altPreselect - 100;
+    settingTargetAltFt.setDoubleValue(altPreselect);
+
+    if (lockRollMode.getValue() == rollModes["OFF"] and
+        lockPitchMode.getValue() == pitchModes["OFF"])
+    {
+      annunciatorAltNumber.setBoolValue(1);
+      if (altAlertOn == 0)
+      {
+        altAlertOn = 1;
+      }
+    }
+    elsif (lockPitchArm.getValue() == pitchArmModes["OFF"])
+    {
+      lockPitchArm.setIntValue(pitchArmModes["ALT"]);
+      annunciatorAltArm.setBoolValue(1);
+    }
+  }
+}
+
+var L = setlistener(power, func {
+  apPower();
+  removelistener(L);
+});
+
+
diff --git a/Aircraft/Generic/limits.nas b/Aircraft/Generic/limits.nas
index 78a472063..7cb872680 100644
--- a/Aircraft/Generic/limits.nas
+++ b/Aircraft/Generic/limits.nas
@@ -14,7 +14,7 @@
 # limits/max-positive-g
 # limits/max-negative-g (must be defined in max-positive-g defined)
 #
-# You can defined multiple max-flap-extension-speed entries, for max extension
+# You can define multiple max-flap-extension-speed entries for max extension
 # speeds for different flap settings.
 
 checkFlaps = func {
diff --git a/Aircraft/Instruments/KAP140TwoAxis.xml b/Aircraft/Instruments/KAP140TwoAxis.xml
index ca85a0ef8..a5889d836 100644
--- a/Aircraft/Instruments/KAP140TwoAxis.xml
+++ b/Aircraft/Instruments/KAP140TwoAxis.xml
@@ -51,7 +51,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/ap</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -80,7 +80,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/rol</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -109,7 +109,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/hdg</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -138,7 +138,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/nav</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -167,7 +167,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/nav-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -196,7 +196,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/apr</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -225,7 +225,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/apr-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -254,7 +254,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/rev</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -283,7 +283,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/rev-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -312,7 +312,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/vs</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -341,7 +341,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/fpm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -370,7 +370,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/vs-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <type>text</type>
@@ -406,7 +406,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/alt</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -435,7 +435,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/gs</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -464,7 +464,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/gs-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -504,7 +504,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.ap_button();
+      kap140.apButton();
      </script>
    </binding>
   </action>
@@ -519,7 +519,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.hdg_button();
+      kap140.hdgButton();
      </script>
    </binding>
   </action>
@@ -534,7 +534,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.nav_button();
+      kap140.navButton();
      </script>
    </binding>
   </action>
@@ -549,7 +549,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.apr_button();
+      kap140.aprButton();
      </script>
    </binding>
   </action>
@@ -564,7 +564,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.alt_button();
+      kap140.altButton();
      </script>
    </binding>
   </action>
@@ -579,7 +579,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.rev_button();
+      kap140.revButton();
      </script>
    </binding>
   </action>
@@ -594,7 +594,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.dn_button();
+      kap140.downButton();
      </script>
    </binding>
   </action>
@@ -609,11 +609,11 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.up_button();
+      kap140.upButton();
      </script>
    </binding>
   </action>
 
  </actions>
 
-</PropertyList>
\ No newline at end of file
+</PropertyList>
diff --git a/Aircraft/Instruments/KAP140TwoAxisAlt.xml b/Aircraft/Instruments/KAP140TwoAxisAlt.xml
index f77468842..195408c2b 100644
--- a/Aircraft/Instruments/KAP140TwoAxisAlt.xml
+++ b/Aircraft/Instruments/KAP140TwoAxisAlt.xml
@@ -1,10 +1,9 @@
 <?xml version="1.0"?>
-
 <!--
 _______________________________________________________________________________
 KAP140 Two Axis with Altitude Preselect autopilot instrument.
 
-Started 2004 by Roy Vegard Ovesen, rvovesen@tiscali.no.
+Started 2004 by Roy Vegard Ovesen.
 
 This is an instrument configuration file for the open-source flight
 simulator FlightGear (www.flightgear.org).  The file is an XML-encoded
@@ -23,7 +22,6 @@ click.  Actions always modify the values of properties, either by
 adjusting them, toggling them, or swaping them with other
 properties' values.
 -->
-
 <PropertyList>
  <name>KAP140TwoAxisAlt</name>
  <w-base>512</w-base>
@@ -51,7 +49,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/ap</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -80,7 +78,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/rol</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -109,7 +107,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/hdg</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -138,7 +136,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/nav</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -167,7 +165,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/nav-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -196,7 +194,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/apr</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -225,7 +223,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/apr-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -254,7 +252,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/rev</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -283,7 +281,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/rev-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -312,7 +310,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/vs</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -341,7 +339,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/vs-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -370,7 +368,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/vs-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <type>text</type>
@@ -406,7 +404,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/alt</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -435,7 +433,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/alt-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -464,7 +462,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/alt-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <type>text</type>
@@ -500,7 +498,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/alt-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -529,7 +527,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/alt-alert</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -558,7 +556,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/gs</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -587,7 +585,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/gs-arm</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -617,7 +615,7 @@ properties' values.
     <and>
      <equals>
       <property>/autopilot/KAP140/annunciators/pt-up</property>
-      <value>on</value>
+      <value type="bool">true</value>
      </equals>
     </and>
    </condition>
@@ -648,7 +646,7 @@ properties' values.
     <and>
      <equals>
       <property>/autopilot/KAP140/annunciators/pt-dn</property>
-      <value>on</value>
+      <value type="bool">true</value>
      </equals>
     </and>
    </condition>
@@ -678,7 +676,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/bs-inhg-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <type>text</type>
@@ -714,7 +712,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/bs-inhg-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -743,7 +741,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/bs-hpa-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <type>text</type>
@@ -779,7 +777,7 @@ properties' values.
    <condition>
     <equals>
      <property>/autopilot/KAP140/annunciators/bs-hpa-number</property>
-     <value>on</value>
+     <value type="bool">true</value>
     </equals>
    </condition>
    <texture>
@@ -819,7 +817,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.ap_button();
+      kap140.apButton();
      </script>
    </binding>
   </action>
@@ -834,7 +832,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.hdg_button();
+      kap140.hdgButton();
      </script>
    </binding>
   </action>
@@ -849,7 +847,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.nav_button();
+      kap140.navButton();
      </script>
    </binding>
   </action>
@@ -864,7 +862,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.apr_button();
+      kap140.aprButton();
      </script>
    </binding>
   </action>
@@ -879,7 +877,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.alt_button();
+      kap140.altButton();
      </script>
    </binding>
   </action>
@@ -894,7 +892,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.rev_button();
+      kap140.revButton();
      </script>
    </binding>
   </action>
@@ -909,7 +907,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.dn_button();
+      kap140.downButton();
      </script>
    </binding>
   </action>
@@ -924,7 +922,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.up_button();
+      kap140.upButton();
      </script>
    </binding>
   </action>
@@ -939,7 +937,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.arm_button();
+      kap140.armButton();
      </script>
    </binding>
   </action>
@@ -955,14 +953,14 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.baro_button_press();
+      kap140.baroButtonPress();
      </script>
    </binding>
    <mod-up>
     <binding>
      <command>nasal</command>
       <script>
-       kap140.baro_button_release();
+       kap140.baroButtonRelease();
       </script>
     </binding>
    </mod-up>
@@ -978,7 +976,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.knob_s_up();
+      kap140.knobSmallUp();
      </script>
    </binding>
   </action>
@@ -993,7 +991,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.knob_l_up();
+      kap140.knobLargeUp();
      </script>
    </binding>
   </action>
@@ -1008,7 +1006,7 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.knob_s_dn();
+      kap140.knobSmallDown();
      </script>
    </binding>
   </action>
@@ -1023,11 +1021,11 @@ properties' values.
    <binding>
     <command>nasal</command>
      <script>
-      kap140.knob_l_dn();
+      kap140.knobLargeDown();
      </script>
    </binding>
   </action>
 
  </actions>
 
-</PropertyList>
+</PropertyList>
\ No newline at end of file
diff --git a/Aircraft/Instruments/fuel-c172s.xml b/Aircraft/Instruments/fuel-c172s.xml
index b8770be70..5d22790b5 100644
--- a/Aircraft/Instruments/fuel-c172s.xml
+++ b/Aircraft/Instruments/fuel-c172s.xml
@@ -207,4 +207,4 @@ Feb 7 2003
 
  </layers>
 
-</PropertyList>
\ No newline at end of file
+</PropertyList>
diff --git a/Aircraft/c172p/Systems/KAP140.xml b/Aircraft/c172p/Systems/KAP140.xml
index 7e9611f91..8e545329c 100644
--- a/Aircraft/c172p/Systems/KAP140.xml
+++ b/Aircraft/c172p/Systems/KAP140.xml
@@ -1,12 +1,8 @@
 <?xml version="1.0"?>
-
 <!-- KAP 140 Autopilot Configuration -->
-
 <!-- Each component is evaluated in the order specified.  You can make up -->
 <!-- property names to pass the result of one component on to a subsequent -->
 <!-- component. -->
-
-
 <PropertyList>
 
   <!-- =============================================================== -->
@@ -20,7 +16,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/nav-hold</prop>
-      <value>nav</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/instrumentation/nav/heading-needle-deflection</prop>
@@ -50,7 +46,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/apr-hold</prop>
-      <value>apr</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/instrumentation/nav/heading-needle-deflection</prop>
@@ -80,7 +76,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/rev-hold</prop>
-      <value>rev</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/instrumentation/nav/heading-needle-deflection</prop>
@@ -112,10 +108,10 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/hdg-hold</prop>
-      <value>hdg</value>
+      <value type="bool">true</value>
     </enable>
     <input>
-      <prop>/autopilot/internal/heading-bug-error-deg</prop> -->
+      <prop>/autopilot/internal/heading-bug-error-deg</prop> --&gt;
 <!--      <prop>/instrumentation/gps/true-bug-error-deg</prop> -->
     </input>
     <reference>
@@ -144,7 +140,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/roll-axis</prop>
-      <value>trn</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/instrumentation/turn-indicator/indicated-turn-rate</prop>
@@ -178,7 +174,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/alt-hold</prop>
-      <value>alt</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/systems/static[0]/pressure-inhg[0]</prop>
@@ -208,7 +204,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/gs-hold</prop>
-      <value>gs</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/instrumentation/nav/gs-needle-deflection</prop>
@@ -247,7 +243,7 @@
     <debug>false</debug>
     <enable>
       <prop>/autopilot/KAP140/locks/pitch-axis</prop>
-      <value>vs</value>
+      <value type="bool">true</value>
     </enable>
     <input>
       <prop>/autopilot/internal/filtered-pressure-rate</prop>
diff --git a/Aircraft/c172p/c172p-set.xml b/Aircraft/c172p/c172p-set.xml
index 328bff87f..33a29534f 100644
--- a/Aircraft/c172p/c172p-set.xml
+++ b/Aircraft/c172p/c172p-set.xml
@@ -118,6 +118,9 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
     <tumble-flag archive="y" type="bool">true</tumble-flag>
    </config>
   </attitude-indicator>
+  <encoder>
+   <serviceable type="bool">true</serviceable>
+  </encoder>
  </instrumentation>
   
  <engines>
@@ -131,7 +134,7 @@ Started October 23 2001 by John Check, fgpanels@rockfish.net
    <file>Aircraft/c172/c172-electrical.nas</file>
   </electrical>
   <kap140>
-   <file>Aircraft/c172p/kap140.nas</file>
+   <file>Aircraft/Generic/kap140.nas</file>
   </kap140>
  </nasal>
   
diff --git a/Aircraft/c172p/kap140.nas b/Aircraft/c172p/kap140.nas
deleted file mode 100644
index 89cf894a3..000000000
--- a/Aircraft/c172p/kap140.nas
+++ /dev/null
@@ -1,1520 +0,0 @@
-##
-# Bendix/King KAP140 Autopilot System
-##
-
-Locks = "/autopilot/KAP140/locks";
-Settings = "/autopilot/KAP140/settings";
-Annunciators = "/autopilot/KAP140/annunciators";
-Internal = "/autopilot/internal";
-
-annunciator = "";
-annunciator_state = "";
-flash_interval = 0.0;
-flash_count = 0.0;
-flash_timer = -1.0;
-
-# baro setting unit: 0=inHg, 1=hPa
-baro_setting_unit = 0;
-baro_setting = 29.92;
-baro_setting_hpa = 1.013;
-baro_setting_adjusting = 0;
-baro_button_down = 0;
-baro_timer_running = 0;
-
-alt_preselect = 0;
-alt_button_timer_running = 0;
-alt_button_timer_ignore = 0;
-alt_alert_on = 0;
-alt_captured = 0;
-
-old_alt_number_state = ["off", 0];
-old_vs_number_state = ["off", 0];
-
-
-flasher = func {
-  flash_timer = -1.0;
-  annunciator = arg[0];
-  flash_interval = arg[1];
-  flash_count = arg[2] + 1;
-  annunciator_state = arg[3];
-
-  flash_timer = 0.0;
-
-  flash_annunciator();
-}
-
-flash_annunciator = func {
-  ##print(annunciator);
-  ##print(flash_interval);
-  ##print(flash_count);
-
-  ##
-  # If flash_timer is set to -1 then flashing is aborted
-  if (flash_timer < -0.5)
-  {
-    ##print ("flash abort ", annunciator);
-    setprop(Annunciators, annunciator, "off");
-    return;
-  }
-
-  if (flash_timer < flash_count)
-  {
-    #flash_timer = flash_timer + 1.0;
-    if (getprop(Annunciators, annunciator) == "on")
-    {
-      setprop(Annunciators, annunciator, "off");
-      settimer(flash_annunciator, flash_interval / 2.0);
-    }
-    else
-    #elsif (getprop(Annunciators, annunciator) == "off")
-    {
-      flash_timer = flash_timer + 1.0;
-      setprop(Annunciators, annunciator, "on");
-      settimer(flash_annunciator, flash_interval);
-    }
-  }
-  else
-  {
-    flash_timer = -1.0;
-    setprop(Annunciators, annunciator, annunciator_state);
-  }
-}
-
-
-pt_check = func {
-  ##print("pitch trim check");
-
-  if (getprop(Locks, "pitch-mode") == "off")
-  {
-    setprop(Annunciators, "pt-up", "off");
-    setprop(Annunciators, "pt-dn", "off");
-    return;
-  }
-
-  else
-  {
-    elevator_control = getprop("/controls/flight/elevator");
-    ##print(elevator_control);
-
-    # Flash the pitch trim up annunciator
-    if (elevator_control < -0.01)
-    {
-      if (getprop(Annunciators, "pt-up") == "off")
-      {
-        setprop(Annunciators, "pt-up", "on");
-      }
-      elsif (getprop(Annunciators, "pt-up") == "on")
-      {
-        setprop(Annunciators, "pt-up", "off");
-      }
-    }
-    # Flash the pitch trim down annunciator
-    elsif (elevator_control > 0.01)
-    {
-      if (getprop(Annunciators, "pt-dn") == "off")
-      {
-        setprop(Annunciators, "pt-dn", "on");
-      }
-      elsif (getprop(Annunciators, "pt-dn") == "on")
-      {
-        setprop(Annunciators, "pt-dn", "off");
-      }
-    }
-
-    else
-    {
-      setprop(Annunciators, "pt-up", "off");
-      setprop(Annunciators, "pt-dn", "off");
-    }
-  }
-
-  settimer(pt_check, 0.5);
-}
-
-
-ap_init = func {
-  ##print("ap init");
-
-  ##
-  # Initialises the autopilot.
-  ##
-
-  setprop(Locks, "alt-hold", "off");
-  setprop(Locks, "apr-hold", "off");
-  setprop(Locks, "gs-hold", "off");
-  setprop(Locks, "hdg-hold", "off");
-  setprop(Locks, "nav-hold", "off");
-  setprop(Locks, "rev-hold", "off");
-  setprop(Locks, "roll-axis", "off");
-  setprop(Locks, "roll-mode", "off");
-  setprop(Locks, "pitch-axis", "off");
-  setprop(Locks, "pitch-mode", "off");
-  setprop(Locks, "roll-arm", "off");
-  setprop(Locks, "pitch-arm", "off");
-  
-  setprop(Settings, "target-alt-pressure", 0.0);
-  setprop(Settings, "target-intercept-angle", 0.0);
-  setprop(Settings, "target-pressure-rate", 0.0);
-  setprop(Settings, "target-turn-rate", 0.0);
-  
-  setprop(Annunciators, "rol", "off");
-  setprop(Annunciators, "hdg", "off");
-  setprop(Annunciators, "nav", "off");
-  setprop(Annunciators, "nav-arm", "off");
-  setprop(Annunciators, "apr", "off");
-  setprop(Annunciators, "apr-arm", "off");
-  setprop(Annunciators, "rev", "off");
-  setprop(Annunciators, "rev-arm", "off");
-  setprop(Annunciators, "vs", "off");
-  setprop(Annunciators, "vs-number", "off");
-  setprop(Annunciators, "fpm", "off");
-  setprop(Annunciators, "alt", "off");
-  setprop(Annunciators, "alt-arm", "off");
-  setprop(Annunciators, "alt-number", "off");
-  setprop(Annunciators, "apr", "off");
-  setprop(Annunciators, "gs", "off");
-  setprop(Annunciators, "gs-arm", "off");
-  setprop(Annunciators, "pt-up", "off");
-  setprop(Annunciators, "pt-dn", "off");
-  setprop(Annunciators, "bs-hpa-number", "off");
-  setprop(Annunciators, "bs-inhg-number", "off");
-  setprop(Annunciators, "ap", "off");
-
-}
-  
-
-ap_button = func {
-  ##print("ap_button");
-
-  ##
-  # Engages the autopilot in Wings level mode (ROL) and Vertical speed hold
-  # mode (VS).
-  ##
-  if (getprop(Locks, "roll-mode") == "off" and
-      getprop(Locks, "pitch-mode") == "off")
-  {
-    flash_timer = -1.0;
-
-    setprop(Locks, "alt-hold", "off");
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "off");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "rol");
-    setprop(Locks, "pitch-axis", "vs");
-    setprop(Locks, "pitch-mode", "vs");
-    setprop(Locks, "roll-arm", "off");
-    setprop(Locks, "pitch-arm", "off");
-
-    setprop(Annunciators, "rol", "on");
-    setprop(Annunciators, "vs", "on");
-    setprop(Annunciators, "vs-number", "on");
-
-    setprop(Settings, "target-turn-rate", 0.0);
-
-    pt_check();
-
-    pressure_rate = getprop(Internal, "pressure-rate");
-    #print(pressure_rate);
-    fpm = -pressure_rate * 58000;
-    #print(fpm);
-    if (fpm > 0.0)
-    {
-      fpm = int(fpm/100 + 0.5) * 100;
-    }
-    else
-    {
-      fpm = int(fpm/100 - 0.5) * 100;
-    }
-    #print(fpm);
-
-    setprop(Settings, "target-pressure-rate", -fpm / 58000);
-
-    if (alt_button_timer_running == 0)
-    {
-      settimer(alt_button_timer, 3.0);
-      alt_button_timer_running = 1;
-      alt_button_timer_ignore = 0;
-      setprop(Annunciators, "alt-number", "off");
-    }
-  }
-  ##
-  # Disengages all modes.
-  ##
-  elsif (getprop(Locks, "roll-mode") != "off" and
-         getprop(Locks, "pitch-mode") != "off")
-  {
-    flash_timer = -1.0;
-
-    setprop(Locks, "alt-hold", "off");
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "off");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "roll-axis", "off");
-    setprop(Locks, "roll-mode", "off");
-    setprop(Locks, "pitch-axis", "off");
-    setprop(Locks, "pitch-mode", "off");
-    setprop(Locks, "roll-arm", "off");
-    setprop(Locks, "pitch-arm", "off");
-
-    setprop(Settings, "target-alt-pressure", 0.0);
-    setprop(Settings, "target-intercept-angle", 0.0);
-    setprop(Settings, "target-pressure-rate", 0.0);
-    setprop(Settings, "target-turn-rate", 0.0);
-
-    setprop(Annunciators, "rol", "off");
-    setprop(Annunciators, "hdg", "off");
-    setprop(Annunciators, "nav", "off");
-    setprop(Annunciators, "nav-arm", "off");
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "apr-arm", "off");
-    setprop(Annunciators, "rev", "off");
-    setprop(Annunciators, "rev-arm", "off");
-    setprop(Annunciators, "vs", "off");
-    setprop(Annunciators, "vs-number", "off");
-    setprop(Annunciators, "alt", "off");
-    setprop(Annunciators, "alt-arm", "off");
-    setprop(Annunciators, "alt-number", "off");
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "gs", "off");
-    setprop(Annunciators, "gs-arm", "off");
-    setprop(Annunciators, "pt-up", "off");
-    setprop(Annunciators, "pt-dn", "off");
-
-    flasher("ap", 1.0, 5, "off");
-  }
-}
-
-
-hdg_button = func {
-  ##print("hdg_button");
-
-  ##
-  # Engages the heading mode (HDG) and vertical speed hold mode (VS). The
-  # commanded vertical speed is set to the vertical speed present at button
-  # press.
-  ##
-  if (getprop(Locks, "roll-mode") == "off" and
-      getprop(Locks, "pitch-mode") == "off")
-  {
-    flash_timer = -1.0;
-
-    setprop(Locks, "alt-hold", "off");
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "hdg");
-    setprop(Locks, "pitch-axis", "vs");
-    setprop(Locks, "pitch-mode", "vs");
-    setprop(Locks, "roll-arm", "off");
-    setprop(Locks, "pitch-arm", "off");
-
-    setprop(Annunciators, "hdg", "on");
-    setprop(Annunciators, "alt", "off");
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "gs", "off");
-    setprop(Annunciators, "nav", "off");
-    setprop(Annunciators, "vs", "on");
-    setprop(Annunciators, "vs-number", "on");
-
-    setprop(Settings, "target-intercept-angle", 0.0);
-
-    pt_check();
-
-    pressure_rate = getprop(Internal, "pressure-rate");
-    fpm = -pressure_rate * 58000;
-    #print(fpm);
-    if (fpm > 0.0)
-    {
-      fpm = int(fpm/100 + 0.5) * 100;
-    }
-    else
-    {
-      fpm = int(fpm/100 - 0.5) * 100;
-    }
-    #print(fpm);
-
-    setprop(Settings, "target-pressure-rate", -fpm / 58000);
-
-    if (alt_button_timer_running == 0)
-    {
-      settimer(alt_button_timer, 3.0);
-      alt_button_timer_running = 1;
-      alt_button_timer_ignore = 0;
-      setprop(Annunciators, "alt-number", "off");
-    }
-  }
-  ##
-  # Switch from ROL to HDG mode, but don't change pitch mode.
-  ##
-  elsif (getprop(Locks, "roll-mode") == "rol")
-  {
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "hdg");
-    setprop(Locks, "roll-arm", "off");
-
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "gs", "off");
-    setprop(Annunciators, "hdg", "on");
-    setprop(Annunciators, "nav", "off");
-    setprop(Annunciators, "rol", "off");
-    setprop(Annunciators, "rev", "off");
-
-    setprop(Settings, "target-intercept-angle", 0.0);
-  }
-  ##
-  # Switch to HDG mode, but don't change pitch mode.
-  ##
-  elsif ( (getprop(Locks, "roll-mode") == "nav" or
-	 getprop(Locks, "roll-arm") == "nav-arm" or
-         getprop(Locks, "roll-mode") == "rev" or
-         getprop(Locks, "roll-arm") == "rev-arm") and
-         flash_timer < -0.5)
-  {
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "hdg");
-    setprop(Locks, "roll-arm", "off");
-
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "gs", "off");
-    setprop(Annunciators, "hdg", "on");
-    setprop(Annunciators, "nav", "off");
-    setprop(Annunciators, "rol", "off");
-    setprop(Annunciators, "rev", "off");
-    setprop(Annunciators, "nav-arm", "off");
-
-    setprop(Settings, "target-intercept-angle", 0.0);
-  }
-  ##
-  # If we already are in HDG mode switch to ROL mode. Again don't touch pitch
-  # mode.
-  ##
-  elsif (getprop(Locks, "roll-mode") == "hdg")
-  {
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "rol");
-
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "gs", "off");
-    setprop(Annunciators, "hdg", "off");
-    setprop(Annunciators, "nav", "off");
-    setprop(Annunciators, "rol", "on");
-
-    setprop(Settings, "target-turn-rate", 0.0);
-  }
-  ##
-  # If we are in APR mode we also have to change pitch mode.
-  # TODO: Should we switch to VS or ALT mode? (currently VS)
-  ##
-  elsif ( (getprop(Locks, "roll-mode") == "apr" or
-         getprop(Locks, "roll-arm") == "apr-arm" or
-         getprop(Locks, "pitch-mode") == "gs" or
-         getprop(Locks, "pitch-arm") == "gs-arm") and
-         flash_timer < -0.5)
-  {
-    setprop(Locks, "alt-hold", "off");
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "hdg");
-    setprop(Locks, "pitch-axis", "vs");
-    setprop(Locks, "pitch-mode", "vs");
-    setprop(Locks, "roll-arm", "off");
-    setprop(Locks, "pitch-arm", "off");
-
-    setprop(Annunciators, "alt", "off");
-    setprop(Annunciators, "alt-arm", "off");
-    setprop(Annunciators, "hdg", "on");
-    setprop(Annunciators, "nav", "off");
-    setprop(Annunciators, "apr", "off");
-    setprop(Annunciators, "apr-arm", "off");
-    setprop(Annunciators, "gs", "off");
-    setprop(Annunciators, "gs-arm", "off");
-    setprop(Annunciators, "vs", "on");
-    setprop(Annunciators, "vs-number", "on");
-
-    setprop(Settings, "target-intercept-angle", 0.0);
-
-    pressure_rate = getprop(Internal, "pressure-rate");
-    #print(pressure_rate);
-    fpm = -pressure_rate * 58000;
-    #print(fpm);
-    if (fpm > 0.0)
-    {
-      fpm = int(fpm/100 + 0.5) * 100;
-    }
-    else
-    {
-      fpm = int(fpm/100 - 0.5) * 100;
-    }
-    #print(fpm);
-
-    setprop(Settings, "target-pressure-rate", -fpm / 58000);
-
-    if (alt_button_timer_running == 0)
-    {
-      settimer(alt_button_timer, 3.0);
-      alt_button_timer_running = 1;
-      alt_button_timer_ignore = 0;
-      setprop(Annunciators, "alt-number", "off");
-    }
-  }
-}
-
-
-nav_button = func {
-  ##print("nav_button");
-
-  ##
-  # If we are in HDG mode we switch to the 45 degree angle intercept NAV mode
-  ##
-  if (getprop(Locks, "roll-mode") == "hdg")
-  {
-    flasher("hdg", 0.5, 8, "off");
-      
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-arm", "nav-arm");
-    setprop(Locks, "roll-mode", "nav");
-
-    setprop(Annunciators, "nav-arm", "on");
-
-    nav_arm_from_hdg();
-  }
-  ##
-  # If we are in ROL mode we switch to the all angle intercept NAV mode.
-  ##
-  elsif (getprop(Locks, "roll-mode") == "rol")
-  {
-    flasher("hdg", 0.5, 8, "off");
-
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "off");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-arm", "nav-arm");
-    setprop(Locks, "roll-mode", "nav");
-
-    setprop(Annunciators, "nav-arm", "on");
-
-    nav_arm_from_rol();
-  }
-  ##
-  # TODO:
-  # NAV mode can only be armed if we are in HDG or ROL mode.
-  # Can anyone verify that this is correct?
-  ##
-}
-
-nav_arm_from_hdg = func
-{
-  ##
-  # Abort the NAV-ARM mode if something has changed the arm mode to something
-  # else than NAV-ARM.
-  ##
-  if (getprop(Locks, "roll-arm") != "nav-arm")
-  {
-    setprop(Annunciators, "nav-arm", "off");
-    return;
-  }
-
-  #setprop(Annunciators, "nav-arm", "on");
-  ##
-  # Wait for the HDG annunciator flashing to finish.
-  ##
-  if (flash_timer > -0.5)
-  {
-    #print("flashing...");
-    settimer(nav_arm_from_hdg, 2.5);
-    return;
-  }
-  ##
-  # Activate the nav-hold controller and check the needle deviation.
-  ##
-  setprop(Locks, "nav-hold", "nav");
-  deviation = getprop("/instrumentation/nav/heading-needle-deflection");
-  ##
-  # If the deflection is more than 3 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 3.0)
-  {
-    #print("deviation");
-    settimer(nav_arm_from_hdg, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 3 degrees turn of the NAV-ARM annunciator
-  # and show the NAV annunciator. End of NAV-ARM sequence.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Locks, "roll-arm", "off");
-    setprop(Annunciators, "nav-arm", "off");
-    setprop(Annunciators, "nav", "on");
-  }
-}
-
-nav_arm_from_rol = func
-{
-  ##
-  # Abort the NAV-ARM mode if something has changed the arm mode to something
-  # else than NAV-ARM.
-  ##
-  if (getprop(Locks, "roll-arm") != "nav-arm")
-  {
-    setprop(Annunciators, "nav-arm", "off");
-    return;
-  }
-  ##
-  # Wait for the HDG annunciator flashing to finish.
-  ##
-  #setprop(Annunciators, "nav-arm", "on");
-  if (flash_timer > -0.5)
-  {
-    #print("flashing...");
-    setprop(Annunciators, "rol", "off");
-    settimer(nav_arm_from_rol, 2.5);
-    return;
-  }
-  ##
-  # Turn the ROL annunciator back on and activate the ROL mode.
-  ##
-  setprop(Annunciators, "rol", "on");
-  setprop(Locks, "roll-axis", "trn");
-  setprop(Settings, "target-turn-rate", 0.0);
-  deviation = getprop("/instrumentation/nav/heading-needle-deflection");
-  ##
-  # If the deflection is more than 3 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 3.0)
-  {
-    #print("deviation");
-    settimer(nav_arm_from_rol, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 3 degrees turn of the NAV-ARM annunciator
-  # and show the NAV annunciator. End of NAV-ARM sequence.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Annunciators, "rol", "off");
-    setprop(Annunciators, "nav-arm", "off");
-    setprop(Annunciators, "nav", "on");
-
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "nav");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "nav");
-    setprop(Locks, "roll-arm", "off");
-  }
-}
-
-apr_button = func {
-  ##print("apr_button");
-  ##
-  # If we are in HDG mode we switch to the 45 degree intercept angle APR mode
-  ##
-  if (getprop(Locks, "roll-mode") == "hdg")
-  {
-    flasher("hdg", 0.5, 8, "off");
-
-    setprop(Locks, "apr-hold", "apr");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-arm", "apr-arm");
-    setprop(Locks, "roll-mode", "apr");
-
-    setprop(Annunciators, "apr-arm", "on");
-
-    apr_arm_from_hdg();
-  }
-  elsif (getprop(Locks, "roll-mode") == "rol")
-  {
-    flasher("hdg", 0.5, 8, "off");
-
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "off");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-arm", "apr-arm");
-    setprop(Locks, "roll-mode", "apr");
-
-    setprop(Annunciators, "apr-arm", "on");
-
-    apr_arm_from_rol();
-  }
-}
-
-apr_arm_from_hdg = func
-{
-  ##
-  # Abort the APR-ARM mode if something has changed the arm mode to something
-  # else than APR-ARM.
-  ##
-  if (getprop(Locks, "roll-arm") != "apr-arm")
-  {
-    setprop(Annunciators, "apr-arm", "off");
-    return;
-  }
-
-  #setprop(Annunciators, "apr-arm", "on");
-  ##
-  # Wait for the HDG annunciator flashing to finish.
-  ##
-  if (flash_timer > -0.5)
-  {
-    #print("flashing...");
-    settimer(apr_arm_from_hdg, 2.5);
-    return;
-  }
-  ##
-  # Activate the apr-hold controller and check the needle deviation.
-  ##
-  setprop(Locks, "apr-hold", "apr");
-  deviation = getprop("/instrumentation/nav/heading-needle-deflection");
-  ##
-  # If the deflection is more than 3 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 3.0)
-  {
-    #print("deviation");
-    settimer(apr_arm_from_hdg, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 3 degrees turn of the APR-ARM annunciator
-  # and show the APR annunciator. End of APR-ARM sequence. Start the GS-ARM
-  # sequence.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Annunciators, "apr-arm", "off");
-    setprop(Annunciators, "apr", "on");
-    setprop(Locks, "pitch-arm", "gs-arm");
-
-    gs_arm();
-  }
-}
-
-apr_arm_from_rol = func
-{
-  ##
-  # Abort the APR-ARM mode if something has changed the roll mode to something
-  # else than APR-ARM.
-  ##
-  if (getprop(Locks, "roll-arm") != "apr-arm")
-  {
-    setprop(Annunciators, "apr-arm", "off");
-    return;
-  }
-
-  #setprop(Annunciators, "apr-arm", "on");
-  ##
-  # Wait for the HDG annunciator flashing to finish.
-  ##
-  if (flash_timer > -0.5)
-  {
-    #print("flashing...");
-    setprop(Annunciators, "rol", "off");
-    settimer(apr_arm_from_rol, 2.5);
-    return;
-  }
-  ##
-  # Turn the ROL annunciator back on and activate the ROL mode.
-  ##
-  setprop(Annunciators, "rol", "on");
-  setprop(Locks, "roll-axis", "trn");
-  setprop(Settings, "target-turn-rate", 0.0);
-  deviation = getprop("/instrumentation/nav/heading-needle-deflection");
-  ##
-  # If the deflection is more than 3 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 3.0)
-  {
-    #print("deviation");
-    settimer(apr_arm_from_rol, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 3 degrees turn of the APR-ARM annunciator
-  # and show the APR annunciator. End of APR-ARM sequence. Start the GS-ARM
-  # sequence.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Annunciators, "rol", "off");
-    setprop(Annunciators, "apr-arm", "off");
-    setprop(Annunciators, "apr", "on");
-
-    setprop(Locks, "apr-hold", "apr");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "apr");
-    setprop(Locks, "roll-arm", "gs-arm");
-
-    gs_arm();
-  }
-}
-
-
-gs_arm = func {
-  ##
-  # Abort the GS-ARM mode if something has changed the arm mode to something
-  # else than GS-ARM.
-  ##
-  if (getprop(Locks, "pitch-arm") != "gs-arm")
-  {
-    setprop(Annunciators, "gs-arm", "off");
-    return;
-  }
-
-  setprop(Annunciators, "gs-arm", "on");
-
-  deviation = getprop("/instrumentation/nav/gs-needle-deflection");
-  ##
-  # If the deflection is more than 1 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 1.0)
-  {
-    #print("deviation");
-    settimer(gs_arm, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 1 degrees turn off the GS-ARM annunciator
-  # and show the GS annunciator. Activate the GS pitch mode.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Annunciators, "alt", "off");
-    setprop(Annunciators, "vs", "off");
-    setprop(Annunciators, "vs-number", "off");
-    setprop(Annunciators, "gs-arm", "off");
-    setprop(Annunciators, "gs", "on");
-
-    setprop(Locks, "alt-hold", "off");
-    setprop(Locks, "gs-hold", "gs");
-    setprop(Locks, "pitch-mode", "gs");
-    setprop(Locks, "pitch-arm", "off");
-  }
-
-}
-
-
-rev_button = func {
-  ##print("rev_button");
-  ##
-  # If we are in HDG mode we switch to the 45 degree intercept angle REV mode
-  ##
-  if (getprop(Locks, "roll-mode") == "hdg")
-  {
-    flasher("hdg", 0.5, 8, "off");
-
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-arm", "rev-arm");
-
-    rev_arm_from_hdg();
-  }
-  elsif (getprop(Locks, "roll-mode") == "rol")
-  {
-    flasher("hdg", 0.5, 8, "off");
-
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "off");
-    setprop(Locks, "hdg-hold", "off");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-arm", "rev-arm");
-
-    rev_arm_from_rol();
-  }
-}
-
-
-rev_arm_from_hdg = func
-{
-  ##
-  # Abort the REV-ARM mode if something has changed the arm mode to something
-  # else than REV-ARM.
-  ##
-  if (getprop(Locks, "roll-arm") != "rev-arm")
-  {
-    setprop(Annunciators, "rev-arm", "off");
-    return;
-  }
-
-  #setprop(Annunciators, "rev-arm", "on");
-  ##
-  # Wait for the HDG annunciator flashing to finish.
-  ##
-  if (flash_timer > -0.5)
-  {
-    #print("flashing...");
-    settimer(rev_arm_from_hdg, 2.5);
-    return;
-  }
-  ##
-  # Activate the rev-hold controller and check the needle deviation.
-  ##
-  setprop(Locks, "rev-hold", "rev");
-  deviation = getprop("/instrumentation/nav/heading-needle-deflection");
-  ##
-  # If the deflection is more than 3 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 3.0)
-  {
-    #print("deviation");
-    settimer(rev_arm_from_hdg, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 3 degrees turn of the REV-ARM annunciator
-  # and show the REV annunciator. End of REV-ARM sequence.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Annunciators, "rev-arm", "off");
-    setprop(Annunciators, "rev", "on");
-    setprop(Locks, "roll-arm", "off");
-  }
-}
-
-
-rev_arm_from_rol = func
-{
-  ##
-  # Abort the REV-ARM mode if something has changed the arm mode to something
-  # else than REV-ARM.
-  ##
-  if (getprop(Locks, "roll-arm") != "rev-arm")
-  {
-    setprop(Annunciators, "rev-arm", "off");
-    return;
-  }
-
-  #setprop(Annunciators, "rev-arm", "on");
-  ##
-  # Wait for the HDG annunciator flashing to finish.
-  ##
-  if (flash_timer > -0.5)
-  {
-    #print("flashing...");
-    setprop(Annunciators, "rol", "off");
-    settimer(rev_arm_from_rol, 2.5);
-    return;
-  }
-  ##
-  # Turn the ROL annunciator back on and activate the ROL mode.
-  ##
-  setprop(Annunciators, "rol", "on");
-  setprop(Locks, "roll-axis", "trn");
-  setprop(Settings, "target-turn-rate", 0.0);
-  deviation = getprop("/instrumentation/nav/heading-needle-deflection");
-  ##
-  # If the deflection is more than 3 degrees wait 5 seconds and check again.
-  ##
-  if (abs(deviation) > 3.0)
-  {
-    #print("deviation");
-    settimer(rev_arm_from_rol, 5);
-    return;
-  }
-  ##
-  # If the deviation is less than 3 degrees turn of the REV-ARM annunciator
-  # and show the REV annunciator. End of REV-ARM sequence.
-  ##
-  elsif (abs(deviation) < 3.1)
-  {
-    #print("capture");
-    setprop(Annunciators, "rol", "off");
-    setprop(Annunciators, "rev-arm", "off");
-    setprop(Annunciators, "rev", "on");
-
-    setprop(Locks, "apr-hold", "off");
-    setprop(Locks, "gs-hold", "off");
-    setprop(Locks, "rev-hold", "rev");
-    setprop(Locks, "hdg-hold", "hdg");
-    setprop(Locks, "nav-hold", "off");
-    setprop(Locks, "roll-axis", "trn");
-    setprop(Locks, "roll-mode", "rev");
-    setprop(Locks, "roll-arm", "off");
-  }
-}
-
-
-alt_button_timer = func {
-  #print("alt button timer");
-  #print(alt_button_timer_ignore);
-  
-  if (alt_button_timer_ignore == 0)
-  {
-      setprop(Annunciators, "vs-number", "off");
-      setprop(Annunciators, "alt-number", "on");
-
-      alt_button_timer_running = 0;
-  }
-  elsif (alt_button_timer_ignore > 0)
-  {
-      alt_button_timer_ignore = alt_button_timer_ignore - 1;
-  }
-}
-
-
-alt_button = func {
-  ##print("alt_button");
-
-  if (getprop(Locks, "pitch-mode") == "alt")
-  {
-    if (alt_button_timer_running == 0)
-    {
-      settimer(alt_button_timer, 3.0);
-      alt_button_timer_running = 1;
-      alt_button_timer_ignore = 0;
-    }
-    setprop(Locks, "alt-hold", "off");
-    
-    setprop(Locks, "pitch-axis", "vs");
-    setprop(Locks, "pitch-mode", "vs");
-    
-    setprop(Annunciators, "alt", "off");
-    setprop(Annunciators, "alt-number", "off");
-    setprop(Annunciators, "vs", "on");    
-    setprop(Annunciators, "vs-number", "on");
-
-    pressure_rate = getprop(Internal, "pressure-rate");
-    fpm = -pressure_rate * 58000;
-    #print(fpm);
-    if (fpm > 0.0)
-    {
-      fpm = int(fpm/100 + 0.5) * 100;
-    }
-    else
-    {
-      fpm = int(fpm/100 - 0.5) * 100;
-    }
-    #print(fpm);
-
-    setprop(Settings, "target-pressure-rate", -fpm / 58000);
-    
-  }
-  elsif (getprop(Locks, "pitch-mode") == "vs")
-  {
-    setprop(Locks, "alt-hold", "alt");
-    setprop(Locks, "pitch-axis", "vs");
-    setprop(Locks, "pitch-mode", "alt");
-
-    setprop(Annunciators, "alt", "on");
-    setprop(Annunciators, "vs", "off");
-    setprop(Annunciators, "vs-number", "off");
-    setprop(Annunciators, "alt-number", "on");
-
-    alt_pressure = getprop("/systems/static/pressure-inhg");
-    alt_ft = (baro_setting - alt_pressure) / 0.00103;
-    if (alt_ft > 0.0)
-    {
-      alt_ft = int(alt_ft/20 + 0.5) * 20;
-    }
-    else
-    {
-      alt_ft = int(alt_ft/20 - 0.5) * 20;
-    }
-    #print(alt_ft);
-
-    alt_pressure = baro_setting - alt_ft * 0.00103;
-    setprop(Settings, "target-alt-pressure", alt_pressure);
-
-  }
-}
-
-
-dn_button = func {
-  ##print("dn_button");
-
-  if (baro_timer_running == 0)
-  {
-    if (getprop(Locks, "pitch-mode") == "vs")
-    {
-      if (alt_button_timer_running == 0)
-      {
-        settimer(alt_button_timer, 3.0);
-        alt_button_timer_running = 1;
-        alt_button_timer_ignore = 0;
-      }
-      elsif (alt_button_timer_running == 1)
-      {
-          settimer(alt_button_timer, 3.0);
-          alt_button_timer_ignore = alt_button_timer_ignore + 1;
-      }
-      Target_VS = getprop(Settings, "target-pressure-rate");
-      setprop(Settings, "target-pressure-rate", Target_VS +
-              0.0017241379310345);
-      setprop(Annunciators, "alt-number", "off");
-      setprop(Annunciators, "vs-number", "on");
-    }
-    elsif (getprop(Locks, "pitch-mode") == "alt")
-    {
-      Target_Pressure = getprop(Settings, "target-alt-pressure");
-      setprop(Settings, "target-alt-pressure", Target_Pressure + 0.0206);
-    }
-  }
-}
-
-up_button = func {
-  ##print("up_button");
-
-  if (baro_timer_running == 0)
-  {
-    if (getprop(Locks, "pitch-mode") == "vs")
-    {
-      if (alt_button_timer_running == 0)
-      {
-        settimer(alt_button_timer, 3.0);
-        alt_button_timer_running = 1;
-        alt_button_timer_ignore = 0;
-      }
-      elsif (alt_button_timer_running == 1)
-      {
-          settimer(alt_button_timer, 3.0);
-          alt_button_timer_ignore = alt_button_timer_ignore + 1;
-      }
-      Target_VS = getprop(Settings, "target-pressure-rate");
-      setprop(Settings, "target-pressure-rate", Target_VS -
-              0.0017241379310345);
-      setprop(Annunciators, "alt-number", "off");
-      setprop(Annunciators, "vs-number", "on");
-    }
-    elsif (getprop(Locks, "pitch-mode") == "alt")
-    {
-      Target_Pressure = getprop(Settings, "target-alt-pressure");
-      setprop(Settings, "target-alt-pressure", Target_Pressure - 0.0206);
-    }
-  }
-}
-
-arm_button = func {
-  #print("arm button");
-  
-  pitch_arm = getprop(Locks, "pitch-arm");
-
-  if (pitch_arm == "off")
-  {
-    setprop(Locks, "pitch-arm", "alt-arm");
-
-    setprop(Annunciators, "alt-arm", "on");
-  }
-  elsif (pitch_arm == "alt-arm")
-  {
-    setprop(Locks, "pitch-arm", "off");
-
-    setprop(Annunciators, "alt-arm", "off");
-  }
-}
-
-
-baro_button_timer = func {
-  #print("baro button timer");
-
-  baro_timer_running = 0;
-  if (baro_button_down == 1)
-  {
-    baro_setting_unit = !baro_setting_unit;
-    baro_button_down = 0;
-    baro_button_press();
-  }
-  elsif (baro_button_down == 0 and
-         baro_setting_adjusting == 0)
-  {
-    setprop(Annunciators, "bs-hpa-number", "off");
-    setprop(Annunciators, "bs-inhg-number", "off");
-    setprop(Annunciators, "alt-number", "on");
-  }
-  elsif (baro_setting_adjusting == 1)
-  {
-    baro_timer_running = 1;
-    baro_setting_adjusting = 0;
-    settimer(baro_button_timer, 3.0);
-  }
-}
-
-baro_button_press = func {
-  #print("baro putton press");
-
-  if (baro_button_down == 0 and
-      baro_timer_running == 0 and
-      alt_button_timer_running == 0)
-  {
-    baro_button_down = 1;
-    baro_timer_running = 1;
-    settimer(baro_button_timer, 3.0);
-    setprop(Annunciators, "alt-number", "off");
-    
-    if (baro_setting_unit == 0)
-    {
-      setprop(Settings, "baro-setting-inhg", baro_setting);
-      
-      setprop(Annunciators, "bs-inhg-number", "on");
-      setprop(Annunciators, "bs-hpa-number", "off");
-    }
-    elsif (baro_setting_unit == 1)
-    {
-      setprop(Settings, "baro-setting-hpa",
-              baro_setting * 0.03386389);
-              
-      setprop(Annunciators, "bs-hpa-number", "on");
-      setprop(Annunciators, "bs-inhg-number", "off");
-    }
-  }
-}
-
-
-baro_button_release = func {
-  #print("baro button release");
-
-  baro_button_down = 0;
-}
-
-
-pow = func {
-  #print(arg[0],arg[1]);
-  return math.exp(arg[1]*math.ln(arg[0]));
-}
-
-
-pressureToHeight = func {
-  p0 = arg[1];    # [Pa]
-  p = arg[0];     # [Pa]
-  t0 = 288.15;    # [K]
-  LR = -0.0065;    # [K/m]
-  g = -9.80665;    # [m/s²]
-  Rd = 287.05307; # [J/kg K]
-  
-  z = -(t0/LR) * (1.0-pow((p/p0),((Rd*LR)/g)));
-  return z;
-}
-
-
-heightToPressure = func {
-  p0 = arg[1];    # [Pa]
-  z = arg[0];     # [m]
-  t0 = 288.15;    # [K]
-  LR = -0.0065;    # [K/m]
-  g = -9.80665;    # [m/s²]
-  Rd = 287.05307; # [J/kg K]
-  
-  p = p0 * pow(((t0+LR*z)/t0),(g/(Rd*LR)));
-  return p;
-}
-
-
-alt_alert = func {
-  #print("alt alert");
-  
-  alt_pressure = getprop("/systems/static/pressure-inhg");
-  alt_m = pressureToHeight(alt_pressure*3386.389, 
-                           baro_setting*3386.389);
-  #print(alt_m);
-
-  alt_ft = alt_m / 0.3048006;
-  #print(alt_ft);
-  alt_difference = abs(alt_preselect - alt_ft);
-  #print(alt_difference);
-
-  if (alt_difference > 1000)
-  {
-    setprop(Annunciators, "alt-alert", "off");
-  }
-  elsif (alt_difference < 1000 and
-         alt_captured == 0)
-  {
-    if (flash_timer < -0.5) {
-      setprop(Annunciators, "alt-alert", "on"); }
-    if (alt_difference < 200)
-    {
-      if (flash_timer < -0.5) {
-        setprop(Annunciators, "alt-alert", "off"); }
-      if (alt_difference < 20)
-      {
-        #print("alt_capture()");
-        alt_captured = 1;
-
-        if (getprop(Locks, "pitch-arm") == "alt-arm")
-        {
-          setprop(Locks, "alt-hold", "alt");
-          setprop(Locks, "pitch-axis", "vs");
-          setprop(Locks, "pitch-mode", "alt");
-          setprop(Locks, "pitch-arm", "off");
-
-          setprop(Annunciators, "alt", "on");
-          setprop(Annunciators, "alt-arm", "off");
-          setprop(Annunciators, "vs", "off");
-          setprop(Annunciators, "vs-number", "off");
-          setprop(Annunciators, "alt-number", "on");
-
-          #alt_pressure = baro_setting - alt_preselect * 0.00103;
-          alt_pressure = heightToPressure(alt_preselect*0.3048006,
-                                          baro_setting*3386.389)/3386.389;
-          setprop(Settings, "target-alt-pressure", alt_pressure);
-        }
-
-        flasher("alt-alert", 1.0, 0, "off");
-      }
-    }
-  }
-  elsif (alt_difference < 1000 and
-         alt_captured == 1)
-  {
-    if (alt_difference > 200)
-    {
-      flasher("alt-alert", 1.0, 5, "on");
-      alt_captured = 0;
-    }
-  }
-  settimer(alt_alert, 2.0);
-}
-    
-
-knob_s_up = func {
-  #print("knob small up");
-
-  if (baro_timer_running == 1)
-  {
-    baro_setting_adjusting = 1;
-    if (baro_setting_unit == 0)
-    {
-      baro_setting = baro_setting + 0.01;
-      baro_setting_hpa = baro_setting * 0.03386389;
-
-      setprop(Settings, "baro-setting-inhg", baro_setting);
-    }
-    elsif (baro_setting_unit == 1)
-    {
-      baro_setting_hpa = baro_setting * 0.03386389;
-      baro_setting_hpa = baro_setting_hpa + 0.001;
-      baro_setting = baro_setting_hpa / 0.03386389;
-
-      setprop(Settings, "baro-setting-hpa", baro_setting_hpa);
-    }
-  }
-  elsif (baro_timer_running == 0 and
-         alt_button_timer_running == 0)
-  {
-    alt_captured = 0;
-    alt_preselect = alt_preselect + 20;
-    setprop(Settings, "target-alt-ft", alt_preselect);
-
-    if (getprop(Locks, "roll-mode") == "off" and
-        getprop(Locks, "pitch-mode") == "off")
-    {
-      setprop(Annunciators, "alt-number", "on");
-      if (alt_alert_on == 0)
-      {
-        alt_alert_on = 1;
-      }
-    }
-    elsif (getprop(Locks, "pitch-arm") == "off")
-    {
-      setprop(Locks, "pitch-arm", "alt-arm");
-      setprop(Annunciators, "alt-arm", "on");
-    }
-  }
-}
-
-
-knob_l_up = func {
-  #print("knob large up");
-
-  if (baro_timer_running == 1)
-  {
-    baro_setting_adjusting = 1;
-    if (baro_setting_unit == 0)
-    {
-      baro_setting = baro_setting + 1.0;
-      baro_setting_hpa = baro_setting * 0.03386389;
-
-      setprop(Settings, "baro-setting-inhg", baro_setting);
-    }
-    elsif (baro_setting_unit == 1)
-    {
-      baro_setting_hpa = baro_setting * 0.03386389;
-      baro_setting_hpa = baro_setting_hpa + 0.1;
-      baro_setting = baro_setting_hpa / 0.03386389;
-
-      setprop(Settings, "baro-setting-hpa", baro_setting_hpa);
-    }
-  }
-  elsif (baro_timer_running == 0 and
-         alt_button_timer_running == 0)
-  {
-    alt_captured = 0;
-    alt_preselect = alt_preselect + 100;
-    setprop(Settings, "target-alt-ft", alt_preselect);
-  
-    if (getprop(Locks, "roll-mode") == "off" and
-        getprop(Locks, "pitch-mode") == "off")
-    {
-      setprop(Annunciators, "alt-number", "on");
-      if (alt_alert_on == 0)
-      {
-        alt_alert_on = 1;
-      }
-    }
-    elsif (getprop(Locks, "pitch-arm") == "off")
-    {
-      setprop(Locks, "pitch-arm", "alt-arm");
-      setprop(Annunciators, "alt-arm", "on");
-    }
-  }
-}
-
-
-knob_s_dn = func {
-  #print("knob small down");
-
-  if (baro_timer_running == 1)
-  {
-    baro_setting_adjusting = 1;
-    if (baro_setting_unit == 0)
-    {
-      baro_setting = baro_setting - 0.01;
-      baro_setting_hpa = baro_setting * 0.03386389;
-
-      setprop(Settings, "baro-setting-inhg", baro_setting);
-    }
-    elsif (baro_setting_unit == 1)
-    {
-      baro_setting_hpa = baro_setting * 0.03386389;
-      baro_setting_hpa = baro_setting_hpa - 0.001;
-      baro_setting = baro_setting_hpa / 0.03386389;
-
-      setprop(Settings, "baro-setting-hpa", baro_setting_hpa);
-    }
-  }
-  elsif (baro_timer_running == 0 and
-         alt_button_timer_running == 0)
-  {
-    alt_captured = 0;
-    alt_preselect = alt_preselect - 20;
-    setprop(Settings, "target-alt-ft", alt_preselect);
- 
-    if (getprop(Locks, "roll-mode") == "off" and
-        getprop(Locks, "pitch-mode") == "off")
-    {
-      setprop(Annunciators, "alt-number", "on");
-      if (alt_alert_on == 0)
-      {
-        alt_alert_on = 1;
-      }
-    }
-    elsif (getprop(Locks, "pitch-arm") == "off")
-    {
-      setprop(Locks, "pitch-arm", "alt-arm");
-      setprop(Annunciators, "alt-arm", "on");
-    }
-  }
-}
-
-
-knob_l_dn = func {
-  #print("knob large down");
-
-  if (baro_timer_running == 1)
-  {
-    baro_setting_adjusting = 1;
-    if (baro_setting_unit == 0)
-    {
-      baro_setting = baro_setting - 1.0;
-      baro_setting_hpa = baro_setting * 0.03386389;
-
-      setprop(Settings, "baro-setting-inhg", baro_setting);
-    }
-    elsif (baro_setting_unit == 1)
-    {
-      baro_setting_hpa = baro_setting * 0.03386389;
-      baro_setting_hpa = baro_setting_hpa - 0.1;
-      baro_setting = baro_setting_hpa / 0.03386389;
-
-      setprop(Settings, "baro-setting-hpa", baro_setting_hpa);
-    }
-  }
-  elsif (baro_timer_running == 0 and
-         alt_button_timer_running == 0)
-  {
-    alt_captured = 0;
-    alt_preselect = alt_preselect - 100;
-    setprop(Settings, "target-alt-ft", alt_preselect);
-
-    if (getprop(Locks, "roll-mode") == "off" and
-        getprop(Locks, "pitch-mode") == "off")
-    {
-      setprop(Annunciators, "alt-number", "on");
-      if (alt_alert_on == 0)
-      {
-        alt_alert_on = 1;
-      }
-    }
-    elsif (getprop(Locks, "pitch-arm") == "off")
-    {
-      setprop(Locks, "pitch-arm", "alt-arm");
-      setprop(Annunciators, "alt-arm", "on");
-    }
-  }
-}
-
-
-ap_init();
-
-alt_alert();