Subversion Repositories NaviCtrl

Rev

Rev 53 | Rev 171 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 53 Rev 85
Line 62... Line 62...
62
 
62
 
63
 
63
 
64
//________________________________________________________________________________________________________________________________________
64
//________________________________________________________________________________________________________________________________________
65
// Module name:                 kml.c
65
// Module name:                 kml.c
66
// Compiler used:               avr-gcc 3.4.5
66
// Compiler used:               avr-gcc 3.4.5
67
// Last Modifikation:   20.07.2008
67
// Last Modifikation:   22.03.2009
68
// Version:                             1.02
68
// Version:                             1.03
69
// Authors:                             Stephan Busker
69
// Authors:                             Stephan Busker, Gregor Stobrawa
70
// Description:                 Source files to write gps-coordinates to a file in the kml (keyhole markup language) fileformat
70
// Description:                 Source files to write gps-coordinates to a file in the kml (keyhole markup language) fileformat
71
//                                              Copyright (C) 2007 Stephan Busker
71
//                                              Copyright (C) 2007 Stephan Busker
72
//........................................................................................................................................
72
//........................................................................................................................................
73
// 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
// 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
74
//                                              extern u8       KML_DocumentInit(KML_Document_t *doc)                                                           // initializes the kml-document to resetvalues.
74
//                                              extern u8       KML_DocumentInit(KML_Document_t *doc)                                                           // initializes the kml-document to resetvalues.
75
//                                              extern u8       KML_DocumentOpen(s8 *, KML_Document_t *);                                                       // opens a new kml document. A filename can be specified.
75
//                                              extern u8       KML_DocumentOpen(s8 *, KML_Document_t *);                                                       // opens a new kml document. A filename can be specified.
76
//                                              extern u8       KML_DocumentClose(KML_Document_t *doc);                                                         // closes an open document
76
//                                              extern u8       KML_DocumentClose(KML_Document_t *doc);                                                         // closes an open document
77
//                                              extern u8   KML_PlaceMarkOpen(s8 *name, KML_Document_t *);                                              // opens a new placemark within the specified document
77
//                                              extern u8   KML_PlaceMarkOpen(KML_Document_t *);                                                                // opens a new placemark within the specified document
78
//                                              extern u8       KML_PlaceMarkClose( KML_Document_t *);                                                          // Closes the placemark
78
//                                              extern u8       KML_PlaceMarkClose(KML_Document_t *);                                                           // Closes the placemark
79
//                                              extern u8       KML_LineStringBegin(KML_Document_t *);                                                          // begins a new line within the actual placemark
79
//                                              extern u8       KML_LineStringBegin(KML_Document_t *);                                                          // begins a new line within the actual placemark
80
//                                              extern u8       KML_LineStringEnd(KML_Document_t *doc);                                                         // ends the actual linestring
80
//                                              extern u8       KML_LineStringEnd(KML_Document_t *doc);                                                         // ends the actual linestring
81
//                                              extern u8       KML_LineStringAddPoint(struct str_gps_nav_data, KML_Document_t *);      // adds a new point (gps-coordinates) to the actual linestring
81
//                                              extern u8       KML_LineStringAddPoint(struct str_gps_nav_data, KML_Document_t *);      // adds a new point (gps-coordinates) to the actual linestring
Line 98... Line 98...
98
//
98
//
99
// Returnvalue: '1' if document was initialized
99
// Returnvalue: '1' if document was initialized
100
//________________________________________________________________________________________________________________________________________
100
//________________________________________________________________________________________________________________________________________
Line 101... Line 101...
101
 
101
 
102
u8 KML_DocumentInit(KML_Document_t *doc)
102
u8 KML_DocumentInit(KML_Document_t *doc)
103
{
-
 
104
        doc->name[0] = 0;                                                                                                                       // name of the document
103
{                                                                                                              
105
        doc->state       = DOC_CLOSED;                                                                                                  // state of the kml-document
104
        doc->state       = KML_DOC_CLOSED;                                                                                                      // state of the kml-document
106
        doc->file        = NULL;
-
 
107
        doc->place.name[0]=0;
-
 
108
        doc->place.description[0]=0;
105
        doc->file        = NULL;
109
        return(1);
106
        return(1);
Line 110... Line 107...
110
}
107
}
111
 
