2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
31 #define DIGIT_TO_VAL(_x) (_x - '0')
32 uint32_t GPS_coord_to_degrees(const char* coordinateString
)
34 const char *fieldSeparator
, *remainingString
;
35 uint8_t degrees
= 0, minutes
= 0;
36 uint16_t fractionalMinutes
= 0;
39 // scan for decimal point or end of field
40 for (fieldSeparator
= coordinateString
; isdigit((unsigned char)*fieldSeparator
); fieldSeparator
++) {
41 if (fieldSeparator
>= coordinateString
+ 15)
42 return 0; // stop potential fail
44 remainingString
= coordinateString
;
47 while ((fieldSeparator
- remainingString
) > 2) {
50 degrees
+= DIGIT_TO_VAL(*remainingString
++);
53 while (fieldSeparator
> remainingString
) {
56 minutes
+= DIGIT_TO_VAL(*remainingString
++);
58 // convert fractional minutes
59 // expect up to four digits, result is in
60 // ten-thousandths of a minute
61 if (*fieldSeparator
== '.') {
62 remainingString
= fieldSeparator
+ 1;
63 for (digitIndex
= 0; digitIndex
< 4; digitIndex
++) {
64 fractionalMinutes
*= 10;
65 if (isdigit((unsigned char)*remainingString
))
66 fractionalMinutes
+= *remainingString
++ - '0';
69 return degrees
* 10000000UL + (minutes
* 1000000UL + fractionalMinutes
* 100UL) / 6;