Rev 434 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
426 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
||
6 | // + Nur für den privaten Gebrauch |
||
7 | // + FOR NON COMMERCIAL USE ONLY |
||
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
15 | // + Verkauf von Luftbildaufnahmen, usw. |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
22 | // + eindeutig als Ursprung verlinkt werden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
25 | // + Benutzung auf eigene Gefahr |
||
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
29 | // + mit unserer Zustimmung zulässig |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
34 | // + this list of conditions and the following disclaimer. |
||
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
36 | // + from this software without specific prior written permission. |
||
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
||
38 | // + for non-commercial use (directly or indirectly) |
||
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
40 | // + with our written permission |
||
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
42 | // + clearly linked as origin |
||
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
||
44 | // |
||
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
55 | // + POSSIBILITY OF SUCH DAMAGE. |
||
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
57 | #include <stdlib.h> |
||
58 | #include <stdio.h> |
||
59 | #include "kml.h" |
||
60 | #include "kml_header.h" |
||
61 | |||
62 | |||
63 | //________________________________________________________________________________________________________________________________________ |
||
64 | // Module name: kml.c |
||
65 | // Compiler used: avr-gcc 3.4.5 |
||
66 | // Last Modifikation: 22.03.2009 |
||
67 | // Version: 1.03 |
||
68 | // Authors: Stephan Busker, Gregor Stobrawa |
||
69 | // Description: Source files to write gps-coordinates to a file in the kml (keyhole markup language) fileformat |
||
70 | // Copyright (C) 2007 Stephan Busker |
||
71 | //........................................................................................................................................ |
||
72 | // Functions: extern u8 KML_LoggGPSCoordinates(struct str_gps_nav_data , KML_Document_t *); // intializes the kml-document with standard filename and adds points to the file |
||
73 | // extern u8 KML_DocumentInit(KML_Document_t *doc) // initializes the kml-document to resetvalues. |
||
74 | // extern u8 KML_DocumentOpen(s8 *, KML_Document_t *); // opens a new kml document. A filename can be specified. |
||
75 | // extern u8 KML_DocumentClose(KML_Document_t *doc); // closes an open document |
||
76 | // extern u8 KML_PlaceMarkOpen(KML_Document_t *); // opens a new placemark within the specified document |
||
77 | // extern u8 KML_PlaceMarkClose(KML_Document_t *); // Closes the placemark |
||
78 | // extern u8 KML_LineStringBegin(KML_Document_t *); // begins a new line within the actual placemark |
||
79 | // extern u8 KML_LineStringEnd(KML_Document_t *doc); // ends the actual linestring |
||
80 | // extern u8 KML_LineStringAddPoint(struct str_gps_nav_data, KML_Document_t *); // adds a new point (gps-coordinates) to the actual linestring |
||
81 | //........................................................................................................................................ |
||
82 | // ext. functions: |
||
83 | // |
||
84 | //........................................................................................................................................ |
||
85 | // |
||
86 | // URL: www.Mikro-Control.de |
||
87 | // mailto: stephan.busker@mikro-control.de |
||
88 | //________________________________________________________________________________________________________________________________________ |
||
89 | |||
90 | |||
91 | |||
92 | //________________________________________________________________________________________________________________________________________ |
||
93 | // Function: KML_DocumentInit(KML_Document_t *) |
||
94 | // |
||
95 | // Description: This function initializes the kml-document for further use. |
||
96 | // |
||
97 | // |
||
98 | // Returnvalue: '1' if document was initialized |
||
99 | //________________________________________________________________________________________________________________________________________ |
||
100 | |||
101 | uint8_t KML_DocumentInit(KML_Document_t *doc) |
||
102 | { |
||
103 | doc->state = KML_DOC_CLOSED; // state of the kml-document |
||
104 | doc->file = NULL; |
||
105 | return(1); |
||
106 | } |
||
107 | |||
108 | //________________________________________________________________________________________________________________________________________ |
||
109 | // Function: KML_Document_Open(void); |
||
110 | // |
||
111 | // Description: This function opens a new KML- document with the specified name and creates the document header within the file. |
||
112 | // |
||
113 | // |
||
114 | // Returnvalue: '1' if the KML- file could be created. |
||
115 | //________________________________________________________________________________________________________________________________________ |
||
116 | |||
117 | uint8_t KML_DocumentOpen(int8_t *name, KML_Document_t *doc) |
||
118 | { |
||
119 | |||
120 | uint8_t retvalue = 0; |
||
121 | |||
122 | if(doc == NULL) return(0); |
||
123 | |||
124 | KML_DocumentInit(doc); // intialize the document with resetvalues |
||
125 | doc->file = fopen_(name,'a'); // open a new file with the specified filename on the memorycard. |
||
126 | |||
127 | if(doc->file != NULL) // could the file be opened? |
||
128 | { |
||
129 | retvalue = 1; // the document could be created on the drive. |
||
130 | doc->state = KML_DOC_OPENED; // change document state to opened. At next a placemark has to be opened. |
||
131 | fwrite_((void*)KML_DOCUMENT_HEADER, sizeof(KML_DOCUMENT_HEADER)-1,1,doc->file);// write the KML-header to the document. |
||
132 | } |
||
133 | |||
134 | return(retvalue); |
||
135 | } |
||
136 | |||
137 | //________________________________________________________________________________________________________________________________________ |
||
138 | // Function: DocumentClose(KML_Document_t *doc); |
||
139 | // |
||
140 | // Description: This function closes the document specified by doc. |
||
141 | // |
||
142 | // |
||
143 | // Returnvalue: '1' if the KML- file could be closed. |
||
144 | //________________________________________________________________________________________________________________________________________ |
||
145 | |||
146 | uint8_t KML_DocumentClose(KML_Document_t *doc) |
||
147 | { |
||
148 | |||
149 | uint8_t retvalue = 1; |
||
150 | |||
151 | if(doc == NULL) return(0); |
||
152 | |||
153 | while(doc->state != KML_DOC_CLOSED) // close linestring, placemark and document before closing the file on the memorycard |
||
154 | { |
||
155 | switch(doc->state) |
||
156 | { |
||
157 | case KML_DOC_LINESTRING_OPENED: |
||
158 | KML_LineStringEnd(doc); // write terminating tag to end linestring. |
||
159 | break; |
||
160 | |||
161 | case KML_DOC_PLACEMARK_OPENED: // write terminating tag to close placemark. |
||
162 | KML_PlaceMarkClose(doc); |
||
163 | break; |
||
164 | |||
165 | case KML_DOC_OPENED: // close the file on the memorycard |
||
166 | if(doc->file != NULL) |
||
167 | { |
||
168 | fwrite_((void*)KML_DOCUMENT_FOOTER, sizeof(KML_DOCUMENT_FOOTER)-1,1,doc->file); // write the KML- footer to the document. |
||
169 | fclose_(doc->file); |
||
170 | retvalue = 1; |
||
171 | } |
||
172 | doc->state = KML_DOC_CLOSED; |
||
173 | break; |
||
174 | |||
175 | default: |
||
176 | doc->state = KML_DOC_CLOSED; |
||
177 | break; |
||
178 | |||
179 | } |
||
180 | } |
||
181 | return(retvalue); |
||
182 | } |
||
183 | |||
184 | //________________________________________________________________________________________________________________________________________ |
||
185 | // Function: u8 KML_PlaceMarkOpen(KML_Document_t *doc); |
||
186 | // |
||
187 | // Description: This function adds a placemark to the document. |
||
188 | // |
||
189 | // |
||
190 | // Returnvalue: '1' if the PlaceMark could be opened |
||
191 | //________________________________________________________________________________________________________________________________________ |
||
192 | |||
193 | uint8_t KML_PlaceMarkOpen(KML_Document_t *doc) |
||
194 | { |
||
195 | uint8_t retvalue = 0; |
||
196 | if(doc->state == KML_DOC_OPENED) |
||
197 | { |
||
198 | if(doc->file != NULL) |
||
199 | { |
||
200 | doc->state = KML_DOC_PLACEMARK_OPENED; |
||
201 | retvalue = 1; |
||
202 | fwrite_((void*)KML_PLACEMARK_HEADER, sizeof(KML_PLACEMARK_HEADER)-1,1,doc->file); |
||
203 | } |
||
204 | } |
||
205 | return(retvalue); |
||
206 | } |
||
207 | |||
208 | //________________________________________________________________________________________________________________________________________ |
||
209 | // Function: u8 PlaceMarkClose(KML_PlaceMark_t *place, File *file); |
||
210 | // |
||
211 | // Description: This function ends the placemark opened before. |
||
212 | // |
||
213 | // |
||
214 | // Returnvalue: 1' if the PlaceMark could be closed |
||
215 | //________________________________________________________________________________________________________________________________________ |
||
216 | |||
217 | uint8_t KML_PlaceMarkClose(KML_Document_t *doc) |
||
218 | { |
||
219 | |||
220 | uint8_t retvalue = 0; // close the Placemark-tag of the corosponding document. |
||
221 | |||
222 | if(doc->state == KML_DOC_PLACEMARK_OPENED) |
||
223 | { |
||
224 | if(doc->file != NULL) |
||
225 | { |
||
226 | doc->state = KML_DOC_OPENED; |
||
227 | fwrite_((void*)KML_PLACEMARK_FOOTER, sizeof(KML_PLACEMARK_FOOTER)-1,1,doc->file); |
||
228 | retvalue = 1; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return(retvalue); |
||
233 | } |
||
234 | |||
235 | //________________________________________________________________________________________________________________________________________ |
||
236 | // Function: u8 LineStringBegin(KML_Document_t *doc); |
||
237 | // |
||
238 | // Description: This function ends the placemark opened before. |
||
239 | // |
||
240 | // |
||
241 | // Returnvalue: '1' if the LineString could be started |
||
242 | //________________________________________________________________________________________________________________________________________ |
||
243 | |||
244 | uint8_t KML_LineStringBegin(KML_Document_t *doc) |
||
245 | { |
||
246 | |||
247 | uint8_t retvalue = 0; |
||
248 | |||
249 | if(doc->state == KML_DOC_PLACEMARK_OPENED) |
||
250 | { |
||
251 | if(doc->file != NULL) |
||
252 | { |
||
253 | doc->state = KML_DOC_LINESTRING_OPENED; |
||
254 | fwrite_((void*)KML_LINESTRING_HEADER, sizeof(KML_LINESTRING_HEADER)-1,1,doc->file); |
||
255 | retvalue = 1; |
||
256 | } |
||
257 | } |
||
258 | return(retvalue); |
||
259 | } |
||
260 | |||
261 | //________________________________________________________________________________________________________________________________________ |
||
262 | // Function: u8 KML_LineStringEnd(KML_Document_t *doc) |
||
263 | // |
||
264 | // Description: This function ends the placemark opened before. |
||
265 | // |
||
266 | // |
||
267 | // Returnvalue: '1' if the LineString could be terminated |
||
268 | //________________________________________________________________________________________________________________________________________ |
||
269 | |||
270 | uint8_t KML_LineStringEnd(KML_Document_t *doc) |
||
271 | { |
||
272 | |||
273 | uint8_t retvalue = 0; |
||
274 | |||
275 | if(doc->state == KML_DOC_LINESTRING_OPENED) |
||
276 | { |
||
277 | if(doc->file != NULL) |
||
278 | { |
||
279 | doc->state = KML_DOC_PLACEMARK_OPENED; |
||
280 | fwrite_((void*)KML_LINESTRING_FOOTER, sizeof(KML_LINESTRING_FOOTER)-1,1,doc->file); |
||
281 | retvalue = 1; |
||
282 | } |
||
283 | } |
||
284 | return(retvalue); |
||
285 | } |
||
286 | |||
287 | //________________________________________________________________________________________________________________________________________ |
||
288 | // Function: u8 LineStringAddPoint(gps_data_t, KML_Document_t *doc) |
||
289 | // |
||
290 | // Description: This function adds a point to the specified document. |
||
291 | // |
||
292 | // |
||
293 | // Returnvalue: '1' if a ppoint was added could be started |
||
294 | //________________________________________________________________________________________________________________________________________ |
||
295 | |||
296 | uint8_t KML_LineStringAddPoint(KML_Document_t *doc) |
||
297 | { |
||
298 | |||
299 | uint8_t retvalue = 0; |
||
300 | int8_t string[50]; |
||
301 | // int32_t rel_altitude = 0; |
||
302 | |||
303 | if(doc == NULL) return(0); |
||
304 | |||
305 | if(GPSData.Position.Status != INVALID) |
||
306 | { |
||
307 | if(doc->state == KML_DOC_LINESTRING_OPENED) |
||
308 | { |
||
309 | if(doc->file != NULL) |
||
310 | { |
||
311 | int16_t i1, i2, i3; |
||
312 | uint8_t sign; |
||
313 | if(GPSData.Position.Longitude < 0) sign = '-'; |
||
314 | else sign = '+'; |
||
315 | i1 = abs((int16_t)(GPSData.Position.Longitude/10000000L)); |
||
316 | i2 = abs((int16_t)((GPSData.Position.Longitude%10000000L)/10000L)); |
||
317 | i3 = abs((int16_t)(((GPSData.Position.Longitude%10000000L)%10000L)/10L)); |
||
318 | sprintf(string,"\r\n%c%d.%.3d%.3d,",sign, i1, i2, i3); |
||
319 | fputs_(string, doc->file); |
||
320 | if(GPSData.Position.Latitude < 0) sign = '-'; |
||
321 | else sign = '+'; |
||
322 | i1 = abs((int16_t)(GPSData.Position.Latitude/10000000L)); |
||
323 | i2 = abs((int16_t)((GPSData.Position.Latitude%10000000L)/10000L)); |
||
324 | i3 = abs((int16_t)(((GPSData.Position.Latitude%10000000L)%10000L)/10L)); |
||
325 | sprintf(string,"%c%d.%.3d%.3d,",sign, i1, i2, i3); |
||
326 | fputs_(string, doc->file); |
||
327 | sign = '+'; |
||
328 | sprintf(string,"%c%d.%.3d",sign, 0, 0); |
||
329 | fputs_(string, doc->file); |
||
330 | retvalue = 1; |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | return(retvalue); |
||
336 | } |
||
337 | |||
338 | //________________________________________________________________________________________________________________________________________ |
||
339 | // Function: u8 KML_LoggGPSCoordinates(gps_data_t *, KML_Document_t *) |
||
340 | // |
||
341 | // Description: This function opens adds gpscoordinates to an KML-Document. The document will be opened, if not already done |
||
342 | // |
||
343 | // |
||
344 | // Returnvalue: '1' if an gps coordinate was logged |
||
345 | //________________________________________________________________________________________________________________________________________ |
||
346 | |||
347 | uint8_t KML_LoggGPSCoordinates(KML_Document_t *doc) |
||
348 | { |
||
349 | uint8_t retval = 0; |
||
350 | while(doc->state != KML_DOC_LINESTRING_OPENED) // automatic create document with default filename on the card. |
||
351 | { |
||
352 | switch(doc->state) |
||
353 | { |
||
354 | case KML_DOC_CLOSED: // document hasn't been opened yet therefore it will be initialized automatically |
||
355 | retval = KML_DocumentOpen("default.kml",doc); // open the kml-document with a standardname. |
||
356 | break; |
||
357 | |||
358 | case KML_DOC_OPENED: // if a document has been opened before but no placemark exists: |
||
359 | retval = KML_PlaceMarkOpen(doc); |
||
360 | break; |
||
361 | |||
362 | case KML_DOC_PLACEMARK_OPENED: // add linestring to the placemark |
||
363 | retval = KML_LineStringBegin(doc); |
||
364 | break; |
||
365 | |||
366 | default: |
||
367 | retval = 0; |
||
368 | break; |
||
369 | |||
370 | } |
||
371 | if(retval != 1) return(retval); // stop on error |
||
372 | } |
||
373 | |||
374 | if(doc->state == KML_DOC_LINESTRING_OPENED) // if the document was opened add coordinates to the document. |
||
375 | { |
||
376 | retval = KML_LineStringAddPoint(doc); |
||
377 | } |
||
378 | return(retval); |
||
379 | } |
||
380 |