QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

handle/handleGPS.cpp

Committer:
dylanembed123
Date:
2014-04-03
Revision:
9:da906eeac51e
Parent:
8:28b866df62cf
Child:
14:6be57da62283

File content as of revision 9:da906eeac51e:

#include "handleGPS.h"

//Serial gps(p28,p27);

void GPSHandle::setup(){
    gps.getSerial().baud(57600);
    sendGpsCommand("PMTK301,1");
    //gps.getSerial().attach(this, handleUpdate, Serial::RxIrq);
    //cs: Send other standard init commands? Not strictly speaking necessary,
    //    but forces "up to date documentation" in the form of always knowing
    //    gps config.
}

void GPSHandle::handleUpdate(){
    static bool reading = false;
    static std::stringstream line;

    char c = gps.getSerial().getc();

    if (reading) {
        if (c == '*') { //sentence buffer complete; we're ignoring the checksum
            std::string field;
            std::getline(line, field, ','); //GPGGA
            std::getline(line, field, ','); //time
            double time = atof(field.c_str());
            std::getline(line, field, ','); //latitude
            double latitude = atof(field.c_str());
            std::getline(line, field, ','); //N or S
            std::getline(line, field, ','); //longitude
            double longitude = atof(field.c_str());
            std::getline(line, field, ','); //E or W
            std::getline(line, field, ','); //skip
            std::getline(line, field, ','); //skip
            std::getline(line, field, ','); //skip
            std::getline(line, field, ','); //altitude
            double altitude = atof(field.c_str());

            //update whatever needs updating when gps updates
//            pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
//                      gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
//                     );

            line.str(string(""));
            reading = false;

        } else {
            line.put(c);
        }

    } else if (c == '$') {
        reading = true;
    }
}

//sends: "$<command>*<checksum>\r\l"
void GPSHandle::sendGpsCommand(std::string command){
    uint8_t checksum = 0;
    //pc.printf("Sending command to gps: ");
    gps.getSerial().putc('$');
    //pc.putc('$');
    char c;
    for (int i = 0; i < command.length(); i++) {
        c = command[i];
        checksum ^= c;
        gps.getSerial().putc(c);
        //pc.putc(c);
    }
    gps.getSerial().putc('*');
    //pc.putc('*');
    string checkSumString;
    while (checksum > 0) {
        uint8_t checksumChar = checksum & 0x0F;
        if (checksumChar >= 10) {
            checksumChar -= 10;
            checksumChar += 'A';
        } else {
            checksumChar += '0';
        }
        checkSumString.push_back((char) checksumChar);
        checksum = checksum >> 4;
    }

    for (int i = checkSumString.length() - 1; i >= 0; i--) {
        gps.getSerial().putc(checkSumString[i]);
        //pc.putc(checkSumString[i]);
    }
    gps.getSerial().putc('\r');
    //pc.putc('\r');
    gps.getSerial().putc('\n');
    //pc.putc('\n');
}

int stringToDecimal(string s)
{
    int mult = 1;
    int result = 0;
    for (int i = s.length() - 1; i >=0; i--) {
        if (s[i] != '.') {
            result += (s[i] - '0') * mult;
            mult *= 10;
        }
    }
    return result;
}

bool GPSHandle::check(){
    return true;
}