Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1704 - 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
        uint16_t i;
122
        int8_t c;
123
        const prog_char *str;
124
 
125
        if(doc == NULL) return(0);
126
 
127
        KML_DocumentInit(doc);                                                                                                          // intialize the document with resetvalues
128
        doc->file = fopen_(name,'a');                                                                                           // open a new file with the specified filename on the memorycard.
129
 
130
        if(doc->file != NULL)                                                                                                           // could the file be opened?
131
        {
132
                retvalue = 1;                                                                                                                   // the document could be created on the drive.
133
                doc->state = KML_DOC_OPENED;                                                                                    // change document state to opened. At next a placemark has to be opened.
134
                str = KML_DOCUMENT_HEADER;                                                                                              // write the KML-header to the document.
135
                for(i= 0; i < sizeof(KML_DOCUMENT_HEADER)-1; i++)
136
                {
137
                        c = (int8_t)pgm_read_byte(str++); // get byte from flash
138
                        fputc_(c, doc->file); // and write that to sd-card
139
                }
140
        }
141
        return(retvalue);
142
}
143
 
144
//________________________________________________________________________________________________________________________________________
145
// Function:    DocumentClose(KML_Document_t *doc);
146
//
147
// Description: This function closes the document specified by doc.
148
//
149
//
150
// Returnvalue: '1' if the KML- file could be closed.
151
//________________________________________________________________________________________________________________________________________
152
 
153
uint8_t KML_DocumentClose(KML_Document_t *doc)
154
{
155
 
156
        uint8_t retvalue = 1;
157
        uint16_t i;
158
        int8_t c;
159
        const prog_char *str;
160
 
161
        if(doc == NULL) return(0);
162
 
163
        while(doc->state != KML_DOC_CLOSED)                                                             // close linestring, placemark and document before closing the file on the memorycard
164
        {
165
                switch(doc->state)
166
                {
167
                        case KML_DOC_LINESTRING_OPENED:
168
                                KML_LineStringEnd(doc);                                                 // write terminating tag to end linestring.
169
                                break;
170
 
171
                        case KML_DOC_PLACEMARK_OPENED:                                                  // write terminating tag to close placemark.
172
                                KML_PlaceMarkClose(doc);
173
                                break;
174
 
175
                        case KML_DOC_OPENED:                                                                    // close the file on the memorycard
176
                                if(doc->file != NULL)
177
                                {
178
                                        str = KML_DOCUMENT_FOOTER;                                              // write the KML- footer to the document.
179
                                        for(i= 0; i < sizeof(KML_DOCUMENT_FOOTER)-1; i++)
180
                                        {
181
                                                c = (int8_t)pgm_read_byte(str++); // get byte from flash
182
                                                fputc_(c, doc->file); // and write that to sd-card
183
                                        }
184
                                        fclose_(doc->file);
185
                                        retvalue = 1;
186
                                }
187
                                doc->state = KML_DOC_CLOSED;
188
                                break;
189
 
190
                        default:
191
                                doc->state = KML_DOC_CLOSED;
192
                                break;
193
 
194
                }
195
        }
196
        return(retvalue);
197
}
198
 
199
//________________________________________________________________________________________________________________________________________
200
// Function:    u8 KML_PlaceMarkOpen(KML_Document_t *doc);
201
//
202
// Description: This function adds a placemark to the document.
203
//
204
//
205
// Returnvalue: '1' if the PlaceMark could be opened
206
//________________________________________________________________________________________________________________________________________
207
 
208
uint8_t KML_PlaceMarkOpen(KML_Document_t *doc)
209
{
210
        uint8_t retvalue = 0;
211
        uint16_t i;
212
        int8_t c;
213
        const prog_char *str;
214
 
215
        if(doc->state == KML_DOC_OPENED)
216
        {
217
                if(doc->file != NULL)
218
                {
219
                        doc->state = KML_DOC_PLACEMARK_OPENED;
220
                        retvalue = 1;
221
                        str = KML_PLACEMARK_HEADER;
222
                        for(i= 0; i < sizeof(KML_PLACEMARK_HEADER)-1; i++)
223
                        {
224
                                c = (int8_t)pgm_read_byte(str++); // get byte from flash
225
                                fputc_(c, doc->file); // and write that to sd-card
226
                        }
227
                }
228
        }
229
        return(retvalue);
230
}
231
 
