forked from Openvario/variod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmea_parser.c
executable file
·191 lines (171 loc) · 4.16 KB
/
nmea_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "nmea_parser.h"
#include "def.h"
#include "utils.h"
extern int g_debug;
extern int g_foreground;
extern FILE *fp_console;
void parse_NMEA_sensor(char* message, t_sensor_context* sensors)
{
char *val;
char buffer[2001];
char delimiter[]=",*";
char *ptr=NULL;
// copy string and initialize strtok function
strncpy(buffer, message, strlen(message));
ptr = strtok(buffer, delimiter);
while (ptr != NULL)
{
switch (*ptr)
{
case '$':
// skip start of NMEA sentence
break;
case 'E':
// TE vario value
// get next value
ptr = strtok(NULL, delimiter);
val = (char *) malloc(strlen(ptr));
strncpy(val,ptr,strlen(ptr));
sensors->e = atof(val);
break;
case 'Q':
// Airspeed value
// get next value
ptr = strtok(NULL, delimiter);
val = (char *) malloc(strlen(ptr));
strncpy(val,ptr,strlen(ptr));
sensors->q = atof(val);
break;
default:
break;
}
// get next part of string
ptr = strtok(NULL, delimiter);
}
}
void parse_NMEA_command(char* message){
char *val;
char buffer[100];
char delimiter[]=",*";
char *ptr;
t_polar polar;
static float fvals[NUM_FV];
// copy string and initialize strtok function
strncpy(buffer, message, strlen(message));
ptr = strtok(buffer, delimiter);
while (ptr != NULL)
{
switch (*ptr)
{
case '$':
// skip start of NMEA sentence
break;
case 'C':
// Command sentence
// get next value
ptr = strtok(NULL, delimiter);
switch (*ptr)
{
case 'M':
if (*(ptr+1) == 'C') {
//Set McCready
//printf("Set McCready\n");
ptr = strtok(NULL, delimiter);
val = (char *) malloc(strlen(ptr));
strncpy(val,ptr,strlen(ptr));
setMC(atof(val));
debug_print("Get McCready Value: %f\n",atof(val));
}
break;
case 'W':
if (*(ptr+1) == 'L') {
//Set Wingload/Ballast
ptr = strtok(NULL, delimiter);
val = (char *) malloc(strlen(ptr));
strncpy(val,ptr,strlen(ptr));
setBallast(atof(val));
debug_print("Get Ballast Value: %f\n",atof(val));
}
break;
case 'B':
if (*(ptr+1) == 'U') {
//Set Bugs
ptr = strtok(NULL, delimiter);
val = (char *) malloc(strlen(ptr));
strncpy(val,ptr,strlen(ptr));
setDegradation(atof(val));
debug_print("Get Bugs Value: %f\n",atof(val));
}
break;
case 'P':
//Set Polar
// depreciated
if (*(ptr+1) == 'O' && *(ptr+2) == 'L') {
ptr = strtok(NULL, delimiter);
val = (char *) malloc(strlen(ptr));
strncpy(val,ptr,strlen(ptr));
polar.a=atof(val);
ptr = strtok(NULL, delimiter);
strncpy(val,ptr,strlen(ptr));
polar.b=atof(val);
ptr = strtok(NULL, delimiter);
strncpy(val,ptr,strlen(ptr));
polar.c=atof(val);
ptr = strtok(NULL, delimiter);
strncpy(val,ptr,strlen(ptr));
polar.w=atof(val);
setPolar(polar.a, polar.b, polar.c, polar.w);
}
break;
case 'R':
//Set Real Polar
if (*(ptr+1) == 'P' && *(ptr+2) == 'O') {
if (read_float_from_sentence(3,fvals,NULL,delimiter)) {
// convert horizontal speed from m/s to km/h
polar.a=fvals[0]/(3.6*3.6);
polar.b=fvals[1]/3.6;
polar.c=fvals[2];
setRealPolar(polar.a, polar.b, polar.c);
debug_print("Get Polar RPO: %f, %f, %f\n",
polar.a,polar.b,polar.c);
}
}
break;
case 'S':
if (*(ptr+1) == 'T' && *(ptr+2) == 'F') {
// Set STF Mode
debug_print("Set STF Mode\n");
set_vario_mode(stf);
}
break;
case 'V':
if (*(ptr+1) == 'U') {
// volume up
debug_print("Volume up\n");
change_volume(+10.0);
}
if (*(ptr+1) == 'D') {
// volume down
debug_print("Volume down\n");
change_volume(-10.0);
}
if (*(ptr+1) == 'M') {
// Toggle Mute
debug_print("Toggle Mute\n");
toggle_mute();
}
if (*(ptr+1) == 'A' && *(ptr+2) == 'R') {
// Set Vario Mode
debug_print("Set Vario Mode\n");
set_vario_mode(vario);
}
break;
}
break;
default:
break;
}
// get next part of string
ptr = strtok(NULL, delimiter);
}
}