Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MTK3339 by
MTK3339.h@2:2391d165df47, 2015-08-01 (annotated)
- Committer:
- tbronez
- Date:
- Sat Aug 01 02:56:26 2015 +0000
- Revision:
- 2:2391d165df47
- Parent:
- 1:3057ad6a5d4b
Increased message buffers by 1 to ensure \0 termination
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tbronez | 1:3057ad6a5d4b | 1 | // Header to represent serial interface to MTK3339 GPS chip |
| tbronez | 1:3057ad6a5d4b | 2 | // Original author: Embedded Artists |
| tbronez | 2:2391d165df47 | 3 | // Revised by T. Bronez, 2015-07-31 |
| tbronez | 1:3057ad6a5d4b | 4 | |
| embeddedartists | 0:bd0fe2412980 | 5 | #ifndef MTK3339_H |
| embeddedartists | 0:bd0fe2412980 | 6 | #define MTK3339_H |
| tbronez | 1:3057ad6a5d4b | 7 | #include <time.h> |
| embeddedartists | 0:bd0fe2412980 | 8 | |
| embeddedartists | 0:bd0fe2412980 | 9 | /** |
| embeddedartists | 0:bd0fe2412980 | 10 | * An interface to the MTK3339 GPS module. |
| embeddedartists | 0:bd0fe2412980 | 11 | */ |
| embeddedartists | 0:bd0fe2412980 | 12 | class MTK3339 { |
| embeddedartists | 0:bd0fe2412980 | 13 | public: |
| embeddedartists | 0:bd0fe2412980 | 14 | |
| embeddedartists | 0:bd0fe2412980 | 15 | enum NmeaSentence { |
| tbronez | 1:3057ad6a5d4b | 16 | NMEA_NONE = 0x00, |
| tbronez | 1:3057ad6a5d4b | 17 | NMEA_GGA = 0x01, |
| tbronez | 1:3057ad6a5d4b | 18 | NMEA_GSA = 0x02, |
| tbronez | 1:3057ad6a5d4b | 19 | NMEA_GSV = 0x04, |
| tbronez | 1:3057ad6a5d4b | 20 | NMEA_RMC = 0x08, |
| tbronez | 1:3057ad6a5d4b | 21 | NMEA_VTG = 0x10 |
| embeddedartists | 0:bd0fe2412980 | 22 | }; |
| embeddedartists | 0:bd0fe2412980 | 23 | |
| tbronez | 1:3057ad6a5d4b | 24 | struct RmcType { |
| tbronez | 1:3057ad6a5d4b | 25 | char status; // A valid, V invalid, blank unknown |
| tbronez | 1:3057ad6a5d4b | 26 | double lat_dd; // latitude in decimal degrees |
| tbronez | 1:3057ad6a5d4b | 27 | double lon_dd; // longitude in decimal degrees |
| tbronez | 1:3057ad6a5d4b | 28 | struct tm gps_tm; // date and time (to seconds) |
| tbronez | 1:3057ad6a5d4b | 29 | int msec; // millseconds |
| embeddedartists | 0:bd0fe2412980 | 30 | }; |
| embeddedartists | 0:bd0fe2412980 | 31 | |
| embeddedartists | 0:bd0fe2412980 | 32 | /** |
| embeddedartists | 0:bd0fe2412980 | 33 | * Create an interface to the MTK3339 GPS module |
| embeddedartists | 0:bd0fe2412980 | 34 | * |
| embeddedartists | 0:bd0fe2412980 | 35 | * @param tx UART TX line pin |
| embeddedartists | 0:bd0fe2412980 | 36 | * @param rx UART RX line pin |
| embeddedartists | 0:bd0fe2412980 | 37 | */ |
| embeddedartists | 0:bd0fe2412980 | 38 | MTK3339(PinName tx, PinName rx); |
| embeddedartists | 0:bd0fe2412980 | 39 | |
| embeddedartists | 0:bd0fe2412980 | 40 | /** |
| embeddedartists | 0:bd0fe2412980 | 41 | * Start to read data from the GPS module. |
| embeddedartists | 0:bd0fe2412980 | 42 | * |
| embeddedartists | 0:bd0fe2412980 | 43 | * @param fptr A pointer to a void function that will be called when there |
| embeddedartists | 0:bd0fe2412980 | 44 | * is data available. |
| embeddedartists | 0:bd0fe2412980 | 45 | * @param mask specifies which sentence types (NmeaSentence) that are of |
| embeddedartists | 0:bd0fe2412980 | 46 | * interest. The callback function will only be called for messages |
| embeddedartists | 0:bd0fe2412980 | 47 | * specified in this mask. |
| embeddedartists | 0:bd0fe2412980 | 48 | */ |
| embeddedartists | 0:bd0fe2412980 | 49 | void start(void (*fptr)(void), int mask); |
| embeddedartists | 0:bd0fe2412980 | 50 | |
| embeddedartists | 0:bd0fe2412980 | 51 | /** |
| embeddedartists | 0:bd0fe2412980 | 52 | * Start to read data from the GPS module. |
| embeddedartists | 0:bd0fe2412980 | 53 | * |
| embeddedartists | 0:bd0fe2412980 | 54 | * @param tptr pointer to the object to call the member function on |
| embeddedartists | 0:bd0fe2412980 | 55 | * @param mptr pointer to the member function to be called |
| embeddedartists | 0:bd0fe2412980 | 56 | * @param mask specifies which sentence types (NmeaSentence) that are of |
| embeddedartists | 0:bd0fe2412980 | 57 | * interest. The member function will only be called for messages |
| embeddedartists | 0:bd0fe2412980 | 58 | * specified in this mask. |
| embeddedartists | 0:bd0fe2412980 | 59 | */ |
| embeddedartists | 0:bd0fe2412980 | 60 | template<typename T> |
| embeddedartists | 0:bd0fe2412980 | 61 | void start(T* tptr, void (T::*mptr)(void), int mask) { |
| embeddedartists | 0:bd0fe2412980 | 62 | if((mptr != NULL) && (tptr != NULL) && mask) { |
| embeddedartists | 0:bd0fe2412980 | 63 | _dataCallback.attach(tptr, mptr); |
| embeddedartists | 0:bd0fe2412980 | 64 | _sentenceMask = mask; |
| embeddedartists | 0:bd0fe2412980 | 65 | _serial.attach(this, &MTK3339::uartIrq, Serial::RxIrq); |
| embeddedartists | 0:bd0fe2412980 | 66 | } |
| embeddedartists | 0:bd0fe2412980 | 67 | } |
| embeddedartists | 0:bd0fe2412980 | 68 | |
| embeddedartists | 0:bd0fe2412980 | 69 | /** |
| embeddedartists | 0:bd0fe2412980 | 70 | * Stop to read data from GPS module |
| embeddedartists | 0:bd0fe2412980 | 71 | */ |
| embeddedartists | 0:bd0fe2412980 | 72 | void stop(); |
| embeddedartists | 0:bd0fe2412980 | 73 | |
| embeddedartists | 0:bd0fe2412980 | 74 | /** |
| embeddedartists | 0:bd0fe2412980 | 75 | * Get the type of the data reported in available data callback. |
| embeddedartists | 0:bd0fe2412980 | 76 | * This method will only return a valid type when called within the |
| embeddedartists | 0:bd0fe2412980 | 77 | * callback. |
| embeddedartists | 0:bd0fe2412980 | 78 | */ |
| embeddedartists | 0:bd0fe2412980 | 79 | NmeaSentence getAvailableDataType(); |
| embeddedartists | 0:bd0fe2412980 | 80 | |
| embeddedartists | 0:bd0fe2412980 | 81 | /** |
| tbronez | 1:3057ad6a5d4b | 82 | * Time, position and date related data |
| tbronez | 1:3057ad6a5d4b | 83 | */ |
| tbronez | 1:3057ad6a5d4b | 84 | enum PubConstants { |
| tbronez | 1:3057ad6a5d4b | 85 | MSG_BUF_SZ = 100 |
| tbronez | 2:2391d165df47 | 86 | }; |
| tbronez | 2:2391d165df47 | 87 | // Leave room for a terminating \0 in each buffer |
| tbronez | 2:2391d165df47 | 88 | char ggaMsg[MSG_BUF_SZ+1]; // longest observed strlen = 72 |
| tbronez | 2:2391d165df47 | 89 | char gsaMsg[MSG_BUF_SZ+1]; // longest observed strlen = 56 |
| tbronez | 2:2391d165df47 | 90 | char gsvMsg[MSG_BUF_SZ+1]; // longest observed strlen = 68 |
| tbronez | 2:2391d165df47 | 91 | char rmcMsg[MSG_BUF_SZ+1]; // longest observed strlen = 70 |
| tbronez | 2:2391d165df47 | 92 | char vtgMsg[MSG_BUF_SZ+1]; // longest observed strlen = 37 |
| embeddedartists | 0:bd0fe2412980 | 93 | |
| tbronez | 1:3057ad6a5d4b | 94 | RmcType rmc; |
| tbronez | 1:3057ad6a5d4b | 95 | void decodeRMC(); |
| embeddedartists | 0:bd0fe2412980 | 96 | |
| embeddedartists | 0:bd0fe2412980 | 97 | private: |
| embeddedartists | 0:bd0fe2412980 | 98 | |
| embeddedartists | 0:bd0fe2412980 | 99 | enum PrivConstants { |
| embeddedartists | 0:bd0fe2412980 | 100 | MTK3339_BUF_SZ = 255 |
| embeddedartists | 0:bd0fe2412980 | 101 | }; |
| embeddedartists | 0:bd0fe2412980 | 102 | |
| embeddedartists | 0:bd0fe2412980 | 103 | enum DataState { |
| embeddedartists | 0:bd0fe2412980 | 104 | StateStart = 0, |
| embeddedartists | 0:bd0fe2412980 | 105 | StateData |
| embeddedartists | 0:bd0fe2412980 | 106 | }; |
| embeddedartists | 0:bd0fe2412980 | 107 | |
| embeddedartists | 0:bd0fe2412980 | 108 | FunctionPointer _dataCallback; |
| embeddedartists | 0:bd0fe2412980 | 109 | char _buf[MTK3339_BUF_SZ]; |
| embeddedartists | 0:bd0fe2412980 | 110 | int _bufPos; |
| embeddedartists | 0:bd0fe2412980 | 111 | DataState _state; |
| embeddedartists | 0:bd0fe2412980 | 112 | int _sentenceMask; |
| embeddedartists | 0:bd0fe2412980 | 113 | NmeaSentence _availDataType; |
| embeddedartists | 0:bd0fe2412980 | 114 | Serial _serial; |
| embeddedartists | 0:bd0fe2412980 | 115 | |
| tbronez | 1:3057ad6a5d4b | 116 | void saveRMC(char* data, int dataLen); |
| tbronez | 1:3057ad6a5d4b | 117 | void saveGGA(char* data, int dataLen); |
| tbronez | 1:3057ad6a5d4b | 118 | void saveGSA(char* data, int dataLen); |
| tbronez | 1:3057ad6a5d4b | 119 | void saveGSV(char* data, int dataLen); |
| tbronez | 1:3057ad6a5d4b | 120 | void saveVTG(char* data, int dataLen); |
| tbronez | 1:3057ad6a5d4b | 121 | void saveData(char* data, int len); |
| embeddedartists | 0:bd0fe2412980 | 122 | void uartIrq(); |
| embeddedartists | 0:bd0fe2412980 | 123 | |
| embeddedartists | 0:bd0fe2412980 | 124 | }; |
| embeddedartists | 0:bd0fe2412980 | 125 | |
| embeddedartists | 0:bd0fe2412980 | 126 | #endif |
| embeddedartists | 0:bd0fe2412980 | 127 |
