]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/ATC-Inputs.cxx
Add an "additive-switch" type.
[flightgear.git] / src / Network / ATC-Inputs.cxx
index 1c97098b2e8541c9668b964a125c95059ef85958..2f63b1be462098884fb150f2cc7bb33cfc6337fe 100644 (file)
 
 #include <simgear/compiler.h>
 
+#if defined( unix ) || defined( __CYGWIN__ )
+#  include <sys/types.h>
+#  include <sys/stat.h>
+#  include <fcntl.h>
+#endif
+
 #include STL_STRING
 
 #include <simgear/debug/logstream.hxx>
@@ -57,6 +63,7 @@ FGATCInput::FGATCInput( const int _board, const SGPath &_config_file ) :
 
 // Read analog inputs
 static void ATCReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
+#if defined( unix ) || defined( __CYGWIN__ )
     // rewind
     lseek( fd, 0, SEEK_SET );
 
@@ -65,11 +72,13 @@ static void ATCReadAnalogInputs( int fd, unsigned char *analog_in_bytes ) {
        SG_LOG( SG_IO, SG_ALERT, "Read failed" );
        exit( -1 );
     }
+#endif
 }
 
 
 // Read status of radio switches and knobs
 static void ATCReadRadios( int fd, unsigned char *switch_data ) {
+#if defined( unix ) || defined( __CYGWIN__ )
     // rewind
     lseek( fd, 0, SEEK_SET );
 
@@ -78,11 +87,13 @@ static void ATCReadRadios( int fd, unsigned char *switch_data ) {
        SG_LOG( SG_IO, SG_ALERT, "Read failed" );
        exit( -1 );
     }
+#endif
 }
 
 
 // Read switch inputs
 static void ATCReadSwitches( int fd, unsigned char *switch_bytes ) {
+#if defined( unix ) || defined( __CYGWIN__ )
     // rewind
     lseek( fd, 0, SEEK_SET );
 
@@ -91,6 +102,7 @@ static void ATCReadSwitches( int fd, unsigned char *switch_bytes ) {
        SG_LOG( SG_IO, SG_ALERT, "Read failed" );
        exit( -1 );
     }
+#endif
 }
 
 
@@ -124,26 +136,21 @@ bool FGATCInput::open() {
     init_config();
 
     SG_LOG( SG_IO, SG_ALERT,
-           "Initializing ATC 610x hardware, please wait ..." );
+           "Initializing ATC input hardware, please wait ..." );
 
-    snprintf( lock_file, 256, "/proc/atc610x/board%d/lock", board );
-    snprintf( analog_in_file, 256, "/proc/atc610x/board%d/analog_in", board );
-    snprintf( radios_file, 256, "/proc/atc610x/board%d/radios", board );
-    snprintf( switches_file, 256, "/proc/atc610x/board%d/switches", board );
+    snprintf( analog_in_file, 256,
+              "/proc/atcflightsim/board%d/analog_in", board );
+    snprintf( radios_file, 256,
+              "/proc/atcflightsim/board%d/radios", board );
+    snprintf( switches_file, 256,
+              "/proc/atcflightsim/board%d/switches", board );
+
+#if defined( unix ) || defined( __CYGWIN__ )
 
     /////////////////////////////////////////////////////////////////////
     // Open the /proc files
     /////////////////////////////////////////////////////////////////////
 
-    lock_fd = ::open( lock_file, O_RDWR );
-    if ( lock_fd == -1 ) {
-       SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
-       char msg[256];
-       snprintf( msg, 256, "Error opening %s", lock_file );
-       perror( msg );
-       exit( -1 );
-    }
-
     analog_in_fd = ::open( analog_in_file, O_RDONLY );
     if ( analog_in_fd == -1 ) {
        SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
@@ -171,12 +178,14 @@ bool FGATCInput::open() {
        exit( -1 );
     }
 
+#endif
+
     /////////////////////////////////////////////////////////////////////
     // Finished initing hardware
     /////////////////////////////////////////////////////////////////////
 
     SG_LOG( SG_IO, SG_ALERT,
-           "Done initializing ATC 610x hardware." );
+           "Done initializing ATC input hardware." );
 
     is_open = true;
 
