Changed gps coordinate formats to use sprintf()

1.2-legacy
srichs 2021-08-30 04:42:14 -06:00
rodzic 8edac1f86c
commit 5c6355489f
1 zmienionych plików z 82 dodań i 58 usunięć

Wyświetl plik

@ -390,6 +390,19 @@ static inline double toDegrees(double r)
return r * 180 / PI; return r * 180 / PI;
} }
// A struct to hold the data for a DMS coordinate.
struct DMS
{
byte latDeg;
byte latMin;
double latSec;
char latCP;
byte lonDeg;
byte lonMin;
double lonSec;
char lonCP;
};
// A struct to hold the data for a UTM coordinate, this is also used when creating an MGRS coordinate. // A struct to hold the data for a UTM coordinate, this is also used when creating an MGRS coordinate.
struct UTM struct UTM
{ {
@ -399,6 +412,17 @@ struct UTM
double northing; double northing;
}; };
// A struct to hold the data for a MGRS coordinate.
struct MGRS
{
byte zone;
char band;
char east100k;
char north100k;
uint32_t easting;
uint32_t northing;
};
/** /**
* Converts lat long coordinates to UTM. * Converts lat long coordinates to UTM.
* based on this: https://github.com/walvok/LatLonToUTM/blob/master/latlon_utm.ino * based on this: https://github.com/walvok/LatLonToUTM/blob/master/latlon_utm.ino
@ -448,42 +472,22 @@ static struct UTM latLongToUTM(const double lat, const double lon)
return utm; return utm;
} }
// Converts lat long coordinates to an UTM string. // Converts lat long coordinates to an MGRS.
static String latLongToUTMStr(double lat, double lon) static struct MGRS latLongToMGRS(double lat, double lon)
{
UTM utm = latLongToUTM(lat, lon);
return String(utm.zone) + String(utm.band) + " " + String(utm.easting, 1) + " " + String(utm.northing, 1);
}
// Converts lat long coordinates to an MGRS string.
static String latLongToMGRSStr(double lat, double lon)
{ {
const String e100kLetters[3] = { "ABCDEFGH", "JKLMNPQR", "STUVWXYZ" }; const String e100kLetters[3] = { "ABCDEFGH", "JKLMNPQR", "STUVWXYZ" };
const String n100kLetters[2] = { "ABCDEFGHJKLMNPQRSTUV", "FGHJKLMNPQRSTUVABCDE" }; const String n100kLetters[2] = { "ABCDEFGHJKLMNPQRSTUV", "FGHJKLMNPQRSTUVABCDE" };
UTM utm = latLongToUTM(lat, lon); UTM utm = latLongToUTM(lat, lon);
String mgrs = String(utm.zone) + String(utm.band) + " "; MGRS mgrs;
mgrs.zone = utm.zone;
mgrs.band = utm.band;
double col = floor(utm.easting / 100000); double col = floor(utm.easting / 100000);
char e100k = e100kLetters[(utm.zone - 1) % 3].charAt(col - 1); mgrs.east100k = e100kLetters[(mgrs.zone - 1) % 3].charAt(col - 1);
double row = (int)floor(utm.northing / 100000.0) % 20; double row = (int)floor(utm.northing / 100000.0) % 20;
char n100k = n100kLetters[(utm.zone - 1) % 2].charAt(row); mgrs.north100k = n100kLetters[(mgrs.zone-1)%2].charAt(row);
int easting = (int)utm.easting % 100000; mgrs.easting = (int)utm.easting % 100000;
int northing = (int)utm.northing % 100000; mgrs.northing = (int)utm.northing % 100000;
return mgrs + String(e100k) + String(n100k) + " " + String(easting) + " " + String(northing); return mgrs;
}
// Converts decimal degrees to degrees minutes seconds.
static String decDegreesToDMS(double val, char compassPoint)
{
double decDeg = val;
if (val < 0)
decDeg = decDeg * -1;
int d = floor(decDeg);
double minutes = (decDeg - d) * 60;
int m = floor(minutes);
int s = (minutes - m) * 60;
return String(d) + "°" + String(m) + "'" + String(s) + "\"" + compassPoint;
} }
/** /**
@ -496,50 +500,70 @@ static String decDegreesToDMS(double val, char compassPoint)
* it will be displayed for a couple of seconds then scroll over to the rest of the string * it will be displayed for a couple of seconds then scroll over to the rest of the string
* for a couple of seconds. * for a couple of seconds.
*/ */
static String latLongToDMS(double lat, double lon) static struct DMS latLongToDMS(double lat, double lon)
{ {
char latCP; // compass point direction for latitude DMS dms;
char lonCP; // compass point direction for longitude
if (lat < 0) latCP = 'S'; if (lat < 0) dms.latCP = 'S';
else latCP = 'N'; else dms.latCP = 'N';
if (lon < 0) lonCP = 'W'; double latDeg = lat;
else lonCP = 'E';
return decDegreesToDMS(lat, latCP) + " " + decDegreesToDMS(lon, lonCP); if (lat < 0)
} latDeg = latDeg * -1;
static String getGPSCoordinateString(const GPSStatus *gps) dms.latDeg = floor(latDeg);
{ double latMin = (latDeg - dms.latDeg) * 60;
auto gpsFormat = radioConfig.preferences.gps_format; dms.latMin = floor(latMin);
String coordinates = ""; dms.latSec = (latMin - dms.latMin) * 60;
if (gpsFormat == GpsCoordinateFormat_GpsFormatDMS) if (lon < 0) dms.lonCP = 'W';
coordinates = latLongToDMS(gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7); else dms.lonCP = 'E';
else if (gpsFormat == GpsCoordinateFormat_GpsFormatUTM)
coordinates = latLongToUTMStr(gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7);
else if (gpsFormat == GpsCoordinateFormat_GpsFormatMGRS)
coordinates = latLongToMGRSStr(gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7);
else // Defaults to decimal degrees
coordinates = String(gps->getLatitude() * 1e-7, 6) + " " + String(gps->getLongitude() * 1e-7, 6);
return coordinates; double lonDeg = lon;
if (lon < 0)
lonDeg = lonDeg * -1;
dms.lonDeg = floor(lonDeg);
double lonMin = (lonDeg - dms.lonDeg) * 60;
dms.lonMin = floor(lonMin);
dms.lonSec = (lonMin - dms.lonMin) * 60;
return dms;
} }
// Draw GPS status coordinates // Draw GPS status coordinates
static void drawGPScoordinates(OLEDDisplay *display, int16_t x, int16_t y, const GPSStatus *gps) static void drawGPScoordinates(OLEDDisplay *display, int16_t x, int16_t y, const GPSStatus *gps)
{ {
auto gpsFormat = radioConfig.preferences.gps_format;
String displayLine = ""; String displayLine = "";
if (!gps->getIsConnected()) if (!gps->getIsConnected()) {
displayLine = "No GPS Module"; displayLine = "No GPS Module";
else if (!gps->getHasLock()) display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth(displayLine))) / 2, y, displayLine);
} else if (!gps->getHasLock()) {
displayLine = "No GPS Lock"; displayLine = "No GPS Lock";
else display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth(displayLine))) / 2, y, displayLine);
displayLine = getGPSCoordinateString(gps); } else {
char coordinateLine[22];
display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth(displayLine))) / 2, y, displayLine); if (gpsFormat == GpsCoordinateFormat_GpsFormatDMS) {
DMS dms = latLongToDMS(gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7);
sprintf(coordinateLine, "%2i°%2i'%2.0f\"%1c %3i°%2i'%2.0f\"", dms.latDeg, dms.latMin, dms.latSec, dms.latCP,
dms.lonDeg, dms.lonMin, dms.lonSec);
} else if (gpsFormat == GpsCoordinateFormat_GpsFormatUTM) {
UTM utm = latLongToUTM(gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7);
sprintf(coordinateLine, "%2i%1c %6.0f %7.0f", utm.zone, utm.band, utm.easting, utm.northing);
} else if (gpsFormat == GpsCoordinateFormat_GpsFormatMGRS) {
MGRS mgrs = latLongToMGRS(gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7);
sprintf(coordinateLine, "%2i%1c %1c%1c %5i %5i", mgrs.zone, mgrs.band, mgrs.east100k, mgrs.north100k,
mgrs.easting, mgrs.northing);
} else // Defaults to decimal degrees
sprintf(coordinateLine, "%f %f", gps->getLatitude() * 1e-7, gps->getLongitude() * 1e-7);
display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth(coordinateLine))) / 2, y, coordinateLine);
}
} }
/// Ported from my old java code, returns distance in meters along the globe /// Ported from my old java code, returns distance in meters along the globe