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

Branch:
Metric
Revision:
106:f016912a03db
Parent:
104:5a19101aaac5
Child:
107:e9be732c1ad4
--- a/utility.cpp	Fri Jun 28 00:13:08 2013 +0000
+++ b/utility.cpp	Fri Jun 28 20:26:11 2013 +0000
@@ -60,121 +60,117 @@
     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
-                // actually the last buffer location cannot be used because then 
-                //   the buffer would look empty after writePointer++
+            // 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?
+ 
+            // pointers are 0 through maxBufLen-1
+            if( (readPointer - writePointer) == 1 || (writePointer - readPointer) == (maxBufLen - 1)) {
+                // the buffer is "full", so Lose this message
                 
-                //if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/16)) // modulo is slow?
-     
-                // 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 < 0 ) 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
                     
-                    // point to the last-stored message
-                    int tempWritePointer = writePointer - 1 ;
-                    if( tempWritePointer < 0 ) 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;
-                        // 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;
-                        
-                        // lay the new count into the comment
-                        sprintf(strLost,"%04d",nLost);
-                        for(i=9;i<13;i++){ 
-                            writeBuffer[tempWritePointer][i]= strLost[i-9];
-                        }
+                    // overlay the last message with a "Lost0002" comment
+                    writeBuffer[tempWritePointer][0]=0;
+                    // 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 {
-                    // 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
-                    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;
-                    }
+                    // at least one message was previously lost
+                    // increment the loat counter
+                    nLost += 1;
                     
-                    //--------------
-                    // 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;
+                    // lay the new count into the comment
+                    sprintf(strLost,"%04d",nLost);
+                    for(i=9;i<13;i++){ 
+                        writeBuffer[tempWritePointer][i]= strLost[i-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,"-- Lost %d Messages.\n", nLost);
-                        printMsg(sTemp); // write buffer overrun
-                        //spkr.beep(500,0.25);
-                        
-                        nLost = 0 ;
-                    }
-                    //--------------
+                }
+            } else {
+                // 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
+                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;
                 }
+                
+                //--------------
+                // 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,"-- Lost %d Messages.\n", nLost);
+                    printMsg(sTemp); // write buffer overrun
+                    //spkr.beep(500,0.25);
+                    
+                    nLost = 0 ;
+                }
+                //--------------
             }
         }
     }else{ // not debugMode - keep code short
         if(logOpen){
-            if(canRXmsg.id>0) {
-                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
-                // another msgId<0x800 (both CANbusses are same priority)
-                if (writePointer >= maxBufLen) {
-                    writePointer = 0;
-                    led3 = !led3;
-                }
-                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;
-                writeBuffer[localWritePointer][2]=(ts&0x00ff);
-                writeBuffer[localWritePointer][3]=canRXmsg.id&0xff;
-                writeBuffer[localWritePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
-                for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[localWritePointer][5]=canRXmsg.data?)
-                    writeBuffer[localWritePointer][i]=canRXmsg.data[i-5];
-                }
-                if (writePointer==readPointer) {
-                    // Just caught up to read pointer
-                    sprintf(sTemp,"Write buffer overrun.\n");
-                    printMsg(sTemp); // write buffer overrun
-                    spkr.beep(500,0.25);
-                }
+            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
+            // another msgId<0x800 (both CANbusses are same priority)
+            if (writePointer >= maxBufLen) {
+                writePointer = 0;
+                led3 = !led3;
+            }
+            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;
+            writeBuffer[localWritePointer][2]=(ts&0x00ff);
+            writeBuffer[localWritePointer][3]=canRXmsg.id&0xff;
+            writeBuffer[localWritePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
+            for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[localWritePointer][5]=canRXmsg.data?)
+                writeBuffer[localWritePointer][i]=canRXmsg.data[i-5];
+            }
+            if (writePointer==readPointer) {
+                // Just caught up to read pointer
+                sprintf(sTemp,"Write buffer overrun.\n");
+                printMsg(sTemp); // write buffer overrun
+                spkr.beep(500,0.25);
             }
         }
     }
@@ -425,9 +421,10 @@
     can1.read(msg1);
     
     if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
-    
-    logCan(1, msg1); // EVcan Message Received
-    led1 = !led1;
+    if(msg1.id>0) {
+        logCan(1, msg1); // EVcan Message Received
+        led1 = !led1;
+    }
 }
 
 void recieve2() {
@@ -436,8 +433,10 @@
     
     if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
     
-    logCan(2, msg2); // CARcan Message Received
-    led2 = !led2;
+    if(msg2.id>0) {
+        logCan(2, msg2); // CARcan Message Received
+        led2 = !led2;
+    }
 }
 
 unsigned char buttonX(unsigned short X, unsigned char columns) {