Browse Source

Add single point saving

k4be 2 years ago
parent
commit
bf8286e006
7 changed files with 102 additions and 33 deletions
  1. 46 19
      soft/gpx.c
  2. 1 0
      soft/gpx.h
  3. 1 8
      soft/main.c
  4. 9 0
      soft/timec.c
  5. 1 0
      soft/timec.h
  6. 33 5
      soft/working_modes.c
  7. 11 1
      soft/working_modes.h

+ 46 - 19
soft/gpx.c

@@ -6,18 +6,20 @@
 #include "gpx.h"
 #include "ff.h"
 #include "settings.h"
+#include "timec.h"
 
 #define KALMAN_Q	8.5e-6
 #define KALMAN_R	4e-5
 #define KALMAN_ERR_MAX	6e-4
 
 __flash const char xml_header[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-		"<gpx xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\" version=\"1.1\" creator=\"k4be\">\n"
-		"\t<trk>\n"
-		"\t\t<trkseg>\n";
+		"<gpx xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\" version=\"1.1\" creator=\"k4be\">\n";
+__flash const char xml_trk_start[] = "\t<trk>\n";
+__flash const char xml_trkseg_end[] = "\t\t</trkseg>\n";
+__flash const char xml_trkseg_start[] = "\t\t<trkseg>\n";
 
 FIL gpx_file;
-static char buf[sizeof(xml_header)+1];
+static char buf[sizeof(xml_header)+sizeof(xml_trk_start)+2];
 
 struct kalman_s {
 	unsigned char initialized;
@@ -88,34 +90,59 @@ unsigned char gpx_init(FIL *file) {
 	gpx.last_saved.lat = 0;
 	gpx.last_saved.time = 0;
 
-	gpx.paused = 0;
+	gpx.paused = 1; /* make it add a <trkseg> tag */
 
 	strcpy_P(buf, xml_header);
+	strcat_P(buf, xml_trk_start);
 	return f_write(file, buf, strlen(buf), &bw);
 }
 
+void gpx_save_single_point(struct location_s *loc) {
+	FIL gpx;
+	UINT bw;
+	unsigned char err = 0;
+	char *time = get_iso_time(loc->time, 1);
+	iso_time_to_filename(time);
+	xsprintf(buf, PSTR("%s-POINT.GPX"), time);
+	xprintf(PSTR("Writing single point in %s\r\n"), buf);
+	if ((err = f_open(&gpx, buf, FA_WRITE | FA_OPEN_ALWAYS))) {
+		f_close(&gpx);
+//		System.status = STATUS_FILE_OPEN_ERROR;
+		xputs_P(PSTR("File open error\r\n"));
+		return;	/* Failed to open file */
+	}
+	strcpy_P(buf, xml_header);
+	err |= f_write(&gpx, buf, strlen(buf), &bw);
+	xsprintf(buf, PSTR("\t<wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n</gpx>\n"), loc->lat, loc->lon);
+	err |= f_write(&gpx, buf, strlen(buf), &bw);
+	err |= f_close(&gpx);
+	if (err) {
+		/* TODO */
+	}
+}
+
 unsigned char gpx_write(struct location_s *loc, FIL *file) {
 	unsigned int bw;
 	const char *time;
 
 	if (System.tracking_paused) {
 		if (!gpx.paused) {
-			strcpy_P(buf, PSTR("\t\t</trkseg>\n"));
+			strcpy_P(buf, xml_trkseg_end);
 			gpx.paused = 1;
-		} else {
-			return 0; /* nothing to store */
 		}
-	} else {
-		if (gpx.paused) {
-			strcpy_P(buf, PSTR("\t\t<trkseg>\n"));
-			f_write(file, buf, strlen(buf), &bw);
-			gpx.paused = 0;
-		}
-		time = get_iso_time(loc->time, 0);
-		xsprintf(buf, PSTR("\t\t\t<trkpt lat=\"%.8f\" lon=\"%.8f\">\n\t\t\t\t<time>%s</time>\n"), loc->lat, loc->lon, time);
-		/* alt */
-		strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
+		return 0; /* nothing to store */
 	}
+
+	if (gpx.paused) {
+		strcpy_P(buf, xml_trkseg_start);
+		f_write(file, buf, strlen(buf), &bw);
+		gpx.paused = 0;
+	}
+	time = get_iso_time(loc->time, 0);
+	xsprintf(buf, PSTR("\t\t\t<trkpt lat=\"%.8f\" lon=\"%.8f\">\n\t\t\t\t<time>%s</time>\n"), loc->lat, loc->lon, time);
+	/* alt */
+	strcat_P(buf, PSTR("\t\t\t</trkpt>\n"));
+
 	return f_write(file, buf, strlen(buf), &bw);
 }
 
@@ -123,7 +150,7 @@ unsigned char gpx_close(FIL *file) {
 	unsigned int bw;
 	buf[0] = '\0';
 	if (!gpx.paused)
-		strcpy_P(buf, PSTR("\t\t</trkseg>\n"));
+		strcpy_P(buf, xml_trkseg_end);
 	strcat_P(buf, PSTR("\t</trk>\n</gpx>\n"));
 	f_write(file, buf, strlen(buf), &bw);
 	return f_close(file);

+ 1 - 0
soft/gpx.h

@@ -8,4 +8,5 @@ unsigned char gpx_init(FIL *file);
 unsigned char gpx_write(struct location_s *loc, FIL *file);
 unsigned char gpx_close(FIL *file);
 void gpx_process_point(struct location_s *loc, FIL *file);
+void gpx_save_single_point(struct location_s *loc);
 

+ 1 - 8
soft/main.c

@@ -414,14 +414,7 @@ int main (void)
 
 			if (localtime && !(FLAGS & F_FILEOPEN)) {
 				char *time = get_iso_time(utc, 1);
-				char *ptr = time;
-				while (*ptr) {
-					switch (*ptr) {
-						case ':': *ptr = '-'; break;
-						case '+': *ptr = 'p'; break;
-					}
-					ptr++;
-				}
+				iso_time_to_filename(time);
 				xsprintf(Line, PSTR("%s-NMEA.LOG"), time);
 				xprintf(__open_msg, Line);
 				if (f_open(&gps_log, Line, FA_WRITE | FA_OPEN_ALWAYS)		/* Open log file */

+ 9 - 0
soft/timec.c

@@ -116,3 +116,12 @@ DWORD get_fattime (void)
            (DWORD)stm->tm_sec >> 1;
 }
 
+void iso_time_to_filename(char *time) {
+	while (*time) {
+		switch (*time) {
+			case ':': *time = '-'; break;
+			case '+': *time = 'p'; break;
+		}
+		time++;
+	}
+}

+ 1 - 0
soft/timec.h

@@ -6,4 +6,5 @@
 signed int local_time_diff(time_t time);
 const char *local_time_mark(time_t time);
 char *get_iso_time(time_t time, unsigned char local);
+void iso_time_to_filename(char *time);
 

+ 33 - 5
soft/working_modes.c

@@ -5,6 +5,7 @@
 #include "xprintf.h"
 #include "settings.h"
 #include "nmea.h"
+#include "gpx.h"
 
 static signed char display_mode_index;
 
@@ -27,11 +28,6 @@ unsigned char tracking_pause(void) {
 	return MODE_NO_CHANGE;
 }
 
-#define STATE_PAUSE_TRACKING_NOTPAUSED	0
-#define STATE_PAUSE_TRACKING_JUSTPAUSED	1
-#define STATE_PAUSE_TRACKING_PAUSED		2
-#define STATE_PAUSE_TRACKING_JUSTUNPAUSED	3
-
 const char *pause_tracking_get_name(void) {
 	static unsigned char state = STATE_PAUSE_TRACKING_NOTPAUSED;
 	switch (state) {
@@ -67,7 +63,39 @@ const char *pause_tracking_get_name(void) {
 	}
 }
 
+const char *save_point_get_name(void) {
+	switch (mp.point_save_state) {
+		default:
+			return PSTR("Zapisz punkt");
+		case STATE_POINT_SAVE_NOT_DONE:
+			if (timer_expired(info_display))
+				mp.point_save_state = STATE_POINT_SAVE_READY;
+			return PSTR("Nie zapisano!");
+		case STATE_POINT_SAVE_DONE:
+			if (timer_expired(info_display))
+				mp.point_save_state = STATE_POINT_SAVE_READY;
+			return PSTR("Zapisano!");
+	}
+}
+
+unsigned char save_point(void) {
+	if (timer_expired(info_display)) { /* don't save too often */
+		if (System.location_valid) {
+			gpx_save_single_point(&location);
+			mp.point_save_state = STATE_POINT_SAVE_DONE;
+		} else {
+			mp.point_save_state = STATE_POINT_SAVE_NOT_DONE;
+		}
+	}
+	set_timer(info_display, 2000);
+	return MODE_NO_CHANGE;
+}
+
 __flash const struct main_menu_pos_s main_menu[MAIN_MENU_MAXPOS+1] = {
+	{
+		.func = save_point,
+		.get_name = save_point_get_name,
+	},
 	{
 		.func = enter_settings,
 		.get_name = enter_settings_get_name,

+ 11 - 1
soft/working_modes.h

@@ -5,7 +5,16 @@
 #define MODE_MAIN_MENU	1
 #define MODE_SETTINGS_MENU	2
 
-#define MAIN_MENU_MAXPOS 1
+#define MAIN_MENU_MAXPOS 2
+
+#define STATE_PAUSE_TRACKING_NOTPAUSED	0
+#define STATE_PAUSE_TRACKING_JUSTPAUSED	1
+#define STATE_PAUSE_TRACKING_PAUSED		2
+#define STATE_PAUSE_TRACKING_JUSTUNPAUSED	3
+
+#define STATE_POINT_SAVE_READY	0
+#define STATE_POINT_SAVE_NOT_DONE	1
+#define STATE_POINT_SAVE_DONE	2
 
 struct main_menu_pos_s {
 	const char * (* get_name)(void);
@@ -15,6 +24,7 @@ struct main_menu_pos_s {
 struct menu_params_s {
 	unsigned char main_menu_pos;
 	unsigned char settings_menu_pos;
+	unsigned char point_save_state;
 };
 
 extern struct menu_params_s mp;