]> git.mxchange.org Git - flightgear.git/blobdiff - utils/GPSsmooth/MIDG-II.cxx
Icons modifications
[flightgear.git] / utils / GPSsmooth / MIDG-II.cxx
index c6c65f87e0163175eb1c597e645baef4350a91f4..fa3ce462d600e775bc16b3c2d8416cbc08346161 100644 (file)
@@ -1,6 +1,8 @@
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
-#endif 
+#endif
+
+#include <simgear/compiler.h>
 
 #include <iostream>
 
 
 #include "MIDG-II.hxx"
 
-SG_USING_STD(cout);
-SG_USING_STD(endl);
+using std::cout;
+using std::endl;
 
 
 MIDGTrack::MIDGTrack() {};
 MIDGTrack::~MIDGTrack() {};
 
 
-
-
+/*
+ * Unused function
+ */
+#if(0)
 static uint32_t read_swab( char *buf, size_t offset, size_t size ) {
     uint32_t result = 0;
 
@@ -49,6 +53,7 @@ static uint32_t read_swab( char *buf, size_t offset, size_t size ) {
 
     return result;
 }
+#endif
 
 
 static bool validate_cksum( uint8_t id, uint8_t size, char *buf,
@@ -86,6 +91,10 @@ static bool validate_cksum( uint8_t id, uint8_t size, char *buf,
 
 void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
 {
+/*
+ * Completely unused parser results. Removed from compiling to remove the warnings
+ */
+#if(0)
     if ( id == 1 ) {
         uint32_t ts;
         uint16_t status;
@@ -96,7 +105,7 @@ void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
         // timestamp
         ts = (uint32_t)read_swab( buf, 0, 4 );
         // cout << "  time stamp = " << ts << endl;
-            
+
         // status
         status = (uint16_t)read_swab( buf, 4, 2 );
         // cout << "  status = " << status << endl;
@@ -238,7 +247,7 @@ void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
         // cout << "  pos = " << posx << "," << posy << "," << posz << endl;
 
         double xyz[3];
-        xyz[0] = posx/100; xyz[1] = posy/100; xyz[2] = posz/100;
+        xyz[0] = (double)posx/100; xyz[1] = (double)posy/100; xyz[2] = (double)posz/100;
         double lat, lon, alt;
         sgCartToGeod(xyz, &lat, &lon, &alt);
         pos->lat_deg = lat * 180.0 / SG_PI;
@@ -310,18 +319,19 @@ void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
         // position dop
         pdop = (uint16_t)read_swab( buf, 32, 2 );
         // cout << "  pdop = " << pdop <<  endl;
-       
+
         // position accuracy
         pacc = (uint16_t)read_swab( buf, 34, 2 );
         // cout << "  pacc = " << pacc <<  endl;
-       
+
         // speed accuracy
         sacc = (uint16_t)read_swab( buf, 36, 2 );
         // cout << "  sacc = " << sacc <<  endl;
-       
+
     } else {
         cout << "unknown id = " << id << endl;
     }
+#endif
 }
 
 
@@ -377,11 +387,12 @@ bool MIDGTrack::load( const string &file ) {
 int myread( SGIOChannel *ch, SGIOChannel *log, char *buf, int length ) {
     bool myeof = false;
     int result = 0;
-    while ( result != length && !myeof ) {
-        result = ch->read( buf, length );
-        if ( ch->get_type() == sgFileType ) {
-            myeof = ((SGFile *)ch)->eof();
-        }
+    if ( !myeof ) {
+      result = ch->read( buf, length );
+      // cout << "wanted " << length << " read " << result << " bytes" << endl;
+      if ( ch->get_type() == sgFileType ) {
+       myeof = ((SGFile *)ch)->eof();
+      }
     }
 
     if ( result > 0 && log != NULL ) {
@@ -391,6 +402,23 @@ int myread( SGIOChannel *ch, SGIOChannel *log, char *buf, int length ) {
     return result;
 }
 
+// attempt to work around some system dependent issues.  Our read can
+// return < data than we want.
+int serial_read( SGSerialPort *serial, char *buf, int length ) {
+    int result = 0;
+    int bytes_read = 0;
+    char *tmp = buf;
+
+    while ( bytes_read < length ) {
+      result = serial->read_port( tmp, length - bytes_read );
+      bytes_read += result;
+      tmp += result;
+      // cout << "  read " << bytes_read << " of " << length << endl;
+    }
+
+    return bytes_read;
+}
+
 // load the next message of a real time data stream
 int MIDGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
                              MIDGpos *pos, MIDGatt *att )
@@ -409,7 +437,9 @@ int MIDGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
     while ( (sync0 != 129 || sync1 != 161) && !myeof ) {
         sync0 = sync1;
         myread( ch, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
-        // cout << "scanning for start of message, eof = " << ch->eof() << endl;
+        // cout << "scanning for start of message "
+       //      << (unsigned int)sync0 << " " << (unsigned int)sync1
+       //      << ", eof = " << ch->eof() << endl;
         if ( ch->get_type() == sgFileType ) {
             myeof = ((SGFile *)ch)->eof();
         }
@@ -429,15 +459,19 @@ int MIDGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
             cout << "ERROR: didn't read enough bytes!" << endl;
         }
     } else {
+#ifdef READ_ONE_BY_ONE
         for ( int i = 0; i < size; ++i ) {
             myread( ch, log, tmpbuf, 1 ); savebuf[i] = tmpbuf[0];
         }
+#else
+       myread( ch, log, savebuf, size );
+#endif
     }
 
     // read checksum
     myread( ch, log, tmpbuf, 1 ); uint8_t cksum0 = (unsigned char)tmpbuf[0];
     myread( ch, log, tmpbuf, 1 ); uint8_t cksum1 = (unsigned char)tmpbuf[0];
-    
+
     if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
         parse_msg( id, savebuf, pos, att );
         return id;
@@ -448,6 +482,64 @@ int MIDGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
 }
 
 
+// load the next message of a real time data stream
+int MIDGTrack::next_message( SGSerialPort *serial, SGIOChannel *log,
+                             MIDGpos *pos, MIDGatt *att )
+{
+    char tmpbuf[256];
+    char savebuf[256];
+
+    cout << "in next_message()" << endl;
+
+    bool myeof = false;
+
+    // scan for sync characters
+    uint8_t sync0, sync1;
+    serial_read( serial, tmpbuf, 2 );
+    sync0 = (unsigned char)tmpbuf[0];
+    sync1 = (unsigned char)tmpbuf[1];
+    while ( (sync0 != 129 || sync1 != 161) && !myeof ) {
+        sync0 = sync1;
+        serial_read( serial, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
+        cout << "scanning for start of message "
+            << (unsigned int)sync0 << " " << (unsigned int)sync1
+            << endl;
+    }
+
+    cout << "found start of message ..." << endl;
+
+    // read message id and size
+    serial_read( serial, tmpbuf, 2 );
+    uint8_t id = (unsigned char)tmpbuf[0];
+    uint8_t size = (unsigned char)tmpbuf[1];
+    // cout << "message = " << (int)id << " size = " << (int)size << endl;
+
+    // load message
+    serial_read( serial, savebuf, size );
+
+    // read checksum
+    serial_read( serial, tmpbuf, 2 );
+    uint8_t cksum0 = (unsigned char)tmpbuf[0];
+    uint8_t cksum1 = (unsigned char)tmpbuf[1];
+
+    if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
+        parse_msg( id, savebuf, pos, att );
+
+       //
+       // FIXME
+       // WRITE DATA TO LOG FILE
+       //
+
+        return id;
+    }
+
+    cout << "Check sum failure!" << endl;
+    return -1;
+
+
+}
+
+
 static double interp( double a, double b, double p, bool rotational = false ) {
     double diff = b - a;
     if ( rotational ) {