ttgo-t-beam-gps-ttn-tracker/src/gps.ino

92 lines
2.3 KiB
C++

/*
GPS module
Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <TinyGPS++.h>
uint32_t LatitudeBinary, LongitudeBinary;
uint16_t altitudeGps;
uint8_t hdopGps;
char t[32]; // used to sprintf for Serial output
TinyGPSPlus _gps;
HardwareSerial _serial_gps(GPS_SERIAL_NUM);
void gps_time(char * buffer, uint8_t size) {
snprintf(buffer, size, "%02d:%02d:%02d", _gps.time.hour(), _gps.time.minute(), _gps.time.second());
}
float gps_latitude() {
return _gps.location.lat();
}
float gps_longitude() {
return _gps.location.lng();
}
float gps_altitude() {
return _gps.altitude.meters();
}
float gps_hdop() {
return _gps.hdop.hdop();
}
uint8_t gps_sats() {
return _gps.satellites.value();
}
void gps_setup() {
_serial_gps.begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
}
static void gps_loop() {
while (_serial_gps.available()) {
_gps.encode(_serial_gps.read());
}
}
void buildPacket(uint8_t txBuffer[9])
{
LatitudeBinary = ((_gps.location.lat() + 90) / 180.0) * 16777215;
LongitudeBinary = ((_gps.location.lng() + 180) / 360.0) * 16777215;
sprintf(t, "Lat: %f", _gps.location.lat());
Serial.println(t);
sprintf(t, "Lng: %f", _gps.location.lng());
Serial.println(t);
txBuffer[0] = ( LatitudeBinary >> 16 ) & 0xFF;
txBuffer[1] = ( LatitudeBinary >> 8 ) & 0xFF;
txBuffer[2] = LatitudeBinary & 0xFF;
txBuffer[3] = ( LongitudeBinary >> 16 ) & 0xFF;
txBuffer[4] = ( LongitudeBinary >> 8 ) & 0xFF;
txBuffer[5] = LongitudeBinary & 0xFF;
altitudeGps = _gps.altitude.meters();
txBuffer[6] = ( altitudeGps >> 8 ) & 0xFF;
txBuffer[7] = altitudeGps & 0xFF;
hdopGps = _gps.hdop.value()/10;
txBuffer[8] = hdopGps & 0xFF;
}