mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-16 12:55:19 +03:00
Adding unit test for some gps conversion code to demystify it.
This commit is contained in:
parent
b81f73cb5d
commit
b0e1c934d4
6 changed files with 112 additions and 43 deletions
|
@ -25,6 +25,7 @@
|
|||
#include "config.h"
|
||||
#include "runtime_config.h"
|
||||
|
||||
#include "gps_conversion.h"
|
||||
#include "gps_common.h"
|
||||
|
||||
extern int16_t debug[4];
|
||||
|
@ -925,47 +926,6 @@ static uint32_t GPS_coord_to_degrees(char *s)
|
|||
}
|
||||
*/
|
||||
|
||||
#define DIGIT_TO_VAL(_x) (_x - '0')
|
||||
uint32_t GPS_coord_to_degrees(char* s)
|
||||
{
|
||||
char *p, *q;
|
||||
uint8_t deg = 0, min = 0;
|
||||
unsigned int frac_min = 0;
|
||||
int i;
|
||||
|
||||
// scan for decimal point or end of field
|
||||
for (p = s; isdigit((unsigned char)*p); p++) {
|
||||
if (p >= s + 15)
|
||||
return 0; // stop potential fail
|
||||
}
|
||||
q = s;
|
||||
|
||||
// convert degrees
|
||||
while ((p - q) > 2) {
|
||||
if (deg)
|
||||
deg *= 10;
|
||||
deg += DIGIT_TO_VAL(*q++);
|
||||
}
|
||||
// convert minutes
|
||||
while (p > q) {
|
||||
if (min)
|
||||
min *= 10;
|
||||
min += DIGIT_TO_VAL(*q++);
|
||||
}
|
||||
// convert fractional minutes
|
||||
// expect up to four digits, result is in
|
||||
// ten-thousandths of a minute
|
||||
if (*p == '.') {
|
||||
q = p + 1;
|
||||
for (i = 0; i < 4; i++) {
|
||||
frac_min *= 10;
|
||||
if (isdigit((unsigned char)*q))
|
||||
frac_min += *q++ - '0';
|
||||
}
|
||||
}
|
||||
return deg * 10000000UL + (min * 1000000UL + frac_min * 100UL) / 6;
|
||||
}
|
||||
|
||||
// helper functions
|
||||
static uint32_t grab_fields(char *src, uint8_t mult)
|
||||
{ // convert string to uint32
|
||||
|
|
46
src/gps_conversion.c
Normal file
46
src/gps_conversion.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define DIGIT_TO_VAL(_x) (_x - '0')
|
||||
uint32_t GPS_coord_to_degrees(char* coordinateString)
|
||||
{
|
||||
char *fieldSeparator, *remainingString;
|
||||
uint8_t degress = 0, minutes = 0;
|
||||
uint16_t fractionalMinutes = 0;
|
||||
uint8_t digitIndex;
|
||||
|
||||
// scan for decimal point or end of field
|
||||
for (fieldSeparator = coordinateString; isdigit((unsigned char)*fieldSeparator); fieldSeparator++) {
|
||||
if (fieldSeparator >= coordinateString + 15)
|
||||
return 0; // stop potential fail
|
||||
}
|
||||
remainingString = coordinateString;
|
||||
|
||||
// convert degrees
|
||||
while ((fieldSeparator - remainingString) > 2) {
|
||||
if (degress)
|
||||
degress *= 10;
|
||||
degress += DIGIT_TO_VAL(*remainingString++);
|
||||
}
|
||||
// convert minutes
|
||||
while (fieldSeparator > remainingString) {
|
||||
if (minutes)
|
||||
minutes *= 10;
|
||||
minutes += DIGIT_TO_VAL(*remainingString++);
|
||||
}
|
||||
// convert fractional minutes
|
||||
// expect up to four digits, result is in
|
||||
// ten-thousandths of a minute
|
||||
if (*fieldSeparator == '.') {
|
||||
remainingString = fieldSeparator + 1;
|
||||
for (digitIndex = 0; digitIndex < 4; digitIndex++) {
|
||||
fractionalMinutes *= 10;
|
||||
if (isdigit((unsigned char)*remainingString))
|
||||
fractionalMinutes += *remainingString++ - '0';
|
||||
}
|
||||
}
|
||||
return degress * 10000000UL + (minutes * 1000000UL + fractionalMinutes * 100UL) / 6;
|
||||
}
|
1
src/gps_conversion.h
Normal file
1
src/gps_conversion.h
Normal file
|
@ -0,0 +1 @@
|
|||
uint32_t GPS_coord_to_degrees(char* s);
|
Loading…
Add table
Add a link
Reference in a new issue