Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Files at this revision

API Documentation at this revision

Comitter:
garygid
Date:
Fri Apr 19 02:06:59 2013 +0000
Parent:
86:d1c9e8ac1c4b
Child:
88:45185a5f1c9b
Commit message:
Toggle Debug on Config Screen, Store all 5 Groups of Requested data.

Changed in this revision

common.h Show annotated file Show diff for this revision Revisions of this file
displayModes.cpp Show annotated file Show diff for this revision Revisions of this file
displayModes.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
utility.cpp Show annotated file Show diff for this revision Revisions of this file
utility.h Show annotated file Show diff for this revision Revisions of this file
--- a/common.h	Wed Apr 17 20:41:14 2013 +0000
+++ b/common.h	Fri Apr 19 02:06:59 2013 +0000
@@ -23,9 +23,17 @@
 #define maxBufLen 1024
 #define canTimeout 5
 #define userTimeout 15
-#define debugMode false
+
+#define BatDataBaseG1 0x24 
+#define BatDataBaseG2 0x00 // Cell Pair data
+#define BatDataBaseG3 0x2B 
+#define BatDataBaseG4 0x20 // Temperature data 0x20 * 7 = 224
+#define BatDataBaseG5 0x31
+#define BatDataBufMax 0x1B2 // 7 x 3E bytes
 
 // gg - 4x4 touch
 // #define tNavRow = 2 ; // for 3x3
 extern unsigned char tNavRow ; // gg - 4x4
 extern char revStr[7]; // gg - version
+
+extern bool debugMode;
--- a/displayModes.cpp	Wed Apr 17 20:41:14 2013 +0000
+++ b/displayModes.cpp	Fri Apr 19 02:06:59 2013 +0000
@@ -336,10 +336,16 @@
         }
         tt.cls();
         tt.locate(0,6);
-        printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",max,min,avg,jv,battData[224+5],battData[224+8],battData[224+11],battData[224+14]);
+        // BatDataBaseG4 * 7 = 224
+        printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
+                    max,min,avg,jv, battData[224+5],battData[224+8],battData[224+11],battData[224+14]);
         tt.locate(0,36);
         for(i=0; i<16; i++){
-            printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",i*6+1,i*6+6,(battData[i*12+3]<<8)+battData[i*12+4],(battData[i*12+5]<<8)+battData[i*12+6],(battData[i*12+7]<<8)+battData[i*12+8],(battData[i*12+9]<<8)+battData[i*12+10],(battData[i*12+11]<<8)+battData[i*12+12],(battData[i*12+13]<<8)+battData[i*12+14]);
+            printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",
+                i*6+1,i*6+6,
+                (battData[i*12+3]<<8)+battData[i*12+4],(battData[i*12+5]<<8)+battData[i*12+6],
+                (battData[i*12+7]<<8)+battData[i*12+8],(battData[i*12+9]<<8)+battData[i*12+10],
+                (battData[i*12+11]<<8)+battData[i*12+12],(battData[i*12+13]<<8)+battData[i*12+14]);
         }
         tt.rect(8+0*41,16,40+0*41,28,Green);
         tt.rect(8+1*41,16,40+1*41,28,Yellow);
@@ -480,11 +486,10 @@
         }
         
         // values, for now
+        // BatDataBaseG4 * 7 = 224
         tt.locate( 0, yWinMax+40 );
         printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
-                max,min,avg,jv,
-                battData[224+5],battData[224+8],
-                battData[224+11],battData[224+14]);
+                max,min,avg,jv,  battData[224+5],battData[224+8],  battData[224+11],battData[224+14]);
         
         // label the X axis (approximate)
         tt.locate( 2, yWinMax+5); printf("%04d", min );
@@ -604,8 +609,10 @@
         }
         
         // the values, for now
+        // BatDataBaseG4 * 7 = 224        
         tt.locate( 0, yWinMax+40 );
-        printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",max,min,avg,jv,battData[224+5],battData[224+8],battData[224+11],battData[224+14]);
+        printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
+                max,min,avg,jv,  battData[224+5],battData[224+8],  battData[224+11],battData[224+14]);
         
         //---------------
         // show the bars
