Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed

Fork of CANary by Tick Tock

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Sun Mar 31 18:57:36 2013 +0000
Parent:
40:0e6e71a7323f
Child:
42:4533b13b297d
Commit message:
Added mpkWh display

Changed in this revision

common.h Show annotated file Show diff for this revision Revisions of this file
displayModes.cpp Show annotated file Show diff for this revision Revisions of this file
displayModes.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
utility.cpp Show annotated file Show diff for this revision Revisions of this file
utility.h Show annotated file Show diff for this revision Revisions of this file
--- a/common.h	Sun Mar 31 17:45:05 2013 +0000
+++ b/common.h	Sun Mar 31 18:57:36 2013 +0000
@@ -1,8 +1,8 @@
 #define offScreen 0
 #define logScreen 1
-#define dteScreen 2
+#define mainScreen 2
 #define brakeScreen 3
-#define testScreen 4
+#define dteScreen 4
 #define monitorScreen 5
 #define changedScreen 6
 #define cpScreen 7
--- a/displayModes.cpp	Sun Mar 31 17:45:05 2013 +0000
+++ b/displayModes.cpp	Sun Mar 31 18:57:36 2013 +0000
@@ -75,7 +75,7 @@
     lastDisplayLoc=displayLoc;
 }
 
-void printDTE (bool force){
+void mainDisplay (bool force){
     unsigned short gids, SOC, packV;
     static unsigned short lgids=0, lSOC=0, lpackV=0;
     static float lmpkWh=0, laccV=0;;
@@ -412,26 +412,20 @@
     }
 }
 
