Tick Tock / Mbed 2 deprecated CANary

Dependencies:   SPI_TFTx2_ILI9341 TFT_fonts TOUCH_TFTx2_ILI9341 mbed

Fork of CANary_corrupt by Tick Tock

Revision:
204:637771cfc74e
Parent:
203:da9b422a6435
Child:
205:51f0db4cab03
--- a/main.cpp	Mon Mar 09 21:49:52 2015 +0000
+++ b/main.cpp	Sat May 30 01:46:42 2015 +0000
@@ -12,8 +12,8 @@
 // * fix bug where charging while on screws up efficiency computation
 // * find better kWh estimate than gids
 
-// rev203
-// * Reformat dte screen to fit more regen
+// rev204
+// * Added a time limit on log files (deletes files older than set days)
 
 // Include this before other header files
 #include "precompile.h"
@@ -27,7 +27,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "203";
+char revStr[7] = "204";
 unsigned long maxTarget = 1000;
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -80,10 +80,12 @@
 bool checkFWupdate = true;
 
 FILE *hfile; // history file
+FILE *rfile;
+FILE *file;
 FIL efile; // external usb file
 FRESULT efr; // external file access flags
 unsigned int bytesRW;
-char fileName[35] = "";
+char logFileName[35] = "";
 char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write
 char indexLastMsg[0x800]={0}; // index table for last message
 CANMessage lastMsg[100]; // table to store last message of eachtype
@@ -127,6 +129,7 @@
 bool laccOn = false;
 float scale12V = 16.2; // R1:R2 ratio
 float kWperGid = 0.080;
+char daysLog = 1; // 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;
@@ -323,14 +326,14 @@
     while (true) {
         if (!logOpen) { // Open new file if one is not already open
             if(logEn&&usbEn){ //logging enabled and USB device detected
-                strftime(fileName, 32, "%m%d%H%M.alc", &t); //mmddhhmm.alc
-                efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS);
+                strftime(logFileName, 32, "%m%d%H%M.alc", &t); //mmddhhmm.alc
+                efr = f_open(&efile,logFileName,FA_WRITE|FA_OPEN_ALWAYS);
                 seconds = time(NULL);
                 t = *localtime(&seconds) ;
                 lastDMode[0]=99;//force refresh
                 lastDMode[1]=99;//force refresh
                 if(efr != FR_OK){
-                    sprintf(sTemp,"\nERR:%d Unable to open %s\n\n\n\n",efr,fileName);
+                    sprintf(sTemp,"\nERR:%d Unable to open %s\n\n\n\n",efr,logFileName);
                     printMsg(sTemp); // cannot open alc file
                     logEn=false;
                     beep(1000,0.25);
@@ -339,7 +342,7 @@
                 } else {
                     logOpen = true;
                     readPointer=writePointer;
-                    sprintf(sTemp,"Starting Can Log %s\n",fileName);
+                    sprintf(sTemp,"Starting Can Log %s\n",logFileName);
                     printMsg(sTemp); // starting alc log file 
                     
                     logTS(); // Date Time at start
@@ -347,6 +350,9 @@
                     sprintf(sTemp,"Cr%s",revStr);
                     logEvent(sTemp); // gg - log firmware version   
                     beep(2000,0.25);
+                    file = fopen("/local/loglog.txt", "a"); // save filename log
+                    fprintf(file,"%s\r\n",logFileName);
+                    fclose(file);
                 }
             }//logging enabled and USB detected
         } else { // if (logOpen)
@@ -372,7 +378,7 @@
                 }
             } // if > 1/16 full, canbus has stopped, or logging stopped
             if (!logEn) {
-                sprintf(sTemp,"Stopping Can Log %s\n",fileName);
+                sprintf(sTemp,"Stopping Can Log %s\n",logFileName);
                 printMsg(sTemp); // stopping alc log file 
                 f_close(&efile);
                 logOpen=false;
@@ -389,6 +395,52 @@
             } else { //detach EVcan so only carcan will trigger wake
                 can1.attach(NULL);
             }// if (logOpen)
+
+            // Take advantage of the idle time to clear some room
+            seconds = time(NULL);
+            t = *localtime(&seconds) ;
+            
+            bool bit = false;
+            int fmon;
+            int fday;
+            int ftime;
+            rfile = fopen("/local/loglog.txt", "r");
+            if (rfile!=NULL){ 
+                file = fopen("/local/loglog.new", "w");
+                while (!feof(rfile)) {
+                    fscanf(rfile,"%2d%2d%4d.alc\r\n",&fmon,&fday,&ftime);
+                    if ((fmon < 12) || (t.tm_mon > 1)){
+                        fday = fday + fmon*31; //crude - february will store 3 extra days of data
+                    }
+                    if ((fday+daysLog)<(t.tm_mday+t.tm_mon*31)){ // Delete all files more than daysLog old
+                        bit=true;
+                        sprintf(sTemp,"%02d%02d%04d.alc",fmon,fday,ftime);
+                        f_unlink(sTemp);
+                        sprintf(sTemp,"Deleted logfile %02d%02d%04d.alc",fmon,fday,ftime);
+                        printMsg(sTemp); // config file loaded
+                    }else{
+                        fprintf(file,"%02d%02d%04d.alc\r\n",fmon,fday,ftime);
+                    }
+                }
+                fclose (file);
+                fclose (rfile);
+                if (bit) {
+                    remove ("/local/loglog.txt");
+                    //rename not working so do it the hard way
+                    //rename ("/local/loglog.new","/local/loglog.txt");
+                    rfile = fopen("/local/loglog.new", "r");
+                    file = fopen("/local/loglog.txt", "w");
+                    while (!feof(rfile)) {
+                        fscanf(rfile,"%s\r\n",&sTemp);
+                        fprintf(file,"%s\r\n",sTemp);
+                    }
+                    fclose (file);
+                    fclose (rfile);
+                }
+                remove ("/local/loglog.new");
+            }                    
+            wait(5); // wait a few seconds to ensure SDRAM is done
+
             seconds = time(NULL);
             t = *localtime(&seconds) ;
             strftime(sTemp, 40, "Sleeping: %a %m/%d/%Y %X\n", &t);
@@ -427,7 +479,7 @@
                 } // if (logOpen)
                 if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter
             } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now)
-                efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS);
+                efr = f_open(&efile,logFileName,FA_WRITE|FA_OPEN_ALWAYS);
                 f_lseek(&efile,0xffffffff); // goto end of file (append existing)
                 logEvent("WakingUp"); // gg - use messeges
                 logTS(); // Date-Time at wakeup