232
//________________________________________________________________________________________________________________________________________
233
// Function:    u8 PlaceMarkClose(KML_PlaceMark_t *place, File *file);
234
//
235
// Description: This function ends the placemark opened before.
236
//
237
//
238
// Returnvalue: 1' if the PlaceMark could be closed
239
//________________________________________________________________________________________________________________________________________
240
 
241
uint8_t KML_PlaceMarkClose(KML_Document_t *doc)
242
{
243
 
244
        uint8_t retvalue = 0;
245
        uint16_t i;
246
        int8_t c;
247
        const prog_char *str;
248
 
249
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
250
        {
251
                if(doc->file != NULL)
252
                {
253
                        doc->state = KML_DOC_OPENED;
254
                        str = KML_PLACEMARK_FOOTER;
255
                        for(i= 0; i < sizeof(KML_PLACEMARK_FOOTER)-1; i++)
256
                        {
257
                                c = (int8_t)pgm_read_byte(str++); // get byte from flash
258
                                fputc_(c, doc->file); // and write that to sd-card
259
                        }
260
                        retvalue = 1;
261
                }
262
        }
263
        return(retvalue);
264
}
265
 
266
//________________________________________________________________________________________________________________________________________
267
// Function:    u8 LineStringBegin(KML_Document_t *doc);
268
//
269
// Description: This function ends the placemark opened before.
270
//
271
//
272
// Returnvalue: '1' if the LineString could be started
273
//________________________________________________________________________________________________________________________________________
274
 
275
uint8_t KML_LineStringBegin(KML_Document_t *doc)
276
{
277
 
278
        uint8_t retvalue = 0;
279
        uint16_t i;
280
        int8_t c;
281
        const prog_char *str;
282
 
283
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
284
        {
285
                if(doc->file != NULL)
286
                {
287
                        doc->state = KML_DOC_LINESTRING_OPENED;
288
                        str = KML_LINESTRING_HEADER;
289
                        for(i= 0; i < sizeof(KML_LINESTRING_HEADER)-1; i++)
290
                        {
291
                                c = (int8_t)pgm_read_byte(str++); // get byte from flash
292
                                fputc_(c, doc->file); // and write that to sd-card
293
                        }
294
                        retvalue = 1;
295
                }
296
        }
297
        return(retvalue);
298
}
299
 
300
//________________________________________________________________________________________________________________________________________
301
// Function:    u8 KML_LineStringEnd(KML_Document_t *doc)
302
//
303
// Description: This function ends the placemark opened before.
304
//
305
//
306
// Returnvalue: '1' if the LineString could be terminated
307
//________________________________________________________________________________________________________________________________________
308
 
309
uint8_t KML_LineStringEnd(KML_Document_t *doc)
310
{
311
 
312
        uint8_t retvalue = 0;
313
        uint16_t i;
314
        int8_t c;
315
        const prog_char *str;
316
 
317
        if(doc->state == KML_DOC_LINESTRING_OPENED)
318
        {
319
                if(doc->file != NULL)
320
                {
321
                        doc->state = KML_DOC_PLACEMARK_OPENED;
322
                        str = KML_LINESTRING_FOOTER;
323
                        for(i= 0; i < sizeof(KML_LINESTRING_FOOTER)-1; i++)
324
                        {
325
                                c = (int8_t)pgm_read_byte(str++); // get byte from flash
326
                                fputc_(c, doc->file); // and write that to sd-card
327
                        }
328
                        retvalue = 1;
329
                }
330
        }
331
        return(retvalue);
332
}
333
 
