454

Fork of C027_Support by Umar Naeem

GPS.h

Committer:
mazgch
Date:
2014-03-24
Revision:
19:2b5d097ca15d
Parent:
18:e5697801df29
Child:
24:0e287a85ac9e

File content as of revision 19:2b5d097ca15d:

#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

class GPSParser
{
public:
    #define WAIT      -1
    #define NOT_FOUND  0
    
    #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);
    
    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& d);
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];
};

class GPSSerial : public SerialPipe, public GPSParser
{
public:
    GPSSerial(PinName tx    _C027DEFAULT( GPSTXD ), 
              PinName rx    _C027DEFAULT( GPSRXD ), 
              int baudrate  _C027DEFAULT( GPSBAUD ),
              int rxSize    = 256 , 
              int txSize    = 128 );
    virtual int getMessage(char* buf, int len);
protected:
    virtual int _send(const void* buf, int len);
};

class GPSI2C : public I2C, public GPSParser
{
public: 
    GPSI2C(PinName sda          _C027DEFAULT( GPSSDA ), 
           PinName scl          _C027DEFAULT( GPSSCL ),
           unsigned char i2cAdr _C027DEFAULT( GPSADR ), 
           int rxSize           = 256 );
    bool detect(void);
    
    virtual int getMessage(char* buf, int len);
    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);
protected:
    bool writeable(void) { return true; }
    bool putc(int c)     { char ch = c; return send(&ch, 1); }
    virtual int _send(const void* buf, int len);
    int _get(char* buf, int len);                 // read the NMEA or UBX stream
    
    Pipe<char> _pipe; 
    bool found;
    unsigned char _i2cAdr; 
    static const char REGLEN;
    static const char REGSTREAM;
};