-
Notifications
You must be signed in to change notification settings - Fork 4
/
gps.c
253 lines (214 loc) · 4.56 KB
/
gps.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "gps.h"
#include "math.h"
#include "uart.h"
#include "string.h"
extern volatile float start_latitude;
extern volatile float start_longitude;
extern volatile float current_latitude;
extern volatile float current_longitude;
sint32_t strlength(char str[])
{
sint32_t i;
/* count the string start from element 0 until the element before the NULL terminator */
for(i = 0; str[i] != '\0'; ++i);
return i;
}
char * mystrtok( char * str, const char *comp)
{
static sint32_t pos;
static char *s;
sint32_t i =0, start = pos;
sint32_t j = 0;
// Copying the string for further calls of strtok
if(str!='\0')
s = str;
i = 0;
//While not end of string
while(s[pos] != '\0')
{
j = 0;
//Comparing of one of the delimiter matches the character in the string
while(comp[j] != '\0')
{
//Pos point to the next location in the string that we have to read
if(s[pos] == comp[j])
{
//Replace the delimter by \0 to break the string
s[pos] = '\0';
pos = pos+1;
//Checking for the case where there is no relevant string before the delimeter.
//start specifies the location from where we have to start reading the next character
if(s[start] != '\0')
return (&s[start]);
else
{
// Move to the next string after the delimiter
start = pos;
// Decrementing as it will be incremented at the end of the while loop
pos--;
break;
}
}
j++;
}
pos++;
}//End of Outer while
s[pos] = '\0';
if(s[start] == '\0')
return '\0';
else
return &s[start];
}
sint32_t strcompare( char* str1, char str2)
{ sint32_t i=0;
while(*(str1+i)!='\0' )
{
if(*(str1+i)!=str2){return 0;}
}
return 1;
}
float atof( char *str)
{
sint32_t i,sum,total,index;
float res;
i=0;
while(str[i])
{
if(str[i]=='.')
{
index=i;
i++;
continue;
}
total=str[i]-48;
if(i==0)
{
sum=total;
i++;
continue;
}
sum=sum*10+total;
i++;
}
index=(strlength(str)-1)-index;
res=sum;
for(i=1;i<=index;i++)
{
res=(float)res/10;
}
return res;
}
void strrcpy( char* destination, const char* source)
{
if (destination == '\0')
{
return ;
}
while (*source != '\0')
{
*destination = *source;
destination++;
source++;
}
*destination = '\0';
}
void readGPSModule(volatile float* ftemp,volatile float* stemp)
{
int u = 1;
int x = 0;
int checkNum = 0;
int counterNum = 0;
int start = 0;
char bufferCheck[7] = { 0 };
char inputBuffer[50] = { 0 };
int found;
int k = 0;
int i;
char element;
while (u)
{
if (!x)
{
element = UART1_getByte();
if (element == '$')
{
if (counterNum > 0)
{int count = 0;
inputBuffer[counterNum] = '\0';
found = 0;
while (inputBuffer[count] != '\0')
{
if (inputBuffer[count++] == 'A')
found = 1;
}
if (found)
x = 1;
}
for ( i = 0; i < 7; i++)
{
bufferCheck[i] = '\0';
}
counterNum = 0;
checkNum = 0;
}
if (checkNum < 7)
{
bufferCheck[checkNum++] = element;
}
else if (checkNum >= 7)
{
if (bufferCheck[0] == '$' && bufferCheck[1] == 'G' && bufferCheck[2] == 'P' && bufferCheck[3] == 'G' && bufferCheck[4] == 'L' && bufferCheck[5] == 'L' && bufferCheck[6] == ',')
{
inputBuffer[counterNum++] = element;
}
}
}
else
{
int b = 0;
int i = 0;
int j = 0;
char firstnum[20] = { 0 };
char secondnum[20] = { 0 };
int startfirst = 1;
int startSecond = 0;
while (inputBuffer[b] != '\0')
{
if (startfirst)
{
if (inputBuffer[b] == ',' && inputBuffer[b + 1] == 'N' && inputBuffer[b + 2] == ',')
{
startfirst = 0;
}
else
{
firstnum[i++] = inputBuffer[b];
}
}
if (startSecond)
{
if (inputBuffer[b] == ',' && inputBuffer[b + 1] == 'E' && inputBuffer[b + 2] == ',')
{
startSecond = 0;
}
else
{
secondnum[j++] = inputBuffer[b];
}
}
if (b > 2)
{
if (inputBuffer[b] == ',' && inputBuffer[b - 1] == 'N' && inputBuffer[b - 2] == ',')
{
startSecond = 1;
j = 0;
}
}
b++;
}
*ftemp = atof(firstnum);
*stemp = atof(secondnum);
u = 0;
}
}
}