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:
8:67eed72f3e10
Parent:
7:17bf9ceaf0aa
Child:
9:c7857e87dd07
--- a/main.cpp	Fri Feb 22 23:24:07 2013 +0000
+++ b/main.cpp	Sat Feb 23 04:10:51 2013 +0000
@@ -35,7 +35,7 @@
 char writeBuffer[maxBufLen][13]; // 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[192];
+unsigned char battData[128]={0};
 unsigned char msgChanged[100]; // inidcates which bytes changed
 char c;
 volatile int writePointer = 0;
@@ -45,9 +45,8 @@
 char counter = 0;
 unsigned char dMode[2] = {4,2}; //display mode
 unsigned char sMode = 0; // setup mode
-unsigned char lastDMode[2]; //last screen mode
-unsigned char lastSMode = 0;
-char displayLog[19][40];
+unsigned char lastDMode[2] = {0,0}; //last screen mode
+char displayLog[20][40];
 unsigned char displayLoc = 0;
 unsigned char indexOffset = 1;
 unsigned char bdi; //battery data index
@@ -69,6 +68,11 @@
     NVIC_EnableIRQ( RTC_IRQn );
 }
 
+void logMsg (char *msg) {
+    strcpy(displayLog[displayLoc],msg);
+    displayLoc=displayLoc>17?0:displayLoc+1;
+}
+
 void touched(){
     LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF);
     secsNoTouch = 0;
@@ -82,14 +86,15 @@
     return ((isecs<<10)+msec) ; // return the two byte time stamp
 }
 
