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 Jun 30 14:45:01 2013 +0000
Branch:
Metric
Parent:
107:e9be732c1ad4
Child:
109:3e6f0e8fca0d
Commit message:
Fixed temperature bug but USB logging is disabled

Changed in this revision

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/displayModes.cpp	Sat Jun 29 02:44:02 2013 +0000
+++ b/displayModes.cpp	Sun Jun 30 14:45:01 2013 +0000
@@ -80,22 +80,24 @@
     if(force||lmiles!=miles_trip[0]){ //only update if changed
         tt.foreground(Navy);
         tt.set_font((unsigned char*) Arial28x28);
-        tt.locate(1,10);
+        tt.locate(6,210);
         printf("kWh : %s : Eff\n",distanceUnit());
         for(int i=0; i<3; i++){
-            tt.locate(1,60+i*40);
-            printf("%3.2f : %3.1f : %2.1f\n",kWh_trip[i],convertDistance(miles_trip[i]),convertDistance(miles_trip[i])/kWh_trip[i]);
+            tt.locate(6,20+i*60);
+            printf("%3.2f : %3.1f : %2.1f \n",kWh_trip[i],convertDistance(miles_trip[i]),convertDistance(miles_trip[i])/kWh_trip[i]);
         }
         tt.set_font((unsigned char*) Arial12x12);
-        showButton(3,1," Reset","",4,4);
-        showButton(3,2," Reset","",4,4);
+        tt.background(DarkCyan);
+        tt.foreground(Yellow);
+        showButton(3,1," Reset","   A",4,4);
+        showButton(3,2," Reset","   B",4,4);
     }
 }
 
 void mainDisplay (bool force, bool showButtons){
     unsigned short gids, SOC, packV;
     static unsigned short lgids=0, lSOC=0, lSOH=0, lpackV=0, maxPS=0;
-    static signed short lbattTemp_x10=0;
+    static float lmaxTemp=0;
     static float lkW=0, laccV=0, lmpkWh=0;
     static unsigned long lAh=0;
     CANMessage msg;
@@ -141,13 +143,10 @@
             printf("%4.1fV \n",(float)packV/2);
             lpackV=packV;
         }
-        //if(force||battTemp_x4!=lbattTemp_x4){
-        if(force||battTemp_x10!=lbattTemp_x10){
+        if(force||maxTemp!=lmaxTemp){
             tt.locate(200,170);
-            //printf("%4.1f%s\n",convertTemperature((float)battTemp_x4*0.25f),temperatureUnit());
-            printf("%4.1f%s\n",convertTemperature((float)battTemp_x10*0.1f),temperatureUnit());
-            //lbattTemp_x4=battTemp_x4;
-            lbattTemp_x10=battTemp_x10;
+            printf("%4.1f%s\n",convertTemperature(maxTemp),temperatureUnit());
+            lmaxTemp=maxTemp;
         }
         if(force||accV!=laccV){
             tt.locate(20,200);
@@ -212,7 +211,7 @@
             lkW=kW[0];
         }
     }
-    if(logEn){
+    if(led4){
         tt.fillcircle(310,10,6,Red);
     }else{
         tt.fillcircle(310,10,6,Navy);
@@ -313,6 +312,10 @@
     msg = lastMsg[indexLastMsg[0x1cb]]; //Get Target and Regen
     regenBraking = (msg.data[0]<<3)+(msg.data[1]>>5);
     targetBraking = (msg.data[2]<<3)+(msg.data[3]>>5);
+    msg = lastMsg[indexLastMsg[0x176]]; //Get Drive Mode
+    if (msg.data[3]==0xAA) { // In Park or Neutral
+        regenBraking = 0;  // No regen when in Neutral
+    }
 
     if (targetBraking<2045){
         if ((targetBraking>50)&&(regenBraking>50)){
@@ -394,8 +397,6 @@
                 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[BatDataBaseG4*7+5]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+8]),sTemperatureUnit,
                     convertTemperature(battData[BatDataBaseG4*7+11]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+14]),sTemperatureUnit);
