Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
108:29b5a760adc2
Parent:
107:e9be732c1ad4
Child:
109:3e6f0e8fca0d
--- a/main.cpp	Sat Jun 29 02:44:02 2013 +0000
+++ b/main.cpp	Sun Jun 30 14:45:01 2013 +0000
@@ -1,5 +1,5 @@
 // main.cpp
-
+//
 //To Do:
 // * USB device detect
 // * Enable file timestamps
@@ -8,17 +8,16 @@
 // * Add 50% charge option
 // * Tire Pressure Sensor display
 // * Fix bug in playback while connected to canbus (hangs)
-// * Force regen display to zero when in neutral
+// ** Force regen display to zero when in neutral
 // * Add coasting regen to regen/braking display
 // * Change semilog efficiency graph to linear with 10 minute values
-// * Add Trip meter (kWh and efficiency for current trip)
 // * Make display updates interruptable for log writes
 // * Add additional 79b bank readouts
 // * Add ability to transfer settings config file to/from USB
 // * Add once-on-powerup/powerdown log
 // * Move log dump to ISR (but CAN RX at higher priority)
-// * Add "recording" indicator on Main
-// * Temperature table `terpolation
+// * Fix bug preventing setting logEn to true by default
+
 
 #include "mbed.h"
 #include "CAN.h"
@@ -30,7 +29,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "107";
+char revStr[7] = "108";
 
 LocalFileSystem local("local");
 
@@ -65,18 +64,19 @@
 // gg - added ZeroSecTick and revStr
 
 bool debugMode = false;
-bool logEn = false, logOpen = false; 
-bool yesBattLog = false ; // gg - Batt Log
-unsigned char tNavRow = 3 ; // gg - 4x4 touch
+bool usbEn = false;
+bool logEn = true;
+bool logOpen = false; 
+bool yesBattLog = false; // gg - Batt Log
+unsigned char tNavRow = 3; // gg - 4x4 touch
 
 FILE *hfile; // config file
 FILE *lfile; // log file
-char fileName[35] = "" ;
+char fileName[35] = "";
 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}; // 7 * 0x3D = BatDataBufMax
 unsigned char battData[BatDataBufMax]={0}; // 7 * 0x3D = BatDataBufMax
 
 unsigned char msgChanged[100]; // inidcates which bytes changed
@@ -99,8 +99,8 @@
 unsigned int fwCount=1;
 unsigned char indexOffset = 1;
 bool showCP = false;
-//bool pollCP = false;
 bool logCP = false; //Turbo3
+bool logOnce = false;
 bool repeatPoll = true;
 bool headlights = false;
 bool tick = false;
@@ -139,11 +139,10 @@
 bool updateDTE = false;
 unsigned short pointerSep;
 unsigned char reqMsgCnt = 99;
-unsigned char battTemp_x4 = 0;
 unsigned long Ah_x10000 = 0;
 unsigned long SOC_x10000 = 0;
 unsigned short SOH_x100 = 0;
