Allows for a GPS module to be connected to a serial port and exposes an easy to use API to get the GPS data.

Fork of MODGPS by Andy K

GPS.cpp

Committer:
xanter
Date:
2018-09-29
Revision:
7:049436bc2225
Parent:
6:64771e31464e

File content as of revision 7:049436bc2225:

/*
    Copyright (c) 2010 Andy Kirkham
 
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
 
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
 
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#include "GPS.h"

GPS::GPS(PinName tx, PinName rx, const char *name) 
    : RawSerial(tx, rx)
    , _ppsInUse(false)
    , _pps(NULL)
    , _second100(new Ticker)
    , _lastByte(0)
    , _gga(NULL)
    , _rmc(NULL)
    , _vtg(NULL)
    , _ukn(NULL)
    , _nmeaOnUart0(false)
{
    attach(callback(this, &GPS::rx_irq));
    _second100->attach_us(callback(this, &GPS::ticktock), GPS_TICKTOCK);
}

void GPS::ppsAttach(PinName irq, ppsEdgeType type) {
    if (_pps != NULL) 
        delete _pps;
    _pps = new InterruptIn(irq);
    if (type == ppsRise) 
        _pps->rise(callback(this, &GPS::pps_irq));
    else 
        _pps->fall(callback(this, &GPS::pps_irq));
    _ppsInUse = true;     
}
    
void GPS::ppsUnattach() {
    if (_pps != NULL) 
        delete _pps;
    _ppsInUse = false;
}
    
double GPS::latitude() {
    double a, b;
    do { 
        a = thePlace.lat; b = thePlace.lat; 
    } while (a != b);
    return a; 
}

double GPS::longitude() { 
    double a, b;
    do { 
        a = thePlace.lon; b = thePlace.lon; 
    } while (a != b);
    return a; 
}

double GPS::altitude() { 
    double a, b;
    do { 
        a = thePlace.alt; b = thePlace.alt; 
    } while (a != b);
    return a; 
}

GPS_Geodetic * GPS::geodetic(GPS_Geodetic *q) {
    GPS_Geodetic a;
    
    if (q == NULL) 
        q = new GPS_Geodetic;
    
    do {
        memcpy(&a, &thePlace, sizeof(GPS_Geodetic));
        memcpy(q,  &thePlace, sizeof(GPS_Geodetic));
    }
    while (memcmp(&a, q, sizeof(GPS_Geodetic)) != 0);
    
    return q;
}

GPS_VTG * GPS::vtg(GPS_VTG *q) {
    GPS_VTG a;
    
    if (q == NULL) 
        q = new GPS_VTG;
    
    do {
        memcpy(&a, &theVTG, sizeof(GPS_VTG));
        memcpy(q,  &theVTG, sizeof(GPS_VTG));
    }
    while (memcmp(&a, q, sizeof(GPS_VTG)) != 0);
    
    return q;
}

void GPS::ticktock() {   
    // Increment the time structure by 1/100th of a second.
    ++theTime; 
    
    // Test the serial queue.
    if (process_required) {
        char *s = buffer[active_buffer == 0 ? 1 : 0];
        if (!strncmp(s, "$GPRMC", 6)) {
            if (_rmc) {
                int i;
                for(i = 0; s[i] != '\n'; i++)
                    _rmc[i] = s[i];
                _rmc[i++] = '\n'; _rmc[i] = '\0';
            }
            theTime.nmea_rmc(s);
            if (cb_rmc)
                cb_rmc.call();
            if (!_ppsInUse) 
                theTime.fractionalReset();
        }
        else if (!strncmp(s, "$GPGGA", 6)) {
            if (_gga) {
                int i;
                for(i = 0; s[i] != '\n'; i++)
                    _gga[i] = s[i];
                _gga[i++] = '\n'; _gga[i] = '\0';
            }            
            thePlace.nmea_gga(s);       
            if (cb_gga)     
                cb_gga.call();
        }
        else if (!strncmp(s, "$GPVTG", 6)) {
            if (_vtg) {
                int i;
                for(i = 0; s[i] != '\n'; i++)
                    _vtg[i] = s[i];
                _vtg[i++] = '\n'; _vtg[i] = '\0';
            }
            theVTG.nmea_vtg(s);    
            if (cb_vtg)        
                cb_vtg.call();
        }
        else {
            if (_ukn) {
                int i;
                for(i = 0; s[i] != '\n'; i++)
                    _ukn[i] = s[i];
                _ukn[i++] = '\n'; _ukn[i] = '\0';
                if (cb_ukn)
                    cb_ukn.call();
            }
        }
        process_required = false;
    }
    
    // If we have a valid GPS time then, once per minute, set the RTC.
    if (theTime.status == 'A' && theTime.second == 0 && theTime.tenths == 0 && theTime.hundreths == 0) {
        // set_time() is defined in rtc_time.h
        // http://mbed.org/projects/libraries/svn/mbed/trunk/rtc_time.h
        set_time(theTime.to_C_tm());        
    }
    
}

void GPS::pps_irq() {
    theTime.fractionalReset();
    theTime++; // Increment the time/date by one second. 
    if (cb_pps)
        cb_pps.call();
}

void GPS::rx_irq() {
    char c;
    while(readable()) {
        c = static_cast<char>(getc());             
        
        // strtok workaround. 
        // Found that ,, together (which some NMEA sentences
        // contain for a null/empty field) confuses strtok()
        // function. Solution:- Push a "zero" into the string 
        // for missing/empty fields.
        if (c == ',' && _lastByte == ',') {
            buffer[active_buffer][rx_buffer_in] = '0';
            if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0;
        }
        
        // Debugging/dumping data. 
//        if (_nmeaOnUart0) LPC_UART0->RBR = c; 
        
        // Put the byte into the string.
        buffer[active_buffer][rx_buffer_in] = c;
        if (++rx_buffer_in >= GPS_BUFFER_LEN) rx_buffer_in = 0;
        
        // Save for next time an irq occurs. See strtok() above.
        _lastByte = c;
        
        // If end of NMEA sentence flag for processing.
        if (c == '\n') {
            active_buffer = active_buffer == 0 ? 1 : 0;
            process_required = true;
            rx_buffer_in = 0;                
        }            
    }
}