Tick Tock / Mbed 2 deprecated CANary

Dependencies:   SPI_TFTx2_ILI9341 TFT_fonts TOUCH_TFTx2_ILI9341 mbed

Fork of CANary_corrupt by Tick Tock

Revision:
207:411cdca211aa
Parent:
206:cb5ef167dc7b
Child:
208:bfb6b68d1677
--- a/main.cpp	Sat Jun 06 19:04:27 2015 +0000
+++ b/main.cpp	Mon Jun 29 13:19:25 2015 +0000
@@ -12,8 +12,8 @@
 // * fix bug where charging while on screws up efficiency computation
 // * find better kWh estimate than gids
 
-// rev206
-// * 
+// rev207
+// * fixed bugs in log file age determination
 
 // Include this before other header files
 #include "precompile.h"
@@ -27,7 +27,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "206";
+char revStr[7] = "207";
 unsigned long maxTarget = 1000;
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -134,7 +134,7 @@
 bool laccOn = false;
 float scale12V = 16.2; // R1:R2 ratio
 float kWperGid = 0.080;
-char daysLog = 2; // How many days of log files to save
+int daysLog = 999; // How many days of log files to save
 unsigned short startGids = 0; // Gids at start of trip
 unsigned short dailyGids = 0; // Gids per day
 bool getGids = false;
@@ -199,6 +199,7 @@
 short uCmdId = 0x79b;
 char uCmdLen = 8;
 char uCmdData[8] = {0x02, 0x21, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff};
+int daysUptoMonth[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
 unsigned short modelYear = 2011;
 bool idir, lidir;
 bool enableSound=true;
@@ -412,9 +413,7 @@
                 int fday;
                 int ftime;
                 int fdays;
-                sprintf(sTemp,"Checking for old log files\n");
-                printMsg(sTemp);                
-                res = f_opendir(&dir, "");                       /* Open the directory */                  
+                res = f_opendir(&dir, ""); //Get USB contents                 
                 if(res == FR_OK) {
                     for (i=1;i<200;i++) {
                         res = f_readdir(&dir, &fno);                   /* Read a directory item */
@@ -425,12 +424,11 @@
                             continue; // Do nothing
                         } else {                                       /* It is a file. */
                             if(sscanf(fn,"%2d%2d%4d.alc",&fmon,&fday,&ftime)==3){
-                                if ((fmon < 12) || (t.tm_mon > 1)){
-                                    fdays = fday + fmon*31 - 31; //crude - february will store 3 extra days of data
-                                }else{
-                                    fdays = fday;
+                                fdays = fday + daysUptoMonth[fmon-1]; //Leap years save one extra day at the end of February
+                                if (fdays>(t.tm_mday+daysUptoMonth[t.tm_mon])){//file cannot be from the future so must be very old
+                                    fdays = fdays - 365;
                                 }
-                                if ((fdays+daysLog)<(t.tm_mday+t.tm_mon*31)){ // Delete all files more than daysLog old
+                                if ((fdays+daysLog)<(t.tm_mday+daysUptoMonth[t.tm_mon])){ // Delete all files more than daysLog old
                                     sprintf(sTemp,"%02d%02d%04d.alc",fmon,fday,ftime);
                                     f_unlink(sTemp);
                                     sprintf(sTemp,"Deleted logfile %02d%02d%04d.alc\n",fmon,fday,ftime);