GPS With Thread

Dependencies:   MODSERIAL mbed-rtos mbed-src

Fork of GPS_U-blox_NEO-6M_Test_Code by Edoardo De Marchi

main.cpp

Committer:
saypulung
Date:
2015-04-26
Revision:
2:13ef79e40e45
Parent:
1:acd907fbcbae

File content as of revision 2:13ef79e40e45:

/*
 * Author: Edoardo De Marchi
 * Date: 22-08-14
 * Notes: Firmware for GPS U-Blox NEO-6M
*/

#include "main.h"
#include "string.h"
#include "rtos.h"
char gpsData[10][500];
void Init()
{
    gps.baud(9600);
    pc.baud(9600);

    pc.printf("Init OK\n");
}

void readGPSThread(void const *args)
{
    char c;
    while(true) 
    {
        if(gps.readable())
        { 
            if(gps.getc() == '$');           // wait a $
            {
                for(int i=0; i<sizeof(cDataBuffer); i++)
                {
                    c = gps.getc();
                    if( c == '\r' )
                    {
                        //int a = sizeof(cDataBuffer);
                        //char data[500];
                        //int ccS = sprintf(data,"%s",cDataBuffer);
                        
                        pc.printf("Message : %s\n", cDataBuffer);
                        if(strncmp(cDataBuffer,"$GPRMC",6)==0)
                        {
                            
                        }
                        //int count = getDataCount(data,",");
                        //pc.printf("Length  : %d\n",count);
                        //fflush(stdin);
                        //fflush(stdout);
                        //parse(cDataBuffer, i);
                        i = sizeof(cDataBuffer);
                    }
                    else
                    {
                        cDataBuffer[i] = c;
                    }                 
                }
            }
         } 
    }
}

int main() 
{   
    Init();
    DigitalOut l1(LED1);
    Thread gpsReader(readGPSThread);
    
    while(1){
        l1 = 1;
        wait(0.5);
        l1=0;
        wait(0.5);
    }
    
}


void parse(char *cmd, int n)
{
    
    char ns, ew, tf, status;
    int fq, nst, fix, date;                                     // fix quality, Number of satellites being tracked, 3D fix
    float latitude, longitude, timefix, speed, altitude;
    
    pc.printf("Parser cooyy\n");
    // Global Positioning System Fix Data
    if(strncmp(cmd,"$GPGGA", 6) == 0) 
    {
        sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
        pc.printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude);
    }
    
    // Satellite status
    if(strncmp(cmd,"$GPGSA", 6) == 0) 
    {
        sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
        pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
    }
    
    // Geographic position, Latitude and Longitude
    if(strncmp(cmd,"$GPGLL", 6) == 0) 
    {
        sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
        pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n", latitude, ns, longitude, ew, timefix);
    }
    
    // Geographic position, Latitude and Longitude
    if(strncmp(cmd,"$GPRMC", 6) == 0) 
    {
        sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
        pc.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", timefix, status, latitude, ns, longitude, ew, speed, date);
    }
}

void splitString(char str[],char *delimiter)
{
    int count=0;
    char *tok = strtok(str,delimiter);
    int idx=0;
    while(0!=tok)
    {
        int cpy = sprintf(gpsData[idx],"%s",tok);
        pc.printf("Data %d = %s\n",idx,gpsData[idx]);
        tok = strtok(NULL,delimiter);
        idx++;
    }   
    //return count;
}