Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Revision:
97:a25940fd7b5b
Parent:
96:a6c6a6fd1d28
Child:
99:c05abf8e1cdc
--- a/main.cpp	Fri May 03 14:31:07 2013 +0000
+++ b/main.cpp	Sat Jun 15 12:23:36 2013 +0000
@@ -8,7 +8,8 @@
 // * Add 50% charge option
 // * Tire Pressure Sensor display
 // * Fix bug in playback while connected to canbus (hangs)
-// * Add temperature to Main screen
+// * Fix no power displayed while charging
+// * Force regen display to zero when in neutral
 
 #include "mbed.h"
 #include "CAN.h"
@@ -51,15 +52,15 @@
 // gg - revStr is used in 2 places
 // gg - and is easy to edit here
 // gg - added ZeroSecTick and revStr
-char revStr[7] = "96";
+char revStr[7] = "97";
 
 bool debugMode = false;
 bool logEn = false, logOpen = false; 
 bool yesBattLog = false ; // gg - Batt Log
 unsigned char tNavRow = 3 ; // gg - 4x4 touch
 
-FILE *cfile;
-FILE *file;
+FILE *hfile; // config file
+FILE *lfile; // log file
 char fileName[35] = "" ;
 char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write
 char indexLastMsg[0x800]={0}; // index table for last message
@@ -173,8 +174,8 @@
     if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over
         sprintf(sTemp,"New binary found.\n");
         printMsg(sTemp); // new binary
-        file = fopen("/local/CANary.bin", "wb");
-        if (file==NULL){ //failed to open destination
+        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 {
@@ -188,14 +189,14 @@
             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, file );
+                fwrite( writeBuffer, sizeof(char), size, lfile );
                 led4=led3;
                 led3=led2;
                 led2=led1;
                 led1=!led4;
             }
         fclose(cfile);
-        fclose(file);
+        fclose(lfile);
         remove("/usb/CANary.bin"); // delete original
         mbed_reset(); //restart
         }
@@ -204,15 +205,15 @@
     secsNoMsg = 0;
 
     //read efficiency history data
