Update ci.yml
[inav.git] / src / main / common / gps_conversion.c
blob0c0d830c5caed69ebbac1b040d8cbac9d0b5bad2
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <ctype.h>
21 #include <string.h>
23 #include "platform.h"
25 #include "common/string_light.h"
27 #ifdef USE_GPS
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;
36 uint8_t digitIndex;
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;
45 // convert degrees
46 while ((fieldSeparator - remainingString) > 2) {
47 if (degress)
48 degress *= 10;
49 degress += DIGIT_TO_VAL(*remainingString++);
51 // convert minutes
52 while (fieldSeparator > remainingString) {
53 if (minutes)
54 minutes *= 10;
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;
70 #endif