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:
131:0d926c080a65
Parent:
130:1a9d2a6d99ce
Child:
132:08748a67280a
--- a/utility.cpp	Mon Jul 22 00:02:51 2013 +0000
+++ b/utility.cpp	Wed Jul 24 11:20:08 2013 +0000
@@ -5,7 +5,7 @@
 
 void RTC_IRQHandler() {
     timer.reset(); // zero ms at the-seconds-tic
-    canIdle=(++secsNoMsg>canTimeout)?true:false;
+    carCanIdle=(++secsNoCarCanMsg>canTimeout)?true:false;
     userIdle=(++secsNoTouch>userTimeout)?true:false;
     LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next
     tick=true;
@@ -55,7 +55,7 @@
     signed long imWs_x4;
     unsigned short ts;
 
-    secsNoMsg=0; // reset deadman switch
+    secsNoCarCanMsg=0; // reset deadman switch
     if(debugMode||(skin==ggSkin)){ 
         // code to insert actual number of dropped frames for overrun debug - skiped in normal mode to keep logcan short
         if(logOpen){
@@ -490,7 +490,7 @@
 void saveConfig(){
     FILE *cfile;
     cfile = fopen("/local/config.txt", "w");    
-    fprintf(cfile,"format 5\r\n");
+    fprintf(cfile,"format 6\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
     fprintf(cfile,"y0_off %d\r\n",tt.y0_off);
     fprintf(cfile,"x0_pp %d\r\n",tt.x0_pp);
@@ -517,12 +517,14 @@
     fprintf(cfile,"DebugMode %d\r\n",(debugMode?1:0));
     fprintf(cfile,"metric %d\r\n",(metric?1:0));
     fprintf(cfile, "firmware %d\r\n", fwCount );            
+    fprintf(cfile,"showHealth %d\r\n",(showHealth?1:0));
     fclose(cfile);
 }
 
 void readConfig(){
     FILE *cfile;
     int ff;
+
     cfile = fopen("/local/config.txt", "r");
     if (cfile==NULL){ // if doesn't exist --> create
         printMsg("No config file found.\n"); // no config file
@@ -571,8 +573,13 @@
             fscanf(cfile, "firmware %d\r\n", &iMetric );
             fwCount = iMetric;
         }
+        if(ff>5){
+            int ishowHealth;
+            fscanf(cfile, "showHealth %d\r\n", &ishowHealth );            
+            showHealth = (bool)ishowHealth;
+        }
         fclose(cfile);
-        if(ff<4){//If not latest format, save as latest format
+        if(ff<6){//If not latest format, save as latest format
             saveConfig();
             printMsg("Config file format updated.\n"); // config forat updates
         }
@@ -745,14 +752,15 @@
         f_printf(&bfile,"%s,",sTemp);           
         f_printf(&bfile,"%d,%d,%d,%d,",(battData[(BatDataBaseG4*7)+ 3]<<8)+battData[(BatDataBaseG4*7)+ 4],battData[(BatDataBaseG4*7)+ 5],(battData[(BatDataBaseG4*7)+ 6]<<8)+battData[(BatDataBaseG4*7)+ 7],battData[(BatDataBaseG4*7)+ 8]);
         f_printf(&bfile,"%d,%d,%d,%d", (battData[(BatDataBaseG4*7)+ 9]<<8)+battData[(BatDataBaseG4*7)+10],battData[(BatDataBaseG4*7)+11],(battData[(BatDataBaseG4*7)+12]<<8)+battData[(BatDataBaseG4*7)+13],battData[(BatDataBaseG4*7)+14]);
-        for(i=0; i<96; i++) {
+        /*for(i=0; i<96; i++) {
             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++) {
-            f_printf(&bfile,",%02x",battData[i]);
-        }*/
+        for(i=0; i<BatDataBufMax; i++) {
+            sprintf(sTemp,",%02x",battData[i]);
+            f_printf(&bfile,"%s",sTemp);
+        }
 
         f_printf(&bfile,"\r\n");
         f_close(&bfile);
@@ -767,6 +775,26 @@
     FRESULT sfr; // external file access flags
     unsigned int bytesRW;
     char sTemp[40];
+    const int bufSize = 2048;
+    char buffer[bufSize];
+    FILE *destFile;    
+
+    // Check for config file on USB drive
+    sfr = f_open(&sfile,"CONFIG.TXT",FA_READ|FA_OPEN_EXISTING);    
+    if(sfr == FR_OK)
+    {        
+        printf("Copy config file from USB\n");
+        destFile = fopen("/local/CONFIG.TXT", "w");
+        while (!f_eof(&sfile))
+        {
+            sfr=f_read(&sfile,&buffer,bufSize,&bytesRW);
+            fwrite(buffer, 1, bytesRW, destFile);
+        }
+        fflush(destFile);
+        fclose(destFile);
+        f_close(&sfile);
+    }    
+    
     sfr = f_open(&sfile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
     if(sfr != FR_OK)
     {        
@@ -811,21 +839,17 @@
     printf("copying new firmware\n");
     tt.cls();
     //Copy the new firmware from usb->local
-    //The newest bin file is the one that is used by the mbed
-    const int bufSize = 2048;
-    FILE *destFile;    
+    //The newest bin file is the one that is used by the mbed  
     sprintf(sTemp,"/local/fw%d.bin",fwCount);
     printf("Writing %s\n",sTemp);
     wait(2);
     destFile = fopen(sTemp, "wb");
     if(destFile == NULL)
     {
-        sprintf(sTemp,"Couldn't Open Destination\n");
-        printf(sTemp);
+        printf("Couldn't Open Destination\n");
         wait(3);
         return;
     }
-    char buffer[bufSize];
 
     while (!f_eof(&sfile))
     {
@@ -852,4 +876,31 @@
         f_unlink("usb.det");
     }
     return(usbEn);
+    //return(true);
 }
+
+//Sample CONFIG.TXT
+/*
+format 5
+x0_off 5732
+y0_off 34009
+x0_pp 77
+y0_pp 106
+x1_off 33955
+y1_off 6310
+x1_pp 80
+y1_pp 104
+x_mid 31986
+dMode0 4
+dMode1 2
+ledHi 0.800
+ledLo 0.100
+pollInt 300
+scale12V 16.20
+skin 0
+dtePeriod 14
+DebugMode 0
+metric 0
+firmware 11
+showHealth 1
+*/
\ No newline at end of file