#ifndef __GPS_DEVICE_H__ #define __GPS_DEVICE_H__ #include "mbed.h" #define GPGGA "GPGGA" class GPSDevice { public: const static int length = 80; const static int baud = 9600; GPSDevice(Serial *uart) : uart(uart) {} /* * Permet de savoir si on lit le début de la trame associée au GPS. */ bool canBeRead() { return uart->readable() && uart->getc() == '$'; } /* * Permet de déterminer le type de trame du GPS. */ void readType(char type[6]){ uart->scanf("%5s", &type); } /* * Permet de lire la trame du GPS de type GGA. */ void readGPGGA() { char msg[100]; while (uart->getc() != ','); //waits for first comma then copies info uart->scanf("%s", &msg); char *tok = strtok(msg, ","); //begins isolating fields of information delimited by a comma int comma = 0; while(tok != NULL){ comma++; switch (comma) { case 1: UTC = tok; break; //GMT time in seconds, saves pointer to string case 2: lat = tok; break; //latitude case 3: latl = tok; break; //lat letter, (N or S) case 4: lon = tok; break; //longitude case 5: lonl = tok; break; //lon letter (W or E) case 6: valid = tok; break; //valid } tok = strtok(NULL, ","); } } /* * Permet de vérifier que la trame lue est valide. */ bool isReadValid() { return atoi(valid) == 1 || atoi(valid) == 2 || atoi(valid) == 3; } char* getUTC() { return UTC; } char* getLatitude() { if(strcmp(latl, "N")){ return lat; } else { char *newLat = ""; strcpy(newLat, "-"); strncat(lat, newLat, sizeof(lat)/sizeof(lat[0])); return newLat; } } char* getLongitude() { if(strcmp(lonl, "E")){ return lon; } else { char *newLon = ""; strcpy(newLon, "-"); strncat(lon, newLon, sizeof(lon)/sizeof(lon[0])); return newLon; } } private: Serial *uart; char *UTC, *lat, *latl, *lon, *lonl, *valid; }; #endif /* #ifndef __GPS_DEVICE_H__*/