Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
98:9f8bab96edff
Parent:
95:248b3c25a7df
Child:
99:c05abf8e1cdc
--- 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