1
0
Fork 0
mirror of https://github.com/iNavFlight/inav.git synced 2025-07-23 16:25:26 +03:00

Merge pull request #3661 from iNavFlight/agh_fix_coordinate_display

Fix missing leading zeroes in when formatting coordinates
This commit is contained in:
Konstantin Sharlaimov 2018-07-28 09:43:35 +02:00 committed by GitHub
commit cf4048d24e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -486,37 +486,28 @@ static uint16_t osdConvertRSSI(void)
static void osdFormatCoordinate(char *buff, char sym, int32_t val) static void osdFormatCoordinate(char *buff, char sym, int32_t val)
{ {
// This should be faster than pow(10, x). Make it available this
// via extern rather than _GNU_SOURCE, since it should be more
// portable.
extern float exp10f(float);
// up to 4 for number + 1 for the symbol + null terminator + fill the rest with decimals // up to 4 for number + 1 for the symbol + null terminator + fill the rest with decimals
const int coordinateMaxLength = osdConfig()->coordinate_digits + 1; const int coordinateLength = osdConfig()->coordinate_digits + 1;
buff[0] = sym; buff[0] = sym;
int32_t integerPart = val / GPS_DEGREES_DIVIDER; int32_t integerPart = val / GPS_DEGREES_DIVIDER;
// Latitude maximum integer width is 3 (-90) while // Latitude maximum integer width is 3 (-90) while
// longitude maximum integer width is 4 (-180). // longitude maximum integer width is 4 (-180).
int integerDigits = tfp_sprintf(buff + 1, (integerPart == 0 && val < 0) ? "-%d" : "%d", integerPart); int integerDigits = tfp_sprintf(buff + 1, (integerPart == 0 && val < 0) ? "-%d" : "%d", integerPart);
// We can show up to 7 digits in decimalPart. Remove // We can show up to 7 digits in decimalPart.
// some if needed.
int32_t decimalPart = abs(val % GPS_DEGREES_DIVIDER); int32_t decimalPart = abs(val % GPS_DEGREES_DIVIDER);
int trim = 7 - MAX(coordinateMaxLength - 1 - integerDigits, 0); STATIC_ASSERT(GPS_DEGREES_DIVIDER == 1e7, adjust_max_decimal_digits);
if (trim > 0) { int decimalDigits = tfp_sprintf(buff + 1 + integerDigits, "%07d", decimalPart);
decimalPart /= (int32_t)exp10f(trim);
}
int decimalDigits = tfp_sprintf(buff + 1 + integerDigits, "%d", decimalPart);
// Embbed the decimal separator // Embbed the decimal separator
buff[1 + integerDigits - 1] += SYM_ZERO_HALF_TRAILING_DOT - '0'; buff[1 + integerDigits - 1] += SYM_ZERO_HALF_TRAILING_DOT - '0';
buff[1 + integerDigits] += SYM_ZERO_HALF_LEADING_DOT - '0'; buff[1 + integerDigits] += SYM_ZERO_HALF_LEADING_DOT - '0';
// Fill up to coordinateMaxLength with zeros // Fill up to coordinateLength with zeros
int total = 1 + integerDigits + decimalDigits; int total = 1 + integerDigits + decimalDigits;
while(total < coordinateMaxLength) { while(total < coordinateLength) {
buff[total] = '0'; buff[total] = '0';
total++; total++;
} }
buff[coordinateMaxLength] = '\0'; buff[coordinateLength] = '\0';
} }
// Used twice, make sure it's exactly the same string // Used twice, make sure it's exactly the same string