-    cfile = fopen("/local/ehist.cny", "r");
-    if (cfile!=NULL){ // found a efficiency history file
+    hfile = fopen("/local/ehist.cny", "r");
+    if (hfile!=NULL){ // found a efficiency history file
         for(i=0;i<39;i++){
-            if(!feof(cfile)){
-                fscanf(cfile,"%f %f\r\n",&mph[i],&kW[i]);
+            if(!feof(hfile)){
+                fscanf(hfile,"%f %f\r\n",&mph[i],&kW[i]);
                 mpkWh[i]=mph[i]/kW[i];
             }
         }
-        fclose(cfile);
+        fclose(hfile);
         sprintf(sTemp,"History Loaded.\n");
         printMsg(sTemp); // History loaded
     } else { // create initial file
@@ -249,10 +250,10 @@
                 strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
                 //sprintf(sTemp,"Using file %s\n",fileName);
                 //printMsg(sTemp); // using alc file ...
-                file = fopen(fileName, "ab");
+                lfile = fopen(fileName, "ab");
                 lastDMode[0]=99;//force refresh
                 lastDMode[1]=99;//force refresh
-                if(file==NULL){
+                if(lfile==NULL){
                     sprintf(sTemp,"\nUnable to open %s\n\n\n\n",fileName);
                     printMsg(sTemp); // cannot open alc file
                     logEn=false;
@@ -275,7 +276,7 @@
             pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
             if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
                 // Dump buffer if > 1/16 full or canbus has stopped
-                if (file == NULL) {
+                if (lfile == NULL) {
                     logOpen = false;
                     sprintf(sTemp,"Failed to append log file.\n");
                     printMsg(sTemp); // failed to append 
@@ -284,7 +285,7 @@
                 } else {
                     while (readPointer != writePointer) {
                         for (j = 0; j<13; j++){
-                            fprintf(file,"%c",writeBuffer[readPointer][j]);
+                            fprintf(lfile,"%c",writeBuffer[readPointer][j]);
                         }
                         if(++readPointer >= maxBufLen){
                             readPointer=0;
@@ -296,7 +297,7 @@
             if (!logEn) {
                 sprintf(sTemp,"Stopping Can Log %s\n",fileName);
                 printMsg(sTemp); // stopping alc log file 
-                fclose(file);
+                fclose(lfile);
                 logOpen=false;
                 pointerSep=0;
             }
@@ -306,7 +307,7 @@
                 autoPoll.detach();
             }
             if (logOpen){
-                fclose(file);
+                fclose(lfile);
             } // if (logOpen)*/
             seconds = time(NULL);
             t = *localtime(&seconds) ;
@@ -327,6 +328,8 @@
                 //__wfi(); // freeze CPU and wait for interrupt (from canbus or touch)
                 Sleep();
             }
+            lastDMode[0]=99;
+            lastDMode[1]=99;
             secsNoTouch=2;
             canIdle=secsNoMsg>canTimeout;
             dled=0.8; // turn on display LED
@@ -338,7 +341,7 @@
                 logOpen = false; // Start new file if asleep for more than 30 minutes
                 if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter
             } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now)
-                file = fopen(fileName, "ab");
+                lfile = fopen(fileName, "ab");
                 logEvent("WakingUp"); // gg - use messeges
                 logTS(); // Date-Time at wakeup
             }
@@ -538,9 +541,9 @@
                                     // Start/stop playback
                                     if(!playbackOpen){
                                         if(!logOpen){
-                                            file = fopen("/usb/playback.alc", "rb");                                          
+                                            lfile = fopen("/usb/playback.alc", "rb");                                          
                                             lastDMode[whichTouched]=99;//force refresh
-                                            if(file==NULL){
+                                            if(lfile==NULL){
                                                 sprintf(sTemp,"Unable to open /usb/playback.alc\n");
                                                 printMsg(sTemp); // no playback.alc
                                                 spkr.beep(1000,0.25);
@@ -560,7 +563,7 @@
                                         }
                                     } else {
                                         playback.detach();
-                                        fclose(file);
+                                        fclose(lfile);
                                         playbackOpen=false;
                                         playbackEn=false;
                                         can1.attach(&recieve1);// Restore CAN data recieve
@@ -684,19 +687,16 @@
             accOn=(accV>5)?true:false;
             if(laccOn&&!accOn){ // Car turned off
                 //write efficiency history data
-                cfile = fopen("/local/ehist.cny", "w");
-                if (cfile!=NULL){ // found a efficiency history file
+                hfile = fopen("/local/ehist.cny", "w");
+                if (hfile!=NULL){ // found a efficiency history file
                     for(i=0;i<39;i++){
-                        fprintf(cfile,"%f %f\r\n",mph[i],kW[i]);
+                        fprintf(hfile,"%f %f\r\n",mph[i],kW[i]);
                     }
-                    fclose(cfile);
+                    fclose(hfile);
                 }
             }
             laccOn=accOn;
-            if(!accOn&&!logEn&&userIdle&&!playbackEn){
-                //sprintf(sTemp,"Display Off %4.2f\n",accV);
-                //printMsg(sTemp); // display off - for debug
-                
+            if(!accOn&&!logEn&&userIdle&&!playbackEn){             
                 dled = 0; // turn off display if car off and logging disabled and no user activity
             }else if(!headlights){
                 dled = ledHi;
@@ -736,8 +736,10 @@
                     mph[i]+=mph[0];
                     mpkWh[i]=average;
                     average=kW[i]/timeConstant[i];
-                    kW[i]-=average;
-                    kW[i]+=kW[0];
+                    if((mph[0]>0)||(kW[0]>0)){ //Not charging - so include in efficiency data
+                        kW[i]-=average;
+                        kW[i]+=kW[0];
+                    }
                     mpkWh[i]/=average;
                     if (mpkWh[i]<0) {
                         mpkWh[i]=99;// negative means inf.
@@ -753,23 +755,16 @@
 
         display=display<1?display+1:0; // toggle display
         updateDisplay(display);
-
-        /*if(pollCP){ // We do this inside main loop instead of ticker so CAN RX will not be blocked
-            sendCPreq(); // send cellpair data request.
-            wait_ms(16);
-            sendTreq(); //send temperature request
-            pollCP=false;
-        }*/
         
         if(step){ // playback
             if(playbackOpen&&playbackEn){
                 for(i=0;i<120;i++){
-                    if(!feof(file)){
-                        fscanf(file,"%5c%8c",&header,&data);
+                    if(!feof(lfile)){
+                        fscanf(lfile,"%5c%8c",&header,&data);
                         logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8)); // Playback
                     } else {
-                        fclose(file); // restart
-                        file = fopen("/usb/playback.alc", "rb");                                          
+                        fclose(lfile); // restart
+                        lfile = fopen("/usb/playback.alc", "rb");                                          
                         lastDMode[whichTouched]=99;//force refresh
                         spkr.beep(2000,0.25);
                     }