-void test(bool force){
-    unsigned char left,right;
-    CANMessage msg;
-    static unsigned char lleft=0;
-
-    msg = lastMsg[indexLastMsg[0x284]];
-    right = msg.data[7];
-    msg = lastMsg[indexLastMsg[0x280]];
-    left = msg.data[6];
-
+void dteDisplay(bool force){
+    static float lmpkWh=0;
+    static float lMPH=0;
     tt.background(Navy);
     if(force) tt.cls();
-    if(force||left!=lleft){
+    if(force||mpkWh!=lmpkWh||MPH!=lMPH){
         tt.foreground(Yellow);
         tt.set_font((unsigned char*) Arial28x28);
         tt.locate(10,200);
-        printf("%d  \n",left);
+        printf("%3.1f\n",MPH);
         tt.locate(200,200);
-        printf("%d  \n",right);
-        lleft=left;
+        printf("%3.1f\n",mpkWh);
+        lmpkWh=mpkWh;
+        lMPH=MPH;
     }
 }
 
@@ -443,14 +437,14 @@
         case logScreen:
             printLog(changed);
             break;
-        case dteScreen:
-            printDTE(changed);
+        case mainScreen:
+            mainDisplay(changed);
             break;
         case brakeScreen:
             braking(changed);
             break;
-        case testScreen:
-            test(changed);
+        case dteScreen:
+            dteDisplay(changed);
             break;
         case monitorScreen:
             printLast(changed);
@@ -485,10 +479,45 @@
             sprintf(sTemp1,"<-Prev");
             sprintf(sTemp2,"");
             showButton(0,2,sTemp1,sTemp2,3,3);
-            sprintf(sTemp1," Select %d",dMode[display]);
-            showButton(1,2,sTemp1,sTemp2,3,3);
             sprintf(sTemp1,"   Next->");
             showButton(2,2,sTemp1,sTemp2,3,3);
+            sprintf(sTemp1," Select");
+            switch (dMode[display]) {
+                case 0:
+                    sprintf(sTemp2,"  Off");
+                    break;
+                case 1:
+                    sprintf(sTemp2,"  Log");
+                    break;
+                case 2:
+                    sprintf(sTemp2,"  Main");
+                    break;
+                case 3:
+                    sprintf(sTemp2," Braking");
+                    break;
+                case 4:
+                    sprintf(sTemp2,"  DTE");
+                    break;
+                case 5:
+                    sprintf(sTemp2," Monitor");
+                    break;
+                case 6:
+                    sprintf(sTemp2,"Delta Mon");
+                    break;
+                case 7:
+                    sprintf(sTemp2,"Cell Pair");
+                    break;
+                case 8:
+                    sprintf(sTemp2," Config");
+                    break;
+                case 9:
+                    sprintf(sTemp2,"Playback");
+                    break;
+                case 10:
+                    sprintf(sTemp2,"Set Time");
+                    break;
+            }
+            showButton(1,2,sTemp1,sTemp2,3,3);
             wait_ms(100); //  pause a moment to reduce flicker
             break;
         case 2: // numpad
@@ -541,7 +570,8 @@
     tt.locate(x1+btnGap,y1+btnGap+30);
     printf("%s\n",text2);
 }
-
+// below is braking screen normalized to power rather than force
+// changed to force since power had too large a dynamic range
 /*void braking (bool force, bool prdata=false){
     unsigned long targetBraking, regenBraking, speed;
     static unsigned long maxTarget = 20000, maxRegen = 20000, tarDivReg = 1000;
--- a/displayModes.h	Sun Mar 31 17:45:05 2013 +0000
+++ b/displayModes.h	Sun Mar 31 18:57:36 2013 +0000
@@ -28,14 +28,16 @@
 extern bool playbackEn;
 extern bool playbackOpen;
 extern float playbackInt;
+extern float kWh;
 extern float mpkWh;
+extern float MPH;
 
 extern "C" {
     void printLast (bool force);
     void printChanged (bool force);
     void printLog (bool force);
-    void printDTE (bool force);
-    void test (bool force);
+    void mainDisplay (bool force);
+    void dteDisplay (bool force);
     void braking (bool force, bool prdata);
     void cpData(bool force);
     void showDateTime(bool force);
--- a/main.cpp	Sun Mar 31 17:45:05 2013 +0000
+++ b/main.cpp	Sun Mar 31 18:57:36 2013 +0000
@@ -1,14 +1,11 @@
 // main.cpp
 
 //To Do:
-// * Use timestamp for mWs but override with playbackInt based calc in playback
-// * Log file playback (to enable testing of new passive display modes without having to drive around)
 // * USB device detect
 // * Ability to update binary from the thumb-drive (requires file timestamp)
 // * Cellpair histogram
 // * Audible friction brake feedback
 // * User-configurable watchpoint
-// * Immediately turn off when car is turned off and logging
 // * Better graphical DTE display with historic efficiency information considered and displayed
 // * Add 50% charge option
 // * Tire Pressure Sensor display
@@ -67,7 +64,7 @@
 volatile bool userIdle;
 bool touched=false; //flag to read touchscreen
 char counter = 0;
-unsigned char dMode[2] = {dteScreen,brakeScreen}; //display mode
+unsigned char dMode[2] = {mainScreen,brakeScreen}; //display mode
 unsigned char sMode = 0; // setup mode
 unsigned char lastDMode[2] = {0,0}; //last screen mode
 unsigned char dtMode = 6;
@@ -87,7 +84,8 @@
 float scale12V = 16.2; // R1:R2 ratio
 signed long mWs_x4 = 0;
 unsigned short numWsamples = 0;
-unsigned long miles = 0;
+unsigned short numSsamples = 0;
+float kW = 0;
 float mpkWh = 0;
 float accV = 0;
 bool playbackEn = false;
@@ -96,6 +94,8 @@
 bool step = false;
 char header[5];
 char data[8];
+signed long motorRPM;
+float MPH = 0;
 
 int main() {
     int readPointer=0;
@@ -454,8 +454,7 @@
         }
 
         if(tick){ // Executes once a second
-            accV=floor(mon12V*scale12V*10+0.5)/10;
-            //accV=mon12V*scale12V;
+            accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
             accOn=(accV>5)?true:false;
             if(!accOn&&!logEn&&userIdle&&!playbackEn){
                 //sprintf(sTemp,"Display Off %4.2f\n",accV);
@@ -466,13 +465,18 @@
             }else{
                 dled = ledLo;
             }
-            //if(mWs_x4>0){
-            if(numWsamples>0){
-                mpkWh= ((float) mWs_x4)/numWsamples/4e3; // just kW for now               
+            if(numSsamples>0){ // Avoid div0
+                MPH=((float) motorRPM)/numSsamples/215; // Empirically derived - may change car to car
             }else{
-                mpkWh=99;
+                MPH=0;
             }
-            miles=0;
+            if(numWsamples>0){ // Avoid div0
+                kW=((float) mWs_x4)/numWsamples/4e3;     
+                mpkWh=floor(MPH*10/kW+0.5)/10; // Round to nearest 10th
+            }else{
+                kW=0;
+            }            motorRPM=0;
+            numSsamples=0;
             mWs_x4=0;
             numWsamples=0;
             if(logCP)
@@ -487,19 +491,14 @@
             sendCPreq(); // send cellpair data request.
             wait_ms(16);
             sendTreq(); //send temperature request
-            //wait_ms(16);
-            //showCP=true;
             pollCP=false;
         }
         
         if(step){ // playback
             if(playbackOpen&&playbackEn){
-                //sprintf(sTemp,"step\n");
-                //logMsg(sTemp);
                 for(i=0;i<120;i++){
                     if(!feof(file)){
                         fscanf(file,"%5c%8c",&header,&data);
-                        //pbts=(header[1]<<8)|header[2];
                         logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8));
                     }else{
                         fclose(file); // restart
--- a/utility.cpp	Sun Mar 31 17:45:05 2013 +0000
+++ b/utility.cpp	Sun Mar 31 18:57:36 2013 +0000
@@ -42,10 +42,9 @@
     static unsigned char ii = 0, lasti = 0; // indexindex
     unsigned char changed,i;
     static unsigned char bdi=0;
-    //unsigned short ms;
-    //static unsigned short lms=0;
     signed short packV;
     signed short packA;
+    static signed short imotorRPM = 0;
     signed long imWs_x4;
     secsNoMsg=0; // reset deadman switch
     if(logOpen){
@@ -78,7 +77,10 @@
         }
         msgChanged[indexLastMsg[canRXmsg.id]]=changed;
     }
+
     lastMsg[indexLastMsg[canRXmsg.id]]=canRXmsg; //Store in table
+
+    //Miscellaneous on-recieve operations below
     if((mType==2)&&(canRXmsg.id==0x358)){ // headlight/turn signal indicator
         headlights = (canRXmsg.data[1]&0x80)?true:false;
     }else if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data?  Need to store all responses
@@ -112,20 +114,21 @@
             battData[i+6]=canRXmsg.data[7];
         }
     }else if((mType==1)&&(canRXmsg.id==0x1db)){ //Battery Volts and Amps
-        //Determined 1db messages are 10ms apart so no need to compute deltaT except to be robust against missed messages
-        //ms=(ts&0xfc00)*1000+(ts&0x03ff); // convert from BCD(ish-ssssss:mmmmmmmmmm) to binary
-        //imWs_x4 = (ms+60000-lms)%60000;
         packV=((canRXmsg.data[2]<<2)|(canRXmsg.data[3]>>6)); // 1 LSB = 0.5V
         packA=((canRXmsg.data[0]<<3)|(canRXmsg.data[1]>>5)); // 1 LSB = 0.5A
         if(packA>0x03ff){
             packA|=0xf800;//extend sign;
         }
-        //mWs_x4=-packA*4000; //test
         imWs_x4 = packV; // Volts*milliSeconds*2
         imWs_x4 *= -packA; // milliWattseconds*4
-        mWs_x4 += imWs_x4; // total mWs_x4
-        numWsamples++;
-        //lms=ms;
+        if (!((imotorRPM<2)&&(imWs_x4<0))){ //Ignore if charging from wall
+            mWs_x4 += imWs_x4; // total mWs_x4
+            numWsamples++;
+        }
+    }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
+        imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
+        motorRPM+=imotorRPM;
+        numSsamples++;
     }
 }
 
@@ -230,11 +233,11 @@
     fprintf(cfile,"y1_pp %d\r\n",tt.y1_pp);
     fprintf(cfile,"x_mid %d\r\n",tt.x_mid);
     if (dMode[0]==config1Screen)
-        fprintf(cfile,"dMode0 %d\r\n",dteScreen);
+        fprintf(cfile,"dMode0 %d\r\n",mainScreen);
     else
         fprintf(cfile,"dMode0 %d\r\n",dMode[0]);
     if (dMode[1]==config1Screen)
-        fprintf(cfile,"dMode1 %d\r\n",dteScreen);
+        fprintf(cfile,"dMode1 %d\r\n",mainScreen);
     else
         fprintf(cfile,"dMode1 %d\r\n",dMode[1]);
     fprintf(cfile,"ledHi %4.3f\r\n",ledHi);
--- a/utility.h	Sun Mar 31 17:45:05 2013 +0000
+++ b/utility.h	Sun Mar 31 18:57:36 2013 +0000
@@ -40,6 +40,8 @@
 extern float playbackInt;
 extern signed long mWs_x4;
 extern unsigned short numWsamples;
+extern signed long motorRPM;
+extern unsigned short numSsamples;
 
 extern "C" {
     void mbed_reset();