.

Dependencies:   SDHCFileSystem mbed

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Mon Dec 24 05:24:25 2012 +0000
Parent:
20:153800633e28
Child:
22:605c2f33b340
Commit message:
Changed log filename to date mmddhhmm.alc.; Now delete all files > 1 month old when going to sleep

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Dec 23 23:10:47 2012 +0000
+++ b/main.cpp	Mon Dec 24 05:24:25 2012 +0000
@@ -105,12 +105,11 @@
 CAN can2(p30, p29);     // CAN2 uses pins 30 and 29 (rx, tx) and pin 28 (rs)
 DigitalOut can2_SleepMode(p28);     // Use pin 28 to control the sleep mode of can2
 bool logOpen = false;
+FILE *rfile;
 FILE *file;
-FILE *fTemp;
 char fileName[35] = "" ;
 char writeBuffer[maxBufLen][13];
 char c;
-char sTemp[35] ;
 volatile int writePointer = 0;
 volatile int secsIdle = canTimeout;
 volatile bool canIdle = false;
@@ -221,6 +220,10 @@
 
 int main() {
     int readPointer=0;
+    int fmon;
+    int fday;
+    int ftime;
+    char sTemp[35];
     unsigned long secs;
     pc.baud(460800);        // change serial interface to pc to 450800, 8N1
     can1.frequency(500000);
@@ -281,24 +284,24 @@
         strftime(sTemp, 32, "%a %m/%d/%Y %X", localtime(&seconds));
         pc.printf("%s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS
     }
-
+    /*if (t.tm_year < 10) {//for test - delete
+        t.tm_year = 2012-1900;//delete
+        set_time(mktime(&t));//delete
+        seconds = time(NULL);//delete
+    }
+    t.tm_mon=(t.tm_mon+1)%12; //for test - delete
+    t.tm_mday=(t.tm_mday+1)%30; //for test - delete
+    set_time(mktime(&t));//delete
+    seconds = time(NULL);//delete*/
+    
     while (true) {
         if(CD == 1) {
             if (!logOpen) { // Open new file if one is not already open
-                int fileNum = 0;
-                sprintf(fileName,"/sd/cancan%d.alc",fileNum);
-                pc.printf("Checking file %s\n",fileName);
-                file = fopen(fileName, "r");
-                while (!(file == NULL)) {
-                    fclose(file);
-                    fileNum=++fileNum%maxFileNum;
-                    sprintf(fileName,"/sd/cancan%d.alc",fileNum);
-                    pc.printf("Checking file %s\n",fileName);
-                    file = fopen(fileName, "r");
-                    pc.printf("Checked file %s\n",fileName);
-                }
+                t = *localtime(&seconds) ;
+                strftime(fileName, 32, "/sd/%m%d%H%M.alc", &t); //mmddhhmm.alc
+
                 pc.printf("Using file %s\n",fileName);
-                file = fopen(fileName, "wb");
+                file = fopen(fileName, "ab");
                 
                 if(file==NULL){
                     pc.printf("\nUnable to open canlog\n\n\n\n");
@@ -306,24 +309,15 @@
                 } else {
                     logOpen = true;
                     readPointer=writePointer;
-                    // delete next opening (only maxFileNum files allowed on SDRAM)
-                    sprintf(sTemp,"/sd/cancan%d.alc",(fileNum+1)%maxFileNum);
-                    remove(sTemp);
-                    fTemp = fopen(sTemp, "r");
-                    if (!(fTemp == NULL)) {
-                        pc.printf("Unable to delete %s\n",sTemp);
-                    } else {
-                        pc.printf("Removed file %s\n",sTemp);
-                    }
                     pc.printf("\nStarting Can Log %s\n",fileName);
-                    // write full timestamp
                     logTS();
-                    //set_timestamp(file,2012,12,23,11,11,11);
+                    fclose(file);
+                    file = fopen("/sd/loglog.txt", "a");
+                    fprintf(file,"%s\r\n",fileName);
                     fclose(file);
                 }
             } // if (!logOpen)
             do {
-                //pc.printf("loop\n");
                 if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/2)||canIdle||(PB1==0)) {
                     // Dump buffer if > 1/2 full, canbus has stopped, or PB1 pressed
                     if (logOpen) {
@@ -345,6 +339,36 @@
                     } // if (logOpen)
                 } // if > 1/2 full, canbus has stopped, or PB1 pressed
                 if (canIdle) { // canbus idle --> sleep to save power
+                    // First take advantage of the idle time to clear some room
+                    // Delete all files more than 1 month old
+                    rfile = fopen("/sd/loglog.txt", "r");
+                    file = fopen("/sd/loglog.new", "w");
+                    while (!feof(rfile)) {
+                        fscanf(rfile,"/sd/%2d%2d%4d.alc\r\n",&fmon,&fday,&ftime);
+                        if ((fmon<t.tm_mon)&&(fday<=t.tm_mday)){
+                            sprintf(sTemp,"/sd/%02d%02d%04d.alc",fmon,fday,ftime);
+                            if ((remove(sTemp)==NULL))
+                                pc.printf("Removed file %s\n",sTemp);
+                        }else{
+                            fprintf(file,"/sd/%02d%02d%04d.alc\r\n",fmon,fday,ftime);
+                        }
+                    }
+
+                    fclose (file);
+                    fclose (rfile);
+                    remove ("/sd/loglog.txt");
+                    //rename not working so do it the hard way
+                    //rename ("/sd/loglog.new","/sd/loglog.txt");
+                    rfile = fopen("/sd/loglog.new", "r");
+                    file = fopen("/sd/loglog.txt", "w");
+                    while (!feof(rfile)) {
+                        fscanf(rfile,"%s\r\n",&sTemp);
+                        fprintf(file,"%s\r\n",sTemp);
+                    }
+                    fclose (file);
+                    fclose (rfile);
+                    remove ("/sd/loglog.new");
+
                     pc.printf("Putting uC to sleep.\n");
                     //LPC_RTC->CIIR=0x00; // block RTC interrupts
                     led1=0;
@@ -355,12 +379,10 @@
                     while (secsIdle>canTimeout)
                         //DeepPowerDown();
                         __wfi(); // freeze CPU and wait for interrupt (from canbus)
-                    if (time(NULL)>(secs+3600)) {
-                        logOpen = false; // Start new file if asleep for more than an hour
+                    if (time(NULL)>(secs+1800)) {
+                        logOpen = false; // Start new file if asleep for more than 30 minutes
                     } else { // insert timestamp on each wake
-                        file = fopen(fileName, "ab");
                         logTS();
-                        fclose(file);
                     }
                     //LPC_RTC->CIIR=0x01; // re-enable RTC interrupts
                 }