Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gps.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2003 Michael 'Mickey' Lauer.  All rights reserved.
00003 **
00004 ** This file is part of Wellenreiter II.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 **********************************************************************/
00015 
00016 #include "gps.h"
00017 
00018 /* OPIE */
00019 #include <opie2/odebug.h>
00020 using namespace Opie::Core;
00021 
00022 /* QT */
00023 #include <qtextstream.h>
00024 
00025 /* STD */
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 
00029 GPS::GPS( QObject* parent, const char * name )
00030     :QObject( parent, name )
00031 {
00032     odebug << "GPS::GPS()" << oendl; 
00033     _socket = new QSocket( this, "gpsd commsock" );
00034 }
00035 
00036 
00037 GPS::~GPS()
00038 {
00039     odebug << "GPS::~GPS()" << oendl; 
00040 }
00041 
00042 
00043 bool GPS::open( const QString& host, int port )
00044 {
00045     _socket->connectToHost( host, port );
00046 }
00047 
00048 
00049 GpsLocation GPS::position() const
00050 {
00051     char buf[256];
00052     double lat = -111.0;
00053     double lon = -111.0;
00054 
00055     int result = _socket->writeBlock( "p\r\n", 3 );
00056     _socket->flush();
00057     if ( result )
00058     {
00059         int numAvail = _socket->bytesAvailable();
00060         odebug << "GPS write succeeded, " << numAvail << " bytes available for reading..." << oendl; 
00061         if ( numAvail )
00062         {                     
00063             int numRead = _socket->readBlock( &buf[0], sizeof buf );
00064             int numScan = ::sscanf( &buf[0], "GPSD,P=%lg %lg", &lat, &lon);
00065             
00066             if ( numRead < 7 || numScan != 2 )
00067             {
00068                 odebug << "GPS read " << numRead << " bytes succeeded, invalid response: '" << &buf[0] << "'" << oendl; 
00069                 return GpsLocation( -111, -111 );
00070             }
00071             else
00072             {
00073                 return GpsLocation( lat, lon );
00074             }
00075         }
00076     }
00077     return GpsLocation( -111, -111 );
00078 }
00079 
00080 
00081 QString GpsLocation::dmsPosition() const
00082 {
00083     if ( _latitude == -111 || _longitude == -111 )
00084         return "N/A";
00085     if ( _latitude == 0.0 && _longitude == 0.0 )
00086         return "NULL";
00087 
00088     /* compute latitude */
00089 
00090     QString dms = "N";
00091     if ( _latitude >= 0 ) dms.append( "+" );
00092 
00093     int trunc = int( _latitude );
00094     float rest = _latitude - trunc;
00095 
00096     float minf = rest * 60;
00097     int minutes = int( minf );
00098 
00099     rest = minf - minutes;
00100     int seconds = int( rest * 60 );
00101 
00102     dms.append( QString::number( trunc ) );
00103     dms.append( "° " );
00104     dms.append( QString::number( ::abs( minutes ) ) );
00105     dms.append( "' " );
00106     dms.append( QString::number( ::abs( seconds ) ) );
00107     dms.append( "'' " );
00108 
00109     /* compute longitude */
00110 
00111     dms.append( " | W" );
00112     if ( _longitude > 0 ) dms.append( "+" );
00113 
00114     trunc = int( _longitude );
00115     rest = _longitude - trunc;
00116 
00117     minf = rest * 60;
00118     minutes = int( minf );
00119 
00120     rest = minf - minutes;
00121     seconds = int( rest * 60 );
00122 
00123     dms.append( QString::number( trunc ) );
00124     dms.append( "° " );
00125     dms.append( QString::number( ::abs( minutes ) ) );
00126     dms.append( "' " );
00127     dms.append( QString::number( ::abs( seconds ) ) );
00128     dms.append( "'' " );
00129 
00130     return dms;
00131 }

Generated on Sat Nov 5 16:17:45 2005 for OPIE by  doxygen 1.4.2