@@ -608,6 +617,7 @@ bool FGATCInput::do_switches() {
             float factor = 1.0;
             int filter = -1;
             float scaled_value = 0.0f;
+           bool invert = false;
 
             // get common options
 
@@ -631,6 +641,10 @@ bool FGATCInput::do_switches() {
             if ( prop != NULL ) {
                 factor = prop->getFloatValue();
             }
+            prop = child->getChild( "invert" );
+            if ( prop != NULL ) {
+                invert = prop->getBoolValue();
+            }
             prop = child->getChild( "steady-state-filter" );
             if ( prop != NULL ) {
                 filter = prop->getIntValue();
@@ -651,6 +665,11 @@ bool FGATCInput::do_switches() {
                 // Fetch the raw value
                 int raw_value = switch_matrix[board][row][col];
 
+               // Invert
+               if ( invert ) {
+                   raw_value = !raw_value;
+               }
+
                 // Cook the value
                 scaled_value = (float)raw_value * factor;
 
@@ -690,6 +709,42 @@ bool FGATCInput::do_switches() {
 
                 // Cook the value
                 scaled_value *= factor;
+            } else if ( cname == "additive-switch" ) {
+                float additive_value = 0.0f;
+               float increment = 0.0f;
+
+                SGPropertyNode *pos;
+                int k = 0;
+                while ( (pos = child->getChild("position", k++)) != NULL ) {
+                    // read the combo position entries from the property tree
+
+                    prop = pos->getChild( "row" );
+                    if ( prop != NULL ) {
+                        row = prop->getIntValue();
+                    }
+                    prop = pos->getChild( "col" );
+                    if ( prop != NULL ) {
+                        col = prop->getIntValue();
+                    }
+                    prop = pos->getChild( "value" );
+                    if ( prop != NULL ) {
+                        increment = prop->getFloatValue();
+                    }
+
+                    // Fetch the raw value
+                    int raw_value = switch_matrix[board][row][col];
+                    // cout << "sm[" << board << "][" << row << "][" << col
+                    //      << "] = " << raw_value << endl;
+
+                    if ( raw_value ) {
+                        // set scaled_value to the first combo_value
+                        // that matches and jump out of loop.
+                        additive_value += increment;
+                    }
+                }
+
+                // Cook the value
+                scaled_value = additive_value * factor;
             }
 
             // handle filter request.  The value of the switch must be
@@ -761,12 +816,12 @@ bool FGATCInput::do_radio_switches() {
                 string type = "";
                 vector <SGPropertyNode *> output_nodes; output_nodes.clear();
                 int byte_num = -1;
-                int right_shift = -1;
+                int right_shift = 0;
                 int mask = 0xff;
                 int factor = 1;
                 int offset = 0;
+                bool invert = false;
                 int scaled_value = 0;
-
                 // get common options
 
                 SGPropertyNode *prop;
@@ -805,12 +860,19 @@ bool FGATCInput::do_radio_switches() {
                 if ( prop != NULL ) {
                     offset = prop->getIntValue();
                 }
+                prop = child->getChild( "invert" );
+                if ( prop != NULL ) {
+                    invert = prop->getBoolValue();
+                }
 
                 // Fetch the raw value
                 int raw_value
                     = (radio_switch_data[byte_num] >> right_shift) & mask;
 
                 // Cook the value
+                if ( invert ) {
+                    raw_value = !raw_value;
+                }
                 scaled_value = raw_value * factor + offset;
 
                 // update the property tree values
@@ -843,17 +905,15 @@ bool FGATCInput::process() {
 
 
 bool FGATCInput::close() {
-    int result;
 
-    result = ::close( lock_fd );
-    if ( result == -1 ) {
-       SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
-       char msg[256];
-       snprintf( msg, 256, "Error closing %s", lock_file );
-       perror( msg );
-       exit( -1 );
+#if defined( unix ) || defined( __CYGWIN__ )
+
+    if ( !is_open ) {
+        return true;
     }
 
+    int result;
+
     result = ::close( analog_in_fd );
     if ( result == -1 ) {
        SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
@@ -881,5 +941,7 @@ bool FGATCInput::close() {
        exit( -1 );
     }
 
+#endif
+
     return true;
 }