Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed

Fork of CANary by Tick Tock

Revision:
131:0d926c080a65
Parent:
130:1a9d2a6d99ce
Child:
132:08748a67280a
--- a/main.cpp	Mon Jul 22 00:02:51 2013 +0000
+++ b/main.cpp	Wed Jul 24 11:20:08 2013 +0000
@@ -4,12 +4,11 @@
 // * Audible friction brake feedback
 // * User-configurable watchpoint
 // * Add 50% charge option
-// * Tire Pressure Sensor display
-// * Fix bug in playback while connected to canbus (hangs)
 // * Add coasting regen to regen/braking display
 // * Change semilog efficiency graph to linear with 10 minute values
 // * Add additional 79b bank readouts
 // * Add ability to transfer settings config file to/from USB
+// * Subtract accessory power from efficiency history (add back in when displaying)
 
 #include "mbed.h"
 #include "CAN.h"
@@ -21,7 +20,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "130"; // gg - revision string, max 6 characters
+char revStr[7] = "131"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -54,7 +53,7 @@
 
 bool debugMode = false;
 bool usbEn = false;
-bool logEn = true;
+bool logEn = false;
 bool logOpen = false; 
 bool yesBattLog = true; // gg - Batt Log
 unsigned char tNavRow = 3; // gg - 4x4 touch
@@ -74,9 +73,9 @@
 char c;
 volatile int writePointer = 0;
 int readPointer=0;
-volatile unsigned short secsNoMsg = 0;
+volatile unsigned short secsNoCarCanMsg = 0;
 volatile unsigned short secsNoTouch = 0;
-volatile bool canIdle;
+volatile bool carCanIdle;
 volatile bool userIdle;
 bool touched=false; //flag to read touchscreen
 unsigned char whichTouched = 0;
@@ -84,7 +83,7 @@
 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;
+unsigned char dtMode = 0;
 char displayLog[20][40];
 unsigned char displayLoc = 0;
 unsigned int fwCount=1;
@@ -139,6 +138,9 @@
 bool metric = false;
 bool shunt[96]={0};
 bool charging=false;
+bool showHealth=false;
+unsigned char saveDmode=99;
+bool moving=false;
 
 int main() {
     //can1SleepMode.mode(OpenDrain);
@@ -181,7 +183,7 @@
     sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
     printMsg(sTemp); // revision
 
-    secsNoMsg = 0;
+    secsNoCarCanMsg = 0;
 
     //read efficiency history data
     hfile = fopen("/local/ehist.cny", "r");
@@ -224,7 +226,7 @@
     secsNoTouch=2;
     while (true) {
         if (!logOpen) { // Open new file if one is not already open
-            if(logEn&&usbEn){ //logging enables and USB device detected
+            if(logEn&&usbEn){ //logging enabled and USB device detected
                 strftime(fileName, 32, "%m%d%H%M.alc", &t); //mmddhhmm.alc
                 efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS);
                 seconds = time(NULL);
@@ -253,7 +255,7 @@
             }//logging enabled and USB detected
         } else { // if (logOpen)
             pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
-            if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
+            if (pointerSep>(maxBufLen/16)||carCanIdle||!logEn) {
                 // Dump buffer if > 1/16 full or canbus has stopped
                 //if (&efile == NULL) {
                 if (efr != FR_OK) {
@@ -283,7 +285,7 @@
                 led4=false;
             }
         } // if logOpen
-        if (canIdle&&userIdle&&!playbackEn) { // canbus idle --> sleep to save power
+        if (carCanIdle&&userIdle&&!playbackEn) { // canbus idle --> sleep to save power
             if (repeatPoll) { // stop autopolling if enabled
                 autoPoll.detach();
             }
@@ -303,7 +305,7 @@
             led4=0;
             dled=0; // turn off display
             secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900
-            while (secsNoMsg>canTimeout && !touched) {
+            while (secsNoCarCanMsg>canTimeout && !touched) {
                 //DeepPowerDown();
                 tt.wfi(); //enable touch interrupt
                 //__wfi(); // freeze CPU and wait for interrupt (from canbus or touch)
@@ -312,7 +314,7 @@
             lastDMode[0]=99;
             lastDMode[1]=99;
             secsNoTouch=2;
-            canIdle=secsNoMsg>canTimeout;
+            carCanIdle=secsNoCarCanMsg>canTimeout;
             dled=0.8; // turn on display LED
             seconds = time(NULL);
             t = *localtime(&seconds) ;
@@ -492,7 +494,7 @@
                                     dMode[whichTouched] = cpScreen ; // GoTo CP Data Screen                                
                                     sMode=0;
                                 } else if (dMode[whichTouched]==dateScreen){
-                                    dtMode=(dtMode<6)?dtMode+1:0;
+                                    dtMode=(dtMode<5)?dtMode+1:0;
                                     lastDMode[whichTouched]=99;
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
@@ -514,7 +516,9 @@
                                 } else if (dMode[whichTouched]==playbackScreen) {
                                     // Start/stop playback
                                     if(!playbackOpen){
-                                        if(!logOpen){
+                                        if(!carCanIdle){
+                                            printMsg("Cannot playback while connected to canbus\n");
+                                        }else if(!logOpen){
                                             efr = f_open(&efile,"playback.alc",FA_READ|FA_OPEN_EXISTING);
                                             lastDMode[whichTouched]=99;//force refresh
                                             if(efr != FR_OK){
@@ -592,7 +596,7 @@
                                 break;
                                 
                              case 12: // left-middle col, bottom row (not nav)
-                                if (dMode[whichTouched] == indexScreen) { // gg - index
+                                if (dMode[whichTouched] == configScreen) { // gg - index
                                     dMode[whichTouched] = dateScreen ; // GoTo Set Date/Time Screen  
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
@@ -602,6 +606,8 @@
                              case 22: // right-middle col, bottom row (not nav)
                                 if (dMode[whichTouched] == indexScreen) { // gg - index
                                     dMode[whichTouched] = logScreen ;    
+                                } else if (dMode[whichTouched]==configScreen) {
+                                    showHealth = !showHealth;
                                 } else {
                                     lastDMode[whichTouched]=99;//repaint to clear highlight
                                 } 
@@ -673,8 +679,11 @@
             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;
-            charging=(mph[0]<0.1)&&(kW[0]<-1); // not moving and generating energy so much be charging
+            moving=(mph[0]>0.1);
+            charging=!moving&&(kW[0]<-1); // not moving and generating energy so must be charging
             if(laccOn&&!accOn){ // Car turned off
+                saveDmode=dMode[0];
+                dMode[0]=healthScreen;
                 if (repeatPoll) { // Log on shutdown if autopoll enabled
                     tripLog(); // Write trip log on powerdown
                 }
@@ -691,10 +700,15 @@
                 miles_trip[0]=0;
                 kWh_trip[0]=0;
                 wait5secs=5;
+                if(showHealth){
+                    saveDmode=dMode[0];
+                    dMode[0]=healthScreen;
+                }
             }
             laccOn=accOn;
             if(!accOn&&!logEn&&userIdle&&!playbackEn){ // Car off and logging disabled and no user activity
                 dled = 0; 
+                dMode[0]=saveDmode;
             }else if(!headlights){
                 dled = ledHi;
             } else {
@@ -708,6 +722,10 @@
                     msgReq.attach(&sendReq,0.015);
                 }
             }
+            if(moving&&(saveDmode<99)&&(wait5secs==0)){
+                dMode[0]=saveDmode;
+                saveDmode=99;
+            }
 
             //compute historic efficiency
             if(numSsamples>0){ // Avoid div0