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:
143:88b5155622a5
Parent:
142:c1a11d6f181c
Child:
144:b0c9d30dd346
--- a/utility.cpp	Sun Aug 04 20:59:27 2013 +0000
+++ b/utility.cpp	Mon Aug 05 03:34:06 2013 +0000
@@ -292,9 +292,10 @@
                     maxTemp+=temp_C[ii];
 
                     // Get state of health
-                    SOH_x100=battData[(BatDataBaseG1*7)+29]*0x100+battData[(BatDataBaseG1*7)+30];
+                    SOH2_x100=battData[(BatDataBaseG1*7)+29]*0x100+battData[(BatDataBaseG1*7)+30];
                     Ah_x10000=battData[(BatDataBaseG1*7)+36]*0x10000+battData[(BatDataBaseG1*7)+37]*0x100+battData[(BatDataBaseG1*7)+38];
                     SOC_x10000=battData[(BatDataBaseG1*7)+32]*0x10000+battData[(BatDataBaseG1*7)+33]*0x100+battData[(BatDataBaseG1*7)+34];
+                    accV2=(float)battData[(BatDataBaseG1*7)+23]/4+(float)battData[(BatDataBaseG1*7)+24]/1024;
                     
                     // Save shunt data
                     for(j=0; j<24; j++){
@@ -518,13 +519,13 @@
     fprintf(cfile,"x1_pp %d\r\n",tt.x1_pp);
     fprintf(cfile,"y1_pp %d\r\n",tt.y1_pp);
     fprintf(cfile,"x_mid %d\r\n",tt.x_mid);
-    if (dMode[0]==configScreen)
-        fprintf(cfile,"dMode0 %d\r\n",mainScreen);
-    else
+    //if (dMode[0]==configScreen)
+    //    fprintf(cfile,"dMode0 %d\r\n",mainScreen);
+    //else
         fprintf(cfile,"dMode0 %d\r\n",dMode[0]);
-    if (dMode[1]==configScreen)
-        fprintf(cfile,"dMode1 %d\r\n",mainScreen);
-    else
+    //if (dMode[1]==configScreen)
+    //    fprintf(cfile,"dMode1 %d\r\n",mainScreen);
+    //else
         fprintf(cfile,"dMode1 %d\r\n",dMode[1]);
     fprintf(cfile,"ledHi %4.3f\r\n",ledHi);
     fprintf(cfile,"ledLo %4.3f\r\n",ledLo);
@@ -782,7 +783,7 @@
         f_lseek(&bfile,0xffffffff); // go to end of file to append
         strftime(sTemp, 40, "%a %m/%d/%Y %X", &t);
         f_printf(&bfile,"%s,",sTemp);
-        sprintf(sTemp,"%d,%3.1f,%d,%5.1f%%,%5.1f%%,%4.2f,%5.1f,%4.1f,%d,%d,%d,%d,%d,%4.1f,%4.1f",odo,accV,gids,(float)SOC/10, (float)SOH_x100/100,(float)Ah_x10000/10000,(float)packV_x2/2,(float)packA_x2/2,max,min,avg,max-min,jv,miles_trip[0],kWh_trip[0]);      
+        sprintf(sTemp,"%d,%3.1f,%d,%5.1f%%,%5.1f%%,%4.2f,%5.1f,%4.1f,%d,%d,%d,%d,%d,%4.1f,%4.1f",odo,accV,gids,(float)SOC/10, (float)SOH2_x100/100,(float)Ah_x10000/10000,(float)packV_x2/2,(float)packA_x2/2,max,min,avg,max-min,jv,miles_trip[0],kWh_trip[0]);      
         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]);
@@ -805,30 +806,42 @@
 //the CANary into a computer for updates.
 void updateFirmware()
 {
-    FIL sfile; // external usb file
+    FIL efile; // external usb file
     FRESULT sfr; // external file access flags
     unsigned int bytesRW;
     char sTemp[40];
     const int bufSize = 2048;
     char buffer[bufSize];
-    FILE *destFile;    
+    FILE *lfile;    
 
     // Check for config file on USB drive
-    sfr = f_open(&sfile,"CONFIG.TXT",FA_READ|FA_OPEN_EXISTING);    
+    sfr = f_open(&efile,"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))
+        lfile = fopen("/local/CONFIG.TXT", "w");
+        while (!f_eof(&efile))
         {
-            sfr=f_read(&sfile,&buffer,bufSize,&bytesRW);
-            fwrite(buffer, 1, bytesRW, destFile);
+            sfr=f_read(&efile,&buffer,bufSize,&bytesRW);
+            fwrite(buffer, 1, bytesRW, lfile);
         }
-        fflush(destFile);
-        fclose(destFile);
-        f_close(&sfile);
-    }    
-    sfr = f_open(&sfile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
+        fflush(lfile);
+        fclose(lfile);
+        f_close(&efile);
+    }else{ // No config file found so put one there
+        sfr = f_open(&efile,"CONFIG.TXT",FA_WRITE|FA_CREATE_NEW);    
+        printf("Copy config file to USB\n");
+        lfile = fopen("/local/CONFIG.TXT", "r");
+        while (!feof(lfile))
+        {
+            bytesRW=fread(buffer, 1, bufSize, lfile);
+            sfr=f_write(&efile,&buffer,bytesRW,&bytesRW);
+        }
+        fflush(lfile);
+        fclose(lfile);
+        f_close(&efile);
+    }
+    sfr = f_open(&efile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
     if(sfr != FR_OK)
     {        
         printf("Couldn't find firmware.bin\n");
@@ -875,23 +888,23 @@
     sprintf(sTemp,"/local/fw%d.bin",fwCount);
     printf("Writing %s\n",sTemp);
     wait(2);
-    destFile = fopen(sTemp, "wb");
-    if(destFile == NULL)
+    lfile = fopen(sTemp, "wb");
+    if(lfile == NULL)
     {
         printf("Couldn't Open Destination\n");
         wait(3);
         return;
     }
 
-    while (!f_eof(&sfile))
+    while (!f_eof(&efile))
     {
-        sfr=f_read(&sfile,&buffer,bufSize,&bytesRW);
-        fwrite(buffer, 1, bytesRW, destFile);
+        sfr=f_read(&efile,&buffer,bufSize,&bytesRW);
+        fwrite(buffer, 1, bytesRW, lfile);
     }
 
-    fflush(destFile);
-    fclose(destFile);
-    f_close(&sfile);
+    fflush(lfile);
+    fclose(lfile);
+    f_close(&efile);
     tt.cls();
     printf("Succesful\n\n");
     printf("Rebooting in 3 seconds\n");