@@ -687,6 +694,14 @@
         sprintf(sTemp1,"Enable");
     }
     showButton(2,1,sTemp1,"Batt Log",4,4);
+    
+    // add Enable/Disable Debug - debugMode
+    if (debugMode) {
+        sprintf(sTemp1,"Disable");
+    } else {
+        sprintf(sTemp1,"Enable");
+    }
+    showButton(3,1,sTemp1," Debug",4,4);    
 }
 
 void pbScreen(bool force, bool showButtons){
--- a/displayModes.h	Wed Apr 17 20:41:14 2013 +0000
+++ b/displayModes.h	Fri Apr 19 02:06:59 2013 +0000
@@ -15,7 +15,10 @@
 extern char displayLog[20][40];
 extern unsigned char displayLoc;
 extern char indexLastMsg[0x800];
-extern unsigned char battData[256];
+
+//extern unsigned char battData[256]; // 
+extern unsigned char battData[BatDataBufMax]; // BatDataBufMax
+
 extern bool showCP;
 extern bool logEn;
 extern bool yesBattLog; // gg - Batt Log
--- a/main.cpp	Wed Apr 17 20:41:14 2013 +0000
+++ b/main.cpp	Fri Apr 19 02:06:59 2013 +0000
@@ -51,8 +51,9 @@
 // gg - revStr is used in 2 places
 // gg - and is easy to edit here
 // gg - added ZeroSecTick and revStr
-char revStr[7] = "86";
+char revStr[7] = "87";
 
+bool debugMode = false;
 bool logEn = false, logOpen = false; 
 bool yesBattLog = false ; // gg - Batt Log
 unsigned char tNavRow = 3 ; // gg - 4x4 touch
@@ -63,7 +64,10 @@
 char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write
 char indexLastMsg[0x800]={0}; // index table for last message
 CANMessage lastMsg[100]; // table to store last message of eachtype
-unsigned char battData[256]={0};
+
+//unsigned char battData[256]={0}; // 7 * 0x3D = BatDataBufMax
+unsigned char battData[BatDataBufMax]={0}; // 7 * 0x3D = BatDataBufMax
+
 unsigned char msgChanged[100]; // inidcates which bytes changed
 char c;
 volatile int writePointer = 0;
@@ -423,13 +427,13 @@
                                     dMode[whichTouched] = brakeScreen ; // GoTo Brake Screen
                                 } else if (dMode[whichTouched]==cpScreen) {
                                     reqMsgCnt=0;
-                                    msgReq.attach(&sendReq,0.015);
+                                    msgReq.attach(&sendReq,0.025);
                                 } else if (dMode[whichTouched]==cpHistScreen) { // gg - hist
                                     reqMsgCnt=0;
-                                    msgReq.attach(&sendReq,0.015);
+                                    msgReq.attach(&sendReq,0.025);
                                 } else if (dMode[whichTouched]==cpBarScreen) { // gg - cpbars
                                     reqMsgCnt=0;
-                                    msgReq.attach(&sendReq,0.015);
+                                    msgReq.attach(&sendReq,0.025);
                                 } else if (dMode[whichTouched]==config1Screen) {
                                     mbed_reset();
                                 } else if (dMode[whichTouched]==playbackScreen) { // pause/unpause
@@ -584,30 +588,46 @@
 
                                 break;
                                 
+                            case 31: // col 3 row 1
+                                if (dMode[whichTouched]==config1Screen) { // gg - Batt Log Enable Button
+                                    debugMode = !debugMode;                                
+                                } else {
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                }                            
+                                break;
+                                
                             //-----------------------------------
                             case 02: // left col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
-                                    dMode[whichTouched] = config1Screen ; // GoTo Config Screen   
-                                }
+                                    dMode[whichTouched] = config1Screen ; // GoTo Config Screen                                   
+                                } else {
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                }            
                                 break;
                                 
                              case 12: // left-middle col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = playbackScreen ; // GoTo Playback Screen   