-                //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[BatDataBaseG4*7+5],battData[BatDataBaseG4*7+8],battData[BatDataBaseG4*7+11],battData[BatDataBaseG4*7+14]);
                 tt.rect(8+0*41,16,40+0*41,28,Green);
                 tt.rect(8+1*41,16,40+1*41,28,Yellow);
                 //tt.rect(8+2*41,16,40+2*41,28,White);
@@ -465,6 +466,7 @@
         showButton(0,2," GoTo","Playback",4,4);               
         showButton(1,2," GoTo","Set Time",4,4);             
         showButton(2,2," GoTo"," Log",4,4);     
+        showButton(3,2," GoTo"," Trip",4,4);     
     
         showCP=false;
     }
@@ -551,8 +553,12 @@
         // values, for now
         // BatDataBaseG4 * 7 = 280
         tt.locate( 0, yWinMax+40 );
-        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[BatDataBaseG4*7+5],battData[BatDataBaseG4*7+8],  battData[BatDataBaseG4*7+11],battData[BatDataBaseG4*7+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[BatDataBaseG4*7+5]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+8]),sTemperatureUnit,
+            convertTemperature(battData[BatDataBaseG4*7+11]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+14]),sTemperatureUnit);
+        //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[BatDataBaseG4*7+5],battData[BatDataBaseG4*7+8],  battData[BatDataBaseG4*7+11],battData[BatDataBaseG4*7+14]);
         
         // label the X axis (approximate)
         tt.locate( 2, yWinMax+5); printf("%04d", min );
@@ -674,8 +680,12 @@
         // the values, for now
         // BatDataBaseG4 * 7 = 280        
         tt.locate( 0, yWinMax+40 );
-        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[BatDataBaseG2*7+BatDataBaseG4*7+5],battData[BatDataBaseG2*7+BatDataBaseG4*7+8],  battData[BatDataBaseG2*7+BatDataBaseG4*7+11],battData[BatDataBaseG2*7+BatDataBaseG4*7+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[BatDataBaseG4*7+5]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+8]),sTemperatureUnit,
+            convertTemperature(battData[BatDataBaseG4*7+11]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+14]),sTemperatureUnit);
+        //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[BatDataBaseG2*7+BatDataBaseG4*7+5],battData[BatDataBaseG2*7+BatDataBaseG4*7+8],  battData[BatDataBaseG2*7+BatDataBaseG4*7+11],battData[BatDataBaseG2*7+BatDataBaseG4*7+14]);
         
         //---------------
         // show the bars
@@ -736,7 +746,7 @@
     showButton(3,0," Use",sTemp1,4,4);
     
     //------- second row -----
