Browse Source

Add DST calculation
Simplify file name generation

k4be 2 years ago
parent
commit
6f8e32d2b9
6 changed files with 144 additions and 36 deletions
  1. 1 1
      soft/Makefile
  2. 1 1
      soft/gpx.c
  3. 14 32
      soft/main.c
  4. 1 2
      soft/main.h
  5. 118 0
      soft/timec.c
  6. 9 0
      soft/timec.h

+ 1 - 1
soft/Makefile

@@ -2,7 +2,7 @@
 PROJECT	= glg
 
 ### Source files and search directory
-CSRC    = main.c uart0.c uart1.c ff.c mmc.c 1wire.c ds18b20.c expander.c HD44780-I2C.c I2C.c xprintf.c gpx.c ffunicode.c display.c working_modes.o
+CSRC    = main.c uart0.c uart1.c ff.c mmc.c 1wire.c ds18b20.c expander.c HD44780-I2C.c I2C.c xprintf.c gpx.c ffunicode.c display.c working_modes.o timec.o
 ASRC    = stime.S
 VPATH   =
 

+ 1 - 1
soft/gpx.c

@@ -90,7 +90,7 @@ unsigned char gpx_write(struct location_s *loc, FIL *file) {
 	unsigned int bw;
 	const char *time;
 	
-	time = get_iso_time(loc->time);
+	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"));

+ 14 - 32
soft/main.c

@@ -25,7 +25,7 @@
 #include "gpx.h"
 #include "display.h"
 #include "working_modes.h"
-
+#include "timec.h"
 
 /*FUSES = {0xFF, 0x11, 0xFE};*/		/* ATmega644PA fuses: Low, High, Extended.
 This is the fuse settings for this project. The fuse bits will be included
@@ -166,23 +166,6 @@ ISR(ADC_vect)
 	}
 }
 
-/*---------------------------------------------------------*/
-/* User Provided Timer Function for FatFs module           */
-/*---------------------------------------------------------*/
-
-DWORD get_fattime (void)
-{
-	struct tm *stm;
-	time_t localtime = utc + LOCALDIFF;
-	stm = gmtime(&localtime);
-    return (DWORD)(stm->tm_year - 80) << 25 |
-           (DWORD)(stm->tm_mon + 1) << 21 |
-           (DWORD)stm->tm_mday << 16 |
-           (DWORD)stm->tm_hour << 11 |
-           (DWORD)stm->tm_min << 5 |
-           (DWORD)stm->tm_sec >> 1;
-}
-
 static void sleep(void) {
 	set_sleep_mode(SLEEP_MODE_IDLE);
 	sleep_enable();
@@ -245,16 +228,6 @@ void beep (UINT len, BYTE cnt)
 	}
 }
 
