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:
leafman
Date:
Mon Jun 10 12:00:48 2013 +0000
Branch:
Metric
Parent:
96:a6c6a6fd1d28
Child:
99:c05abf8e1cdc
Commit message:
Metric Initial Commit

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	Fri May 03 14:31:07 2013 +0000
+++ b/common.h	Mon Jun 10 12:00:48 2013 +0000
@@ -11,7 +11,7 @@
 #define changedScreen 10
 #define playbackScreen 11
 #define dateScreen 12
-#define config1Screen 13
+#define configScreen 13
 #define indexScreen 14
 #define maxScreens 14
 
--- a/displayModes.cpp	Fri May 03 14:31:07 2013 +0000
+++ b/displayModes.cpp	Mon Jun 10 12:00:48 2013 +0000
@@ -101,8 +101,8 @@
             printf("%4.1f kWh \n",(float)(gids-5)*0.075);
             tt.set_font((unsigned char*) SCProSB31x55);
             tt.foreground(Green);
-            tt.locate(60,96);
-            printf("%4.1f mi  \n",mpkWh[dtePeriod]*((float)(gids-5)*.075));
+            tt.locate(60,96);            
+            printf("%4.1f %s  \n",convertDistance(mpkWh[dtePeriod]*((float)(gids-5)*.075)),distanceUnit()); //LM - add metric conversion
             lgids=gids;
             lmpkWh=mpkWh[dtePeriod];
             tt.foreground(Yellow);
@@ -120,7 +120,7 @@
         }
         if(force||battTemp_x4!=lbattTemp_x4){
             tt.locate(200,170);
-            printf("%4.1fF\n",(float)battTemp_x4*9/20+32);
+            printf("%4.1f%s\n",convertTemperature((float)battTemp_x4*0.25f),temperatureUnit());
             lbattTemp_x4=battTemp_x4;
         }
         if(force||accV!=laccV){
@@ -149,7 +149,7 @@
             tt.foreground(Green);
             //tt.locate(60,96);
             tt.locate(60,116); // gg - move down a little
-            printf("%4.1f mi  \n",(float)(gids-5)*0.31); // Approx for now
+            printf("%4.1f %s  \n",convertDistance((float)(gids-5)*0.31),distanceUnit()); // Approx for now - LM added metric
             lgids=gids;
             tt.foreground(Yellow);
             tt.set_font((unsigned char*) Arial28x28);
@@ -343,8 +343,10 @@
         tt.cls();
         tt.locate(0,6);
         // BatDataBaseG4 * 7 = 224
-        printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
-                    max,min,avg,jv, battData[224+5],battData[224+8],battData[224+11],battData[224+14]);
+        char* sTemperatureUnit = temperatureUnit();
+        printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %2.0f%s %2.0f%s %2.0f%s %2.0f%s\n\n",
+                    max,min,avg,jv, convertTemperature(battData[224+5]),sTemperatureUnit,convertTemperature(battData[224+8]),sTemperatureUnit,
+                    convertTemperature(battData[224+11]),sTemperatureUnit,convertTemperature(battData[224+14]),sTemperatureUnit);
         tt.locate(0,36);
         for(i=0; i<16; i++){
             printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",
@@ -403,11 +405,11 @@
         showButton(0,1," GoTo","CP Data",4,4);               
         showButton(1,1," GoTo","CP Hist",4,4);               
         showButton(2,1," GoTo","CP Bars",4,4);     
-        // bottom (not Nav) row
-        showButton(0,2," GoTo"," Config",4,4);               
-        showButton(1,2," GoTo","Playback",4,4);               
-        showButton(2,2," GoTo","Set Time",4,4);             
-        showButton(3,2," GoTo"," Log",4,4);     
+        showButton(3,1," GoTo"," Config",4,4);               
+        // bottom (not Nav) row        
+        showButton(0,2," GoTo","Playback",4,4);               
+        showButton(1,2," GoTo","Set Time",4,4);             
+        showButton(2,2," GoTo"," Log",4,4);     
     
         showCP=false;
     }
