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:
Fri Sep 27 04:43:41 2013 +0000
Parent:
149:e9739523109f
Child:
151:3047ebb3c9a8
Commit message:
// Removed air drag comp; // Added user message Id's to the config file; // Added copy of ehist.cny to USB on config save and load if present on firmware update

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	Wed Sep 25 03:33:18 2013 +0000
+++ b/common.h	Fri Sep 27 04:43:41 2013 +0000
@@ -15,7 +15,7 @@
 #define indexScreen 14
 #define tripScreen 15
 #define healthScreen 16
-#define debugScreen 17
+#define testScreen 17
 #define maxScreens 17
 
 #define btnGap 10
--- a/displayModes.cpp	Wed Sep 25 03:33:18 2013 +0000
+++ b/displayModes.cpp	Fri Sep 27 04:43:41 2013 +0000
@@ -7,7 +7,7 @@
 void mainDisplay (bool force, bool showButtons){
     unsigned short gids, SOC_x10, packV_x2, tireP;
     float useable_kWh,dte;
-    //unsigned char aTemp;
+    unsigned char aTemp;
     static unsigned short lgids=0, lSOC=0, lpackV_x2=0, ltireP=0;
     static unsigned char laTemp=0;
     static float lmaxTemp=0, lkW=0, laccV=0, lmpkWh=0;
@@ -20,7 +20,12 @@
     SOC_x10 = (msg.data[0]<<2)+(msg.data[1]>>6);
     msg = lastMsg[indexLastMsg[0x1db]]; //Get pack volts
     packV_x2 = (msg.data[2]<<2)+(msg.data[3]>>6);
-
+    msg = lastMsg[indexLastMsg[0x54c]]; //Get ambient
+    aTemp = msg.data[6]-56;
+    //msg = lastMsg[indexLastMsg[0x79a]]; //Get ambient and cabin temperature
+    //aTemp = msg.data[5]-41; // Need to add convertsion to C if metric
+    //c1Temp = msg.data[4]-41;
+    //c2Temp = msg.data[6]-41;
     msg = lastMsg[indexLastMsg[0x385]]; //Get tire pressure
     tireP = msg.data[2]+msg.data[3]+msg.data[4]+msg.data[5];
 
@@ -41,7 +46,7 @@
             printf("%4.1f kWh \n",useable_kWh);
 
             // Display DTE
-            dte=convertDistance(minTripEff*nomDrag/airDrag(ambient_F)*useable_kWh);
+            dte=convertDistance(minTripEff*useable_kWh);
             tt.foreground(Green);
             tt.locate(20,80);            
             if(dte>=9.5){
@@ -52,8 +57,7 @@
 
             tt.set_font((unsigned char*) SCProSB31x55);
             tt.foreground(Yellow);
-            //Totest:convert from 25C to current ambient temperature
-            dte=convertDistance(mpkWh[dtePeriod]);
+            dte=convertDistance(mpkWh[dtePeriod]*useable_kWh);
             if(dte>199){
                 dte=199;
             }
@@ -72,7 +76,7 @@
             tt.locate(195,106);
             printf("%s\n",distanceUnit()); //LM - add metric conversion
 
-            dte=convertDistance(maxTripEff*nomDrag/airDrag(ambient_F)*useable_kWh);
+            dte=convertDistance(maxTripEff*useable_kWh);
             tt.foreground(Orange);
             if(dte>=99.5){
                 tt.locate(255,80);            
@@ -117,11 +121,11 @@
             lpackV_x2=packV_x2;
             ltireP=0;//Force tire pressure redraw, too
         }
-        if(force||ambient_F!=laTemp){
+        if(force||aTemp!=laTemp){
             tt.foreground(Cyan);
             tt.locate(20,146);
-            printf("%2.0f%s\n",convertF(ambient_F),temperatureUnit());
-            laTemp=ambient_F;
+            printf("%2.0f%s\n",convertF(aTemp),temperatureUnit());
+            laTemp=aTemp;
         }
         if(force||maxTemp!=lmaxTemp){
             tt.foreground(Cyan);
@@ -1135,7 +1139,6 @@
         tt.set_font((unsigned char*) SCProSB31x55);
         tt.foreground(Green);
         if (showMiles){
-            //Totest:convert from 25C to current ambient temperature
             float miles = mpkWh[dtePeriod]*((float)(gids-5)*.075);
             miles = convertDistance(miles); // LM - Metric support
             // Right justify
@@ -1152,11 +1155,9 @@
             tt.foreground(Cyan);
             tt.set_font((unsigned char*) Arial24x23);
             tt.locate(198,70);
-            //Totest:convert from 25C to current ambient temperature
             printf("%3.1f \n",mpkWh[dtePeriod]);
         } else {
             tt.locate(200,10);
-            //Totest:convert from 25C to current ambient temperature
             printf("%3.1f \n",mpkWh[dtePeriod]);
             tt.foreground(Cyan);
             tt.set_font((unsigned char*) Arial24x23);
@@ -1254,7 +1255,7 @@
     }
 }
 
-void debugDisplay (bool force, bool showButtons){
+void testDisplay (bool force, bool showButtons){
     static unsigned short maxPS=0;
     unsigned char i, uData[8];
     CANMessage msg;
@@ -1271,8 +1272,7 @@
     }
     if(pointerSep>maxPS){maxPS=pointerSep;}
     tt.locate(10,10);
-    //printf("%3d sep  %3d max\n",pointerSep,maxPS);
-    printf("%f %4.3f \n", ambient_F, airDrag(ambient_F));
+    printf("%3d sep  %3d max\n",pointerSep,maxPS);
     tt.locate(10,40);
     printf("%4.2fV %4.2fV \n",accV,accV2);
     for (i=0; i<4; i++){
@@ -1335,8 +1335,8 @@
         case healthScreen:
             healthDisplay(changed,showButtons);
             break;
-        case debugScreen:
-            debugDisplay(changed,showButtons);
+        case testScreen:
+            testDisplay(changed,showButtons);
             break;
         default:
             if (changed){
@@ -1409,6 +1409,9 @@
                     case healthScreen:
                         sprintf(sTemp2,"Health");
                         break;
+                    case testScreen:
+                        sprintf(sTemp2," Test");
+                        break;
                     case indexScreen: // gg - index
                         sprintf(sTemp2," Index");
                         break;
@@ -1498,7 +1501,7 @@
 float convertC(float input) 
 {
     if (!metric) {
-        //convert C to F!
+        //convert!
         float output = input *1.8f;
         output += 32.0f;
         return output;   
@@ -1509,7 +1512,7 @@
 float convertF(float input) 
 {
     if (metric) {
-        //convert F to C!
+        //convert!
         float output = input -32.0f;
         output /= 1.8f;
         return output;   
@@ -1535,4 +1538,3 @@
         return "C";
     return "F";
 }
-
--- a/displayModes.h	Wed Sep 25 03:33:18 2013 +0000
+++ b/displayModes.h	Fri Sep 27 04:43:41 2013 +0000
@@ -63,8 +63,6 @@
 extern bool brakeMon;
 extern float curEff;
 extern unsigned short uMsgId[8];
-extern float ambient_F;
-extern float nomDrag;
 
 extern "C" {
     void printLast (bool force, bool showButtons);
@@ -84,7 +82,6 @@
     float convertDistance(float input); // LM - Metric
     float convertC(float input); // LM - Metric
     float convertF(float input); // LM - Metric
-    float airDrag(float F);
     char* distanceUnit(); // LM - Metric
     char* temperatureUnit(); // LM - Metric
 }
\ No newline at end of file
--- a/main.cpp	Wed Sep 25 03:33:18 2013 +0000
+++ b/main.cpp	Fri Sep 27 04:43:41 2013 +0000
@@ -12,11 +12,11 @@
 // * Add per-charge efficiency meter; add to triplop.txt
 // * Add tire pressure cal (40psi for me = FR 38, RR 38.2, FL 37.8, RL 38 - maybe 2psi error on my tire gauge?)
 // * Add heater activation warning message
-// * Add trip efficiency reset
-
+// * Add trip max/min efficiency reset
 
-// rev149
-// Added temperature compensation for efficiency/DTE computation
+// rev150
+// Added user message Id's to the config file
+// Added copy of ehist.cny to USB on config save and load if present on firmware update
 
 #include "mbed.h"
 #include "CAN.h"
@@ -27,7 +27,7 @@
 #include "utility.h"
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
-char revStr[7] = "149"; // gg - revision string, max 6 characters
+char revStr[7] = "150"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -133,7 +133,6 @@
 float mph[39]={0};
 float kW[39]={0};
 float mpkWh[39]={0};
-float ambient_F;
 float unloadedV_x2,Resr,curRmax,curRmin,redRmax,redRmin,incRmax,incRmin;
 signed short Imax, Imin;
 // Logarithmic division scale (roughly - snapped to common units of time)
@@ -156,8 +155,7 @@
 unsigned char saveDmode=99;
 bool moving=false;
 unsigned short chirpInt;
-unsigned short uMsgId[8] = {0x5103, 0x50a3, 0x54a4, 0x54b4, 0x54c0, 0x55b4, 0x0000, 0x0000}; // messages to display on debug screen msgId:byte
-float nomDrag;
+unsigned short uMsgId[8] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}; // messages to display on debug screen msgId:byte
 
 int main() {
     char sTemp[40];
@@ -205,9 +203,7 @@
     printMsg(sTemp); // revision
 
     //read efficiency history data
-    if(!tt.is_touched()){  //skip if screen touched (reset efficiency)
-        hfile = fopen("/local/ehist.cny", "r");
-    }
+    hfile = fopen("/local/ehist.cny", "r");
     if (hfile!=NULL){ // found a efficiency history file
         for(i=0;i<39;i++){
             if(!feof(hfile)){
@@ -229,8 +225,6 @@
             mpkWh[i]=4;
         }
     }
-    
-    nomDrag = airDrag(77); // Compute drag coefficient for 25C
 
     // Read config file
     readConfig();
@@ -713,7 +707,6 @@
 
         if(tick){ // Executes once a second
             tick=false;
-            ambient_F = lastMsg[indexLastMsg[0x54c]].data[6]-56;
             curEff = miles_trip[0]/kWh_trip[0];
             headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
             if(accOn&&indexLastMsg[0x355]>0){
@@ -810,7 +803,7 @@
 
             if(numWsamples>0){ // Avoid div0
                 mpkWh[0]=mph[0];
-                kW[0]=((float) mWs_x4)/numWsamples/4e3;
+                kW[0]=((float) mWs_x4)/numWsamples/4e3;     
                 mpkWh[0]/=kW[0];
                 if (mpkWh[0]<0) {
                     mpkWh[0]=99;// negative means inf.
@@ -819,8 +812,6 @@
                 kW[0]=0;
                 mpkWh[0]=0;
             }
-            // TOTest: Normalize kW[0] to 25C (after computing mpkWh[0])
-            kW[0]=kW[0]/airDrag(ambient_F)*nomDrag;
             numWsamples=0;
 
             if (!charging){
--- a/utility.cpp	Wed Sep 25 03:33:18 2013 +0000
+++ b/utility.cpp	Fri Sep 27 04:43:41 2013 +0000
@@ -508,14 +508,15 @@
 }
 
 void saveConfig(){
-    printMsg("Saving local config file.\n"); // no config file
     FILE *cfile;
     FIL efile; // external usb file
     FRESULT sfr; // external file access flags
     unsigned int bytesRW;
     const int bufSize = 2048;
     char buffer[bufSize];
-
+    char sTemp[16];
+    printMsg("Saving local config file.\n");
+    wait(2);
     cfile = fopen("/local/config.txt", "w");    
     fprintf(cfile,"format 8\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
@@ -547,13 +548,16 @@
     fprintf(cfile,"showHealth %d\r\n",(showHealth?1:0));
     fprintf(cfile,"brakeMon %d\r\n",(brakeMon?1:0));
     fprintf(cfile,"brkMonRate %d\r\n", brkMonRate );            
-    fprintf(cfile,"brkMonThr %d\r\n", brkMonThr );
+    fprintf(cfile,"brkMonThr %d\r\n", brkMonThr );            
     for(char i=0;i<8;i++){
-        fprintf(cfile,"usrMsgId %4x\r\n", i, uMsgId[i] );
+        sprintf(sTemp,"usrMsgId %04x",uMsgId[i]);
+        fprintf(cfile,"%s\r\n", sTemp );
     }
     fclose(cfile);
     
     // Make copy of CONFIG.TXT
+    printMsg("Saving CONFIG.BAK.\n");
+    wait(2);
     cfile = fopen("/local/CONFIG.TXT", "r");
     sfr = f_open(&efile,"CONFIG.BAK",FA_WRITE|FA_CREATE_NEW);    
     if((cfile != NULL)&&(sfr == FR_OK)){
@@ -567,12 +571,30 @@
         fclose(cfile);
         f_close(&efile);
     }
+
+    // Make copy of ehist.cny
+    printMsg("Saving ehist.bak.\n");
+    wait(2);
+    cfile = fopen("/local/ehist.cny", "r");
+    sfr = f_open(&efile,"ehist.bak",FA_WRITE|FA_CREATE_NEW);    
+    if((cfile != NULL)&&(sfr == FR_OK)){
+        printMsg("Copy config file to USB\n");
+        while (!feof(cfile))
+        {
+            bytesRW=fread(buffer, 1, bufSize, cfile);
+            sfr=f_write(&efile,&buffer,bytesRW,&bytesRW);
+        }
+        fflush(cfile);
+        fclose(cfile);
+        f_close(&efile);
+    }
     wait(2);
 }
 
 void readConfig(){
     FILE *cfile;
     int ff;
+    char sTemp[16];
 
     cfile = fopen("/local/config.txt", "r");
     if (cfile==NULL){ // if doesn't exist --> create
@@ -584,7 +606,7 @@
         saveConfig();
     } else {
         ledHi = 0.8;
-        ledLo = 0.1;
+        ledLo = 0.3;
         pollInt = 60;
         scale12V = 16.2;
         skin = ttSkin;
@@ -636,7 +658,8 @@
         }
         if(ff>7){
             for(char i=0;i<8;i++){
-                fscanf(cfile, "usrMsgId %4x\r\n", &uMsgId[i] );        
+                fscanf(cfile, "usrMsgId %s\r\n", &sTemp );
+                sscanf(sTemp,"%4x", &uMsgId[i]);
             }
         }
         fclose(cfile);
@@ -657,7 +680,7 @@
             dMode[0]=4;
             dMode[1]=2;
             ledHi=0.800;
-            ledLo=0.100;
+            ledLo=0.300;
             pollInt=300;
             scale12V=16.20;
             skin=0;
@@ -678,8 +701,7 @@
             uMsgId[6]=0x0000;
             uMsgId[7]=0x0000;
             }
-
-        if(ff<7){//If not latest format, save as latest format
+        if(ff<8){//If not latest format, save as latest format
             saveConfig();
             printMsg("Config file format updated.\n"); // config forat updates
         }
@@ -862,12 +884,6 @@
             bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
             f_printf(&bfile,",%d",bd);
         }
-        // temporariliy dump everything
-        /*for(i=0; i<BatDataBufMax; i++) {
-            sprintf(sTemp,",%02x",battData[i]);
-            f_printf(&bfile,"%s",sTemp);
-        }*/
-
         f_printf(&bfile,"\r\n");
         f_close(&bfile);
     }
@@ -901,10 +917,32 @@
             fclose(lfile);
         }
         f_close(&efile);
+        int fwc_tmp = fwCount;
         readConfig();
+        fwCount = fwc_tmp; // Do no overwrite fwcount when loading new config
         wait(2);
     }
 
+    // Check for history file on USB drive
+    sfr = f_open(&efile,"ehist.cny",FA_READ|FA_OPEN_EXISTING);    
+    if(sfr == FR_OK)
+    {        
+        printf("Copy ehist file from USB\n");
+        lfile = fopen("/local/ehist.cny", "w");
+        if(lfile != NULL) {
+            while (!f_eof(&efile))
+            {
+                sfr=f_read(&efile,&buffer,bufSize,&bytesRW);
+                fwrite(buffer, 1, bytesRW, lfile);
+            }
+            fflush(lfile);
+            fclose(lfile);
+        }
+        f_close(&efile);
+        wait(2);
+    }
+
+
     sfr = f_open(&efile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);    
     if(sfr != FR_OK)
     {        
@@ -998,20 +1036,6 @@
     }
 }
 
-float airDrag(float F)
-{
-    float drag_F[16] = {1.47,1.39,1.363,1.337,1.312,1.288,1.265,1.243,1.221,1.2,1.18,1.161,1.142,1.124,1.106,1.034};
-    float temp_F[16] = {-31,-4,5,14,23,32,41,50,59,68,77,86,95,104,113,149};
-    char ii=0;
-    float drag;
-    while(ambient_F<=temp_F[++ii]) { } // Find section in table
-    drag=(ambient_F-temp_F[ii]);
-    drag/=(temp_F[ii-1]-temp_F[ii]);
-    drag*=(drag_F[ii-1]-drag_F[ii]);
-    drag+=drag_F[ii];
-    return drag;
-}
-
 //Sample CONFIG.TXT
 /*
 format 7
@@ -1027,7 +1051,7 @@
 dMode0 4
 dMode1 2
 ledHi 0.800
-ledLo 0.100
+ledLo 0.300
 pollInt 300
 scale12V 16.20
 skin 0
@@ -1039,4 +1063,12 @@
 brakeMon 1
 brkMonRate 400000
 brkMonThr 4000
-*/
+usrMsgId 5103
+usrMsgId 50a3
+usrMsgId 54a4
+usrMsgId 54b4
+usrMsgId 54c0
+usrMsgId 55b4
+usrMsgId 0000
+usrMsgId 0000
+*/
\ No newline at end of file
--- a/utility.h	Wed Sep 25 03:33:18 2013 +0000
+++ b/utility.h	Fri Sep 27 04:43:41 2013 +0000
@@ -77,7 +77,6 @@
 extern Ticker geiger;
 extern unsigned short chirpInt;
 extern unsigned short uMsgId[8];
-extern float ambient_F;
 
 extern "C" {
     void mbed_reset();
@@ -106,7 +105,6 @@
     void updateFirmware(); // LM - Update firmware off USB
     bool detectUSB();
     void chirp();
-    float airDrag(float);
 }
 
 //LEAF OBD