334
//________________________________________________________________________________________________________________________________________
335
// Function:    u8 LineStringAddPoint(gps_data_t, KML_Document_t *doc)
336
//
337
// Description: This function adds a point to the specified document.
338
//
339
//
340
// Returnvalue: '1' if a ppoint was added could be started
341
//________________________________________________________________________________________________________________________________________
342
 
343
uint8_t KML_LineStringAddPoint(KML_Document_t *doc)
344
{
345
 
346
        uint8_t retvalue = 0;
347
        int8_t string[50];
348
 
349
        if(doc == NULL) return(0);
350
 
351
        if(GPSData.Position.Status != INVALID)
352
        {
353
                if(doc->state == KML_DOC_LINESTRING_OPENED)
354
                {
355
                        if(doc->file != NULL)
356
                        {
357
                                int16_t i1, i2, i3;
358
                                uint8_t sign;
359
                                if(GPSData.Position.Longitude < 0) sign = '-';
360
                                else sign = '+';
361
                                i1 = abs((int16_t)(GPSData.Position.Longitude/10000000L));
362
                                i2 = abs((int16_t)((GPSData.Position.Longitude%10000000L)/10000L));
363
                                i3 = abs((int16_t)(((GPSData.Position.Longitude%10000000L)%10000L)/10L));
364
                                sprintf(string,"\r\n%c%d.%.3d%.3d,",sign, i1, i2, i3);
365
                                fputs_(string, doc->file);
366
                                if(GPSData.Position.Latitude < 0) sign = '-';
367
                                else sign = '+';
368
                                i1 = abs((int16_t)(GPSData.Position.Latitude/10000000L));
369
                                i2 = abs((int16_t)((GPSData.Position.Latitude%10000000L)/10000L));
370
                                i3 = abs((int16_t)(((GPSData.Position.Latitude%10000000L)%10000L)/10L));
371
                                sprintf(string,"%c%d.%.3d%.3d,",sign, i1, i2, i3);
372
                                fputs_(string, doc->file);
373
                                sign = '+';
374
                                sprintf(string,"%c%d.%.3d",sign, 0, 0);
375
                                fputs_(string, doc->file);
376
                                retvalue = 1;
377
                        }
378
                }
379
        }
380
        return(retvalue);
381
}
382
 
383
//________________________________________________________________________________________________________________________________________
384
// Function:    u8 KML_LoggGPSCoordinates(gps_data_t *, KML_Document_t *)
385
//
386
// Description: This function opens adds gpscoordinates to an KML-Document. The document will be opened, if not already done
387
//
388
//
389
// Returnvalue: '1' if an gps coordinate was logged
390
//________________________________________________________________________________________________________________________________________
391
 
392
uint8_t KML_LoggGPSCoordinates(KML_Document_t *doc)
393
{
394
        uint8_t retval = 0;
395
        while(doc->state != KML_DOC_LINESTRING_OPENED)                                  // automatic create document with default filename on the card.
396
        {
397
                switch(doc->state)
398
                {
399
                        case KML_DOC_CLOSED:                                                                    // document hasn't been opened yet therefore it will be initialized automatically
400
                                retval = KML_DocumentOpen("default.kml",doc);           // open the kml-document with a standardname.
401
                        break;
402
 
403
                        case KML_DOC_OPENED:                                                                    // if a document has been opened before but no placemark exists:
404
                                retval = KML_PlaceMarkOpen(doc);
405
                        break;
406
 
407
                        case KML_DOC_PLACEMARK_OPENED:                                                  // add linestring to the placemark
408
                                retval = KML_LineStringBegin(doc);
409
                        break;
410
 
411
                        default:
412
                                retval = 0;
413
                        break;
414
 
415
                }
416
                if(retval != 1) return(retval); // stop on error
417
        }
418
 
419
        if(doc->state == KML_DOC_LINESTRING_OPENED)                                             // if the document was opened add coordinates to the document.
420
        {
421
                retval = KML_LineStringAddPoint(doc);
422
        }
423
        return(retval);
424
}
425