@@ -654,7 +656,7 @@
 }
 
 //---------------
-void config1(bool force, bool showButtons){
+void config(bool force, bool showButtons){
     if (force) {
         tt.background(Black);
         tt.cls();
@@ -708,6 +710,14 @@
         sprintf(sTemp1,"Enable");
     }
     showButton(3,1,sTemp1," Debug",4,4);    
+    
+    if(metric)
+        showButton(0,2,"Imperial","",4,4);
+    else
+       showButton(0,2,"Metric","",4,4);
+    
+    showButton(3,2,"Update","Firmware",4,4);
+    
 }
 
 void pbScreen(bool force, bool showButtons){
@@ -841,8 +851,8 @@
         for(i=0;i<10;i++){
             y=200-i*20;
             tt.locate(10,y-8);
-            if (showMiles){
-                printf("%3.0f\n",i*((float)(gids-5)*.075));
+            if (showMiles){                                    
+                printf("%3.0f\n",convertDistance(i*((float)(gids-5)*.075))); // LM - Added metric support
             }else{
                 printf("%d.0\n",i);
             }
@@ -872,6 +882,7 @@
         tt.foreground(Green);
         if (showMiles){
             float miles = mpkWh[dtePeriod]*((float)(gids-5)*.075);
+            miles = convertDistance(miles); // LM - Metric support
             // Right justify
             if (miles>99.9){ //space=18; num=31; . = 23
                 tt.locate(161,8);
@@ -998,8 +1009,8 @@
         case cpScreen:
             cpData(changed||showCP,(display==whichTouched));
             break;
-        case config1Screen:
-            config1(changed,(display==whichTouched));
+        case configScreen:
+            config(changed,(display==whichTouched));
             break;
         case playbackScreen:
             pbScreen(changed,(display==whichTouched));
@@ -1066,9 +1077,9 @@
                     case cpScreen:
                         sprintf(sTemp2,"CP Data");
                         break;
-                    case config1Screen:
+                    case configScreen:
                         sprintf(sTemp2," Config");
-                        break;
+                        break;          
                     case playbackScreen:
                         sprintf(sTemp2,"Playback");
                         break;
@@ -1164,6 +1175,38 @@
     printf("%s\n",text2);
 }
 
+
+//The temps are stored as metric, distances as imperial... I'm assuming the input based on that - LM
+float convertTemperature(float input) 
+{
+    if (!metric) {
+        //convert!
+        float output = input *1.8f;
+        output += 32.0f;
+        return output;   
+    }
+    return input;
+}
+float convertDistance(float input)
+{
+    if (metric) {
+        return input / 0.62137f;
+    }
+    return input;
+}
+char* distanceUnit()
+{
+    if(metric)
+        return "km";
+    return "mi";
+}
+char* temperatureUnit()
+{
+    if(metric)
+        return "C";
+    return "F";
+}
+
 //-------------
 // below is braking screen normalized to power rather than force
 // changed to force since power had too large a dynamic range
--- a/displayModes.h	Fri May 03 14:31:07 2013 +0000
+++ b/displayModes.h	Mon Jun 10 12:00:48 2013 +0000
@@ -41,6 +41,7 @@
 extern unsigned char tNavRow; // gg - 4x4
 extern unsigned short pointerSep; // log write buffer pointer separation
 extern unsigned char battTemp_x4;
+extern bool metric;
 
 extern "C" {
     void printLast (bool force, bool showButtons);
@@ -57,5 +58,8 @@
     void updateDisplay(char display);
     void showButton(unsigned char column, unsigned char row, char * text1, char * text2, unsigned char columns, unsigned char rows);
     void highlightButton(unsigned char column, unsigned char row, unsigned char tScn, unsigned char columns, unsigned char rows);
-
+    float convertDistance(float input); // LM - Metric
+    float convertTemperature(float input); // LM - Metric
+    char* distanceUnit(); // LM - Metric
+    char* temperatureUnit(); // LM - Metric
 }
\ No newline at end of file
--- a/main.cpp	Fri May 03 14:31:07 2013 +0000
+++ b/main.cpp	Mon Jun 10 12:00:48 2013 +0000
@@ -51,7 +51,7 @@
 // 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; 
@@ -85,6 +85,7 @@
 unsigned char dtMode = 6;
 char displayLog[20][40];
 unsigned char displayLoc = 0;
+unsigned int fwCount=1;
 unsigned char indexOffset = 1;
 bool showCP = false;
 //bool pollCP = false;
@@ -126,6 +127,8 @@
 unsigned short pointerSep;
 unsigned char reqMsgCnt = 99;
 unsigned char battTemp_x4 = 0;
+bool metric = false;
+
 int main() {
     char sTemp[40];
     unsigned long secs;
@@ -406,7 +409,7 @@
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = mainScreen ; // GoTo Main Screen
                                     sMode=0;
-                                } else if (dMode[whichTouched]==config1Screen) {
+                                } else if (dMode[whichTouched]==configScreen) {
                                     wait_ms(500);
                                     tt.background(Black);
                                     tt.calibrate();
@@ -437,7 +440,7 @@
                                 } else if (dMode[whichTouched]==cpBarScreen) { // gg - cpbars
                                     reqMsgCnt=0;
                                     msgReq.attach(&sendReq,0.015);
-                                } else if (dMode[whichTouched]==config1Screen) {
+                                } else if (dMode[whichTouched]==configScreen) {
                                     mbed_reset();
                                 } else if (dMode[whichTouched]==playbackScreen) { // pause/unpause
                                     playbackEn=!playbackEn;
@@ -458,7 +461,7 @@
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = effScreen ; // GoTo EFF Screen
                                     sMode=0;
-                                } else if (dMode[whichTouched]==config1Screen) {
+                                } else if (dMode[whichTouched]==configScreen) {
                                     sprintf(sTemp,"Saving config file.\n");
                                     printMsg(sTemp); // saving config
                                     saveConfig();
@@ -479,7 +482,7 @@
                                 
                             case 30: // right-most on top row
                                 
-                                if (dMode[whichTouched]==config1Screen) {
+                                if (dMode[whichTouched]==configScreen) {
                                     // step through skins
                                     if( skin < maxSkin ) skin += 1 ;
                                     else skin = 0 ;
@@ -505,7 +508,7 @@
                             //----------------------------------
                             //----------------------------------
                             case 01: // left col middle row
-                                if (dMode[whichTouched]==config1Screen) {
+                                if (dMode[whichTouched]==configScreen) {
                                     logEn = !logEn;
                                     if (!logEn) {
                                         repeatPoll=false; // disable auto polling, too
@@ -524,7 +527,7 @@
                                 break;
                             //------------------------------
                             case 11:
-                                if (dMode[whichTouched]==config1Screen){
+                                if (dMode[whichTouched]==configScreen){
                                     repeatPoll = !repeatPoll;
                                     if (repeatPoll) {
                                         autoPoll.attach(&autoPollISR,pollInt);
@@ -577,7 +580,7 @@
                                 break;
                             //---------------------------------
                             case 21: // col 2 row 1
-                                if (dMode[whichTouched]==config1Screen) { // gg - Batt Log Enable Button
+                                if (dMode[whichTouched]==configScreen) { // gg - Batt Log Enable Button
                                     yesBattLog = !yesBattLog;
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = cpBarScreen ; // GoTo CP Bars Screen  
@@ -592,8 +595,10 @@
                                 break;
                                 
                             case 31: // col 3 row 1
-                                if (dMode[whichTouched]==config1Screen) { // gg - Batt Log Enable Button
+                                if (dMode[whichTouched]==configScreen) { // gg - Batt Log Enable Button
                                     debugMode = !debugMode;                                
+                                } else if (dMode[whichTouched] == indexScreen) { // gg - index
+                                    dMode[whichTouched] = configScreen ; // GoTo Config Screen                                   
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 }                            
@@ -602,7 +607,9 @@
                             //-----------------------------------
                             case 02: // left col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
-                                    dMode[whichTouched] = config1Screen ; // GoTo Config Screen                                   
+                                    dMode[whichTouched] = playbackScreen ; // GoTo Playback Screen                                    
+                                } else if (dMode[whichTouched]==configScreen) {
+                                    metric = !metric; // toggle metric/imperial display
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 }            
@@ -610,7 +617,7 @@
                                 
                              case 12: // left-middle col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
-                                    dMode[whichTouched] = playbackScreen ; // GoTo Playback Screen   
+                                    dMode[whichTouched] = dateScreen ; // GoTo Set Date/Time Screen  
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 } 
@@ -618,19 +625,18 @@
                              
                              case 22: // right-middle col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
-                                    dMode[whichTouched] = dateScreen ; // GoTo Set Date/Time Screen   
+                                    dMode[whichTouched] = logScreen ;    
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 } 
                                 break;
 
-                             case 32: // right col, bottom row (not nav)
-                                if (dMode[whichTouched] == indexScreen) { // gg - index
-                                    dMode[whichTouched] = logScreen ; // GoTo Log Screen
-                                    sMode=0;
-                                } else {
+                             case 32: // right col, bottom row (not nav)  
+                                if (dMode[whichTouched] == configScreen) {
+                                    updateFirmware();
+                                } else {                             
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
-                                } 
+                                }
                                 break;
 
                             //-----------------------------------
--- a/utility.cpp	Fri May 03 14:31:07 2013 +0000
+++ b/utility.cpp	Mon Jun 10 12:00:48 2013 +0000
@@ -419,8 +419,8 @@
 void saveConfig(){
     FILE *cfile;
     cfile = fopen("/local/config.txt", "w");
-    //fprintf(cfile,"format 3\r\n");
-    fprintf(cfile,"format 4\r\n");
+    
+    fprintf(cfile,"format 5\r\n");
     fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
     fprintf(cfile,"y0_off %d\r\n",tt.y0_off);
     fprintf(cfile,"x0_pp %d\r\n",tt.x0_pp);
@@ -430,11 +430,11 @@
     fprintf(cfile,"x1_pp %d\r\n",tt.x1_pp);
     fprintf(cfile,"y1_pp %d\r\n",tt.y1_pp);
     fprintf(cfile,"x_mid %d\r\n",tt.x_mid);
-    if (dMode[0]==config1Screen)
+    if (dMode[0]==configScreen)
         fprintf(cfile,"dMode0 %d\r\n",mainScreen);
     else
         fprintf(cfile,"dMode0 %d\r\n",dMode[0]);
-    if (dMode[1]==config1Screen)
+    if (dMode[1]==configScreen)
         fprintf(cfile,"dMode1 %d\r\n",mainScreen);
     else
         fprintf(cfile,"dMode1 %d\r\n",dMode[1]);
@@ -445,6 +445,8 @@
     fprintf(cfile,"skin %d\r\n",skin);
     fprintf(cfile,"dtePeriod %d\r\n",dtePeriod);
     fprintf(cfile,"DebugMode %d\r\n",(debugMode?1:0));
+    fprintf(cfile,"metric %d\r\n",(metric?1:0));
+    fprintf(cfile, "firmware %d\r\n", fwCount );            
     fclose(cfile);
 }
 
@@ -468,32 +470,39 @@
         pollInt = 300;
         scale12V = 16.2;
         skin = ttSkin;
-        fscanf(cfile, "format %d\r\n", &ff ) ;
-        fscanf(cfile, "x0_off %d\r\n", &tt.x0_off ) ;
-        fscanf(cfile, "y0_off %d\r\n", &tt.y0_off ) ;
-        fscanf(cfile, "x0_pp %d\r\n", &tt.x0_pp ) ;
-        fscanf(cfile, "y0_pp %d\r\n", &tt.y0_pp ) ;
-        fscanf(cfile, "x1_off %d\r\n", &tt.x1_off ) ;
-        fscanf(cfile, "y1_off %d\r\n", &tt.y1_off ) ;
-        fscanf(cfile, "x1_pp %d\r\n", &tt.x1_pp ) ;
-        fscanf(cfile, "y1_pp %d\r\n", &tt.y1_pp ) ;
-        fscanf(cfile, "x_mid %d\r\n", &tt.x_mid ) ;
-        fscanf(cfile, "dMode0 %d\r\n", &dMode[0] ) ;
-        fscanf(cfile, "dMode1 %d\r\n", &dMode[1] ) ;
+        fscanf(cfile, "format %d\r\n", &ff );
+        fscanf(cfile, "x0_off %d\r\n", &tt.x0_off );
+        fscanf(cfile, "y0_off %d\r\n", &tt.y0_off );
+        fscanf(cfile, "x0_pp %d\r\n", &tt.x0_pp );
+        fscanf(cfile, "y0_pp %d\r\n", &tt.y0_pp );
+        fscanf(cfile, "x1_off %d\r\n", &tt.x1_off );
+        fscanf(cfile, "y1_off %d\r\n", &tt.y1_off );
+        fscanf(cfile, "x1_pp %d\r\n", &tt.x1_pp );
+        fscanf(cfile, "y1_pp %d\r\n", &tt.y1_pp );
+        fscanf(cfile, "x_mid %d\r\n", &tt.x_mid );
+        fscanf(cfile, "dMode0 %d\r\n", &dMode[0] );
+        fscanf(cfile, "dMode1 %d\r\n", &dMode[1] );
         if(ff>1){
-            fscanf(cfile, "ledHi %f\r\n", &ledHi ) ;
-            fscanf(cfile, "ledLo %f\r\n", &ledLo ) ;
-            fscanf(cfile, "pollInt %d\r\n", &pollInt ) ;
-            fscanf(cfile, "scale12V %f\r\n", &scale12V ) ;
+            fscanf(cfile, "ledHi %f\r\n", &ledHi );
+            fscanf(cfile, "ledLo %f\r\n", &ledLo );
+            fscanf(cfile, "pollInt %d\r\n", &pollInt );
+            fscanf(cfile, "scale12V %f\r\n", &scale12V );
         }
         if(ff>2){
-            fscanf(cfile, "skin %d\r\n", &skin ) ;
-            fscanf(cfile, "dtePeriod %d\r\n", &dtePeriod ) ;
+            fscanf(cfile, "skin %d\r\n", &skin );
+            fscanf(cfile, "dtePeriod %d\r\n", &dtePeriod );
         }
         if(ff>3){
-            int iDebug ;
-            fscanf(cfile, "DebugMode %d\r\n", &iDebug ) ;
-            debugMode = (iDebug==1)?true:false ;
+            int iDebug;
+            fscanf(cfile, "DebugMode %d\r\n", &iDebug );            
+            debugMode = (bool)iDebug;
+        }
+        if(ff>4) {
+            int iMetric;
+            fscanf(cfile, "metric %d\r\n", &iMetric );            
+            metric = (bool)iMetric; 
+            fscanf(cfile, "firmware %d\r\n", &iMetric );
+            fwCount = iMetric;
         }
         fclose(cfile);
         
@@ -621,3 +630,85 @@
     showCP=true;
 }
 
+
+//LM - updates firmware off a usb key, eliminating the need to plug
+//the CANary into a computer for updates.
+void updateFirmware()
+{
+    fwCount ++;
+    saveConfig();    
+    tt.cls();
+    printf("Saved Configuration\n");
+    //delete all bin files in /local
+    DIR *dir;
+    char sTemp[40];
+    struct dirent *ent;
+    printf("Starting update\n");
+    printf("deleting old firmware files\n");
+    if ((dir = opendir ("/local/")) != NULL) {
+      /* print all the files and directories within directory */      
+      while ((ent = readdir (dir)) != NULL) {
+            //printf("FILE: %s\n",ent->d_name);
+            char dest[4] = "";
+            strncat(dest, &ent->d_name[strlen(ent->d_name)-3],3);            
+            dest[0] = tolower(dest[0]);
+            dest[1] = tolower(dest[1]);
+            dest[2] = tolower(dest[2]);                        
+            if(strcmp(dest,"bin")==0)
+            {                            
+                sprintf(sTemp,"/local/%s",ent->d_name);
+                int result = remove(sTemp);       
+                printf("REMOVED: %s",ent->d_name);
+            }
+      }
+      closedir (dir);
+    } else {
+      /* could not open directory */
+        printf("Couldnt open folder");
+        wait(5);
+        return;
+    }        
+    printf("copying new firmware\n");
+    tt.cls();
+    //Copy the new firmware from usb->local
+    //The newest bin file is the one that is used by the mbed
+    const int bufSize = 2048;
+    FILE *destFile;    
+    sprintf(sTemp,"/local/fw%d.bin",fwCount);
+    printf("Writing %s\n",sTemp);
+    wait(2);
+    destFile = fopen(sTemp, "wb");
+    FILE *srcFile;    
+    srcFile = fopen("/usb/firmware.bin", "rb");    
+    if(srcFile ==NULL)
+    {        
+        sprintf(sTemp,"Couldn't find firmware.bin\n");
+        printf(sTemp);
+        wait(5);
+        return;
+    }
+    if(destFile == NULL)
+    {
+        sprintf(sTemp,"Couldn't Open Destination\n");
+        printf(sTemp);
+        wait(5);
+        return;
+    }
+    char buffer[bufSize];
+
+    while (!feof(srcFile))
+    {
+        int n = fread(buffer, 1, bufSize, srcFile);
+        fwrite(buffer, 1, n, destFile);
+    }
+
+    fflush(destFile);
+    fclose(destFile);
+    fclose(srcFile);
+    tt.cls();
+    printf("Succesful\n\n");
+    printf("Rebooting in 5 seconds\n");
+    wait(5);
+    //Now run new firmware
+    mbed_reset();
+}
\ No newline at end of file
--- a/utility.h	Fri May 03 14:31:07 2013 +0000
+++ b/utility.h	Mon Jun 10 12:00:48 2013 +0000
@@ -4,7 +4,7 @@
 #include "common.h"
 #include "TOUCH_TFTx2.h"
 #include "beep.h"
-
+#include <cctype>
 extern Timer timer;
 extern Ticker msgReq;
 extern volatile unsigned short secsNoMsg;
@@ -31,6 +31,7 @@
 
 extern CANMessage lastMsg[100];
 extern CAN can1,can2;
+extern unsigned int fwCount;
 extern DigitalOut can1SleepMode,can2SleepMode;
 //extern bool pollCP;
 extern bool tick;
@@ -54,6 +55,7 @@
 extern Beep spkr;
 extern unsigned char reqMsgCnt;
 extern unsigned char battTemp_x4;
+extern bool metric;
 
 extern "C" {
     void mbed_reset();
@@ -79,6 +81,7 @@
     void readConfig();
     void upDate(unsigned char field, bool updownbar);
     void logPackVoltages(); // Turbo3
+    void updateFirmware(); // LM - Update firmware off USB
 }
 
 //LEAF OBD