108
 
Line 129... Line 126...
129
        doc->file = fopen_(name,'a');                                                                                           // open a new file with the specified filename on the memorycard.
126
        doc->file = fopen_(name,'a');                                                                                           // open a new file with the specified filename on the memorycard.
Line 130... Line 127...
130
 
127
 
131
        if(doc->file != NULL)                                                                                                           // could the file be opened?
128
        if(doc->file != NULL)                                                                                                           // could the file be opened?
132
        {
129
        {
133
                retvalue = 1;                                                                                                                   // the document could be created on the drive.
130
                retvalue = 1;                                                                                                                   // the document could be created on the drive.
134
                doc->state = DOC_OPENED;                                                                                                // change document state to opened. At next a placemark has to be opened.
131
                doc->state = KML_DOC_OPENED;                                                                                            // change document state to opened. At next a placemark has to be opened.
135
                fwrite_((void*)KML_DOCUMENT_HEADER, sizeof(KML_DOCUMENT_HEADER)-1,1,doc->file);// write the KML- footer to the document.
132
                fwrite_((void*)KML_DOCUMENT_HEADER, sizeof(KML_DOCUMENT_HEADER)-1,1,doc->file);// write the KML-header to the document.
Line 136... Line 133...
136
        }
133
        }
137
 
134
 
Line 152... Line 149...
152
 
149
 
Line 153... Line 150...
153
        u8 retvalue = 1;
150
        u8 retvalue = 1;
Line 154... Line 151...
154
 
151
 
155
        if(doc == NULL) return(0);
152
        if(doc == NULL) return(0);
156
 
153
 
157
        while(doc->state != DOC_CLOSED)                                                         // close linestring, placemark and document before closing the file on the memorycard
154
        while(doc->state != KML_DOC_CLOSED)                                                             // close linestring, placemark and document before closing the file on the memorycard
158
        {
155
        {
159
                switch(doc->state)
156
                switch(doc->state)
160
                {
-
 
161
                        case DOC_LINESTRING_OPENED:
157
                {
Line 162... Line 158...
162
                                KML_LineStringEnd(doc);                                                 // write terminating tag to end linestring.
158
                        case KML_DOC_LINESTRING_OPENED:
163
                                doc->state = DOC_PLACEMARK_OPENED;
159
                                KML_LineStringEnd(doc);                                                 // write terminating tag to end linestring.
164
                                break;
-
 
165
 
160
                                break;
Line 166... Line 161...
166
                        case DOC_PLACEMARK_OPENED:                                                      // write terminating tag to close placemark.
161
 
167
                                KML_PlaceMarkClose(doc);
162
                        case KML_DOC_PLACEMARK_OPENED:                                                  // write terminating tag to close placemark.
168
                                doc->state = DOC_OPENED;
163
                                KML_PlaceMarkClose(doc);
169
                                break;
164
                                break;
170
 
165
 
171
                        case DOC_OPENED:                                                                        // close the file on the memorycard
166
                        case KML_DOC_OPENED:                                                                    // close the file on the memorycard
172
                                if(doc->file != NULL)
167
                                if(doc->file != NULL)
173
                                {
168
                                {
174
                                        fwrite_((void*)KML_DOCUMENT_FOOTER, sizeof(KML_DOCUMENT_FOOTER)-1,1,doc->file); // write the KML- footer to the document.
169
                                        fwrite_((void*)KML_DOCUMENT_FOOTER, sizeof(KML_DOCUMENT_FOOTER)-1,1,doc->file); // write the KML- footer to the document.
Line 175... Line 170...
175
                                        fclose_(doc->file);
170
                                        fclose_(doc->file);
176
                                        retvalue = 1;
171
                                        retvalue = 1;
177
                                }
172
                                }
Line 178... Line 173...
178
                                doc->state = DOC_CLOSED;
173
                                doc->state = KML_DOC_CLOSED;
179
                                break;
174
                                break;
180
 
175
 
181
                        default:
176
                        default:
Line 182... Line 177...
182
                                doc->state = DOC_CLOSED;
177
                                doc->state = KML_DOC_CLOSED;
183
                                break;
178
                                break;
184
 
179
 
185
                }
180
                }
186
        }
181
        }
