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:
85:5c27e88b3fbe
Parent:
83:52b1f330a62d
Child:
86:d1c9e8ac1c4b
--- a/utility.cpp	Wed Apr 17 12:05:08 2013 +0000
+++ b/utility.cpp	Wed Apr 17 12:45:13 2013 +0000
@@ -39,36 +39,130 @@
 }
 
 void logCan (char mType, CANMessage canRXmsg) {
+
+   // re-arranged to put static first
+    static unsigned char ii = 0;
+    static unsigned char lasti = 0; // indexindex
+    static unsigned char bdi=0;
+    static signed short imotorRPM = 0;
+    static unsigned short nLost = 0; // gg - overrun
+ 
     char sTemp[40];    
-    unsigned short ts = getTimeStamp();
-    static unsigned char ii = 0, lasti = 0; // indexindex
     unsigned char changed,i;
-    static unsigned char bdi=0;
     signed short packV;
     signed short packA;
-    static signed short imotorRPM = 0;
     signed long imWs_x4;
-    
+    unsigned short ts=0; //moved getTimeStamp call to inside debug loop for speed
+
     secsNoMsg=0; // reset deadman switch
-    if(logOpen){
-        if(canRXmsg.id>0) {
-            writeBuffer[writePointer][0]=mType;
-            writeBuffer[writePointer][1]=(ts&0xff00)>>8;
-            writeBuffer[writePointer][2]=(ts&0x00ff);
-            writeBuffer[writePointer][3]=canRXmsg.id&0xff;
-            writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
-            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];
+    if(debugMode){ // code to insert actual number of dropped frames for overrun debug - skiped in normal mode to keep logcan short
+        ts = getTimeStamp();
+        if(logOpen){
+            if(canRXmsg.id>0) {
+                // check to see if buffer is already full (read - write) = 1
+                // actually the last buffer location cannot be used because then 
+                //   the buffer would look empty after writePointer++
+                
+                //if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/16)) // modulo is slow?
+     
+                // maxBufLen = 512, so pointers are 0 through 511
+                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;
+                    char strLost[9] ;
+     
+                    if( nLost == 0 ) {
+                        // this is the first message lost 
+                        //   and we must overwrite the last message with an FFE comment message
+                        // So, there will be two messages lost as the comment message is laid in.
+                        nLost = 2;
+                        sprintf(strLost,"%s","Lost0002"); // indicate two messages lost
+                        
+                        // 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);
+                        writeBuffer[tempWritePointer][3]=0xfe; // MsgID, low byte
+                        writeBuffer[tempWritePointer][4]=0xff; // Len nibble, and MsgID high nibble
+                            
+                        for(i=5;i<13;i++){ 
+                            writeBuffer[tempWritePointer][i]= strLost[i-5];
+                        }
+                    } else {
+                        // increment the loat counter
+                        nLost += 1;
+                        
+                        // lay the new count into the comment
+                        sprintf(strLost,"%04d",nLost);
+                        for(i=9;i<13;i++){ 
+                            writeBuffer[tempWritePointer][i]= strLost[i-9];
+                        }
+                    }
+                } else {
+                    // is room to insert the message
+                    // get it inserted quickly
+                    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];
+                    }
+                    //--------------
+                    // 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
+                    //    overflows to writePointer = readPointer
+                    //    which would make the buffer look empty
+                    if (++writePointer >= maxBufLen) {
+                        writePointer = 0;
+                        led3 = !led3;
+                    }
+                    //--------------
+                    // 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);
+                        logMsg(sTemp); // write buffer overrun
+                        spkr.beep(500,0.25);
+                        
+                        nLost = 0 ;
+                    }
+                    //--------------
+                }
             }
-            if (++writePointer >= maxBufLen) {
-                writePointer = 0;
-                led3 = !led3;
-            }
-            if (writePointer==readPointer) {
-                // Just overwrote an entry that hasn't been sent to thumbdrive
-                sprintf(sTemp,"Write buffer overrun.\n");
-                logMsg(sTemp); // write buffer overrun
-                spkr.beep(500,0.25);
+        }
+    }else{ // not debugMode - keep code short
+        if(logOpen){
+            if(canRXmsg.id>0) {
+                writeBuffer[writePointer][0]=mType;
+                writeBuffer[writePointer][1]=(ts&0xff00)>>8;
+                writeBuffer[writePointer][2]=(ts&0x00ff);
+                writeBuffer[writePointer][3]=canRXmsg.id&0xff;
+                writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
+                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];
+                }
+                if (++writePointer >= maxBufLen) {
+                    writePointer = 0;
+                    led3 = !led3;
+                }
+                if (writePointer==readPointer) {
+                    // Just overwrote an entry that hasn't been sent to thumbdrive
+                    sprintf(sTemp,"Write buffer overrun.\n");
+                    logMsg(sTemp); // write buffer overrun
+                    spkr.beep(500,0.25);
+                }
             }
         }
     }
@@ -78,8 +172,9 @@
             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
         }
-        if(dMode[0]==changedScreen||dMode[1]==changedScreen){
+        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
             for(i=0;i<8;i++){
                 if(lastMsg[indexLastMsg[canRXmsg.id]].data[i]!=canRXmsg.data[i]){
                     changed |= 1<<i;
@@ -113,8 +208,6 @@
                 }
                 lasti=i; //remember the msb to detect rollover next time around
                 i+=bdi;
-                //if(i==22) logCP=true; //Turbo3
-                //if( (i==22) && (yesBattLog) ) logCP=true; // only if enabled gg - Batt Log 
                 if(i==22){
                     logCP=yesBattLog; // Only log if logging enabled
                     showCP=true; // Always show