Switch over to using SGIOChannel so we can try to read data live from
the serial port at some future time.
This commit is contained in:
parent
4f72904235
commit
2f98caeba8
3 changed files with 30 additions and 26 deletions
|
@ -3,6 +3,7 @@
|
|||
#include <plib/ul.h>
|
||||
|
||||
#include <simgear/constants.h>
|
||||
#include <simgear/io/sg_file.hxx>
|
||||
#include <simgear/math/sg_geodesy.hxx>
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/misc/strutils.hxx>
|
||||
|
@ -319,6 +320,7 @@ void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
|
|||
|
||||
// load the specified file, return the number of records loaded
|
||||
bool MIDGTrack::load( const string &file ) {
|
||||
int count = 0;
|
||||
|
||||
MIDGpos pos;
|
||||
MIDGatt att;
|
||||
|
@ -329,16 +331,17 @@ bool MIDGTrack::load( const string &file ) {
|
|||
pos_data.clear();
|
||||
att_data.clear();
|
||||
|
||||
// openg the file
|
||||
fd = fopen( file.c_str(), "r" );
|
||||
|
||||
if ( fd == NULL ) {
|
||||
// open the file
|
||||
SGFile input( file );
|
||||
if ( !input.open( SG_IO_IN ) ) {
|
||||
cout << "Cannot open file: " << file << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
while ( ! feof( fd ) ) {
|
||||
int id = next_message( fd, &pos, &att );
|
||||
while ( ! input.eof() ) {
|
||||
// cout << "looking for next message ..." << endl;
|
||||
int id = next_message( &input, &pos, &att );
|
||||
count++;
|
||||
|
||||
if ( id == 10 ) {
|
||||
if ( att.get_msec() > att_time ) {
|
||||
|
@ -357,38 +360,41 @@ bool MIDGTrack::load( const string &file ) {
|
|||
}
|
||||
}
|
||||
|
||||
cout << "processed " << count << " messages" << endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// load the next message of a real time data stream
|
||||
int MIDGTrack::next_message( FILE *fd, MIDGpos *pos, MIDGatt *att ) {
|
||||
int MIDGTrack::next_message( SGIOChannel *ch, MIDGpos *pos, MIDGatt *att ) {
|
||||
char tmpbuf[256];
|
||||
char savebuf[256];
|
||||
|
||||
// scan for sync characters
|
||||
int sync0, sync1;
|
||||
sync0 = fgetc( fd );
|
||||
sync1 = fgetc( fd );
|
||||
while ( (sync0 != 129 || sync1 != 161) && !feof(fd) ) {
|
||||
uint8_t sync0, sync1;
|
||||
ch->read( tmpbuf, 1 ); sync0 = (unsigned char)tmpbuf[0];
|
||||
ch->read( tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
|
||||
while ( (sync0 != 129 || sync1 != 161) && !ch->eof() ) {
|
||||
sync0 = sync1;
|
||||
sync1 = fgetc( fd );
|
||||
ch->read( tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
|
||||
}
|
||||
|
||||
// cout << "start of message ..." << endl;
|
||||
|
||||
// read message id and size
|
||||
int id = fgetc( fd );
|
||||
int size = fgetc( fd );
|
||||
// cout << "message = " << id << " size = " << size << endl;
|
||||
ch->read( tmpbuf, 1 ); uint8_t id = (unsigned char)tmpbuf[0];
|
||||
ch->read( tmpbuf, 1 ); uint8_t size = (unsigned char)tmpbuf[0];
|
||||
// cout << "message = " << (int)id << " size = " << (int)size << endl;
|
||||
|
||||
// load message
|
||||
char buf[256];
|
||||
fread( buf, size, 1, fd );
|
||||
ch->read( savebuf, size );
|
||||
|
||||
// read checksum
|
||||
int cksum0 = fgetc( fd );
|
||||
int cksum1 = fgetc( fd );
|
||||
ch->read( tmpbuf, 1 ); uint8_t cksum0 = (unsigned char)tmpbuf[0];
|
||||
ch->read( tmpbuf, 1 ); uint8_t cksum1 = (unsigned char)tmpbuf[0];
|
||||
|
||||
if ( validate_cksum( id, size, buf, cksum0, cksum1 ) ) {
|
||||
parse_msg( id, buf, pos, att );
|
||||
if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
|
||||
parse_msg( id, savebuf, pos, att );
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,6 @@
|
|||
|
||||
#include <simgear/compiler.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -25,6 +23,7 @@ typedef unsigned int uint32_t;
|
|||
# error "Port me! Platforms that don't have <stdint.h> need to define int8_t, et. al."
|
||||
#endif
|
||||
|
||||
#include <simgear/io/iochannel.hxx>
|
||||
|
||||
SG_USING_STD(cout);
|
||||
SG_USING_STD(endl);
|
||||
|
@ -130,7 +129,6 @@ private:
|
|||
|
||||
vector <MIDGpos> pos_data;
|
||||
vector <MIDGatt> att_data;
|
||||
FILE *fd;
|
||||
|
||||
// parse message and put current data into vector if message has a
|
||||
// newer time stamp than existing data.
|
||||
|
@ -143,7 +141,7 @@ public:
|
|||
|
||||
// read/parse the next message from the specified data stream,
|
||||
// returns id # if a valid message found.
|
||||
int next_message( FILE *fd, MIDGpos *pos, MIDGatt *att );
|
||||
int next_message( SGIOChannel *ch, MIDGpos *pos, MIDGatt *att );
|
||||
|
||||
// load the named file into internal buffers
|
||||
bool load( const string &file );
|
||||
|
|
|
@ -13,7 +13,7 @@ MIDGsmooth_SOURCES = \
|
|||
MIDG_main.cxx
|
||||
|
||||
MIDGsmooth_LDADD = \
|
||||
-lsgtiming -lsgmath -lsgmisc -lsgdebug -lplibnet -lplibul \
|
||||
-lsgio -lsgtiming -lsgmath -lsgmisc -lsgdebug -lplibnet -lplibul \
|
||||
$(joystick_LIBS) $(network_LIBS) $(base_LIBS) -lz
|
||||
|
||||
INCLUDES = -I$(top_srcdir)/src
|
||||
|
|
Loading…
Reference in a new issue