Own fork of C027_Support
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of C027_Support by
GPS.h@51:e7b81c31baec, 2014-05-12 (annotated)
- Committer:
- mazgch
- Date:
- Mon May 12 07:09:31 2014 +0000
- Revision:
- 51:e7b81c31baec
- Parent:
- 40:295099ff5338
- Child:
- 74:208e3e32d263
convert defines to enums to avoid duplicate defines
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 1:f41579f4e2ed | 1 | #pragma once |
mazgch | 1:f41579f4e2ed | 2 | |
mazgch | 2:b6012cd91657 | 3 | #include "mbed.h" |
mazgch | 2:b6012cd91657 | 4 | #include "Pipe.h" |
mazgch | 1:f41579f4e2ed | 5 | #include "SerialPipe.h" |
mazgch | 2:b6012cd91657 | 6 | |
mazgch | 19:2b5d097ca15d | 7 | #ifdef TARGET_UBLOX_C027 |
mazgch | 19:2b5d097ca15d | 8 | // if we detect the C027 platform we will assign the |
mazgch | 19:2b5d097ca15d | 9 | // default pinname and baudrate in the constructor |
mazgch | 19:2b5d097ca15d | 10 | // this helper macro will be used. |
mazgch | 19:2b5d097ca15d | 11 | #define _C027DEFAULT(name) = name |
mazgch | 19:2b5d097ca15d | 12 | #else |
mazgch | 19:2b5d097ca15d | 13 | #define _C027DEFAULT(name) |
mazgch | 19:2b5d097ca15d | 14 | #endif |
mazgch | 9:e7a5959ffae1 | 15 | |
mazgch | 38:e6cab4632d84 | 16 | /** basic gps parser class |
mazgch | 38:e6cab4632d84 | 17 | */ |
mazgch | 2:b6012cd91657 | 18 | class GPSParser |
mazgch | 2:b6012cd91657 | 19 | { |
mazgch | 2:b6012cd91657 | 20 | public: |
mazgch | 51:e7b81c31baec | 21 | enum { |
mazgch | 51:e7b81c31baec | 22 | // getLine Responses |
mazgch | 51:e7b81c31baec | 23 | WAIT = -1, //!< wait for more incoming data (the start of a message was found, or no data available) |
mazgch | 51:e7b81c31baec | 24 | NOT_FOUND = 0, //!< a parser concluded the the current offset of the pipe doe not contain a valid message |
mazgch | 39:9b4b9433e220 | 25 | |
mazgch | 51:e7b81c31baec | 26 | #define LENGTH(x) (x & 0x00FFFF) //!< extract/mask the length |
mazgch | 51:e7b81c31baec | 27 | #define PROTOCOL(x) (x & 0xFF0000) //!< extract/mask the type |
mazgch | 39:9b4b9433e220 | 28 | |
mazgch | 51:e7b81c31baec | 29 | UNKNOWN = 0x000000, //!< message type is unknown |
mazgch | 51:e7b81c31baec | 30 | UBX = 0x100000, //!< message if of protocol NMEA |
mazgch | 51:e7b81c31baec | 31 | NMEA = 0x200000 //!< message if of protocol UBX |
mazgch | 51:e7b81c31baec | 32 | }; |
mazgch | 51:e7b81c31baec | 33 | |
mazgch | 39:9b4b9433e220 | 34 | /** Get a line from the physical interface. This fucntion |
mazgch | 39:9b4b9433e220 | 35 | needs to be implemented in the inherited class. |
mazgch | 39:9b4b9433e220 | 36 | \param buf the buffer to store it |
mazgch | 39:9b4b9433e220 | 37 | \param len size of the buffer |
mazgch | 39:9b4b9433e220 | 38 | \return type and length if something was found, |
mazgch | 39:9b4b9433e220 | 39 | WAIT if not enough data is available |
mazgch | 39:9b4b9433e220 | 40 | NOT_FOUND if nothing was found |
mazgch | 39:9b4b9433e220 | 41 | */ |
mazgch | 39:9b4b9433e220 | 42 | virtual int getMessage(char* buf, int len) = 0; |
mazgch | 2:b6012cd91657 | 43 | |
mazgch | 39:9b4b9433e220 | 44 | /** send a buffer |
mazgch | 39:9b4b9433e220 | 45 | \param buf the buffer to write |
mazgch | 39:9b4b9433e220 | 46 | \param len size of the buffer to write |
mazgch | 39:9b4b9433e220 | 47 | \return bytes written |
mazgch | 39:9b4b9433e220 | 48 | */ |
mazgch | 4:c959dd4c5fe8 | 49 | virtual int send(const char* buf, int len); |
mazgch | 39:9b4b9433e220 | 50 | |
mazgch | 39:9b4b9433e220 | 51 | /** send a NMEA message, this function just takes the |
mazgch | 39:9b4b9433e220 | 52 | payload and calculates and adds checksum. ($ and *XX\r\n will be added) |
mazgch | 39:9b4b9433e220 | 53 | \param buf the message payload to write |
mazgch | 39:9b4b9433e220 | 54 | \param len size of the message payload to write |
mazgch | 39:9b4b9433e220 | 55 | \return total bytes written |
mazgch | 39:9b4b9433e220 | 56 | */ |
mazgch | 4:c959dd4c5fe8 | 57 | virtual int sendNmea(const char* buf, int len); |
mazgch | 39:9b4b9433e220 | 58 | |
mazgch | 39:9b4b9433e220 | 59 | /** send a UBX message, this function just takes the |
mazgch | 39:9b4b9433e220 | 60 | payload and calculates and adds checksum. |
mazgch | 39:9b4b9433e220 | 61 | \param cls the UBX class id |
mazgch | 39:9b4b9433e220 | 62 | \param id the UBX message id |
mazgch | 39:9b4b9433e220 | 63 | \param buf the message payload to write |
mazgch | 39:9b4b9433e220 | 64 | \param len size of the message payload to write |
mazgch | 39:9b4b9433e220 | 65 | \return total bytes written |
mazgch | 39:9b4b9433e220 | 66 | */ |
mazgch | 40:295099ff5338 | 67 | virtual int sendUbx(unsigned char cls, unsigned char id, |
mazgch | 40:295099ff5338 | 68 | const void* buf = NULL, int len = 0); |
mazgch | 40:295099ff5338 | 69 | |
mazgch | 39:9b4b9433e220 | 70 | /** Power off the gps, it can be again woken up by an |
mazgch | 39:9b4b9433e220 | 71 | edge on the serial port on the external interrupt pin. |
mazgch | 39:9b4b9433e220 | 72 | */ |
mazgch | 31:a0bed6c1e05d | 73 | void powerOff(void); |
mazgch | 2:b6012cd91657 | 74 | |
mazgch | 39:9b4b9433e220 | 75 | /** get the first character of a NMEA field |
mazgch | 39:9b4b9433e220 | 76 | \param ix the index of the field to find |
mazgch | 39:9b4b9433e220 | 77 | \param start the start of the buffer |
mazgch | 39:9b4b9433e220 | 78 | \param end the end of the buffer |
mazgch | 39:9b4b9433e220 | 79 | \return the pointer to the first character of the field. |
mazgch | 39:9b4b9433e220 | 80 | */ |
mazgch | 2:b6012cd91657 | 81 | static const char* findNmeaItemPos(int ix, const char* start, const char* end); |
mazgch | 39:9b4b9433e220 | 82 | |
mazgch | 39:9b4b9433e220 | 83 | /** extract a double value from a buffer containing a NMEA message |
mazgch | 39:9b4b9433e220 | 84 | \param ix the index of the field to extract |
mazgch | 39:9b4b9433e220 | 85 | \param buf the NMEA message |
mazgch | 39:9b4b9433e220 | 86 | \param len the size of the NMEA message |
mazgch | 39:9b4b9433e220 | 87 | \param val the extracted value |
mazgch | 39:9b4b9433e220 | 88 | \return true if successful, false otherwise |
mazgch | 39:9b4b9433e220 | 89 | */ |
mazgch | 2:b6012cd91657 | 90 | static bool getNmeaItem(int ix, char* buf, int len, double& val); |
mazgch | 39:9b4b9433e220 | 91 | |
mazgch | 39:9b4b9433e220 | 92 | /** extract a interger value from a buffer containing a NMEA message |
mazgch | 39:9b4b9433e220 | 93 | \param ix the index of the field to extract |
mazgch | 39:9b4b9433e220 | 94 | \param buf the NMEA message |
mazgch | 39:9b4b9433e220 | 95 | \param len the size of the NMEA message |
mazgch | 39:9b4b9433e220 | 96 | \param val the extracted value |
mazgch | 39:9b4b9433e220 | 97 | \param base the numeric base to be used (e.g. 8, 10 or 16) |
mazgch | 39:9b4b9433e220 | 98 | \return true if successful, false otherwise |
mazgch | 39:9b4b9433e220 | 99 | */ |
mazgch | 2:b6012cd91657 | 100 | static bool getNmeaItem(int ix, char* buf, int len, int& val, int base/*=10*/); |
mazgch | 39:9b4b9433e220 | 101 | |
mazgch | 39:9b4b9433e220 | 102 | /** extract a char value from a buffer containing a NMEA message |
mazgch | 39:9b4b9433e220 | 103 | \param ix the index of the field to extract |
mazgch | 39:9b4b9433e220 | 104 | \param buf the NMEA message |
mazgch | 39:9b4b9433e220 | 105 | \param len the size of the NMEA message |
mazgch | 39:9b4b9433e220 | 106 | \param val the extracted value |
mazgch | 39:9b4b9433e220 | 107 | \return true if successful, false otherwise |
mazgch | 39:9b4b9433e220 | 108 | */ |
mazgch | 2:b6012cd91657 | 109 | static bool getNmeaItem(int ix, char* buf, int len, char& val); |
mazgch | 39:9b4b9433e220 | 110 | |
mazgch | 39:9b4b9433e220 | 111 | /** extract a latitude/longitude value from a buffer containing a NMEA message |
mazgch | 39:9b4b9433e220 | 112 | \param ix the index of the field to extract (will extract ix and ix + 1) |
mazgch | 39:9b4b9433e220 | 113 | \param buf the NMEA message |
mazgch | 39:9b4b9433e220 | 114 | \param len the size of the NMEA message |
mazgch | 39:9b4b9433e220 | 115 | \param val the extracted latitude or longitude |
mazgch | 39:9b4b9433e220 | 116 | \return true if successful, false otherwise |
mazgch | 39:9b4b9433e220 | 117 | */ |
mazgch | 20:535ef78655df | 118 | static bool getNmeaAngle(int ix, char* buf, int len, double& val); |
mazgch | 39:9b4b9433e220 | 119 | |
mazgch | 2:b6012cd91657 | 120 | protected: |
mazgch | 39:9b4b9433e220 | 121 | /** Get a line from the physical interface. |
mazgch | 39:9b4b9433e220 | 122 | \param pipe the receiveing pipe to parse messages |
mazgch | 39:9b4b9433e220 | 123 | \param buf the buffer to store it |
mazgch | 39:9b4b9433e220 | 124 | \param len size of the buffer |
mazgch | 39:9b4b9433e220 | 125 | \return type and length if something was found, |
mazgch | 39:9b4b9433e220 | 126 | WAIT if not enough data is available |
mazgch | 39:9b4b9433e220 | 127 | NOT_FOUND if nothing was found |
mazgch | 39:9b4b9433e220 | 128 | */ |
mazgch | 2:b6012cd91657 | 129 | static int _getMessage(Pipe<char>* pipe, char* buf, int len); |
mazgch | 39:9b4b9433e220 | 130 | |
mazgch | 39:9b4b9433e220 | 131 | /** Check if the current offset of the pipe contains a NMEA message. |
mazgch | 39:9b4b9433e220 | 132 | \param pipe the receiveing pipe to parse messages |
mazgch | 39:9b4b9433e220 | 133 | \param len numer of bytes to parse at maximum |
mazgch | 39:9b4b9433e220 | 134 | \return length if something was found (including the NMEA frame) |
mazgch | 39:9b4b9433e220 | 135 | WAIT if not enough data is available |
mazgch | 39:9b4b9433e220 | 136 | NOT_FOUND if nothing was found |
mazgch | 39:9b4b9433e220 | 137 | */ |
mazgch | 2:b6012cd91657 | 138 | static int _parseNmea(Pipe<char>* pipe, int len); |
mazgch | 39:9b4b9433e220 | 139 | |
mazgch | 39:9b4b9433e220 | 140 | /** Check if the current offset of the pipe contains a UBX message. |
mazgch | 39:9b4b9433e220 | 141 | \param pipe the receiveing pipe to parse messages |
mazgch | 39:9b4b9433e220 | 142 | \param len numer of bytes to parse at maximum |
mazgch | 39:9b4b9433e220 | 143 | \return length if something was found (including the UBX frame) |
mazgch | 39:9b4b9433e220 | 144 | WAIT if not enough data is available |
mazgch | 39:9b4b9433e220 | 145 | NOT_FOUND if nothing was found |
mazgch | 39:9b4b9433e220 | 146 | */ |
mazgch | 2:b6012cd91657 | 147 | static int _parseUbx(Pipe<char>* pipe, int len); |
mazgch | 39:9b4b9433e220 | 148 | |
mazgch | 39:9b4b9433e220 | 149 | /** Write bytes to the physical interface. This function |
mazgch | 39:9b4b9433e220 | 150 | needs to be implemented by the inherited class. |
mazgch | 39:9b4b9433e220 | 151 | \param buf the buffer to write |
mazgch | 39:9b4b9433e220 | 152 | \param len size of the buffer to write |
mazgch | 39:9b4b9433e220 | 153 | \return bytes written |
mazgch | 39:9b4b9433e220 | 154 | */ |
mazgch | 4:c959dd4c5fe8 | 155 | virtual int _send(const void* buf, int len) = 0; |
mazgch | 39:9b4b9433e220 | 156 | |
mazgch | 39:9b4b9433e220 | 157 | static const char toHex[16]; //!< num to hex conversion |
mazgch | 2:b6012cd91657 | 158 | }; |
mazgch | 2:b6012cd91657 | 159 | |
mazgch | 38:e6cab4632d84 | 160 | /** gps class which uses a serial port |
mazgch | 38:e6cab4632d84 | 161 | as physical interface. |
mazgch | 38:e6cab4632d84 | 162 | */ |
mazgch | 9:e7a5959ffae1 | 163 | class GPSSerial : public SerialPipe, public GPSParser |
mazgch | 1:f41579f4e2ed | 164 | { |
mazgch | 1:f41579f4e2ed | 165 | public: |
mazgch | 38:e6cab4632d84 | 166 | /** Constructor |
mazgch | 38:e6cab4632d84 | 167 | \param tx is the serial ports transmit pin (gps to CPU) |
mazgch | 38:e6cab4632d84 | 168 | \param rx is the serial ports receive pin (CPU to gps) |
mazgch | 38:e6cab4632d84 | 169 | \param baudrate the baudrate of the gps use 9600 |
mazgch | 38:e6cab4632d84 | 170 | \param rxSize the size of the serial rx buffer |
mazgch | 38:e6cab4632d84 | 171 | \param txSize the size of the serial tx buffer |
mazgch | 38:e6cab4632d84 | 172 | */ |
mazgch | 19:2b5d097ca15d | 173 | GPSSerial(PinName tx _C027DEFAULT( GPSTXD ), |
mazgch | 19:2b5d097ca15d | 174 | PinName rx _C027DEFAULT( GPSRXD ), |
mazgch | 19:2b5d097ca15d | 175 | int baudrate _C027DEFAULT( GPSBAUD ), |
mazgch | 19:2b5d097ca15d | 176 | int rxSize = 256 , |
mazgch | 19:2b5d097ca15d | 177 | int txSize = 128 ); |
mazgch | 39:9b4b9433e220 | 178 | |
mazgch | 38:e6cab4632d84 | 179 | /** Get a line from the physical interface. |
mazgch | 38:e6cab4632d84 | 180 | \param buf the buffer to store it |
mazgch | 38:e6cab4632d84 | 181 | \param len size of the buffer |
mazgch | 38:e6cab4632d84 | 182 | \return type and length if something was found, |
mazgch | 38:e6cab4632d84 | 183 | WAIT if not enough data is available |
mazgch | 38:e6cab4632d84 | 184 | NOT_FOUND if nothing was found |
mazgch | 38:e6cab4632d84 | 185 | */ |
mazgch | 2:b6012cd91657 | 186 | virtual int getMessage(char* buf, int len); |
mazgch | 39:9b4b9433e220 | 187 | |
mazgch | 2:b6012cd91657 | 188 | protected: |
mazgch | 38:e6cab4632d84 | 189 | /** Write bytes to the physical interface. |
mazgch | 38:e6cab4632d84 | 190 | \param buf the buffer to write |
mazgch | 38:e6cab4632d84 | 191 | \param len size of the buffer to write |
mazgch | 38:e6cab4632d84 | 192 | \return bytes written |
mazgch | 38:e6cab4632d84 | 193 | */ |
mazgch | 4:c959dd4c5fe8 | 194 | virtual int _send(const void* buf, int len); |
mazgch | 1:f41579f4e2ed | 195 | }; |
mazgch | 2:b6012cd91657 | 196 | |
mazgch | 38:e6cab4632d84 | 197 | /** gps class which uses a i2c as physical interface. |
mazgch | 38:e6cab4632d84 | 198 | */ |
mazgch | 2:b6012cd91657 | 199 | class GPSI2C : public I2C, public GPSParser |
mazgch | 2:b6012cd91657 | 200 | { |
mazgch | 2:b6012cd91657 | 201 | public: |
mazgch | 38:e6cab4632d84 | 202 | /** Constructor |
mazgch | 38:e6cab4632d84 | 203 | \param sda is the I2C SDA pin (between CPU and GPS) |
mazgch | 38:e6cab4632d84 | 204 | \param scl is the I2C SCL pin (CPU to GPS) |
mazgch | 38:e6cab4632d84 | 205 | \param adr the I2C address of the GPS set to (66<<1) |
mazgch | 38:e6cab4632d84 | 206 | \param rxSize the size of the serial rx buffer |
mazgch | 38:e6cab4632d84 | 207 | */ |
mazgch | 19:2b5d097ca15d | 208 | GPSI2C(PinName sda _C027DEFAULT( GPSSDA ), |
mazgch | 19:2b5d097ca15d | 209 | PinName scl _C027DEFAULT( GPSSCL ), |
mazgch | 19:2b5d097ca15d | 210 | unsigned char i2cAdr _C027DEFAULT( GPSADR ), |
mazgch | 19:2b5d097ca15d | 211 | int rxSize = 256 ); |
mazgch | 39:9b4b9433e220 | 212 | |
mazgch | 38:e6cab4632d84 | 213 | /** helper function to probe the i2c device |
mazgch | 38:e6cab4632d84 | 214 | \return true if successfully detected the gps. |
mazgch | 38:e6cab4632d84 | 215 | */ |
mazgch | 2:b6012cd91657 | 216 | bool detect(void); |
mazgch | 2:b6012cd91657 | 217 | |
mazgch | 38:e6cab4632d84 | 218 | /** Get a line from the physical interface. |
mazgch | 38:e6cab4632d84 | 219 | \param buf the buffer to store it |
mazgch | 38:e6cab4632d84 | 220 | \param len size of the buffer |
mazgch | 38:e6cab4632d84 | 221 | \return type and length if something was found, |
mazgch | 38:e6cab4632d84 | 222 | WAIT if not enough data is available |
mazgch | 38:e6cab4632d84 | 223 | NOT_FOUND if nothing was found |
mazgch | 38:e6cab4632d84 | 224 | */ |
mazgch | 2:b6012cd91657 | 225 | virtual int getMessage(char* buf, int len); |
mazgch | 38:e6cab4632d84 | 226 | |
mazgch | 38:e6cab4632d84 | 227 | /** send a buffer |
mazgch | 38:e6cab4632d84 | 228 | \param buf the buffer to write |
mazgch | 38:e6cab4632d84 | 229 | \param len size of the buffer to write |
mazgch | 38:e6cab4632d84 | 230 | \return bytes written |
mazgch | 38:e6cab4632d84 | 231 | */ |
mazgch | 4:c959dd4c5fe8 | 232 | virtual int send(const char* buf, int len); |
mazgch | 38:e6cab4632d84 | 233 | |
mazgch | 38:e6cab4632d84 | 234 | /** send a NMEA message, this function just takes the |
mazgch | 38:e6cab4632d84 | 235 | payload and calculates and adds checksum. ($ and *XX\r\n will be added) |
mazgch | 38:e6cab4632d84 | 236 | \param buf the message payload to write |
mazgch | 38:e6cab4632d84 | 237 | \param len size of the message payload to write |
mazgch | 38:e6cab4632d84 | 238 | \return total bytes written |
mazgch | 38:e6cab4632d84 | 239 | */ |
mazgch | 3:c7cd4887560d | 240 | virtual int sendNmea(const char* buf, int len); |
mazgch | 38:e6cab4632d84 | 241 | |
mazgch | 38:e6cab4632d84 | 242 | /** send a UBX message, this function just takes the |
mazgch | 38:e6cab4632d84 | 243 | payload and calculates and adds checksum. |
mazgch | 38:e6cab4632d84 | 244 | \param cls the UBX class id |
mazgch | 38:e6cab4632d84 | 245 | \param id the UBX message id |
mazgch | 38:e6cab4632d84 | 246 | \param buf the message payload to write |
mazgch | 38:e6cab4632d84 | 247 | \param len size of the message payload to write |
mazgch | 38:e6cab4632d84 | 248 | \return total bytes written |
mazgch | 38:e6cab4632d84 | 249 | */ |
mazgch | 39:9b4b9433e220 | 250 | virtual int sendUbx(unsigned char cls, unsigned char id, |
mazgch | 39:9b4b9433e220 | 251 | const void* buf = NULL, int len = 0); |
mazgch | 39:9b4b9433e220 | 252 | |
mazgch | 2:b6012cd91657 | 253 | protected: |
mazgch | 38:e6cab4632d84 | 254 | /** check if the port is writeable (like SerialPipe) |
mazgch | 38:e6cab4632d84 | 255 | \return true if writeable |
mazgch | 38:e6cab4632d84 | 256 | */ |
mazgch | 9:e7a5959ffae1 | 257 | bool writeable(void) { return true; } |
mazgch | 39:9b4b9433e220 | 258 | |
mazgch | 38:e6cab4632d84 | 259 | /** Write a character (like SerialPipe) |
mazgch | 38:e6cab4632d84 | 260 | \param c the character to write |
mazgch | 38:e6cab4632d84 | 261 | \return true if succesffully written |
mazgch | 38:e6cab4632d84 | 262 | */ |
mazgch | 9:e7a5959ffae1 | 263 | bool putc(int c) { char ch = c; return send(&ch, 1); } |
mazgch | 39:9b4b9433e220 | 264 | |
mazgch | 38:e6cab4632d84 | 265 | /** Write bytes to the physical interface. |
mazgch | 38:e6cab4632d84 | 266 | \param buf the buffer to write |
mazgch | 38:e6cab4632d84 | 267 | \param len size of the buffer to write |
mazgch | 38:e6cab4632d84 | 268 | \return bytes written |
mazgch | 38:e6cab4632d84 | 269 | */ |
mazgch | 4:c959dd4c5fe8 | 270 | virtual int _send(const void* buf, int len); |
mazgch | 39:9b4b9433e220 | 271 | |
mazgch | 38:e6cab4632d84 | 272 | /** read bytes from the physical interface. |
mazgch | 38:e6cab4632d84 | 273 | \param buf the buffer to read into |
mazgch | 38:e6cab4632d84 | 274 | \param len size of the read buffer |
mazgch | 38:e6cab4632d84 | 275 | \return bytes read |
mazgch | 38:e6cab4632d84 | 276 | */ |
mazgch | 38:e6cab4632d84 | 277 | int _get(char* buf, int len); |
mazgch | 3:c7cd4887560d | 278 | |
mazgch | 38:e6cab4632d84 | 279 | Pipe<char> _pipe; //!< the rx pipe |
mazgch | 38:e6cab4632d84 | 280 | bool found; //!< flag if device is detected. |
mazgch | 38:e6cab4632d84 | 281 | unsigned char _i2cAdr; //!< the i2c address |
mazgch | 38:e6cab4632d84 | 282 | static const char REGLEN; //!< the length i2c register address |
mazgch | 38:e6cab4632d84 | 283 | static const char REGSTREAM;//!< the stream i2c register address |
mazgch | 2:b6012cd91657 | 284 | }; |