Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
99:c05abf8e1cdc
Parent:
98:9f8bab96edff
Parent:
97:a25940fd7b5b
Child:
100:63a7456fc972
--- a/utility.cpp	Mon Jun 10 12:00:48 2013 +0000
+++ b/utility.cpp	Sat Jun 15 18:47:38 2013 +0000
@@ -148,7 +148,7 @@
         }
     }else{ // not debugMode - keep code short
         if(logOpen){
-            NVIC_DisableIRQ(CAN_IRQn);  // Block interrupts until write pointer assigned
+            NVIC_DisableIRQ(CAN_IRQn); // Block interrupts until write pointer assigned
             int localWritePointer = writePointer++; // create local copy to make logCan reentrant
             // note that the static variables do not prevent safe reentry
             // since they are only used for msgId<0x800 which will never interrupt
@@ -157,7 +157,7 @@
                 writePointer = 0;
                 led3 = !led3;
             }
-            NVIC_EnableIRQ(CAN_IRQn);  // Unblock interrupts once local pointer set and global pointer incremented
+            NVIC_EnableIRQ(CAN_IRQn); // Unblock interrupts once local pointer set and global pointer incremented
             ts=getTimeStamp();
             writeBuffer[localWritePointer][0]=mType;
             writeBuffer[localWritePointer][1]=(ts&0xff00)>>8;
@@ -213,28 +213,38 @@
             if(canRXmsg.data[0]<0x20){
                 if(canRXmsg.data[3]==2){//Group 2 = cellpair data
                     bdi=BatDataBaseG2; // index offset for CP data (uses 00 - 1C)
-                    sprintf(sTemp,"  Getting cell pair data\n");
-                    printMsg(sTemp);
+                    if(debugMode){
+                        sprintf(sTemp,"  Getting cell pair data\n");
+                        printMsg(sTemp);
+                    }
                     
                 }else if(canRXmsg.data[3]==4){//Group 4 = temperature data
                     bdi=BatDataBaseG4; // index offset for Temperature data (uses 20 - 22)
-                    sprintf(sTemp,"  Getting temperature data\n");
-                    printMsg(sTemp);
+                    if(debugMode){
+                        sprintf(sTemp,"  Getting temperature data\n");
+                        printMsg(sTemp);
+                    }
                     
                 }else if(canRXmsg.data[3]==1){//Group 1 data
                     bdi=BatDataBaseG1; // index offset for Group 1 data (uses 20 - 22)
-                    sprintf(sTemp,"  Getting Group 1 data\n");
-                    printMsg(sTemp);
+                    if(debugMode){
+                        sprintf(sTemp,"  Getting Group 1 data\n");
+                        printMsg(sTemp);
+                    }
                     
                 }else if(canRXmsg.data[3]==3){//Group 3 data
                     bdi=BatDataBaseG3; // index offset for Group 3 data (uses 20 - 22)
-                    sprintf(sTemp,"  Getting Group 3 data\n");
-                    printMsg(sTemp);
+                    if(debugMode){
+                        sprintf(sTemp,"  Getting Group 3 data\n");
+                        printMsg(sTemp);
+                    }
                     
                 }else if(canRXmsg.data[3]==5){//Group 5 data
                     bdi=BatDataBaseG5; // index offset for Group 5 data (uses 20 - 22)
-                    sprintf(sTemp,"  Getting Group 5 data\n");
-                    printMsg(sTemp);
+                    if(debugMode){
+                        sprintf(sTemp,"  Getting Group 5 data\n");
+                        printMsg(sTemp);
+                    }
                     
                 }else bdi=0xff; // ignore other messages (for now)
                 lasti=0;
@@ -275,10 +285,10 @@
             packA -= 1; //Slight correction to value required (unique to my Leaf?)
             imWs_x4 = packV; // Volts*milliSeconds*2
             imWs_x4 *= -packA; // milliWattseconds*4
-            if (!((imotorRPM<2)&&(imWs_x4<0))){ //Ignore if charging from wall
+            //if (!((imotorRPM<2)&&(imWs_x4<0))){ //Ignore if charging from wall
                 mWs_x4 += imWs_x4; // total mWs_x4
                 numWsamples++;
-            }
+            //}
         }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
             imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
             if(imotorRPM<0){ // take absolute value
@@ -372,9 +382,9 @@
 }
 
 void autoPollISR(){
-    char sTemp[40]; // just for debug
-    sprintf(sTemp,"Requesting cp data\n"); // just for debug
-    printMsg(sTemp); // just for debug
+    //char sTemp[40]; // just for debug
+    //sprintf(sTemp,"Requesting cp data\n"); // just for debug
+    //printMsg(sTemp); // just for debug
     reqMsgCnt = 0; //reset message counter
     msgReq.attach(&sendReq,0.015);
 }
@@ -418,8 +428,7 @@
 
 void saveConfig(){
     FILE *cfile;
-    cfile = fopen("/local/config.txt", "w");
-    
+    cfile = fopen("/local/config.txt", "w");    
     fprintf(cfile,"format 5\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
     fprintf(cfile,"y0_off %d\r\n",tt.y0_off);
@@ -505,8 +514,6 @@
             fwCount = iMetric;
         }
         fclose(cfile);
-        
-        //if(ff<3){//If not latest format, save as latest format
         if(ff<4){//If not latest format, save as latest format
             saveConfig();
             sprintf(sTemp,"Config file format updated.\n");
@@ -630,18 +637,26 @@
     showCP=true;
 }
 
-
 //LM - updates firmware off a usb key, eliminating the need to plug
 //the CANary into a computer for updates.
 void updateFirmware()
 {
+    char sTemp[40];
+    FILE *srcFile;    
+    srcFile = fopen("/usb/firmware.bin", "rb");    
+    if(srcFile ==NULL)
+    {        
+        sprintf(sTemp,"Couldn't find firmware.bin\n");
+        printf(sTemp);
+        wait(5);
+        return;
+    }
     fwCount ++;
     saveConfig();    
     tt.cls();
     printf("Saved Configuration\n");
     //delete all bin files in /local
     DIR *dir;
-    char sTemp[40];
     struct dirent *ent;
     printf("Starting update\n");
     printf("deleting old firmware files\n");
@@ -678,15 +693,6 @@
     printf("Writing %s\n",sTemp);
     wait(2);
     destFile = fopen(sTemp, "wb");
-    FILE *srcFile;    
-    srcFile = fopen("/usb/firmware.bin", "rb");    
-    if(srcFile ==NULL)
-    {        
-        sprintf(sTemp,"Couldn't find firmware.bin\n");
-        printf(sTemp);
-        wait(5);
-        return;
-    }
     if(destFile == NULL)
     {
         sprintf(sTemp,"Couldn't Open Destination\n");
@@ -711,4 +717,4 @@
     wait(5);
     //Now run new firmware
     mbed_reset();
-}
\ No newline at end of file
+}