]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/atc610x.cxx
Fixed some stupidity.
[flightgear.git] / src / Network / atc610x.cxx
index f2a9a34d4c0878d059320fe78a628744b9b16dfd..edec4630cfc9da6abda40a8794cba7ac13dfe192 100644 (file)
@@ -44,6 +44,7 @@
 #include <simgear/io/iochannel.hxx>
 #include <simgear/math/sg_types.hxx>
 #include <simgear/misc/props.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
@@ -204,6 +205,19 @@ void ATC610xSetLamp( int fd, int channel, bool value ) {
 }
 
 
+void FGATC610x::init_config() {
+#if defined( unix ) || defined( __CYGWIN__ )
+    // Next check home directory for .fgfsrc.hostname file
+    char *envp = ::getenv( "HOME" );
+    if ( envp != NULL ) {
+        SGPath atc610x_config( envp );
+        atc610x_config.append( ".fgfs-atc610x.xml" );
+        readProperties( atc610x_config.str(), globals->get_props() );
+    }
+#endif
+}
+
+
 // Open and initialize ATC 610x hardware
 bool FGATC610x::open() {
     if ( is_enabled() ) {
@@ -212,6 +226,9 @@ bool FGATC610x::open() {
        return false;
     }
 
+    // This loads the config parameters generated by "simcal"
+    init_config();
+
     SG_LOG( SG_IO, SG_ALERT,
            "Initializing ATC 610x hardware, please wait ..." );
 
@@ -434,6 +451,34 @@ bool FGATC610x::open() {
     xpdr_sby_ann = fgGetNode( "/radios/kt-70/annunciators/sby", true );
     xpdr_reply_ann = fgGetNode( "/radios/kt-70/annunciators/reply", true );
 
+    elevator_center = fgGetNode( "/input/atc610x/elevator/center", 0 );
+    elevator_min = fgGetNode( "/input/atc610x/elevator/min", 0 );
+    elevator_max = fgGetNode( "/input/atc610x/elevator/max", 0 );
+
+    ailerons_center = fgGetNode( "/input/atc610x/ailerons/center", 0 );
+    ailerons_min = fgGetNode( "/input/atc610x/ailerons/min", 0 );
+    ailerons_max = fgGetNode( "/input/atc610x/ailerons/max", 0 );
+
+    rudder_center = fgGetNode( "/input/atc610x/rudder/center", 0 );
+    rudder_min = fgGetNode( "/input/atc610x/rudder/min", 0 );
+    rudder_max = fgGetNode( "/input/atc610x/rudder/max", 0 );
+
+    throttle_min = fgGetNode( "/input/atc610x/throttle/min", 0 );
+    throttle_max = fgGetNode( "/input/atc610x/throttle/max", 0 );
+
+    mixture_min = fgGetNode( "/input/atc610x/mixture/min", 0 );
+    mixture_max = fgGetNode( "/input/atc610x/mixture/max", 0 );
+
+    trim_center = fgGetNode( "/input/atc610x/trim/center", 0 );
+    trim_min = fgGetNode( "/input/atc610x/trim/min", 0 );
+    trim_max = fgGetNode( "/input/atc610x/trim/max", 0 );
+
+    nav1vol_min = fgGetNode( "/input/atc610x/nav1vol/min", 0 );
+    nav1vol_max = fgGetNode( "/input/atc610x/nav1vol/max", 0 );
+
+    nav2vol_min = fgGetNode( "/input/atc610x/nav2vol/min", 0 );
+    nav2vol_max = fgGetNode( "/input/atc610x/nav2vol/max", 0 );
+
     return true;
 }
 
@@ -447,6 +492,48 @@ bool FGATC610x::open() {
 #define ATC_ELEVATOR_CENTER 543
 #define ATC_RUDDER_CENTER 519
 
+// scale a number between min and max (with center defined) to a scale
+// from -1.0 to 1.0
+static double scale( int center, int min, int max, int value ) {
+    // cout << center << " " << min << " " << max << " " << value << " ";
+    double result;
+    double range;
+
+    if ( value <= center ) {
+        range = center - min;
+        result = (value - center) / range;
+    } else {
+        range = max - center;
+        result = (value - center) / range;            
+    }
+
+    if ( result < -1.0 ) result = -1.0;
+    if ( result > 1.0 ) result = 1.0;
+
+    // cout << result << endl;
+
+    return result;
+}
+
+
+// scale a number between min and max to a scale from 0.0 to 1.0
+static double scale( int min, int max, int value ) {
+    // cout << center << " " << min << " " << max << " " << value << " ";
+    double result;
+    double range;
+
+    range = max - min;
+    result = (value - min) / range;
+
+    if ( result < 0.0 ) result = 0.0;
+    if ( result > 1.0 ) result = 1.0;
+
+    // cout << result << endl;
+
+    return result;
+}
+
+
 bool FGATC610x::do_analog_in() {
     // Read raw data in byte form
     ATC610xReadAnalogInputs( analog_in_fd, analog_in_bytes );
@@ -464,33 +551,41 @@ bool FGATC610x::do_analog_in() {
     float tmp, tmp1, tmp2;
 
     // aileron
-    tmp = (float)(analog_in_data[0] - ATC_AILERON_CENTER) / 256.0f;
+    tmp = scale( ailerons_center->getIntValue(), ailerons_min->getIntValue(),
+                 ailerons_max->getIntValue(), analog_in_data[0] );
     fgSetFloat( "/controls/aileron", tmp );
     // cout << "aileron = " << analog_in_data[0] << " = " << tmp;
 
     // elevator
-    tmp = (float)(analog_in_data[4] - ATC_ELEVATOR_TRIM_CENTER) / 512.0f;
-    fgSetFloat( "/controls/elevator-trim", tmp );
+    tmp = -scale( elevator_center->getIntValue(), elevator_min->getIntValue(),
+                  elevator_max->getIntValue(), analog_in_data[5] );
+    fgSetFloat( "/controls/elevator", tmp );
     // cout << "trim = " << analog_in_data[4] << " = " << tmp;
 
-    // trim
-    tmp = (float)(ATC_ELEVATOR_CENTER - analog_in_data[5]) / 100.0f;
-    fgSetFloat( "/controls/elevator", tmp );
+    // elevator trim
+    tmp = scale( trim_center->getIntValue(), trim_min->getIntValue(),
+                 trim_max->getIntValue(), analog_in_data[4] );
+    fgSetFloat( "/controls/elevator-trim", tmp );
     // cout << " elev = " << analog_in_data[5] << " = " << tmp << endl;
 
     // mixture
-    tmp = (float)analog_in_data[7] / 680.0f;
+    tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
+                 analog_in_data[7] );
     fgSetFloat( "/controls/mixture[0]", tmp );
     fgSetFloat( "/controls/mixture[1]", tmp );
 
     // throttle
-    tmp = ((float)analog_in_data[8] - 141.0) / 632.0f;
+    tmp = scale( mixture_min->getIntValue(), mixture_max->getIntValue(),
+                 analog_in_data[8] );
     fgSetFloat( "/controls/throttle[0]", tmp );
     fgSetFloat( "/controls/throttle[1]", tmp );
 
+#if 0
     // rudder
-    tmp = (float)(ATC_RUDDER_CENTER - analog_in_data[10]) / 145.0f;
+    tmp = scale( rudder_center->getIntValue(), rudder_min->getIntValue(),
+                 rudder_max->getIntValue(), analog_in_data[10] );
     fgSetFloat( "/controls/rudder", tmp );
+#endif
 
     // nav1 volume
     tmp = (float)analog_in_data[25] / 1024.0f;
@@ -571,6 +666,10 @@ bool FGATC610x::do_radio_switches() {
        fgSetInt( "/radios/dme/switch-position", 3 );
     }
 
+    // Com1 Power
+    fgSetBool( "/radios/comm[0]/inputs/power-btn",
+               radio_switch_data[7] & 0x01 );
+
     // Com1 Swap
     int com1_swap = !((radio_switch_data[7] >> 1) & 0x01);
     static int last_com1_swap;
@@ -582,6 +681,10 @@ bool FGATC610x::do_radio_switches() {
     }
     last_com1_swap = com1_swap;
 
+    // Com2 Power
+    fgSetBool( "/radios/comm[1]/inputs/power-btn",
+               radio_switch_data[15] & 0x01 );
+
     // Com2 Swap
     int com2_swap = !((radio_switch_data[15] >> 1) & 0x01);
     static int last_com2_swap;
@@ -900,7 +1003,7 @@ bool FGATC610x::do_radio_switches() {
     fgSetInt( "/radios/kr-87/inputs/set-rst-btn",
               !(radio_switch_data[23] >> 4 & 0x01) );
     fgSetInt( "/radios/kr-87/inputs/power-btn",
-              radio_switch_data[23] >> 4 & 0x01 );
+              radio_switch_data[23] >> 5 & 0x01 );
     /* cout << "adf = " << !(radio_switch_data[23] & 0x01)
          << " bfo = " << !(radio_switch_data[23] >> 1 & 0x01)
          << " stby = " << !(radio_switch_data[23] >> 2 & 0x01)
@@ -1140,7 +1243,7 @@ bool FGATC610x::do_radio_display() {
     // turns on the decimal point
 
     // ADF standby frequency / timer
-    if ( adf_vol->getDoubleValue() >= 0.01 ) {
+    if ( adf_power->getBoolValue() ) {
         if ( adf_stby_mode->getIntValue() == 0 ) {
             // frequency
             float adf_stby = adf_stby_freq->getFloatValue();