187
        return(retvalue);
182
        return(retvalue);
188
}
183
}
189
 
184
 
Line 190... Line 185...
190
//________________________________________________________________________________________________________________________________________
185
//________________________________________________________________________________________________________________________________________
191
// Function:    u8 PlaceMarkOpen(s8 *name, File *file);
186
// Function:    u8 KML_PlaceMarkOpen(KML_Document_t *doc);
192
//
-
 
193
// Description: This function adds a placemark to the document.
187
//
194
//
-
 
195
//
188
// Description: This function adds a placemark to the document.
196
// Returnvalue: '1' if the PlaceMark could be opened
189
//
-
 
190
//
-
 
191
// Returnvalue: '1' if the PlaceMark could be opened
197
//________________________________________________________________________________________________________________________________________
192
//________________________________________________________________________________________________________________________________________
198
 
193
 
199
u8 KML_PlaceMarkOpen(s8 *name, KML_Document_t *doc)
194
u8 KML_PlaceMarkOpen(KML_Document_t *doc)
-
 
195
{
200
{
196
        u8 retvalue = 0;
201
 
-
 
202
        u8 retvalue = 0;
197
        if(doc->state == KML_DOC_OPENED)
203
 
198
        {
Line 204... Line 199...
204
        if(doc->file != NULL)
199
                if(doc->file != NULL)
205
        {
200
                {
Line 223... Line 218...
223
u8 KML_PlaceMarkClose(KML_Document_t *doc)
218
u8 KML_PlaceMarkClose(KML_Document_t *doc)
224
{
219
{
Line 225... Line 220...
225
 
220
 
Line 226... Line 221...
226
        u8 retvalue = 0;                                                                                                                        // close the Placemark-tag of the corosponding document.
221
        u8 retvalue = 0;                                                                                                                        // close the Placemark-tag of the corosponding document.
227
 
222
 
228
        if(doc->state == DOC_PLACEMARK_OPENED)
223
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
229
        {
224
        {
230
                if(doc->file != NULL)
225
                if(doc->file != NULL)
231
                {
226
                {
232
                        doc->state = DOC_OPENED;
227
                        doc->state = KML_DOC_OPENED;
233
                        fwrite_((void*)KML_PLACEMARK_FOOTER, sizeof(KML_PLACEMARK_FOOTER)-1,1,doc->file);
228
                        fwrite_((void*)KML_PLACEMARK_FOOTER, sizeof(KML_PLACEMARK_FOOTER)-1,1,doc->file);
234
                        retvalue = 1;
229
                        retvalue = 1;
Line 250... Line 245...
250
u8 KML_LineStringBegin(KML_Document_t *doc)
245
u8 KML_LineStringBegin(KML_Document_t *doc)
251
{
246
{
Line 252... Line 247...
252
 
247
 
Line 253... Line 248...
253
        u8 retvalue = 0;
248
        u8 retvalue = 0;
254
 
249
 
-
 
250
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
-
 
251
        {
255
        if(doc->file != NULL)
252
                if(doc->file != NULL)
256
        {
253
                {
257
                doc->state = DOC_LINESTRING_OPENED;
254
                        doc->state = KML_DOC_LINESTRING_OPENED;
-
 
255
                        fwrite_((void*)KML_LINESTRING_HEADER, sizeof(KML_LINESTRING_HEADER)-1,1,doc->file);
258
                fwrite_((void*)KML_LINESTRING_HEADER, sizeof(KML_LINESTRING_HEADER)-1,1,doc->file);
256
                        retvalue = 1;
259
                retvalue = 1;
-
 
260
        }
257
                }
261
 
258
        }
Line 262... Line 259...
262
        return(retvalue);
259
        return(retvalue);
263
}
260
}
264
 
261
 
265
//________________________________________________________________________________________________________________________________________
262
//________________________________________________________________________________________________________________________________________
266
// Function:    u8 LineStringEnd(KML_Document_t *doc)
263
// Function:    u8 KML_LineStringEnd(KML_Document_t *doc)
267
//
264
//
268
// Description: This function ends the placemark opened before.
265
// Description: This function ends the placemark opened before.
Line 274... Line 271...
274
u8 KML_LineStringEnd(KML_Document_t *doc)
271
u8 KML_LineStringEnd(KML_Document_t *doc)
275
{
272
{
Line 276... Line 273...
276
 
273
 
Line 277... Line 274...
277
        u8 retvalue = 0;
274
        u8 retvalue = 0;
278
 
-
 
279
        if(doc->state == DOC_LINESTRING_OPENED);
275
 
-
 
276
        if(doc->state == KML_DOC_LINESTRING_OPENED)
-
 
277
        {
280
        if(doc->file != NULL)
278
                if(doc->file != NULL)
281
        {
279
                {
282
                doc->state = DOC_PLACEMARK_OPENED;
280
                        doc->state = KML_DOC_PLACEMARK_OPENED;
-
 
281
                        fwrite_((void*)KML_LINESTRING_FOOTER, sizeof(KML_LINESTRING_FOOTER)-1,1,doc->file);
283
                fwrite_((void*)KML_LINESTRING_FOOTER, sizeof(KML_LINESTRING_FOOTER)-1,1,doc->file);
282
                        retvalue = 1;
284
                retvalue = 1;
-
 
285
        }
283
                }
286
 
284
        }
Line 287... Line 285...
287
        return(retvalue);
285
        return(retvalue);
288
}
286
}
Line 294... Line 292...
294
//
292
//
295
//
293
//
296
// Returnvalue: '1' if a ppoint was added could be started
294
// Returnvalue: '1' if a ppoint was added could be started
297
//________________________________________________________________________________________________________________________________________
295
//________________________________________________________________________________________________________________________________________
Line 298... Line 296...
298
 
296
 
299
u8 KML_LineStringAddPoint(GPS_Pos_t * pGPS_Position ,KML_Document_t *doc)
297
u8 KML_LineStringAddPoint(KML_Document_t *doc)
Line 300... Line 298...
300
{
298
{
301
 
299
 
302
        u8 retvalue = 0;
300
        u8 retvalue = 0;
Line 303... Line 301...
303
        s8 string[50];
301
        s8 string[50];
Line 304... Line 302...
304
        s32 rel_altitude = 0;
302
        s32 rel_altitude = 0;
305
 
303
 
306
        if(doc == NULL || pGPS_Position == NULL) return(0);
304
        if(doc == NULL) return(0);
307
 
305
 
308
        if((pGPS_Position->Status != INVALID) && (GPS_HomePosition.Status != INVALID))
306
        if((GPSData.Position.Status != INVALID) && (GPS_HomePosition.Status != INVALID))
309
        {
307
        {
310
                if(doc->state == DOC_LINESTRING_OPENED)
308
                if(doc->state == KML_DOC_LINESTRING_OPENED)
311
                {
309
                {
312
                        if(doc->file != NULL)
310
                        if(doc->file != NULL)
313
                        {
311
                        {
314
                                s32 i1, i2;
312
                                s32 i1, i2;
315
                                u8 sign;
313
                                u8 sign;
316
                                if(pGPS_Position->Longitude < 0) sign = '-';
314
                                if(GPSData.Position.Longitude < 0) sign = '-';
317
                                else sign = '+';
315
                                else sign = '+';
318
                                i1 = abs(pGPS_Position->Longitude)/10000000L;
316
                                i1 = abs(GPSData.Position.Longitude)/10000000L;
319
                                i2 = abs(pGPS_Position->Longitude)%10000000L;
317
                                i2 = abs(GPSData.Position.Longitude)%10000000L;
320
                                sprintf(string,"\r\n%c%ld.%07ld,",sign, i1, i2);
318
                                sprintf(string,"\r\n%c%ld.%07ld,",sign, i1, i2);
321
                                fputs_(string, doc->file);
319
                                fputs_(string, doc->file);
322
                                if(pGPS_Position->Latitude < 0) sign = '-';
320
                                if(GPSData.Position.Latitude < 0) sign = '-';
323
                                else sign = '+';
321
                                else sign = '+';
324
                                i1 = abs(pGPS_Position->Latitude)/10000000L;
322
                                i1 = abs(GPSData.Position.Latitude)/10000000L;
325
                                i2 = abs(pGPS_Position->Latitude)%10000000L;
323
                                i2 = abs(GPSData.Position.Latitude)%10000000L;
326
                                sprintf(string,"%c%ld.%07ld,",sign, i1, i2);
324
                                sprintf(string,"%c%ld.%07ld,",sign, i1, i2);
327
                                fputs_(string, doc->file);
325
                                fputs_(string, doc->file);
328
                                // calculate relative altitude with respect to the altitude of the home position
326
                                // calculate relative altitude with respect to the altitude of the home position
329
                                rel_altitude = pGPS_Position->Altitude - GPS_HomePosition.Altitude;
327
                                rel_altitude = GPSData.Position.Altitude - GPS_HomePosition.Altitude;
330
                                if(rel_altitude < 0) rel_altitude = 0; // avoid negative altitudes in log
328
                                if(rel_altitude < 0) rel_altitude = 0; // avoid negative altitudes in log
Line 347... Line 345...
347
//
345
//
348
//
346
//
349
// Returnvalue: '1' if an gps coordinate was logged
347
// Returnvalue: '1' if an gps coordinate was logged
350
//________________________________________________________________________________________________________________________________________
348
//________________________________________________________________________________________________________________________________________
Line 351... Line 349...
351
 
349
 
352
u8 KML_LoggGPSCoordinates(GPS_Pos_t* pGPS_Position, KML_Document_t *doc)
350
u8 KML_LoggGPSCoordinates(KML_Document_t *doc)
353
{
351
{
354
        u8 retval = 0;
352
        u8 retval = 0;
355
        while(doc->state != DOC_LINESTRING_OPENED)                                      // automatic create document with default filename on the card.
353
        while(doc->state != KML_DOC_LINESTRING_OPENED)                                  // automatic create document with default filename on the card.
356
        {
354
        {
357
                switch(doc->state)
355
                switch(doc->state)
358
                {
356
                {
359
                        case DOC_CLOSED:                                                                        // document hasn't been opened yet therefore it will be initialized automatically
-
 
360
                                KML_DocumentInit(doc);                                                  // initialize the document to default values
357
                        case KML_DOC_CLOSED:                                                                    // document hasn't been opened yet therefore it will be initialized automatically
361
                                retval = KML_DocumentOpen("default.kml",doc);   // open the kml-document with a standardname.
-
 
362
                                if(retval)
-
 
363
                                {
-
 
364
                                        doc->state = DOC_OPENED;
-
 
365
                                }
358
                                retval = KML_DocumentOpen("default.kml",doc);   // open the kml-document with a standardname.
Line 366... Line 359...
366
                        break;
359
                        break;
367
 
360
 
368
                        case DOC_OPENED:                                                                        // if a document has been opened before but no placemark exists:
-
 
369
                                retval = KML_PlaceMarkOpen("MIKROKOPTER",doc);
-
 
370
                                if(retval)
-
 
371
                                {
-
 
372
                                        doc->state = DOC_PLACEMARK_OPENED;                      // add a placemark to the document.
361
                        case KML_DOC_OPENED:                                                                    // if a document has been opened before but no placemark exists:
Line 373... Line 362...
373
                                }
362
                                retval = KML_PlaceMarkOpen(doc);
374
                        break;
363
                        break;
375
 
-
 
376
                        case DOC_PLACEMARK_OPENED:                                                      // add linestring to the placemark
-
 
377
                                retval = KML_LineStringBegin(doc);
-
 
378
                                if(retval)
-
 
379
                                {
364
 
Line 380... Line 365...
380
                                        doc->state = DOC_LINESTRING_OPENED;
365
                        case KML_DOC_PLACEMARK_OPENED:                                                  // add linestring to the placemark
381
                                }
366
                                retval = KML_LineStringBegin(doc);
382
                        break;
367
                        break;
Line 383... Line 368...
383
 
368
 
384
                        default:
369
                        default:
385
                                retval = 1;
370
                                retval = 0;
Line 386... Line 371...
386
                        break;
371
                        break;
387
 
372
 
388
                }
373
                }
389
                if(retval != 1) return(retval); // stop on error
374
                if(retval != 1) return(retval); // stop on error
390
        }
375
        }
391
 
376