Dual CANbus monitor and instrumentation cluster supporting ILI9341 display controller

Dependencies:   SPI_TFTx2_ILI9341 TOUCH_TFTx2_ILI9341 TFT_fonts mbed

Fork of CANary by Tick Tock

Revision:
150:ef46ce63345c
Parent:
149:e9739523109f
Child:
151:3047ebb3c9a8
--- a/utility.cpp	Wed Sep 25 03:33:18 2013 +0000
+++ b/utility.cpp	Fri Sep 27 04:43:41 2013 +0000
@@ -508,14 +508,15 @@
 }
 
 void saveConfig(){
-    printMsg("Saving local config file.\n"); // no config file
     FILE *cfile;
     FIL efile; // external usb file
     FRESULT sfr; // external file access flags
     unsigned int bytesRW;
     const int bufSize = 2048;
     char buffer[bufSize];
-
+    char sTemp[16];
+    printMsg("Saving local config file.\n");
+    wait(2);
     cfile = fopen("/local/config.txt", "w");    
     fprintf(cfile,"format 8\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
@@ -547,13 +548,16 @@
     fprintf(cfile,"showHealth %d\r\n",(showHealth?1:0));
     fprintf(cfile,"brakeMon %d\r\n",(brakeMon?1:0));
     fprintf(cfile,"brkMonRate %d\r\n", brkMonRate );            
-    fprintf(cfile,"brkMonThr %d\r\n", brkMonThr );
+    fprintf(cfile,"brkMonThr %d\r\n", brkMonThr );            
     for(char i=0;i<8;i++){
-        fprintf(cfile,"usrMsgId %4x\r\n", i, uMsgId[i] );
+        sprintf(sTemp,"usrMsgId %04x",uMsgId[i]);
+        fprintf(cfile,"%s\r\n", sTemp );
     }
     fclose(cfile);
     
     // Make copy of CONFIG.TXT
+    printMsg("Saving CONFIG.BAK.\n");
+    wait(2);
     cfile = fopen("/local/CONFIG.TXT", "r");
     sfr = f_open(&efile,"CONFIG.BAK",FA_WRITE|FA_CREATE_NEW);    
     if((cfile != NULL)&&(sfr == FR_OK)){
@@ -567,12 +571,30 @@
         fclose(cfile);
         f_close(&efile);
     }
+
+    // Make copy of ehist.cny
+    printMsg("Saving ehist.bak.\n");
+    wait(2);
+    cfile = fopen("/local/ehist.cny", "r");
+    sfr = f_open(&efile,"ehist.bak",FA_WRITE|FA_CREATE_NEW);    
+    if((cfile != NULL)&&(sfr == FR_OK)){
+        printMsg("Copy config file to USB\n");
+        while (!feof(cfile))
+        {
+            bytesRW=fread(buffer, 1, bufSize, cfile);
+            sfr=f_write(&efile,&buffer,bytesRW,&bytesRW);
+        }
+        fflush(cfile);
+        fclose(cfile);
+        f_close(&efile);
+    }
     wait(2);
 }
 
 void readConfig(){
     FILE *cfile;
     int ff;
+    char sTemp[16];
 
     cfile = fopen("/local/config.txt", "r");
     if (cfile==NULL){ // if doesn't exist --> create
@@ -584,7 +606,7 @@
         saveConfig();
     } else {
         ledHi = 0.8;
-        ledLo = 0.1;
+        ledLo = 0.3;
         pollInt = 60;
         scale12V = 16.2;
         skin = ttSkin;
@@ -636,7 +658,8 @@
         }
         if(ff>7){
             for(char i=0;i<8;i++){
-                fscanf(cfile, "usrMsgId %4x\r\n", &uMsgId[i] );        
+                fscanf(cfile, "usrMsgId %s\r\n", &sTemp );
+                sscanf(sTemp,"%4x", &uMsgId[i]);
             }
         }
         fclose(cfile);
@@ -657,7 +680,7 @@
             dMode[0]=4;
             dMode[1]=2;
             ledHi=0.800;
-            ledLo=0.100;
+            ledLo=0.300;
             pollInt=300;
             scale12V=16.20;
             skin=0;
@@ -678,8 +701,7 @@
             uMsgId[6]=0x0000;
             uMsgId[7]=0x0000;
             }
-
-        if(ff<7){//If not latest format, save as latest format
+        if(ff<8){//If not latest format, save as latest format
             saveConfig();
             printMsg("Config file format updated.\n"); // config forat updates
         }
@@ -862,12 +884,6 @@
             bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
             f_printf(&bfile,",%d",bd);
         }
-        // temporariliy dump everything
-        /*for(i=0; i<BatDataBufMax; i++) {
-            sprintf(sTemp,",%02x",battData[i]);
-            f_printf(&bfile,"%s",sTemp);
-        }*/
-
         f_printf(&bfile,"\r\n");
         f_close(&bfile);
     }
@@ -901,10 +917,32 @@
             fclose(lfile);
         }
         f_close(&efile);
+        int fwc_tmp = fwCount;
         readConfig();
+        fwCount = fwc_tmp; // Do no overwrite fwcount when loading new config
         wait(2);
     }
 
+    // Check for history file on USB drive
+    sfr = f_open(&efile,"ehist.cny",FA_READ|FA_OPEN_EXISTING);    
+    if(sfr == FR_OK)
+    {        
+        printf("Copy ehist file from USB\n");
+        lfile = fopen("/local/ehist.cny", "w");
+        if(lfile != NULL) {
+            while (!f_eof(&efile))
+            {
+                sfr=f_read(&efile,&buffer,bufSize,&bytesRW);
+                fwrite(buffer, 1, bytesRW, lfile);
+            }
+            fflush(lfile);
+            fclose(lfile);
+        }
+        f_close(&efile);
+        wait(2);
+    }
+
+
     sfr = f_open(&efile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
     if(sfr != FR_OK)
     {        
@@ -998,20 +1036,6 @@
     }
 }
 
-float airDrag(float F)
-{
-    float drag_F[16] = {1.47,1.39,1.363,1.337,1.312,1.288,1.265,1.243,1.221,1.2,1.18,1.161,1.142,1.124,1.106,1.034};
-    float temp_F[16] = {-31,-4,5,14,23,32,41,50,59,68,77,86,95,104,113,149};
-    char ii=0;
-    float drag;
-    while(ambient_F<=temp_F[++ii]) { } // Find section in table
-    drag=(ambient_F-temp_F[ii]);
-    drag/=(temp_F[ii-1]-temp_F[ii]);
-    drag*=(drag_F[ii-1]-drag_F[ii]);
-    drag+=drag_F[ii];
-    return drag;
-}
-
 //Sample CONFIG.TXT
 /*
 format 7
@@ -1027,7 +1051,7 @@
 dMode0 4
 dMode1 2
 ledHi 0.800
-ledLo 0.100
+ledLo 0.300
 pollInt 300
 scale12V 16.20
 skin 0
@@ -1039,4 +1063,12 @@
 brakeMon 1
 brkMonRate 400000
 brkMonThr 4000
-*/
+usrMsgId 5103
+usrMsgId 50a3
+usrMsgId 54a4
+usrMsgId 54b4
+usrMsgId 54c0
+usrMsgId 55b4
+usrMsgId 0000
+usrMsgId 0000
+*/
\ No newline at end of file