-signed short battTemp_x10 = 0;
+float maxTemp = 0;
 bool metric = false;
 
 int main() {
@@ -152,7 +151,7 @@
     unsigned char i,j,display=0,lwt=0;
     point lastTouch;
     float average;
-
+    usbEn=detectUSB();
     tt.set_orientation(1);
     tt.background(Black);
     tt.set_display(2);       // select both displays
@@ -186,41 +185,6 @@
     sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
     printMsg(sTemp); // revision
 
-    // Look for new binary on thumbdrive
-    // Can't make this work right now since USB doesn't attach the right timestamp (so new binary isn't loaded)
-    /*cfile = fopen("/usb/CANary.bin", "rb");
-    lastDMode[whichTouched]=99;//force refresh
-    if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over
-        sprintf(sTemp,"New binary found.\n");
-        printMsg(sTemp); // new binary
-        lfile = fopen("/local/CANary.bin", "wb");
-        if (lfile==NULL){ //failed to open destination
-            sprintf(sTemp,"Unable to open destination file.\n");
-            printMsg(sTemp); // cannot open CANary.bin
-        } else {
-            tt.set_display(2);
-            tt.foreground(White);
-            tt.background(Black);
-            tt.cls();
-            tt.locate(1,40);
-            printf("%s\n","Copying binary - Do no remove power.");
-            tt.locate(1,80);
-            printf("CANary will reset when complete.\n");
-            wait(1); //Wait 1 sec for display DMA to finish before writing file
-            while ( int size = fread( writeBuffer, sizeof(char), maxBufLen*13, cfile )){
-                fwrite( writeBuffer, sizeof(char), size, lfile );
-                led4=led3;
-                led3=led2;
-                led2=led1;
-                led1=!led4;
-            }
-        fclose(cfile);
-        fclose(lfile);
-        remove("/usb/CANary.bin"); // delete original
-        mbed_reset(); //restart
-        }
-    }*/
-
     secsNoMsg = 0;
 
     //read efficiency history data
@@ -248,7 +212,10 @@
 
     // Read config file
     readConfig();
-
+    if (repeatPoll) { // enable autopolling if enabled
+        autoPoll.attach(&autoPollISR,pollInt);
+    }
+    
     // Start monitors
     can1.monitor(true); // set to snoop mode
     can2.monitor(true); // set to snoop mode
@@ -263,12 +230,10 @@
     secsNoTouch=2;
     while (true) {
         if (!logOpen) { // Open new file if one is not already open
-            if(logEn){ //logging enable
+            if(logEn&&usbEn){ //logging enables and USB device detected
                 seconds = time(NULL);
                 t = *localtime(&seconds) ;
                 strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
-                //sprintf(sTemp,"Using file %s\n",fileName);
-                //printMsg(sTemp); // using alc file ...
                 lfile = fopen(fileName, "ab");
                 lastDMode[0]=99;//force refresh
                 lastDMode[1]=99;//force refresh
@@ -286,11 +251,10 @@
                     logTS(); // Date Time at start
                     logEvent("Starting"); // Log startup msg for testing
                     sprintf(sTemp,"Cr%s",revStr);
-                    logEvent(sTemp); // gg - log firmware version
-                    
+                    logEvent(sTemp); // gg - log firmware version   
                     spkr.beep(2000,0.25);
                 }
-            }//logging enabled
+            }//logging enabled and USB detected
         } else { // if (logOpen)
             pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
             if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
@@ -322,6 +286,7 @@
                 fclose(lfile);
                 logOpen=false;
                 pointerSep=0;
+                led4=false;
             }
         } // if logOpen
         if (canIdle&&userIdle&&!playbackEn) { // canbus idle --> sleep to save power
@@ -618,8 +583,10 @@
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = configScreen ; // GoTo Config Screen                                   
                                 } else if (dMode[whichTouched] == tripScreen) {
-                                    miles_trip[0]=0;
-                                    kWh_trip[0]=0;
+                                    miles_trip[1]=0;
+                                    kWh_trip[1]=0;
+                                    sMode=0;
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 }                            
@@ -656,8 +623,12 @@
                                 if (dMode[whichTouched] == configScreen) {
                                     updateFirmware();
                                 } else if (dMode[whichTouched] == tripScreen) {
-                                    miles_trip[1]=0;
-                                    kWh_trip[1]=0;
+                                    miles_trip[2]=0;
+                                    kWh_trip[2]=0;
+                                    sMode=0;
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                } else if (dMode[whichTouched] == indexScreen) {
+                                    dMode[whichTouched] = tripScreen ;    
                                 } else {                             
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 }
@@ -709,10 +680,14 @@
         }
 
         if(tick){ // Executes once a second
+            tick=false;
             headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
             accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
             accOn=(accV>5)?true:false;
             if(laccOn&&!accOn){ // Car turned off
+                if (repeatPoll) { // Log on shutdown if autopoll enabled
+                    tripLog(); // Write trip log on powerdown
+                }
                 //write efficiency history data
                 hfile = fopen("/local/ehist.cny", "w");
                 if (hfile!=NULL){ // found a efficiency history file
@@ -723,8 +698,12 @@
                 }
             }
             if(!laccOn&&accOn){ // Car turned on
-                miles_trip[2]=0;
-                kWh_trip[2]=0;
+                miles_trip[0]=0;
+                kWh_trip[0]=0;
+                if (repeatPoll) { // Poll on startup if autopoll enabled
+                    logOnce=true;
+                    sendReq();
+                }
             }
             laccOn=accOn;
             if(!accOn&&!logEn&&userIdle&&!playbackEn){             
@@ -744,11 +723,11 @@
             if(mph[0]>99){
                 mph[0]=0;
             }
-            mpkWh[0]=mph[0];
             miles_trip[0]+=mph[0]/3600;
             miles_trip[1]+=mph[0]/3600;
             miles_trip[2]+=mph[0]/3600;
 
+            mpkWh[0]=mph[0];
             if(numWsamples>0){ // Avoid div0
                 kW[0]=((float) mWs_x4)/numWsamples/4e3;     
                 mpkWh[0]/=kW[0];
@@ -785,9 +764,16 @@
                }
             }
             updateDTE=true;
-            if(logCP)
+            if(logCP&&usbEn){
+                if(logOnce){
+                    tripLog();
+                    logOnce=false;
+                }
                 logPackVoltages(); // Turbo3, only call
-            tick=false;
+            }
+            if(!usbEn){
+                usbEn=detectUSB(); // Keep looking if none found
+            }
         }
 
         display=display<1?display+1:0; // toggle display