-
-/* Make ISO time string */
-const char *get_iso_time(time_t time) {
-	static char output[32];
-	struct tm *ct = gmtime(&time);
-
-	xsprintf(output, PSTR("%4u-%02u-%02uT%02u:%02u:%02u.000Z"), ct->tm_year+1900, ct->tm_mon+1, ct->tm_mday, ct->tm_hour, ct->tm_min, ct->tm_sec);
-	return output;
-}
-
 /* Compare sentence header string */
 BYTE gp_comp (const char *str1, __flash const char *str2)
 {
@@ -597,7 +570,7 @@ int main (void)
 
 			tmp_utc = gps_parse(Line);
 			if (tmp_utc) {
-				localtime = tmp_utc + LOCALDIFF * 3600L;	/* Local time */
+				localtime = tmp_utc + local_time_diff(tmp_utc) * 3600L;	/* Local time */
 				ct = *gmtime(&localtime);
 				if (timer_expired(system_log)) {
 					set_timer(system_log, 5000);
@@ -635,7 +608,16 @@ int main (void)
 			}
 
 			if (localtime && !(FLAGS & F_FILEOPEN)) {
-				xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u.LOG"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
+				char *time = get_iso_time(utc, 1);
+				char *ptr = time;
+				while (*ptr) {
+					switch (*ptr) {
+						case ':': *ptr = '-'; break;
+						case '+': *ptr = 'p'; break;
+					}
+					ptr++;
+				}
+				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 */
 					|| f_lseek(&gps_log, f_size(&gps_log)) 					/* Append mode */
@@ -646,7 +628,7 @@ int main (void)
 				}
 				wdt_reset();
 
-				xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u.GPX"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
+				xsprintf(Line, PSTR("%s.GPX"), time);
 				xprintf(__open_msg, Line);
 				if (f_open(&gpx_file, Line, FA_WRITE | FA_OPEN_ALWAYS)		/* Open log file */
 					|| f_lseek(&gpx_file, f_size(&gpx_file)) 				/* Append mode */
@@ -658,7 +640,7 @@ int main (void)
 				}
 				wdt_reset();
 
-				xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u-SYSTEM.LOG"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
+				xsprintf(Line, PSTR("%s-SYSTEM.LOG"), time);
 				xprintf(__open_msg, Line);
 				if (f_open(&system_log, Line, FA_WRITE | FA_OPEN_ALWAYS)	/* Open log file */
 					|| f_lseek(&system_log, f_size(&system_log)) 			/* Append mode */

+ 1 - 2
soft/main.h

@@ -7,7 +7,6 @@
 #include "stime.h"
 #include "expander.h"
 
-#define LOCALDIFF	+1			/* Time difference from UTC [hours] */
 #define	IVT_SYNC	180			/* f_sync() interval (0:no periodic sync) [sec] */
 #define POWER_SW_TIME	300		/* power switch hold time to power off [10ms] */
 
@@ -168,7 +167,7 @@ static inline unsigned int atomic_get_uint(volatile unsigned int *volatile data)
 
 
 void disk_timerproc (void); /* mmc.h */
-const char *get_iso_time(time_t time);
+char *get_iso_time(time_t time, unsigned char local);
 void close_files(unsigned char flush_logs);
 unsigned char getkey(void);
 

+ 118 - 0
soft/timec.c

@@ -0,0 +1,118 @@
+#include <avr/pgmspace.h>
+#include "stime.h"
+#include "timec.h"
+#include "main.h"
+#include "ff.h"
+#include "xprintf.h"
+
+__flash const unsigned char mi[] = {3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
+
+unsigned char calc_weekday(struct tm *ct) {
+	unsigned char w, x;
+
+	x = ct->tm_year - 100;
+	w = x%7;
+	if(x != 0) {		/* if year is not 2000 */
+		/* Count only past years, for example if current year is 2004, count only 2000-2003, so x-- */
+		x--;
+		w += x >> 2;	/* add number of years which are divisible by 4 4 */
+		w -= x/100;		/* subtract number of years which are divisible by 100 */
+		w++;			/* add the year 2000 which is divisible by 400 */
+	}
+			/* Add first weekday (Saturday 2000-01-01) */
+	w+=4;
+	
+/* Months */
+	if(ct->tm_mon+1 > 1) {		/* if it's not January */
+		w += mi[ct->tm_mon+1 - 2];	/* add array increment */
+	}
+/* Days */
+	w += ct->tm_mday;
+
+/*
+** If current month is March or later, and it's a leap year, this means February
+** was 29 days so we have to add 1.
+*/
+	if(ct->tm_mon+1 > 2)
+		if(!(ct->tm_year&3)) w+=1;
+
+
+/* Result modulo 7 */
+	return (w%7)+1; /* 1 = Monday, 7 = Sunday */
+}
+
+signed int local_time_diff(time_t time) {
+	signed char offset = TIME_OFFSET;
+	unsigned char t;
+	struct tm *ct = gmtime(&time);
+	unsigned char weekday = calc_weekday(ct);
+
+	if (ct->tm_mon+1 == 3 || ct->tm_mon+1 == 10){
+		t = 35 + weekday - ct->tm_mday;
+		t += 31;
+		t = t%7;
+		t = 31-t;
+		if (ct->tm_mon+1 == 3){ // March
+			if (ct->tm_mday==t) { // last Sunday
+				if (ct->tm_hour >= 2 )
+					offset = SUMMER_TIME_OFFSET;
+			} else if (ct->tm_mday > t) { // after last Sunday
+				offset = SUMMER_TIME_OFFSET;
+			}
+		} else { // October
+			if (ct->tm_mday == t) { // last Sunday
+				if(ct->tm_hour < 2)
+					offset = SUMMER_TIME_OFFSET;
+			} else if (ct->tm_mday < t) { // before last Sunday
+				offset = SUMMER_TIME_OFFSET;
+			}
+		}
+	} else if (ct->tm_mon+1 > 3 && ct->tm_mon+1 < 10) {
+		offset = SUMMER_TIME_OFFSET;
+	}
+	return offset;
+}
+
+const char *local_time_mark(time_t time) {
+	static char buf[4];
+	signed int time_diff = local_time_diff(time);
+	if (time_diff < 0) { 
+		buf[0] = '-';
+		time_diff = -time_diff;
+	} else {
+		buf[0] = '+';
+	}
+	buf[1] = time_diff/10 + '0'; /* never greater than 23 */
+	buf[2] = time_diff%10 + '0';
+	buf[3] = '\0';
+	return buf;
+}
+
+/* Make ISO time string */
+char *get_iso_time(time_t time, unsigned char local) {
+	static char output[32];
+	if (local)
+		time += local_time_diff(time) * (signed int)3600;
+	struct tm *ct = gmtime(&time);
+
+	xsprintf(output, PSTR("%4u-%02u-%02uT%02u:%02u:%02u.000%s"), ct->tm_year+1900, ct->tm_mon+1, ct->tm_mday, ct->tm_hour, ct->tm_min, ct->tm_sec, local?local_time_mark(time):"Z");
+	return output;
+}
+
+/*---------------------------------------------------------*/
+/* User Provided Timer Function for FatFs module           */
+/*---------------------------------------------------------*/
+
+DWORD get_fattime (void)
+{
+	struct tm *stm;
+	time_t localtime = utc + local_time_diff(utc) * (signed int)3600;
+	stm = gmtime(&localtime);
+    return (DWORD)(stm->tm_year - 80) << 25 |
+           (DWORD)(stm->tm_mon + 1) << 21 |
+           (DWORD)stm->tm_mday << 16 |
+           (DWORD)stm->tm_hour << 11 |
+           (DWORD)stm->tm_min << 5 |
+           (DWORD)stm->tm_sec >> 1;
+}
+

+ 9 - 0
soft/timec.h

@@ -0,0 +1,9 @@
+#pragma once
+
+#define TIME_OFFSET	+1 /* CET */
+#define SUMMER_TIME_OFFSET	+2 /* CEST */
+
+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);
+