-    if (logEn) {
+    if (logEn&&usbEn) {
         sprintf(sTemp1,"Disable");
     } else {
         sprintf(sTemp1,"Enable");
@@ -1016,6 +1026,10 @@
     msg = lastMsg[indexLastMsg[0x1cb]]; //Get Target and Regen
     regenBraking = (msg.data[0]<<3)+(msg.data[1]>>5);
     targetBraking = (msg.data[2]<<3)+(msg.data[3]>>5);
+    msg = lastMsg[indexLastMsg[0x176]]; //Get Drive Mode
+    if (msg.data[3]==0xAA) { // In Park or Neutral
+        regenBraking = 0;  // No regen when in Neutral
+    }
 
     if (targetBraking<2045){
         if ((targetBraking>50)&&(regenBraking>50)){
@@ -1298,6 +1312,10 @@
     msg = lastMsg[indexLastMsg[0x1cb]]; //Get Target and Regen
     regenBraking = (msg.data[0]<<3)+(msg.data[1]>>5);
     targetBraking = (msg.data[2]<<3)+(msg.data[3]>>5);
+    msg = lastMsg[indexLastMsg[0x176]]; //Get Drive Mode
+    if (msg.data[3]==0xAA) { // In Park or Neutral
+        regenBraking = 0;  // No regen when in Neutral
+    }
     msg = lastMsg[indexLastMsg[0x176]]; //Get rpms - not sure what this is but scales to mph with .0725
     rpm = ((short)msg.data[0]<<8)+msg.data[1];
     speed =rpm>0?rpm>>3:-rpm>>3; //Take absolute to get speed; div8
--- a/displayModes.h	Sat Jun 29 02:44:02 2013 +0000
+++ b/displayModes.h	Sun Jun 30 14:45:01 2013 +0000
@@ -21,6 +21,7 @@
 
 extern bool showCP;
 extern bool logEn;
+extern bool usbEn;
 extern bool yesBattLog; // gg - Batt Log
 extern bool repeatPoll;
 extern unsigned char dMode[2];
@@ -40,10 +41,10 @@
 extern unsigned char skin;
 extern unsigned char dtePeriod;
 extern bool updateDTE;
+extern DigitalOut led4;
 extern unsigned char tNavRow; // gg - 4x4
 extern unsigned short pointerSep; // log write buffer pointer separation
-extern unsigned char battTemp_x4;
-extern signed short battTemp_x10;
+extern float maxTemp;
 extern unsigned long Ah_x10000;
 extern unsigned long SOC_x10000;
 extern unsigned short SOH_x100;
--- a/main.cpp	Sat Jun 29 02:44:02 2013 +0000
+++ b/main.cpp	Sun Jun 30 14:45:01 2013 +0000
@@ -1,5 +1,5 @@
 // main.cpp
-
+//
 //To Do:
 // * USB device detect
 // * Enable file timestamps
@@ -8,17 +8,16 @@
 // * Add 50% charge option
 // * Tire Pressure Sensor display
 // * Fix bug in playback while connected to canbus (hangs)
-// * Force regen display to zero when in neutral
+// ** Force regen display to zero when in neutral
 // * Add coasting regen to regen/braking display
 // * Change semilog efficiency graph to linear with 10 minute values
-// * Add Trip meter (kWh and efficiency for current trip)
 // * Make display updates interruptable for log writes
 // * Add additional 79b bank readouts
 // * Add ability to transfer settings config file to/from USB
 // * Add once-on-powerup/powerdown log
 // * Move log dump to ISR (but CAN RX at higher priority)
-// * Add "recording" indicator on Main
-// * Temperature table `terpolation
+// * Fix bug preventing setting logEn to true by default
+
 
 #include "mbed.h"
 #include "CAN.h"
@@ -30,7 +29,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "107";
+char revStr[7] = "108";
 
 LocalFileSystem local("local");
 
@@ -65,18 +64,19 @@
 // gg - added ZeroSecTick and revStr
 
 bool debugMode = false;
-bool logEn = false, logOpen = false; 
-bool yesBattLog = false ; // gg - Batt Log
-unsigned char tNavRow = 3 ; // gg - 4x4 touch
+bool usbEn = false;
+bool logEn = true;
+bool logOpen = false; 
+bool yesBattLog = false; // gg - Batt Log
+unsigned char tNavRow = 3; // gg - 4x4 touch
 
 FILE *hfile; // config file
 FILE *lfile; // log file
-char fileName[35] = "" ;
+char fileName[35] = "";
 char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write
 char indexLastMsg[0x800]={0}; // index table for last message
 CANMessage lastMsg[100]; // table to store last message of eachtype
 
-//unsigned char battData[256]={0}; // 7 * 0x3D = BatDataBufMax
 unsigned char battData[BatDataBufMax]={0}; // 7 * 0x3D = BatDataBufMax
 
 unsigned char msgChanged[100]; // inidcates which bytes changed
@@ -99,8 +99,8 @@
 unsigned int fwCount=1;
 unsigned char indexOffset = 1;
 bool showCP = false;
-//bool pollCP = false;
 bool logCP = false; //Turbo3
+bool logOnce = false;
 bool repeatPoll = true;
 bool headlights = false;
 bool tick = false;
@@ -139,11 +139,10 @@
 bool updateDTE = false;
 unsigned short pointerSep;
 unsigned char reqMsgCnt = 99;
-unsigned char battTemp_x4 = 0;
 unsigned long Ah_x10000 = 0;
 unsigned long SOC_x10000 = 0;
 unsigned short SOH_x100 = 0;
-signed short battTemp_x10 = 0;
+float maxTemp = 0;
 bool metric = false;
 
 int main() {
@@ -152,7 +151,7 @@
     unsigned char i,j,display=0,lwt=0;
     point lastTouch;
     float average;
-
+    usbEn=detectUSB();
     tt.set_orientation(1);
     tt.background(Black);
     tt.set_display(2);       // select both displays
@@ -186,41 +185,6 @@
     sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
     printMsg(sTemp); // revision
 
-    // Look for new binary on thumbdrive
-    // Can't make this work right now since USB doesn't attach the right timestamp (so new binary isn't loaded)
-    /*cfile = fopen("/usb/CANary.bin", "rb");
-    lastDMode[whichTouched]=99;//force refresh
-    if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over
-        sprintf(sTemp,"New binary found.\n");
-        printMsg(sTemp); // new binary
-        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 {
-            tt.set_display(2);
-            tt.foreground(White);
-            tt.background(Black);
-            tt.cls();
-            tt.locate(1,40);
-            printf("%s\n","Copying binary - Do no remove power.");
-            tt.locate(1,80);
-            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, lfile );
-                led4=led3;
-                led3=led2;
-                led2=led1;
-                led1=!led4;
-            }
-        fclose(cfile);
-        fclose(lfile);
-        remove("/usb/CANary.bin"); // delete original
-        mbed_reset(); //restart
-        }
-    }*/
-
     secsNoMsg = 0;
 
     //read efficiency history data
@@ -248,7 +212,10 @@
 
     // Read config file
     readConfig();
-
+    if (repeatPoll) { // enable autopolling if enabled
+        autoPoll.attach(&autoPollISR,pollInt);
+    }
+    
     // Start monitors
     can1.monitor(true); // set to snoop mode
     can2.monitor(true); // set to snoop mode
@@ -263,12 +230,10 @@
     secsNoTouch=2;
     while (true) {
         if (!logOpen) { // Open new file if one is not already open
-            if(logEn){ //logging enable
+            if(logEn&&usbEn){ //logging enables and USB device detected
                 seconds = time(NULL);
                 t = *localtime(&seconds) ;
                 strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
-                //sprintf(sTemp,"Using file %s\n",fileName);
-                //printMsg(sTemp); // using alc file ...
                 lfile = fopen(fileName, "ab");
                 lastDMode[0]=99;//force refresh
                 lastDMode[1]=99;//force refresh
@@ -286,11 +251,10 @@
                     logTS(); // Date Time at start
                     logEvent("Starting"); // Log startup msg for testing
                     sprintf(sTemp,"Cr%s",revStr);
-                    logEvent(sTemp); // gg - log firmware version
-                    
+                    logEvent(sTemp); // gg - log firmware version   
                     spkr.beep(2000,0.25);
                 }
-            }//logging enabled
+            }//logging enabled and USB detected
         } else { // if (logOpen)
             pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
             if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
@@ -322,6 +286,7 @@
                 fclose(lfile);
                 logOpen=false;
                 pointerSep=0;
+                led4=false;
             }
         } // if logOpen
         if (canIdle&&userIdle&&!playbackEn) { // canbus idle --> sleep to save power
@@ -618,8 +583,10 @@
                                 } else if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = configScreen ; // GoTo Config Screen                                   
                                 } else if (dMode[whichTouched] == tripScreen) {
-                                    miles_trip[0]=0;
-                                    kWh_trip[0]=0;
+                                    miles_trip[1]=0;
+                                    kWh_trip[1]=0;
+                                    sMode=0;
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 }                            
@@ -656,8 +623,12 @@
                                 if (dMode[whichTouched] == configScreen) {
                                     updateFirmware();
                                 } else if (dMode[whichTouched] == tripScreen) {
-                                    miles_trip[1]=0;
-                                    kWh_trip[1]=0;
+                                    miles_trip[2]=0;
+                                    kWh_trip[2]=0;
+                                    sMode=0;
+                                    lastDMode[whichTouched]=99;//repaint to clear highlight
+                                } else if (dMode[whichTouched] == indexScreen) {
+                                    dMode[whichTouched] = tripScreen ;    
                                 } else {                             
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 }
@@ -709,10 +680,14 @@
         }
 
         if(tick){ // Executes once a second
+            tick=false;
             headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false;  // headlight/turn signal indicator
             accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
             accOn=(accV>5)?true:false;
             if(laccOn&&!accOn){ // Car turned off
+                if (repeatPoll) { // Log on shutdown if autopoll enabled
+                    tripLog(); // Write trip log on powerdown
+                }
                 //write efficiency history data
                 hfile = fopen("/local/ehist.cny", "w");
                 if (hfile!=NULL){ // found a efficiency history file
@@ -723,8 +698,12 @@
                 }
             }
             if(!laccOn&&accOn){ // Car turned on
-                miles_trip[2]=0;
-                kWh_trip[2]=0;
+                miles_trip[0]=0;
+                kWh_trip[0]=0;
+                if (repeatPoll) { // Poll on startup if autopoll enabled
+                    logOnce=true;
+                    sendReq();
+                }
             }
             laccOn=accOn;
             if(!accOn&&!logEn&&userIdle&&!playbackEn){             
@@ -744,11 +723,11 @@
             if(mph[0]>99){
                 mph[0]=0;
             }
-            mpkWh[0]=mph[0];
             miles_trip[0]+=mph[0]/3600;
             miles_trip[1]+=mph[0]/3600;
             miles_trip[2]+=mph[0]/3600;
 
+            mpkWh[0]=mph[0];
             if(numWsamples>0){ // Avoid div0
                 kW[0]=((float) mWs_x4)/numWsamples/4e3;     
                 mpkWh[0]/=kW[0];
@@ -785,9 +764,16 @@
                }
             }
             updateDTE=true;
-            if(logCP)
+            if(logCP&&usbEn){
+                if(logOnce){
+                    tripLog();
+                    logOnce=false;
+                }
                 logPackVoltages(); // Turbo3, only call
-            tick=false;
+            }
+            if(!usbEn){
+                usbEn=detectUSB(); // Keep looking if none found
+            }
         }
 
         display=display<1?display+1:0; // toggle display
--- a/utility.cpp	Sat Jun 29 02:44:02 2013 +0000
+++ b/utility.cpp	Sun Jun 30 14:45:01 2013 +0000
@@ -49,14 +49,11 @@
  
     char sTemp[40];    
     unsigned char changed;
-    unsigned short i; // was unsigned char
-    signed short j;
+    unsigned short i,j,k;
     signed short packV;
     signed short packA;
     signed long imWs_x4;
     unsigned short ts;
-    long temp_adc[6]       = {1000,589,487,401,365,0 };
-    signed short temp_C[6] = {-270, 130, 230, 320, 360, 760};
 
     secsNoMsg=0; // reset deadman switch
     if(debugMode||(skin==ggSkin)){ 
@@ -263,33 +260,31 @@
                     logCP=yesBattLog; // Only log if logging enabled
                     showCP=true; // Always show
                     // 2013 models only have three sensors
-                    battTemp_x4=battData[(BatDataBaseG4*7)+5]+battData[(BatDataBaseG4*7)+8]+battData[(BatDataBaseG4*7)+11]+battData[(BatDataBaseG4*7)+14];
                     // Or =25+(467-ADC)/9.33 (C)
-                    // Find hottest temperature
-                    battTemp_x10=battData[(BatDataBaseG4*7)+3]*0x100+battData[(BatDataBaseG4*7)+4];
+                    // Find hottest temperature by finding smallest ADC value
+                    k=battData[(BatDataBaseG4*7)+3]*0x100+battData[(BatDataBaseG4*7)+4];
                     j=battData[(BatDataBaseG4*7)+6]*0x100+battData[(BatDataBaseG4*7)+7];
-                    if(j<battTemp_x10){
-                        battTemp_x10=j;
+                    if(j<k){
+                        k=j;
                         }
                     j=battData[(BatDataBaseG4*7)+9]*0x100+battData[(BatDataBaseG4*7)+10];
-                    if(j<battTemp_x10){
-                        battTemp_x10=j;
+                    if(j<k){
+                        k=j;
                         }
                     j=battData[(BatDataBaseG4*7)+12]*0x100+battData[(BatDataBaseG4*7)+13];
-                    if(j<battTemp_x10){
-                        battTemp_x10=j;
+                    if(j<k){
+                        k=j;
                         }
-                    //interpolate for temperature
-                    char k=0;
-                    while(battTemp_x10<=temp_adc[++k]) { } // Find section in table
-                    j=battTemp_x10-temp_adc[k];
-                    j*=(temp_C[k]-temp_C[k-1]);
-                    j/=(temp_adc[k]-temp_adc[k-1]);
-                    j+=temp_C[k];
-                    battTemp_x10=j;
-                    //battTemp_x10*=100;
-                    //battTemp_x10/=93;
-                    //battTemp_x10+=250;
+                    //interpolate from lookup table
+                    unsigned short temp_adc[6] = { 1000,  589,  487,  401,  365,  000};
+                    float            temp_C[6] = {-27.0, 13.0, 23.0, 32.0, 36.0, 76.0};
+                    char ii=0;
+                    while(k<=temp_adc[++ii]) { } // Find section in table
+                    maxTemp=(float)(k-temp_adc[ii]);
+                    maxTemp/=(float)(temp_adc[ii-1]-temp_adc[ii]);
+                    maxTemp*=(temp_C[ii-1]-temp_C[ii]);
+                    maxTemp+=temp_C[ii];
+
                     SOH_x100=battData[(BatDataBaseG1*7)+29]*0x100+battData[(BatDataBaseG1*7)+30];
                     Ah_x10000=battData[(BatDataBaseG1*7)+36]*0x10000+battData[(BatDataBaseG1*7)+37]*0x100+battData[(BatDataBaseG1*7)+38];
                     SOC_x10000=battData[(BatDataBaseG1*7)+32]*0x10000+battData[(BatDataBaseG1*7)+33]*0x100+battData[(BatDataBaseG1*7)+34];
@@ -651,12 +646,67 @@
     }
     
     FILE *bfile;
-    //bfile = fopen("/local/batvolt.txt", "a");
     bfile = fopen("/usb/batvolt.txt", "a");
     if(bfile!=NULL) {
         strftime(sTemp, 40, "%a %m/%d/%Y %X", &t);
-        fprintf(bfile,"%s,%d,%5.1f%%,%5.1f,%5.1f,%d,%d,%d,%d,%d",sTemp,gids,(float)SOC/10,(float)packV/2,(float)packA/2,max,min,avg,max-min,jv);
-        // BatDataBaseG4 * 7 = (BatDataBaseG4*7)        
+        fprintf(bfile,"%s,%d,%5.1f%%,%5.1f,%5.1f,%d,%d,%d,%d,%d",sTemp,gids,(float)SOC/10,(float)packV/2,(float)packA/2,max,min,avg,max-min,jv);    
+        fprintf(bfile,"%d,%d,%d,%d,",(battData[(BatDataBaseG4*7)+ 3]<<8)+battData[(BatDataBaseG4*7)+ 4],battData[(BatDataBaseG4*7)+ 5],(battData[(BatDataBaseG4*7)+ 6]<<8)+battData[(BatDataBaseG4*7)+ 7],battData[(BatDataBaseG4*7)+ 8]);
+        fprintf(bfile,"%d,%d,%d,%d", (battData[(BatDataBaseG4*7)+ 9]<<8)+battData[(BatDataBaseG4*7)+10],battData[(BatDataBaseG4*7)+11],(battData[(BatDataBaseG4*7)+12]<<8)+battData[(BatDataBaseG4*7)+13],battData[(BatDataBaseG4*7)+14]);
+        for(i=0; i<96; i++) {
+            bd=(battData[i*2+3]<<8)+battData[i*2+4];
+            fprintf(bfile,",%d",bd);
+        }
+        fprintf(bfile,"\r\n");
+        fclose(bfile);
+    }
+    logCP=false;
+    showCP=true;
+}
+
+void tripLog() { // Daily log
+    char sTemp[40];
+    struct tm t; // pointer to a static tm structure
+    short unsigned max, min, jv, i, bd;
+    unsigned avg;
+    unsigned short gids, SOC, packV;
+    signed short packA;
+    time_t seconds ;
+    
+    CANMessage msg;
+    
+    seconds = time(NULL); // Turbo3
+    t = *localtime(&seconds) ; // Turbo3 
+    
+    msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids
+    gids = (msg.data[0]<<2)+(msg.data[1]>>6);
+    msg = lastMsg[indexLastMsg[0x55b]]; //Get SOC
+    SOC = (msg.data[0]<<2)+(msg.data[1]>>6);
+    msg = lastMsg[indexLastMsg[0x1db]]; //Get pack volts
+    packV = (msg.data[2]<<2)+(msg.data[3]>>6);
+    packA = (msg.data[0]<<3)+(msg.data[1]>>5);
+    if (packA & 0x400) packA |= 0xf800;
+    
+    max=0;
+    min=9999;
+    avg=0;
+    for(i=0; i<96; i++) {
+        bd=(battData[i*2+3]<<8)+battData[i*2+4];
+        avg+=bd;
+        if(bd>max) max=bd;
+        if(bd<min) min=bd;
+    }
+    avg /= 96;
+    if(min<3713) {
+        jv=avg-(max-avg)*1.5;
+    } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement
+        jv=0;
+    }
+    
+    FILE *bfile;
+    bfile = fopen("/usb/triplog.txt", "a");
+    if(bfile!=NULL) {
+        strftime(sTemp, 40, "%a %m/%d/%Y %X", &t);
+        fprintf(bfile,"%s,%d,%5.1f%%,%5.1f%%, %4.2f, %5.1f,%5.1f,%d,%d,%d,%d,%d",sTemp,gids,(float)SOC/10, (float)SOH_x100/100,(float)Ah_x10000/10000,(float)packV/2,(float)packA/2,max,min,avg,max-min,jv);      
         fprintf(bfile,"%d,%d,%d,%d,",(battData[(BatDataBaseG4*7)+ 3]<<8)+battData[(BatDataBaseG4*7)+ 4],battData[(BatDataBaseG4*7)+ 5],(battData[(BatDataBaseG4*7)+ 6]<<8)+battData[(BatDataBaseG4*7)+ 7],battData[(BatDataBaseG4*7)+ 8]);
         fprintf(bfile,"%d,%d,%d,%d", (battData[(BatDataBaseG4*7)+ 9]<<8)+battData[(BatDataBaseG4*7)+10],battData[(BatDataBaseG4*7)+11],(battData[(BatDataBaseG4*7)+12]<<8)+battData[(BatDataBaseG4*7)+13],battData[(BatDataBaseG4*7)+14]);
         for(i=0; i<96; i++) {
@@ -681,7 +731,7 @@
     {        
         sprintf(sTemp,"Couldn't find firmware.bin\n");
         printf(sTemp);
-        wait(5);
+        wait(3);
         lastDMode[whichTouched]=99;//force refresh
         return;
     }
@@ -714,7 +764,7 @@
     } else {
       /* could not open directory */
         printf("Couldnt open folder");
-        wait(5);
+        wait(3);
         return;
     }        
     printf("copying new firmware\n");
@@ -731,7 +781,7 @@
     {
         sprintf(sTemp,"Couldn't Open Destination\n");
         printf(sTemp);
-        wait(5);
+        wait(3);
         return;
     }
     char buffer[bufSize];
@@ -747,8 +797,18 @@
     fclose(srcFile);
     tt.cls();
     printf("Succesful\n\n");
-    printf("Rebooting in 5 seconds\n");
-    wait(5);
+    printf("Rebooting in 3 seconds\n");
+    wait(3);
     //Now run new firmware
     mbed_reset();
 }
+
+bool detectUSB(void){
+    bool usbEn=false;
+    /*FILE *tfile = fopen("/usb/presence.det", "w");
+    usbEn=(tfile==NULL)?false:true;
+    if(usbEn){
+        fclose(lfile);
+    }*/
+    return(usbEn);
+}
--- a/utility.h	Sat Jun 29 02:44:02 2013 +0000
+++ b/utility.h	Sun Jun 30 14:45:01 2013 +0000
@@ -54,8 +54,7 @@
 extern unsigned char dtePeriod;
 extern Beep spkr;
 extern unsigned char reqMsgCnt;
-extern unsigned char battTemp_x4;
-extern signed short battTemp_x10;
+extern float maxTemp;
 extern unsigned long Ah_x10000;
 extern unsigned long SOC_x10000;
 extern unsigned short SOH_x100;
@@ -87,7 +86,9 @@
     void readConfig();
     void upDate(unsigned char field, bool updownbar);
     void logPackVoltages(); // Turbo3
+    void tripLog(); // Turbo3
     void updateFirmware(); // LM - Update firmware off USB
+    bool detectUSB();
 }
 
 //LEAF OBD