]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/ATC-Inputs.cxx
remove redundant vector::clear(). A just created vector *is* clear.
[flightgear.git] / src / Network / ATC-Inputs.cxx
index 3209827e0dc488760eba0df36b49c45d9a4416e9..369a2d3bc15d0b3e990a10b1fc6199d7bcf7787a 100644 (file)
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
@@ -31,6 +31,8 @@
 #  include <sys/types.h>
 #  include <sys/stat.h>
 #  include <fcntl.h>
+#  include <unistd.h>
+#  include <istream>
 #endif
 
 #include <errno.h>
@@ -222,7 +224,8 @@ bool FGATCInput::open() {
 /////////////////////////////////////////////////////////////////////
 
 // scale a number between min and max (with center defined) to a scale
-// from -1.0 to 1.0
+// from -1.0 to 1.0.  The deadband value is symmetric, so specifying
+// '1' will give you a deadband of +/-1
 static double scale( int center, int deadband, int min, int max, int value ) {
     // cout << center << " " << min << " " << max << " " << value << " ";
     double result;
@@ -231,9 +234,11 @@ static double scale( int center, int deadband, int min, int max, int value ) {
     if ( value <= (center - deadband) ) {
         range = (center - deadband) - min;
         result = (value - (center - deadband)) / range;
-    } else {
+    } else if ( value >= (center + deadband) ) {
         range = max - (center + deadband);
         result = (value - (center + deadband)) / range;            
+    } else {
+        result = 0.0;
     }
 
     if ( result < -1.0 ) result = -1.0;
@@ -263,6 +268,18 @@ static double scale( int min, int max, int value ) {
 }
 
 
+static double clamp( double min, double max, double value ) {
+    double result = value;
+
+    if ( result < min ) result = min;
+    if ( result > max ) result = max;
+
+    // cout << result << endl;
+
+    return result;   
+}
+
+
 static int tony_magic( int raw, int obs[3] ) {
     int result = 0;
 
@@ -397,11 +414,12 @@ bool FGATCInput::do_analog_in() {
             string name = "";
             string type = "";
             string subtype = "";
-            vector <SGPropertyNode *> output_nodes; output_nodes.clear();
+            vector <SGPropertyNode *> output_nodes;
             int center = -1;
             int min = 0;
             int max = 1023;
            int deadband = 0;
+            float offset = 0.0;
             float factor = 1.0;
             if ( cname == "channel" ) {
                 SGPropertyNode *prop;
@@ -440,6 +458,10 @@ bool FGATCInput::do_analog_in() {
                 if ( prop != NULL ) {
                     deadband = prop->getIntValue();
                 }
+                prop = child->getChild( "offset" );
+                if ( prop != NULL ) {
+                    offset = prop->getFloatValue();
+                }
                 prop = child->getChild( "factor" );
                 if ( prop != NULL ) {
                     factor = prop->getFloatValue();
@@ -467,6 +489,14 @@ bool FGATCInput::do_analog_in() {
                             scaled_value = scale( min, max, raw_value );
                         }
                         scaled_value *= factor;
+                        scaled_value += offset;
+
+                        // final sanity clamp
+                        if ( center >= 0 ) {
+                            scaled_value = clamp( -1.0, 1.0, scaled_value );
+                        } else {
+                            scaled_value = clamp( 0.0, 1.0, scaled_value );
+                        }
 
                         // update the property tree values
                         for ( j = 0; j < (int)output_nodes.size(); ++j ) {
@@ -483,6 +513,14 @@ bool FGATCInput::do_analog_in() {
                         scaled_value = scale( min, max, raw_value );
                     }
                     scaled_value *= factor;
+                    scaled_value += offset;
+
+                    // final sanity clamp
+                    if ( center >= 0 ) {
+                        scaled_value = clamp( -1.0, 1.0, scaled_value );
+                    } else {
+                        scaled_value = clamp( 0.0, 1.0, scaled_value );
+                    }
 
                     // update the property tree values
                     for ( j = 0; j < (int)output_nodes.size(); ++j ) {
@@ -622,7 +660,7 @@ bool FGATCInput::do_switches() {
             string cname = child->getName();
             string name = "";
             string type = "";
-            vector <SGPropertyNode *> output_nodes; output_nodes.clear();
+            vector <SGPropertyNode *> output_nodes;
             int row = -1;
             int col = -1;
             float factor = 1.0;
@@ -825,7 +863,7 @@ bool FGATCInput::do_radio_switches() {
             if ( cname == "switch" ) {
                 string name = "";
                 string type = "";
-                vector <SGPropertyNode *> output_nodes; output_nodes.clear();
+                vector <SGPropertyNode *> output_nodes;
                 int byte_num = -1;
                 int right_shift = 0;
                 int mask = 0xff;