-                                }
+                                } else {
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                } 
                                 break;
                              
                              case 22: // right-middle col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = dateScreen ; // GoTo Set Date/Time Screen   
-                                }
+                                } else {
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                } 
                                 break;
 
                              case 32: // right col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = logScreen ; // GoTo Log Screen
                                     sMode=0;
-                                }
+                                } else {
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                } 
                                 break;
 
                             //-----------------------------------
--- a/utility.cpp	Wed Apr 17 20:41:14 2013 +0000
+++ b/utility.cpp	Fri Apr 19 02:06:59 2013 +0000
@@ -48,14 +48,16 @@
     static unsigned short nLost = 0; // gg - overrun
  
     char sTemp[40];    
-    unsigned char changed,i;
+    unsigned char changed;
+    unsigned short i; // was unsigned char
     signed short packV;
     signed short packA;
     signed long imWs_x4;
-    unsigned short ts=getTimeStamp();
+    unsigned short ts;
 
     secsNoMsg=0; // reset deadman switch
-    if(debugMode){ // code to insert actual number of dropped frames for overrun debug - skiped in normal mode to keep logcan short
+    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){
             if(canRXmsg.id>0) {
                 // check to see if buffer is already full (read - write) = 1
@@ -64,13 +66,13 @@
                 
                 //if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/16)) // modulo is slow?
      
-                // maxBufLen = 512, so pointers are 0 through 511
+                // pointers are 0 through maxBufLen-1
                 if( (readPointer - writePointer) == 1 || (writePointer - readPointer) == (maxBufLen - 1)) {
                     // the buffer is "full", so Lose this message
                     
                     // point to the last-stored message
                     int tempWritePointer = writePointer - 1 ;
-                    if( tempWritePointer == -1 ) tempWritePointer = maxBufLen - 1;
+                    if( tempWritePointer < 0 ) tempWritePointer = maxBufLen - 1;
                     char strLost[9] ;
      
                     if( nLost == 0 ) {
@@ -82,15 +84,18 @@
                         
                         // overlay the last message with a "Lost0002" comment
                         writeBuffer[tempWritePointer][0]=0;
-                        writeBuffer[tempWritePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_
-                        writeBuffer[tempWritePointer][2]=(ts&0x00ff);
+                        // leave the ts of the overlaid message
+                        //writeBuffer[tempWritePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_
+                        //writeBuffer[tempWritePointer][2]=(ts&0x00ff);
+                        // force the MsgID to an Event Message 
                         writeBuffer[tempWritePointer][3]=0xfe; // MsgID, low byte
                         writeBuffer[tempWritePointer][4]=0xff; // Len nibble, and MsgID high nibble
-                            
+                        // lay in the "Lost0002" text
                         for(i=5;i<13;i++){ 
                             writeBuffer[tempWritePointer][i]= strLost[i-5];
                         }
                     } else {
+                        // at least one message was previously lost
                         // increment the loat counter
                         nLost += 1;
                         
@@ -101,26 +106,26 @@
                         }
                     }
                 } else {
-                    // is room to insert the message
+                    // there is room to insert the message
                     // get it inserted quickly
+                    ts=getTimeStamp(); 
                     writeBuffer[writePointer][0]=mType;
                     writeBuffer[writePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_
                     writeBuffer[writePointer][2]=(ts&0x00ff);
                     writeBuffer[writePointer][3]=canRXmsg.id&0xff; // MsgID, low byte
-                    writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); // Len nibble, and MsgID high nibble
-                    for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
-                        writeBuffer[writePointer][i]=canRXmsg.data[i-5];
+                    char sLen = canRXmsg.len ;
+                    writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(sLen<<4); // Len nibble, and MsgID high nibble
+                    for(i=0;i<8;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
+                        if(i<sLen) 
+                            writeBuffer[writePointer][i+5]=canRXmsg.data[i];
+                        else // i>=sLen
+                            // force unused data bytes to FF for CAN-Do compatibility
+                            writeBuffer[writePointer][i+5]=0xFF;
                     }
+                    
                     //--------------
-                    // force unused data bytes to FF for CAN-Do compatibility - gg - force FF
-                    if(canRXmsg.len < 8){
-                        for(i=canRXmsg.len; i<8; i++) {
-                            writeBuffer[writePointer][i+5]=0xFF;
-                        }
-                    }
-                    //--------------
-                    // note, this is not protected from the interrupt
-                    // due to the nLost code above, this no longer
+                    // Note, this is not protected from the interrupt.
+                    // Due to the nLost code above, this no longer
                     //    overflows to writePointer = readPointer
                     //    which would make the buffer look empty
                     if (++writePointer >= maxBufLen) {
@@ -131,9 +136,9 @@
                     // log a local message if we had lost messages. gg - logcan
                     if( nLost > 0 ) {
                         // We previously lost messages that did not get into the buffer
-                        sprintf(sTemp,"-- Write Buffer Lost [%d]\n", nLost);
+                        sprintf(sTemp,"-- Lost %d Messages.\n", nLost);
                         logMsg(sTemp); // write buffer overrun
-                        spkr.beep(500,0.25);
+                        //spkr.beep(500,0.25);
                         
                         nLost = 0 ;
                     }
@@ -144,6 +149,7 @@
     }else{ // not debugMode - keep code short
         if(logOpen){
             if(canRXmsg.id>0) {
+                ts=getTimeStamp(); // only use
                 writeBuffer[writePointer][0]=mType;
                 writeBuffer[writePointer][1]=(ts&0xff00)>>8;
                 writeBuffer[writePointer][2]=(ts&0x00ff);
@@ -168,9 +174,22 @@
 
     if(canRXmsg.id<0x800){ // Block FFE and FFF messages
         if(indexLastMsg[canRXmsg.id]==0) { //Check if no entry
-            ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used
-            indexLastMsg[canRXmsg.id]=ii; //Create entry if first message
+            //ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used
+            if(ii<99) {
+                //indexLastMsg[canRXmsg.id]=ii; //Create entry if first message
+                indexLastMsg[canRXmsg.id]=++ii; //Create entry for first MsgID occurance
+                // ii max is 99 here
+            } else {
+                // the ii array is full, more than 100 MsgIDs found
+                if(ii==99) {
+                    ii++; // step to 100 to log only one error
+                    sprintf(sTemp,"MsgID buffer overrun.\n");
+                    logMsg(sTemp); // write buffer overrun
+                }
+            }
         }
+        
+        //----------------
         if(dMode[0]==changedScreen||dMode[1]==changedScreen){// Skip if not using (for execution speed)
             changed=msgChanged[indexLastMsg[canRXmsg.id]];
             // This is cleared in the main loop when reset button is touched
@@ -183,36 +202,59 @@
         }
     
         lastMsg[indexLastMsg[canRXmsg.id]]=canRXmsg; //Store in table
-    
+        
+        //-------------------
         //Miscellaneous on-recieve operations below
         if((mType==2)&&(canRXmsg.id==0x358)){ // headlight/turn signal indicator
             headlights = (canRXmsg.data[1]&0x80)?true:false;
+            
         }else if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data?  Need to store all responses
             if(canRXmsg.data[0]<0x20){
-                if(canRXmsg.data[3]==2){//cellpair data
-                    bdi=0;
-                    sprintf(sTemp,"Getting cell pair data\n");
+                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");
+                    logMsg(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");
                     logMsg(sTemp);
-                }else if(canRXmsg.data[3]==4){//temperature data
-                    bdi=0x20;
-                    sprintf(sTemp,"Getting temperature data\n");
+                    
+                }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");
                     logMsg(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");
+                    logMsg(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");
+                    logMsg(sTemp);
+                    
                 }else bdi=0xff; // ignore other messages (for now)
                 lasti=0;
             }
+            
             if(bdi<0xff){
                 i=canRXmsg.data[0]&0x0f; //lower nibble of D0 is index
                 if(lasti>i){ //detect rollover and offset index appropriately
-                    bdi=0x10;
+                    bdi=BatDataBaseG2 + 0x10; // only for CP data
                 }
                 lasti=i; //remember the msb to detect rollover next time around
                 i+=bdi;
-                if(i==22){
+                //-------
+                if(i==BatDataBaseG4+2){ // Last of Temperature data
                     logCP=yesBattLog; // Only log if logging enabled
                     showCP=true; // Always show
                 }
+                //-------
                 i*=7;
-                if(i<0xfa){ // Is there a better way to do this?
+                //if(i<0xfa){ // Is there a better way to do this?
+                if(i+6 < BatDataBufMax) {
                     battData[i+0]=canRXmsg.data[1];
                     battData[i+1]=canRXmsg.data[2];
                     battData[i+2]=canRXmsg.data[3];
@@ -246,6 +288,7 @@
     }
 }
 
+//-----------------------------
 void logTS () {
     CANMessage tsMsg;
     unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900
@@ -374,8 +417,9 @@
 
 void autoPollISR(){
     reqMsgCnt = 0; //reset message counter
-    msgReq.attach(&sendReq,0.015);
+    msgReq.attach(&sendReq,0.025);
 }
+
 void playbackISR() { //Used for autoplayback
     step=true;
 }
@@ -416,7 +460,8 @@
 void saveConfig(){
     FILE *cfile;
     cfile = fopen("/local/config.txt", "w");
-    fprintf(cfile,"format 3\r\n");
+    //fprintf(cfile,"format 3\r\n");
+    fprintf(cfile,"format 4\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);
@@ -440,6 +485,7 @@
     fprintf(cfile,"scale12V %4.2f\r\n",scale12V);
     fprintf(cfile,"skin %d\r\n",skin);
     fprintf(cfile,"dtePeriod %d\r\n",dtePeriod);
+    fprintf(cfile,"DebugMode %d\r\n",(debugMode?1:0));
     fclose(cfile);
 }
 
@@ -485,8 +531,15 @@
             fscanf(cfile, "skin %d\r\n", &skin ) ;
             fscanf(cfile, "dtePeriod %d\r\n", &dtePeriod ) ;
         }
+        if(ff>3){
+            int iDebug ;
+            fscanf(cfile, "DebugMode %d\r\n", &iDebug ) ;
+            debugMode = (iDebug==1)?true:false ;
+        }
         fclose(cfile);
-        if(ff<3){//If not latest format, save as latest format
+        
+        //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");
             logMsg(sTemp); // config forat updates
@@ -595,6 +648,7 @@
     if(bfile!=NULL) {
         strftime(sTemp, 40, "%a %m/%d/%Y %X", &t);
         fprintf(bfile,"%s,%d,%5.1f%%,%5.1f,%5.1f,%d,%d,%d,%d,%d",sTemp,gids,(float)SOC/10,(float)packV/2,(float)packA/2,max,min,avg,max-min,jv);
+        // BatDataBaseG4 * 7 = 224        
         fprintf(bfile,"%d,%d,%d,%d,",(battData[224+ 3]<<8)+battData[224+ 4],battData[224+ 5],(battData[224+ 6]<<8)+battData[224+ 7],battData[224+ 8]);
         fprintf(bfile,"%d,%d,%d,%d", (battData[224+ 9]<<8)+battData[224+10],battData[224+11],(battData[224+12]<<8)+battData[224+13],battData[224+14]);
         for(i=0; i<96; i++) {
--- a/utility.h	Wed Apr 17 20:41:14 2013 +0000
+++ b/utility.h	Fri Apr 19 02:06:59 2013 +0000
@@ -25,7 +25,10 @@
 extern char indexLastMsg[0x800];
 extern unsigned char dMode[2];
 extern unsigned char msgChanged[100];
-extern unsigned char battData[256];
+
+//extern unsigned char battData[256]; // 
+extern unsigned char battData[BatDataBufMax]; // BatDataBufMax
+
 extern CANMessage lastMsg[100];
 extern CAN can1,can2;
 extern DigitalOut can1SleepMode,can2SleepMode;