support library for C027 helper functions for Buffer Pipes, Buffered Serial Port (rtos capable) and GPS parsing. It includes modem APIs for USSD, SMS and Sockets.

Dependents:   HTTPClient_Cellular_HelloWorld Cellular_HelloMQTT MbedSmartRestMain Car_Bon_car_module ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GPS.h Source File

GPS.h

00001 #pragma once 
00002 
00003 #include "mbed.h"
00004 #include "Pipe.h"
00005 #include "SerialPipe.h"
00006 
00007 #ifdef TARGET_UBLOX_C027
00008  #define GPS_IF(onboard, shield) onboard
00009 #else
00010  #define GPS_IF(onboard, shield) shield
00011 #endif
00012 
00013 /** basic gps parser class 
00014 */
00015 class GPSParser
00016 {
00017 public:
00018     /** Power on / Wake up the gps 
00019     */
00020     virtual bool init(PinName pn) = 0;
00021     
00022     enum { 
00023         // getLine Responses
00024         WAIT      = -1, //!< wait for more incoming data (the start of a message was found, or no data available)
00025         NOT_FOUND =  0, //!< a parser concluded the the current offset of the pipe doe not contain a valid message
00026     
00027         #define LENGTH(x)   (x & 0x00FFFF)  //!< extract/mask the length
00028         #define PROTOCOL(x) (x & 0xFF0000)  //!< extract/mask the type
00029 
00030         UNKNOWN   = 0x000000,       //!< message type is unknown 
00031         UBX       = 0x100000,       //!< message if of protocol NMEA
00032         NMEA      = 0x200000        //!< message if of protocol UBX
00033     };
00034     
00035     /** Get a line from the physical interface. This fucntion 
00036         needs to be implemented in the inherited class.
00037         \param buf the buffer to store it
00038         \param len size of the buffer
00039         \return type and length if something was found, 
00040                 WAIT if not enough data is available
00041                 NOT_FOUND if nothing was found
00042     */ 
00043     virtual int getMessage(char* buf, int len) = 0;
00044     
00045     /** send a buffer
00046         \param buf the buffer to write
00047         \param len size of the buffer to write
00048         \return bytes written
00049     */
00050     virtual int send(const char* buf, int len);
00051     
00052     /** send a NMEA message, this function just takes the 
00053         payload and calculates and adds checksum. ($ and *XX\r\n will be added)
00054         \param buf the message payload to write
00055         \param len size of the message payload to write
00056         \return total bytes written
00057     */
00058     virtual int sendNmea(const char* buf, int len);
00059     
00060     /** send a UBX message, this function just takes the 
00061         payload and calculates and adds checksum.
00062         \param cls the UBX class id 
00063         \param id the UBX message id
00064         \param buf the message payload to write
00065         \param len size of the message payload to write
00066         \return total bytes written
00067     */
00068     virtual int sendUbx(unsigned char cls, unsigned char id, 
00069                         const void* buf = NULL, int len = 0);
00070     
00071     /** Power off the gps, it can be again woken up by an 
00072         edge on the serial port on the external interrupt pin. 
00073     */
00074     void powerOff(void);
00075     
00076     /** get the first character of a NMEA field
00077         \param ix the index of the field to find
00078         \param start the start of the buffer
00079         \param end the end of the buffer
00080         \return the pointer to the first character of the field. 
00081     */
00082     static const char* findNmeaItemPos(int ix, const char* start, const char* end);
00083     
00084     /** extract a double value from a buffer containing a NMEA message
00085         \param ix the index of the field to extract
00086         \param buf the NMEA message
00087         \param len the size of the NMEA message
00088         \param val the extracted value
00089         \return true if successful, false otherwise
00090     */
00091     static bool getNmeaItem(int ix, char* buf, int len, double& val);
00092     
00093     /** extract a interger value from a buffer containing a NMEA message
00094         \param ix the index of the field to extract
00095         \param buf the NMEA message
00096         \param len the size of the NMEA message
00097         \param val the extracted value
00098         \param base the numeric base to be used (e.g. 8, 10 or 16)
00099         \return true if successful, false otherwise
00100     */
00101     static bool getNmeaItem(int ix, char* buf, int len, int& val, int base/*=10*/);
00102     
00103     /** extract a char value from a buffer containing a NMEA message
00104         \param ix the index of the field to extract
00105         \param buf the NMEA message
00106         \param len the size of the NMEA message
00107         \param val the extracted value
00108         \return true if successful, false otherwise
00109     */
00110     static bool getNmeaItem(int ix, char* buf, int len, char& val);
00111     
00112     /** extract a latitude/longitude value from a buffer containing a NMEA message
00113         \param ix the index of the field to extract (will extract ix and ix + 1)
00114         \param buf the NMEA message
00115         \param len the size of the NMEA message
00116         \param val the extracted latitude or longitude
00117         \return true if successful, false otherwise
00118     */
00119     static bool getNmeaAngle(int ix, char* buf, int len, double& val);
00120     
00121 protected:
00122     /** Get a line from the physical interface. 
00123         \param pipe the receiveing pipe to parse messages 
00124         \param buf the buffer to store it
00125         \param len size of the buffer
00126         \return type and length if something was found, 
00127                 WAIT if not enough data is available
00128                 NOT_FOUND if nothing was found
00129     */ 
00130     static int _getMessage(Pipe<char> * pipe, char* buf, int len);
00131     
00132     /** Check if the current offset of the pipe contains a NMEA message.
00133         \param pipe the receiveing pipe to parse messages 
00134         \param len numer of bytes to parse at maximum
00135         \return length if something was found (including the NMEA frame) 
00136                 WAIT if not enough data is available
00137                 NOT_FOUND if nothing was found
00138     */ 
00139     static int _parseNmea(Pipe<char> * pipe, int len);
00140     
00141     /** Check if the current offset of the pipe contains a UBX message.
00142         \param pipe the receiveing pipe to parse messages 
00143         \param len numer of bytes to parse at maximum
00144         \return length if something was found (including the UBX frame)
00145                 WAIT if not enough data is available
00146                 NOT_FOUND if nothing was found
00147     */ 
00148     static int _parseUbx(Pipe<char> * pipe, int len);
00149     
00150     /** Write bytes to the physical interface. This function 
00151         needs to be implemented by the inherited class. 
00152         \param buf the buffer to write
00153         \param len size of the buffer to write
00154         \return bytes written
00155     */
00156     virtual int _send(const void* buf, int len) = 0;
00157     
00158     static const char toHex[16]; //!< num to hex conversion
00159 #ifdef TARGET_UBLOX_C027
00160     bool _onboard;
00161 #endif
00162 };
00163 
00164 /** gps class which uses a serial port 
00165     as physical interface. 
00166 */
00167 class GPSSerial : public SerialPipe, public GPSParser
00168 {
00169 public:
00170     /** Constructor
00171         \param tx is the serial ports transmit pin (gps to CPU)
00172         \param rx is the serial ports receive pin (CPU to gps)
00173         \param baudrate the baudrate of the gps use 9600
00174         \param rxSize the size of the serial rx buffer
00175         \param txSize the size of the serial tx buffer
00176     */
00177     GPSSerial(PinName tx    GPS_IF( = GPSTXD, /* = D8 */), // resistor on shield not populated 
00178               PinName rx    GPS_IF( = GPSRXD, /* = D9 */), // resistor on shield not populated 
00179               int baudrate  GPS_IF( = GPSBAUD, = 9600 ),
00180               int rxSize    = 256 , 
00181               int txSize    = 128 );
00182               
00183     //! Destructor
00184     virtual ~GPSSerial(void);
00185     
00186     virtual bool init(PinName pn = NC);
00187     
00188     /** Get a line from the physical interface. 
00189         \param buf the buffer to store it
00190         \param len size of the buffer
00191         \return type and length if something was found, 
00192                 WAIT if not enough data is available
00193                 NOT_FOUND if nothing was found
00194     */ 
00195     virtual int getMessage(char* buf, int len);
00196     
00197 protected:
00198     /** Write bytes to the physical interface.
00199         \param buf the buffer to write
00200         \param len size of the buffer to write
00201         \return bytes written
00202     */
00203     virtual int _send(const void* buf, int len);
00204 };
00205 
00206 /** gps class which uses a i2c as physical interface. 
00207 */
00208 class GPSI2C : public I2C, public GPSParser
00209 {
00210 public: 
00211     /** Constructor
00212         \param sda is the I2C SDA pin (between CPU and GPS)
00213         \param scl is the I2C SCL pin (CPU to GPS) 
00214         \param adr the I2C address of the GPS set to (66<<1)
00215         \param rxSize the size of the serial rx buffer
00216     */
00217     GPSI2C(PinName sda          GPS_IF( = GPSSDA, = D14 ), 
00218            PinName scl          GPS_IF( = GPSSCL, = D15 ),
00219            unsigned char i2cAdr GPS_IF( = GPSADR, = (66<<1) ), 
00220            int rxSize           = 256 );
00221     //! Destructor
00222     virtual ~GPSI2C(void);
00223     
00224     /** helper function to probe the i2c device
00225         \return true if successfully detected the gps. 
00226     */ 
00227     virtual bool init(PinName pn = GPS_IF( GPSINT, NC /* D7 resistor R67 on shield not mounted */));
00228     
00229     /** Get a line from the physical interface. 
00230         \param buf the buffer to store it
00231         \param len size of the buffer
00232         \return type and length if something was found, 
00233                 WAIT if not enough data is available
00234                 NOT_FOUND if nothing was found
00235     */ 
00236     virtual int getMessage(char* buf, int len);
00237     
00238     /** send a buffer
00239         \param buf the buffer to write
00240         \param len size of the buffer to write
00241         \return bytes written
00242     */
00243     virtual int send(const char* buf, int len);
00244     
00245     /** send a NMEA message, this function just takes the 
00246         payload and calculates and adds checksum. ($ and *XX\r\n will be added)
00247         \param buf the message payload to write
00248         \param len size of the message payload to write
00249         \return total bytes written
00250     */
00251     virtual int sendNmea(const char* buf, int len);
00252     
00253     /** send a UBX message, this function just takes the 
00254         payload and calculates and adds checksum.
00255         \param cls the UBX class id 
00256         \param id the UBX message id
00257         \param buf the message payload to write
00258         \param len size of the message payload to write
00259         \return total bytes written
00260     */
00261     virtual int sendUbx(unsigned char cls, unsigned char id, 
00262                         const void* buf = NULL, int len = 0);
00263                         
00264 protected:
00265     /** check if the port is writeable (like SerialPipe)
00266         \return true if writeable        
00267     */
00268     bool writeable(void) { return true; }
00269     
00270     /** Write a character (like SerialPipe)
00271         \param c  the character to write
00272         \return true if succesffully written 
00273     */
00274     bool putc(int c)     { char ch = c; return send(&ch, 1); }
00275     
00276     /** Write bytes to the physical interface.
00277         \param buf the buffer to write
00278         \param len size of the buffer to write
00279         \return bytes written
00280     */
00281     virtual int _send(const void* buf, int len);
00282     
00283     /** read bytes from the physical interface.
00284         \param buf the buffer to read into
00285         \param len size of the read buffer 
00286         \return bytes read
00287     */
00288     int _get(char* buf, int len);
00289     
00290     Pipe<char>  _pipe;           //!< the rx pipe
00291     unsigned char _i2cAdr;      //!< the i2c address
00292     static const char REGLEN;   //!< the length i2c register address
00293     static const char REGSTREAM;//!< the stream i2c register address
00294 };