Own fork of C027_Support

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of C027_Support by u-blox

GPS.h

Committer:
mazgch
Date:
2014-04-11
Revision:
38:e6cab4632d84
Parent:
31:a0bed6c1e05d
Child:
39:9b4b9433e220

File content as of revision 38:e6cab4632d84:

#pragma once 

#include "mbed.h"
#include "Pipe.h"
#include "SerialPipe.h"

#ifdef TARGET_UBLOX_C027
 // if we detect the C027 platform we will assign the 
 // default pinname and baudrate in the constructor 
 // this helper macro will be used. 
 #define _C027DEFAULT(name) = name
#else
 #define _C027DEFAULT(name)
#endif

/** basic gps parser class 
*/
class GPSParser
{
public:
    #define WAIT      -1
    #define NOT_FOUND  0
    
    #define UNKNOWN     0x000000
    #define UBX         0x100000
    #define NMEA        0x200000
    #define LENGTH(x)   (x & 0x00FFFF)
    #define PROTOCOL(x) (x & 0xFF0000)

    virtual int getMessage(char* buf, int len) = 0; 
    virtual int send(const char* buf, int len);
    virtual int sendNmea(const char* buf, int len);
    virtual int sendUbx(unsigned char cls, unsigned char id, const void* buf = NULL, int len = 0);
    void powerOff(void);
    
    static const char* findNmeaItemPos(int ix, const char* start, const char* end);
    static bool getNmeaItem(int ix, char* buf, int len, double& val);
    static bool getNmeaItem(int ix, char* buf, int len, int& val, int base/*=10*/);
    static bool getNmeaItem(int ix, char* buf, int len, char& val);
    static bool getNmeaAngle(int ix, char* buf, int len, double& val);
protected:
    static int _getMessage(Pipe<char>* pipe, char* buf, int len);
    static int _parseNmea(Pipe<char>* pipe, int len);
    static int _parseUbx(Pipe<char>* pipe, int len);
    virtual int _send(const void* buf, int len) = 0;
    static const char toHex[16];
};

/** gps class which uses a serial port 
    as physical interface. 
*/
class GPSSerial : public SerialPipe, public GPSParser
{
public:
    /** Constructor
        \param tx is the serial ports transmit pin (gps to CPU)
        \param rx is the serial ports receive pin (CPU to gps)
        \param baudrate the baudrate of the gps use 9600
        \param rxSize the size of the serial rx buffer
        \param txSize the size of the serial tx buffer
    */
    GPSSerial(PinName tx    _C027DEFAULT( GPSTXD ), 
              PinName rx    _C027DEFAULT( GPSRXD ), 
              int baudrate  _C027DEFAULT( GPSBAUD ),
              int rxSize    = 256 , 
              int txSize    = 128 );
    /** Get a line from the physical interface. 
        \param buf the buffer to store it
        \param len size of the buffer
        \return type and length if something was found, 
                WAIT if not enough data is available
                NOT_FOUND if nothing was found
    */ 
    virtual int getMessage(char* buf, int len);
protected:
    /** Write bytes to the physical interface.
        \param buf the buffer to write
        \param len size of the buffer to write
        \return bytes written
    */
    virtual int _send(const void* buf, int len);
};

/** gps class which uses a i2c as physical interface. 
*/
class GPSI2C : public I2C, public GPSParser
{
public: 
    /** Constructor
        \param sda is the I2C SDA pin (between CPU and GPS)
        \param scl is the I2C SCL pin (CPU to GPS) 
        \param adr the I2C address of the GPS set to (66<<1)
        \param rxSize the size of the serial rx buffer
    */
    GPSI2C(PinName sda          _C027DEFAULT( GPSSDA ), 
           PinName scl          _C027DEFAULT( GPSSCL ),
           unsigned char i2cAdr _C027DEFAULT( GPSADR ), 
           int rxSize           = 256 );
    /** helper function to probe the i2c device
        \return true if successfully detected the gps. 
    */ 
    bool detect(void);
    
    /** Get a line from the physical interface. 
        \param buf the buffer to store it
        \param len size of the buffer
        \return type and length if something was found, 
                WAIT if not enough data is available
                NOT_FOUND if nothing was found
    */ 
    virtual int getMessage(char* buf, int len);
    
    /** send a buffer
        \param buf the buffer to write
        \param len size of the buffer to write
        \return bytes written
    */
    virtual int send(const char* buf, int len);
    
    /** send a NMEA message, this function just takes the 
        payload and calculates and adds checksum. ($ and *XX\r\n will be added)
        \param buf the message payload to write
        \param len size of the message payload to write
        \return total bytes written
    */
    virtual int sendNmea(const char* buf, int len);
    
    /** send a UBX message, this function just takes the 
        payload and calculates and adds checksum.
        \param cls the UBX class id 
        \param id the UBX message id
        \param buf the message payload to write
        \param len size of the message payload to write
        \return total bytes written
    */
    virtual int sendUbx(unsigned char cls, unsigned char id, const void* buf = NULL, int len = 0);
protected:
    /** check if the port is writeable (like SerialPipe)
        \return true if writeable        
    */
    bool writeable(void) { return true; }
    /** Write a character (like SerialPipe)
        \param c  the character to write
        \return true if succesffully written 
    */
    bool putc(int c)     { char ch = c; return send(&ch, 1); }
    /** Write bytes to the physical interface.
        \param buf the buffer to write
        \param len size of the buffer to write
        \return bytes written
    */
    virtual int _send(const void* buf, int len);
    /** read bytes from the physical interface.
        \param buf the buffer to read into
        \param len size of the read buffer 
        \return bytes read
    */
    int _get(char* buf, int len);
    
    Pipe<char> _pipe;           //!< the rx pipe
    bool found;                 //!< flag if device is detected.
    unsigned char _i2cAdr;      //!< the i2c address
    static const char REGLEN;   //!< the length i2c register address
    static const char REGSTREAM;//!< the stream i2c register address
};