Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Revision:
87:46ac3f2519d6
Parent:
86:d1c9e8ac1c4b
Child:
88:45185a5f1c9b
--- 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++) {