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)
Diff: BG96_GNSS.cpp
- Revision:
- 3:98d27fc2eed5
- Parent:
- 2:8b0663001935
- Child:
- 4:8c7cf672e009
diff -r 8b0663001935 -r 98d27fc2eed5 BG96_GNSS.cpp --- a/BG96_GNSS.cpp Tue Mar 03 14:27:09 2020 +0100 +++ b/BG96_GNSS.cpp Tue Nov 10 09:06:10 2020 +0100 @@ -4,34 +4,6 @@ { _bg96_at_handler = CellularContext::get_default_instance()->get_device()->get_at_handler(); printf("[GPS] Created BG96 Object with AT HANDLER ADDR[%p].\r\n", _bg96_at_handler); - _bg96_instance = NULL; -} - -uint8_t BG96_GNSS::_get_instance() -{ - if (_bg96_instance == NULL) { - _bg96_instance = _bg96_at_handler->get_instance(_bg96_at_handler->get_file_handle(), - *_dummy_eventqueue, 1000, "\r\n", 100, false); - return SUCCESS; - } - else { - printf("[GPS] Failed while creating BG96 instance.\r\n"); - return FAILURE; - } -} - -uint8_t BG96_GNSS::_close_instance() -{ - if (_bg96_instance != NULL) { - _bg96_instance->close(); - _bg96_instance = NULL; - printf("[GPS] Closed BG96 instance.\r\n"); - return SUCCESS; - } - else { - printf("[GPS] Failed while closing BG96 instance.\r\n"); - return FAILURE; - } } uint8_t BG96_GNSS::set_gps_power(bool state) @@ -39,23 +11,17 @@ char _at_cmd[16]; uint8_t status = SUCCESS; - if (_get_instance() == SUCCESS) { - sprintf((char *)_at_cmd, "%s", state ? "AT+QGPS=1" : "AT+QGPSEND"); - _bg96_instance->lock(); - _bg96_instance->cmd_start(_at_cmd); - _bg96_instance->cmd_stop_read_resp(); - if (_bg96_instance->get_last_error() != NSAPI_ERROR_OK) { - printf("[GPS] Failure while trying to change BG96 power.\r\n"); - status = FAILURE; - } - printf("[GPS] Powered ON the GPS module.\r\n"); - _bg96_instance->unlock(); - _close_instance(); - } - else { - printf("[GPS] Failure while trying to get BG96 AT instance.\r\n"); + sprintf((char *)_at_cmd, "%s", state ? "AT+QGPS=1" : "AT+QGPSEND"); + _bg96_at_handler->lock(); + _bg96_at_handler->cmd_start(_at_cmd); + _bg96_at_handler->cmd_stop_read_resp(); + if (_bg96_at_handler->get_last_error() != NSAPI_ERROR_OK) { + printf("[GPS] Failure while trying to change BG96 power.\r\n"); status = FAILURE; - } + } + printf("[GPS] Powered ON the GPS module.\r\n"); + _bg96_at_handler->unlock(); + return status; } @@ -63,31 +29,25 @@ { uint8_t status = SUCCESS; - if (_get_instance() == SUCCESS) { - _bg96_instance->lock(); - _bg96_instance->cmd_start("AT+QGPSLOC=2"); - _bg96_instance->cmd_stop(); + _bg96_at_handler->lock(); + _bg96_at_handler->cmd_start("AT+QGPSLOC=2"); + _bg96_at_handler->cmd_stop(); - if (_bg96_instance->get_last_error() == NSAPI_ERROR_DEVICE_ERROR) { - printf("[GPS] Error while trying to get GPS data.\r\n"); + if (_bg96_at_handler->get_last_error() == NSAPI_ERROR_DEVICE_ERROR) { + printf("[GPS] Error while trying to get GPS data.\r\n"); + status = FAILURE; + } + else { + _bg96_at_handler->set_stop_tag("OK"); + _bg96_at_handler->resp_start(); + if (_read_gps(data) == FAILURE) { + printf("[GPS] Error while trying to read GPS data.\r\n"); status = FAILURE; } - else { - _bg96_instance->set_stop_tag("OK"); - _bg96_instance->resp_start(); - if (_read_gps(data) == FAILURE) { - printf("[GPS] Error while trying to read GPS data.\r\n"); - status = FAILURE; - } - _bg96_instance->resp_stop(); - } - _bg96_instance->unlock(); - _close_instance(); + _bg96_at_handler->resp_stop(); } - else { - printf("[GPS] Failure while trying to get BG96 AT instance.\r\n"); - status = FAILURE; - } + _bg96_at_handler->unlock(); + return status; } @@ -96,22 +56,22 @@ ssize_t bg96_read_size; uint8_t status = SUCCESS; - bg96_read_size = _bg96_instance->read_string(data->utc, sizeof(data->utc)); // header + bg96_read_size = _bg96_at_handler->read_string(data->utc, sizeof(data->utc)); // header if (bg96_read_size == -1) { status = FAILURE; } else { // bg96_read_size = _bg96_instance->read_string(data->utc, sizeof(data->utc)); // time - bg96_read_size = _bg96_instance->read_string(data->lat, sizeof(data->lat)); // lat - bg96_read_size = _bg96_instance->read_string(data->lon, sizeof(data->lon)); // lon - bg96_read_size = _bg96_instance->read_string(data->hdop, sizeof(data->hdop)); // hdop - bg96_read_size = _bg96_instance->read_string(data->altitude, sizeof(data->altitude)); // altitude - bg96_read_size = _bg96_instance->read_string(data->fix, sizeof(data->fix)); // fix - bg96_read_size = _bg96_instance->read_string(data->cog, sizeof(data->cog)); // cog - bg96_read_size = _bg96_instance->read_string(data->spkm, sizeof(data->spkm)); // spkm - bg96_read_size = _bg96_instance->read_string(data->spkn, sizeof(data->spkn)); // spkn - bg96_read_size = _bg96_instance->read_string(data->date, sizeof(data->date)); // date - bg96_read_size = _bg96_instance->read_string(data->nsat, sizeof(data->nsat)); // nsat + bg96_read_size = _bg96_at_handler->read_string(data->lat, sizeof(data->lat)); // lat + bg96_read_size = _bg96_at_handler->read_string(data->lon, sizeof(data->lon)); // lon + bg96_read_size = _bg96_at_handler->read_string(data->hdop, sizeof(data->hdop)); // hdop + bg96_read_size = _bg96_at_handler->read_string(data->altitude, sizeof(data->altitude)); // altitude + bg96_read_size = _bg96_at_handler->read_string(data->fix, sizeof(data->fix)); // fix + bg96_read_size = _bg96_at_handler->read_string(data->cog, sizeof(data->cog)); // cog + bg96_read_size = _bg96_at_handler->read_string(data->spkm, sizeof(data->spkm)); // spkm + bg96_read_size = _bg96_at_handler->read_string(data->spkn, sizeof(data->spkn)); // spkn + bg96_read_size = _bg96_at_handler->read_string(data->date, sizeof(data->date)); // date + bg96_read_size = _bg96_at_handler->read_string(data->nsat, sizeof(data->nsat)); // nsat } return status;