Graham Cantin / MODGPS

Dependents:   Telnet_server

Fork of MODGPS by Andy K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GPS.cpp Source File

GPS.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "GPS.h"
00024 
00025 GPS::GPS(PinName tx, PinName rx, const char *name) : Serial(tx, rx, name) 
00026 {
00027     switch(_uidx) {
00028         case 1:   _base = LPC_UART1; break;
00029         case 2:   _base = LPC_UART2; break;
00030         case 3:   _base = LPC_UART3; break;
00031         default : _base = NULL;      break;
00032     }
00033     
00034     _pps = NULL;
00035     _ppsInUse = false;
00036     
00037     if (_base != NULL) attach(this, &GPS::rx_irq);
00038     
00039     _second100 = new Ticker;
00040     _second100->attach_us(this, &GPS::ticktock, GPS_TICKTOCK);
00041 }
00042 
00043 void 
00044 GPS::ppsAttach(PinName irq, ppsEdgeType type) 
00045 {
00046     if (_pps != NULL) delete(_pps);
00047     _pps = new InterruptIn(irq);
00048     if (type == ppsRise ) _pps->rise(this, &GPS::pps_irq);
00049     else _pps->fall(this, &GPS::pps_irq);
00050     _ppsInUse = true;     
00051 }
00052     
00053 void 
00054 GPS::ppsUnattach(void) 
00055 {
00056     if (_pps != NULL) delete(_pps);
00057     _ppsInUse = false;
00058 }
00059     
00060 double 
00061 GPS::latitude(void)  
00062 {
00063     double a, b;
00064     do { a = thePlace.lat; b = thePlace.lat; }  while (a != b);
00065     return a; 
00066 }
00067 
00068 double 
00069 GPS::longitude(void) 
00070 { 
00071     double a, b;
00072     do { a = thePlace.lon; b = thePlace.lon; } while (a != b);
00073     return a; 
00074 }
00075 
00076 double 
00077 GPS::altitude(void)  
00078 { 
00079     double a, b;
00080     do { a = thePlace.alt; b = thePlace.alt; } while (a != b);
00081     return a; 
00082 }
00083 
00084 GPS_Geodetic *
00085 GPS::geodetic(GPS_Geodetic *q)
00086 {
00087     GPS_Geodetic a, b;
00088     
00089     if (q == NULL) q = new GPS_Geodetic;
00090     
00091     do {
00092         memcpy(&a, &thePlace, sizeof(GPS_Geodetic));
00093         memcpy(&b, &thePlace, sizeof(GPS_Geodetic));
00094     }
00095     while (memcmp(&a, &b, sizeof(GPS_Geodetic)) != 0);
00096     
00097     return q;
00098 }
00099 
00100 void
00101 GPS::ticktock(void)
00102 {
00103     // Increment the time structure by 1/100th of a second.
00104     ++theTime; 
00105     
00106     // Test the serial queue.
00107     if (process_required) {
00108         char *s = buffer[active_buffer == 0 ? 1 : 0];
00109         if (!strncmp(s, "$GPRMC", 6)) {
00110             theTime.nmea_rmc(s);
00111             cb_rmc.call();
00112             if (!_ppsInUse) theTime.fractionalReset();
00113         }
00114         else if (!strncmp(s, "$GPGGA", 6)) {
00115             thePlace.nmea_gga(s);            
00116             cb_gga.call();
00117         }
00118         process_required = false;
00119     }
00120 }
00121 
00122 void 
00123 GPS::pps_irq(void)
00124 {
00125     theTime.fractionalReset();
00126     theTime++; // Increment the time/date by one second. 
00127     cb_pps.call();
00128 }
00129     
00130 void 
00131 GPS::rx_irq(void)
00132 {
00133     uint32_t iir __attribute__((unused));
00134     char c;
00135     
00136     if (_base) {
00137         iir = (uint32_t)*((char *)_base + GPS_IIR); 
00138         while((int)(*((char *)_base + GPS_LSR) & 0x1)) {
00139             c = (char)(*((char *)_base + GPS_RBR) & 0xFF); 
00140             buffer[active_buffer][rx_buffer_in++] = c;
00141             rx_buffer_in &= (GPS_BUFFER_LEN - 1);
00142             if (c == '\n') {
00143                 active_buffer = active_buffer == 0 ? 1 : 0;
00144                 process_required = true;
00145                 rx_buffer_in = 0;                
00146             }
00147         }
00148     }
00149 }