Simple driver for GNSS functions on BG96 module.

Dependents:   mbed-os-example-cellular-gps-bg96

Note: this is early version

BG96 module needs to be already running to use this. Configuration only inside of this class. (hardcoded values)

BG96_GNSS.cpp

Committer:
Pawel Zarembski
Date:
2020-02-19
Revision:
1:c3a5d3a0b437
Parent:
0:6a2a480672be
Child:
2:8b0663001935

File content as of revision 1:c3a5d3a0b437:

#include "BG96_GNSS.h"
#include "mbed.h"
#include "mbed_debug.h"

BG96_GNSS::BG96_GNSS()
{
    this->_serial = new UARTSerial(D1, D0, 115200); 

    this->_parser = new ATCmdParser(_serial);
    this->_parser->debug_on(false);
    this->_parser->set_delimiter("\r\n");
    this->_parser->set_timeout(1000);
}

int BG96_GNSS::check_if_ready(void)
{
    int counter = 0;
    _parser->flush();

    while ((counter++) < 10) {
        if (_parser->recv("RDY")) {
            printf("GPS ready\r\n");
            return SUCCESS;
        }
        else if (_parser->send("AT") && _parser->recv("OK")) {
            printf("GPS already available\r\n");
            return SUCCESS;
        }
        thread_sleep_for(1);
    }

    printf("GPS not ready\r\n");
    return FAILURE;
}

int BG96_GNSS::set_gps_power(bool state)
{
    char _buf[32];

    sprintf((char *)_buf, "%s", state ? "AT+QGPS=1" : "AT+QGPSEND");

    if (_parser->send(_buf) && _parser->recv("OK")) {
        printf("GPS Power: %s\r\n", state ? "On" : "Off");
        return SUCCESS;
    } 
    else {
        printf("Set GPS Power %s failed\r\n", state ? "On" : "Off");  
        return FAILURE;
    }
}

int BG96_GNSS::get_gps_data(gps_data *data)
{ 
    bool ok = false;
    char _buf[100];
    int error;

    _parser->flush();
    // first check if there is no error, possibly fixing is not done yet (error 516)
    _parser->send((char*)"AT+QGPSLOC=2");
    ok = _parser->recv("+CME ERROR: ");
    if (ok) {
        _parser->recv("%s\r\n", _buf);
        sscanf(_buf,"%d", &error);   
        printf("+CME ERROR: %d\r\n", error);
        return FAILURE;
    }

    _parser->flush();
    // if there is no error, repeat to save data
    _parser->send((char*)"AT+QGPSLOC=2");
    ok = _parser->recv("+QGPSLOC: ");
    if (ok) {
        _parser->recv("%s\r\n", _buf);
        sscanf(_buf,"%f,%f,%f,%f,%f,%d,%f,%f,%f,%6s,%d",
                &data->utc, &data->lat, &data->lon, &data->hdop,
                &data->altitude, &data->fix, &data->cog,
                &data->spkm, &data->spkn, data->date, &data->nsat);
        ok = _parser->recv("OK");
        return SUCCESS;
    }
    else {
        return FAILURE;
    }
}