Revised Embedded Artists' MTK3339 library to save all raw GPS messages and enable optional decoding of RMC message.

Fork of MTK3339 by EmbeddedArtists AB

Committer:
tbronez
Date:
Fri Jul 31 21:20:55 2015 +0000
Revision:
1:3057ad6a5d4b
Parent:
0:bd0fe2412980
Child:
2:2391d165df47
Highly modified GPS interface based on original EA interface

Who changed what in which revision?

UserRevisionLine numberNew 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 1:3057ad6a5d4b 3 // Revised by T. Bronez, 2015-05-28
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 1:3057ad6a5d4b 86 };
tbronez 1:3057ad6a5d4b 87 char ggaMsg[MSG_BUF_SZ];
tbronez 1:3057ad6a5d4b 88 char gsaMsg[MSG_BUF_SZ];
tbronez 1:3057ad6a5d4b 89 char gsvMsg[MSG_BUF_SZ];
tbronez 1:3057ad6a5d4b 90 char rmcMsg[MSG_BUF_SZ];
tbronez 1:3057ad6a5d4b 91 char vtgMsg[MSG_BUF_SZ];
embeddedartists 0:bd0fe2412980 92
tbronez 1:3057ad6a5d4b 93 RmcType rmc;
tbronez 1:3057ad6a5d4b 94 void decodeRMC();
embeddedartists 0:bd0fe2412980 95
embeddedartists 0:bd0fe2412980 96 private:
embeddedartists 0:bd0fe2412980 97
embeddedartists 0:bd0fe2412980 98 enum PrivConstants {
embeddedartists 0:bd0fe2412980 99 MTK3339_BUF_SZ = 255
embeddedartists 0:bd0fe2412980 100 };
embeddedartists 0:bd0fe2412980 101
embeddedartists 0:bd0fe2412980 102 enum DataState {
embeddedartists 0:bd0fe2412980 103 StateStart = 0,
embeddedartists 0:bd0fe2412980 104 StateData
embeddedartists 0:bd0fe2412980 105 };
embeddedartists 0:bd0fe2412980 106
embeddedartists 0:bd0fe2412980 107 FunctionPointer _dataCallback;
embeddedartists 0:bd0fe2412980 108 char _buf[MTK3339_BUF_SZ];
embeddedartists 0:bd0fe2412980 109 int _bufPos;
embeddedartists 0:bd0fe2412980 110 DataState _state;
embeddedartists 0:bd0fe2412980 111 int _sentenceMask;
embeddedartists 0:bd0fe2412980 112 NmeaSentence _availDataType;
embeddedartists 0:bd0fe2412980 113 Serial _serial;
embeddedartists 0:bd0fe2412980 114
tbronez 1:3057ad6a5d4b 115 void saveRMC(char* data, int dataLen);
tbronez 1:3057ad6a5d4b 116 void saveGGA(char* data, int dataLen);
tbronez 1:3057ad6a5d4b 117 void saveGSA(char* data, int dataLen);
tbronez 1:3057ad6a5d4b 118 void saveGSV(char* data, int dataLen);
tbronez 1:3057ad6a5d4b 119 void saveVTG(char* data, int dataLen);
tbronez 1:3057ad6a5d4b 120 void saveData(char* data, int len);
embeddedartists 0:bd0fe2412980 121 void uartIrq();
embeddedartists 0:bd0fe2412980 122
embeddedartists 0:bd0fe2412980 123 };
embeddedartists 0:bd0fe2412980 124
embeddedartists 0:bd0fe2412980 125 #endif
embeddedartists 0:bd0fe2412980 126