:)

Dependencies:   mbed

main.cpp

Committer:
kayu
Date:
2017-03-10
Revision:
0:447ddbf579d9
Child:
1:cfbdc934e8c6

File content as of revision 0:447ddbf579d9:

#include "mbed.h"
 
#define MESS_LEN    100     // maximum message length
#define MAX_STR     20
#define STR_LEN     40 
 
Serial _gps(A0, A1); //tx,rx
Serial pc(SERIAL_TX,SERIAL_RX);
DigitalOut myled(LED1);

char nmea[MESS_LEN] = {0xA0};
char gp[MAX_STR][STR_LEN]; 


void read_nmea(void);           // read nmea message from gps module
int chop_message(void);         // chop the nmea messeage
void present_array(void);       // send a part of the available info to the laptop
void setNmeaMessages(const bool , const bool , const bool , const bool , const bool , const bool ); 
void setUpdateRate(const uint8_t );

int main() {
     printf("HERE!\n");
     _gps.baud(9600);
    setUpdateRate(10);
    setNmeaMessages(true,true,true,true,true,true);    
    
    while (1) {
    
        read_nmea();
        chop_message();
        present_array();
        wait(1);
        
    }
   
}

//--------------------read nmea from GPS unit--------------------
void read_nmea(void) {
    _gps.scanf("%s,",nmea);
    pc.printf("Raw message %s \n\r",nmea); // just for testing
}
 
//---------------- chop the nmea message separated by comma's-----------
int chop_message(void) {
    for (int k=0; k<MAX_STR; k++) {          // just to make sure thet the char array is set to 0x00
        for (int l=0; l<STR_LEN; l++) {
            gp[k][l]= 0x00;
        };
    }
    int strcnt=0;                           // string counter
    int strpos=0;                           // position inside the string
    for (int k=0; k < MESS_LEN; k++) {
        if (nmea[k] == '*') {               // detect end of message is found
            gp[strcnt][strpos]= 0x00;       // close the string with 0x00
            return 0;                       // the work is done, end of this function
        }
        if (nmea[k] == 0x2c) {             // detect the comma
            if (strpos == 0) {
                gp[strcnt][0]= 'E';        // comma at position zero, string must be empty
                gp[strcnt][1]= 'm';        // comma at position zero, string must be empty
                gp[strcnt][2]= 'p';        // comma at position zero, string must be empty
                gp[strcnt][3]= 't';        // comma at position zero, string must be empty
                gp[strcnt][4]= 'y';        // comma at position zero, string must be empty
                gp[strcnt][5]= 0x00;       // comma at position zero, string must be empty
            } else {
                gp[strcnt][strpos]= 0x00;   // end the previous string
            }
            strcnt += 1;                    // increment to the next string
            strpos =0;                      // start at position zero
        } else {
            gp[strcnt][strpos]= nmea[k];    // add char to string
            strpos += 1;
        }
    }
    return 0;
}
 
// --------------- Dump only $GPGGA on the screen------------------
void present_array(void) {
    if ((gp[0][2]=='P') and (gp[0][3]=='G') and (gp[0][4]=='G') and (gp[0][5]=='A')) {
        pc.printf("Chop results ");
        for (int k=0; k<MAX_STR; k++) {
            pc.printf("r%d=%s ",k, gp[k]);
        }
        pc.printf("\n\r");
    }
}
//----------------Set Msg Command-----------------------------
void setNmeaMessages(const bool gga, const bool gsa, const bool gsv, const bool gll, const bool rmc, const bool vtg) {
    char cmd[16] = {
        0xA0, 0xA1, 0x00, 0x09,
        0x08, gga, gsa, gsv, gll, rmc, vtg, 0x00, 0x01,
        0x00, 0x0D, 0x0A
    };
    for(int i = 4; i < 13; i++) cmd[13] ^= cmd[i];
    for(int i = 0; i < 16; i++) _gps.putc(cmd[i]);
}

//----------------Set Update Rate-----------------------------
void setUpdateRate(const uint8_t rate) {
    char cmd[10] = {
        0xA0, 0xA1, 0x00, 0x03,
        0x0E, rate, 0x01,
        0x00, 0x0D, 0x0A
    };
    for(int i = 4; i < 7; i++) cmd[7] ^= cmd[i];
    for(int i = 0; i < 10; i++) _gps.putc(cmd[i]);
}