2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
25 #include "common/string_light.h"
30 #define DIGIT_TO_VAL(_x) (_x - '0')
31 uint32_t GPS_coord_to_degrees(const char* coordinateString
)
33 const char *fieldSeparator
, *remainingString
;
34 uint8_t degress
= 0, minutes
= 0;
35 uint16_t fractionalMinutes
= 0;
38 // scan for decimal point or end of field
39 for (fieldSeparator
= coordinateString
; sl_isdigit((unsigned char)*fieldSeparator
); fieldSeparator
++) {
40 if (fieldSeparator
>= coordinateString
+ 15)
41 return 0; // stop potential fail
43 remainingString
= coordinateString
;
46 while ((fieldSeparator
- remainingString
) > 2) {
49 degress
+= DIGIT_TO_VAL(*remainingString
++);
52 while (fieldSeparator
> remainingString
) {
55 minutes
+= DIGIT_TO_VAL(*remainingString
++);
57 // convert fractional minutes
58 // expect up to four digits, result is in
59 // ten-thousandths of a minute
60 if (*fieldSeparator
== '.') {
61 remainingString
= fieldSeparator
+ 1;
62 for (digitIndex
= 0; digitIndex
< 4; digitIndex
++) {
63 fractionalMinutes
*= 10;
64 if (sl_isdigit((unsigned char)*remainingString
))
65 fractionalMinutes
+= *remainingString
++ - '0';
68 return degress
* 10000000UL + (minutes
* 1000000UL + fractionalMinutes
* 100UL) / 6;