]> git.mxchange.org Git - flightgear.git/blobdiff - src/Network/native_gui.cxx
Merge branch 'next' of http://git.gitorious.org/fg/flightgear into next
[flightgear.git] / src / Network / native_gui.cxx
index 5dbcf9520c0e8617f6b4d50b72e52e084a373601..eaae3c32a0adbabb07b8242d5621b88d46f7616a 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started January 2002.
 //
-// Copyright (C) 2002  Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 2002  Curtis L. Olson - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -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$
 
 #include <simgear/io/iochannel.hxx>
 #include <simgear/timing/sg_time.hxx>
 
-#include <FDM/flight.hxx>
-#include <Time/tmp.hxx>
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
+#include <Scenery/scenery.hxx>
+#include <FDM/flightProperties.hxx>
 
 #include "native_gui.hxx"
 
 // FreeBSD works better with this included last ... (?)
-#if defined(WIN32) && !defined(__CYGWIN__)
+#if defined( _MSC_VER )
 #  include <windows.h>
+#elif defined( __MINGW32__ )
+#  include <winsock2.h>
 #else
 #  include <netinet/in.h>      // htonl() ntohl()
 #endif
 
 
+// #define FG_USE_NETWORK_BYTE_ORDER
+#if defined( FG_USE_NETWORK_BYTE_ORDER )
+
 // The function htond is defined this way due to the way some
 // processors and OSes treat floating point values.  Some will raise
 // an exception whenever a "bad" floating point value is loaded into a
@@ -69,6 +74,21 @@ static void htond (double &x)
         return;
     }
 }