-void logCan (char mtype, CANMessage canRXmsg) {
+void logCan (char mType, CANMessage canRXmsg) {
+    //char sTemp[8];
     unsigned short ts = getTimeStamp();
     unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900
-    static unsigned char ii = 0, lastMSB = 0; // indexindex
+    static unsigned char ii = 0, lasti = 0; // indexindex
     unsigned char changed,i;
     if(logOpen){
         if(canRXmsg.id>0) {
-            writeBuffer[writePointer][0]=mtype;
+            writeBuffer[writePointer][0]=mType;
             writeBuffer[writePointer][1]=((secs%60)<<2)+((ts&0x300)>>8);
             writeBuffer[writePointer][2]=ts&0xff;
             writeBuffer[writePointer][3]=canRXmsg.id&0xff;
@@ -117,17 +122,18 @@
         msgChanged[indexLastMsg[canRXmsg.id]]=changed;
     }
     lastMsg[indexLastMsg[canRXmsg.id]]=canRXmsg; //Store in table
-    if(canRXmsg.id==0x7bb){ // is battery data?  Need to store all responses
-        i=canRXmsg.data[0]&0x0f; //lower nibble of D0 is index
-        if(lastMSB>0&i<8){ //detect rolloever and offset index appropriately
-            bdi+=0x10;
+    if(mType==1&&canRXmsg.id==0x7bb){ // is battery data?  Need to store all responses
+        if(canRXmsg.data[0]<0x20){
+            lasti=0;
         }
-        if(canRXmsg.data[0]&0x10>0){ //clear lastMSB if new response.  bit4 of D0 set on first message of set
-            lastMSB=0;
-        } else {
-            lastMSB=i>>3; //remember the msb to detect rollover next time around
+        i=canRXmsg.data[0]&0x0f; //lower nibble of D0 is index
+        if(lasti>i){ //detect rolloever and offset index appropriately
+            bdi=0x10;
         }
+        lasti=i; //remember the msb to detect rollover next time around
         i+=bdi;
+        //sprintf(sTemp,"d%d  ",i);
+        //logMsg(sTemp);
         i*=7;
         if(i<0xc0){
             battData[i+0]=canRXmsg.data[1];
@@ -158,22 +164,43 @@
 }
 
 void sendCPreq() {
+    char i;
+    //char sTemp[8];
     char data[8] = {0x02, 0x21, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff};
-    bdi = 0; //reset battery data table index
-    //can2.monitor(false); // set to active mode
+    can2.monitor(false); // set to active mode
     can2SleepMode = 0; // enable TX
     can2.write(CANMessage(0x79b, data, 8));
-    can2SleepMode = 1; // disable TX
     data[0]=0x30; //change to request next line message
     data[1]=0x01;
     data[2]=0x00;
-    for(int i=0;i<29;i++){
+    for(i=0;i<64;i++){
         wait_ms(16); //wait 16ms
-        can2SleepMode = 0; // enable TX
         can2.write(CANMessage(0x79b, data, 8));
-        can2SleepMode = 1; // disable TX
+        //sprintf(sTemp,"c%d ",i);
+        //logMsg(sTemp);
     }
-    //can2.monitor(true); // set to snoop mode
+    can2SleepMode = 1; // disable TX
+    can2.monitor(true); // set to snoop mode
+}
+
+void sendTreq() {
+    char i;
+    //char sTemp[8];
+    char data[8] = {0x02, 0x21, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff};
+    can2.monitor(false); // set to active mode
+    can2SleepMode = 0; // enable TX
+    can2.write(CANMessage(0x79b, data, 8));
+    data[0]=0x30; //change to request next line message
+    data[1]=0x01;
+    data[2]=0x00;
+    for(i=0;i<8;i++){
+        wait_ms(16); //wait 16ms
+        can2.write(CANMessage(0x79b, data, 8));
+        //sprintf(sTemp,"c%d ",i);
+        //logMsg(sTemp);
+    }
+    can2SleepMode = 1; // disable TX
+    can2.monitor(true); // set to snoop mode
 }
 
 void recieve1() {
@@ -194,11 +221,6 @@
     led2 = !led2;
 }
 
-void logMsg (char *msg) {
-    strcpy(displayLog[displayLoc],msg);
-    displayLoc=displayLoc>17?0:displayLoc+1;
-}
-
 void printLast (bool force){
     CANMessage msg;
     tt.locate(0,6);
@@ -248,7 +270,7 @@
     lastDisplayLoc=displayLoc;
 }
 
-void dte (bool force){
+void printDTE (bool force){
     unsigned short gids, SOC, packV;
     static unsigned short lgids=0, lSOC=0, lpackV=0;
     CANMessage msg;
@@ -270,6 +292,7 @@
         tt.locate(10,200);
         printf("%4.1f kWh\n",(float)gids*0.08);
         tt.set_font((unsigned char*) SCProSB31x55);
+        //tt.set_font((unsigned char*) Neu42x35);
         tt.foreground(Green);
         tt.locate(60,96);
         printf("%4.1f mi\n",(float)gids*0.33); // Approx for now
@@ -380,25 +403,26 @@
     tt.foreground(White);
     tt.background(Maroon);
     tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
-    tt.cls();
+    if(force) tt.cls();
     tt.locate(0,6);
     printf("cellpair data\n\n");
     if(force) tt.cls(); // Just clear screen if forced - always update display
     for(int i=0; i<16; i++){
-        printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",i*6,i*6+5,(battData[i*12+0]<<8)+battData[i*12+1],(battData[i*12+2]<<8)+battData[i*12+3],(battData[i*12+4]<<8)+battData[i*12+5],(battData[i*12+6]<<8)+battData[i*12+7],(battData[i*12+8]<<8)+battData[i*12+9],(battData[i*12+10]<<8)+battData[i*12+11]);
+        //printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",i*6,i*6+5,(battData[i*12+5]<<8)+battData[i*12+4],(battData[i*12+7]<<8)+battData[i*12+6],(battData[i*12+9]<<8)+battData[i*12+8],(battData[i*12+11]<<8)+battData[i*12+10],(battData[i*12+13]<<8)+battData[i*12+12],(battData[i*12+15]<<8)+battData[i*12+14]);
+        printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",i*6,i*6+5,(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]);
     }
 }
 
 void updateDisplay(char display){
     bool changed;
-    changed = dMode[display]!=lastDMode[display]||(lastSMode>0&&sMode==0);
+    changed = dMode[display]!=lastDMode[display];
     tt.set_display(display);
     switch (dMode[display]) {
         case logMode:
             printLog(changed);
             break;
         case dteMode:
-            dte(changed);
+            printDTE(changed);
             break;
         case brakeMode:
             braking(changed,true);
@@ -417,7 +441,7 @@
             break;
         default:
             tt.background(Black);
-            if(dMode[display]!=lastDMode[display]||(lastSMode>0&&sMode==0)) tt.cls();
+            tt.cls();
             break;
     }
     lastDMode[display]=dMode[display];
@@ -445,7 +469,6 @@
         default:
             break;
     }
-    lastSMode=sMode;
 }
 
 int main() {
@@ -455,8 +478,8 @@
     char i,j,display=0;
     point lastTouch;    
 
-    //can1.monitor(true); // set to snoop mode
-    //can2.monitor(true); // set to snoop mode
+    can1.monitor(true); // set to snoop mode
+    can2.monitor(true); // set to snoop mode
     //LPC_CAN1->MOD |= 1;          // Disble CAN controller
     //LPC_CAN1->MOD |= (1 << 1);   // Put into listen only mode
     //LPC_CAN1->MOD &= ~(1);       // Re-enable CAN controller
@@ -635,19 +658,19 @@
                             indexOffset=indexOffset<77?indexOffset+4:80;
                         }
                     } else if (dMode[i]==cpMode) {
-                        if (lastTouch.x>btn32x1 && lastTouch.x<btn32x2)
+                        if (lastTouch.x>btn32x1 && lastTouch.x<btn32x2){
+                            bdi=0;
                             sendCPreq(); // send CP data request.
-                            wait(0.1);
-                            fclose(file); // close and reopen file to force buffer empty
-                            wait(0.1);
-                            file = fopen(fileName, "ab");
-
+                        }
                     }
-                }
+                } //top of screen
             }
-            //secsNoMsg = 0;
         } else { // userIdle
-            sMode=0;
+            if(sMode==1){
+                sMode=0;
+                lastDMode[0]=99;
+                lastDMode[1]=99;
+            }
         }
         display=display<1?display+1:0; // toggle display
         updateDisplay(display);