+static void htonf (float &x)   
+{
+    if ( sgIsLittleEndian() ) {
+        int    *Float_Overlay;
+        int     Holding_Buffer;
+    
+        Float_Overlay = (int *) &x;
+        Holding_Buffer = Float_Overlay [0];
+    
+        Float_Overlay [0] = htonl (Holding_Buffer);
+    } else {
+        return;
+    }
+}
+#endif
 
 
 FGNativeGUI::FGNativeGUI() {
@@ -95,28 +115,46 @@ bool FGNativeGUI::open() {
 
     set_enabled( true );
 
-    cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
+    fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
     return true;
 }
 
 
 void FGProps2NetGUI( FGNetGUI *net ) {
-    int i;
+    static SGPropertyNode *nav_freq
+       = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
+    static SGPropertyNode *nav_target_radial
+       = fgGetNode("/instrumentation/nav/radials/target-radial-deg", true);
+    static SGPropertyNode *nav_inrange
+       = fgGetNode("/instrumentation/nav/in-range", true);
+    static SGPropertyNode *nav_loc
+       = fgGetNode("/instrumentation/nav/nav-loc", true);
+    static SGPropertyNode *nav_gs_dist_signed
+       = fgGetNode("/instrumentation/nav/gs-distance", true);
+    static SGPropertyNode *nav_loc_dist
+       = fgGetNode("/instrumentation/nav/nav-distance", true);
+    static SGPropertyNode *nav_reciprocal_radial
+       = fgGetNode("/instrumentation/nav/radials/reciprocal-radial-deg", true);
+    static SGPropertyNode *nav_gs_deflection
+       = fgGetNode("/instrumentation/nav/gs-needle-deflection", true);
+    unsigned int i;
+
+    static FlightProperties* fdm_state = new FlightProperties;
 
     // Version sanity checking
     net->version = FG_NET_GUI_VERSION;
 
     // Aero parameters
-    net->longitude = cur_fdm_state->get_Longitude();
-    net->latitude = cur_fdm_state->get_Latitude();
-    net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
-    net->phi = cur_fdm_state->get_Phi();
-    net->theta = cur_fdm_state->get_Theta();
-    net->psi = cur_fdm_state->get_Psi();
+    net->longitude = fdm_state->get_Longitude();
+    net->latitude = fdm_state->get_Latitude();
+    net->altitude = fdm_state->get_Altitude() * SG_FEET_TO_METER;
+    net->phi = fdm_state->get_Phi();
+    net->theta = fdm_state->get_Theta();
+    net->psi = fdm_state->get_Psi();
 
     // Velocities
-    net->vcas = cur_fdm_state->get_V_calibrated_kts();
-    net->climb_rate = cur_fdm_state->get_Climb_Rate();
+    net->vcas = fdm_state->get_V_calibrated_kts();
+    net->climb_rate = fdm_state->get_Climb_Rate();
 
     // Consumables
     net->num_tanks = FGNetGUI::FG_MAX_TANKS;
@@ -125,69 +163,141 @@ void FGProps2NetGUI( FGNetGUI *net ) {
         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
     }
 
-    // the following really aren't used in this context
+    // Environment
     net->cur_time = globals->get_time_params()->get_cur_time();
     net->warp = globals->get_warp();
+    net->ground_elev = fdm_state->get_Runway_altitude_m();
+
+    // Approach
+    net->tuned_freq = nav_freq->getDoubleValue();
+    net->nav_radial = nav_target_radial->getDoubleValue();
+    net->in_range = nav_inrange->getBoolValue();
+
+    if ( nav_loc->getBoolValue() ) {
+        // is an ILS
+        net->dist_nm
+            = nav_gs_dist_signed->getDoubleValue()
+              * SG_METER_TO_NM;
+    } else {
+        // is a VOR
+        net->dist_nm = nav_loc_dist->getDoubleValue()
+            * SG_METER_TO_NM;
+    }
+
+    net->course_deviation_deg
+        = nav_reciprocal_radial->getDoubleValue()
+        - nav_target_radial->getDoubleValue();
+
+    if ( net->course_deviation_deg < -1000.0 
+         || net->course_deviation_deg > 1000.0 )
+    {
+        // Sanity check ...
+        net->course_deviation_deg = 0.0;
+    }
+    while ( net->course_deviation_deg >  180.0 ) {
+        net->course_deviation_deg -= 360.0;
+    }
+    while ( net->course_deviation_deg < -180.0 ) {
+        net->course_deviation_deg += 360.0;
+    }
+    if ( fabs(net->course_deviation_deg) > 90.0 )
+        net->course_deviation_deg
+            = ( net->course_deviation_deg<0.0
+                ? -net->course_deviation_deg - 180.0
+                : -net->course_deviation_deg + 180.0 );
+
+    if ( nav_loc->getBoolValue() ) {
+        // is an ILS
+        net->gs_deviation_deg
+            = nav_gs_deflection->getDoubleValue()
+            / 5.0;
+    } else {
+        // is an ILS
+        net->gs_deviation_deg = -9999.0;
+    }
 
+#if defined( FG_USE_NETWORK_BYTE_ORDER )
     // Convert the net buffer to network format
     net->version = htonl(net->version);
 
     htond(net->longitude);
     htond(net->latitude);
-    htond(net->altitude);
-    htond(net->phi);
-    htond(net->theta);
-    htond(net->psi);
-    htond(net->vcas);
-    htond(net->climb_rate);
+    htonf(net->altitude);
+    htonf(net->phi);
+    htonf(net->theta);
+    htonf(net->psi);
+    htonf(net->vcas);
+    htonf(net->climb_rate);
 
     for ( i = 0; i < net->num_tanks; ++i ) {
-        htond(net->fuel_quantity[i]);
+        htonf(net->fuel_quantity[i]);
     }
     net->num_tanks = htonl(net->num_tanks);
 
     net->cur_time = htonl( net->cur_time );
     net->warp = htonl( net->warp );
+    net->ground_elev = htonl( net->ground_elev );
+
+    htonf(net->tuned_freq);
+    htonf(net->nav_radial);
+    net->in_range = htonl( net->in_range );
+    htonf(net->dist_nm);
+    htonf(net->course_deviation_deg);
+    htonf(net->gs_deviation_deg);
+#endif
 }
 
 
 void FGNetGUI2Props( FGNetGUI *net ) {
-    int i;
+    unsigned int i;
 
+#if defined( FG_USE_NETWORK_BYTE_ORDER )
     // Convert to the net buffer from network format
     net->version = ntohl(net->version);
 
     htond(net->longitude);
     htond(net->latitude);
-    htond(net->altitude);
-    htond(net->phi);
-    htond(net->theta);
-    htond(net->psi);
-    htond(net->vcas);
-    htond(net->climb_rate);
+    htonf(net->altitude);
+    htonf(net->phi);
+    htonf(net->theta);
+    htonf(net->psi);
+    htonf(net->vcas);
+    htonf(net->climb_rate);
 
     net->num_tanks = htonl(net->num_tanks);
     for ( i = 0; i < net->num_tanks; ++i ) {
-       htond(net->fuel_quantity[i]);
+       htonf(net->fuel_quantity[i]);
     }
 
     net->cur_time = ntohl(net->cur_time);
     net->warp = ntohl(net->warp);
+    net->ground_elev = htonl( net->ground_elev );
+
+    htonf(net->tuned_freq);
+    htonf(net->nav_radial);
+    net->in_range = htonl( net->in_range );
+    htonf(net->dist_nm);
+    htonf(net->course_deviation_deg);
+    htonf(net->gs_deviation_deg);
+#endif
 
     if ( net->version == FG_NET_GUI_VERSION ) {
+        FlightProperties fdm_state;
+        
         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
-        // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
+        // cout << "sea level rad = " << fdm_state->get_Sea_level_radius()
        //      << endl;
-        cur_fdm_state->_updateGeodeticPosition( net->latitude,
-                                                net->longitude,
-                                                net->altitude
-                                                  * SG_METER_TO_FEET );
-        cur_fdm_state->_set_Euler_Angles( net->phi,
+  
+        fdm_state.set_Latitude(net->latitude);
+        fdm_state.set_Longitude(net->longitude);
+        fdm_state.set_Altitude(net->altitude * SG_METER_TO_FEET);
+
+        fdm_state.set_Euler_Angles( net->phi,
                                           net->theta,
                                           net->psi );
 
-        cur_fdm_state->_set_V_calibrated_kts( net->vcas );
-        cur_fdm_state->_set_Climb_Rate( net->climb_rate );
+        fdm_state.set_V_calibrated_kts( net->vcas );
+        fdm_state.set_Climb_Rate( net->climb_rate );
 
        for (i = 0; i < net->num_tanks; ++i ) {
            SGPropertyNode * node
@@ -200,6 +310,16 @@ void FGNetGUI2Props( FGNetGUI *net ) {
        }
 
         globals->set_warp( net->warp );
+
+        // Approach
+        fgSetDouble( "/instrumentation/nav[0]/frequencies/selected-mhz",
+                     net->tuned_freq );
+        fgSetBool( "/instrumentation/nav[0]/in-range", net->in_range );
+        fgSetDouble( "/instrumentation/dme/indicated-distance-nm", net->dist_nm );
+        fgSetDouble( "/instrumentation/nav[0]/heading-needle-deflection",
+                     net->course_deviation_deg );
+        fgSetDouble( "/instrumentation/nav[0]/gs-needle-deflection",
+                     net->gs_deviation_deg );
     } else {
        SG_LOG( SG_IO, SG_ALERT,
                 "Error: version mismatch in FGNetNativeGUI2Props()" );
@@ -217,7 +337,7 @@ bool FGNativeGUI::process() {
     int length = sizeof(buf);
 
     if ( get_direction() == SG_IO_OUT ) {
-       // cout << "size of cur_fdm_state = " << length << endl;
+       // cout << "size of fdm_state = " << length << endl;
        FGProps2NetGUI( &buf );
        if ( ! io->write( (char *)(& buf), length ) ) {
            SG_LOG( SG_IO, SG_ALERT, "Error writing data." );