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 Mar 23 22:04:44 2014 +0000
Parent:
178:bf6404312c45
Child:
180:5fdeeb86f3a3
Commit message:
Fixed bugs in whpg display

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
--- a/displayModes.cpp	Wed Mar 19 14:08:56 2014 +0000
+++ b/displayModes.cpp	Sun Mar 23 22:04:44 2014 +0000
@@ -1194,7 +1194,7 @@
         tt.line(x,10,x,220,DarkGrey);     
         x=50+38*6;
         lar=0;
-        }
+    }
     toVal=33;
     if(force||lgids!=gids){ // update Y axis when kWh changes
         tt.set_font((unsigned char*) Arial24x23);
@@ -1424,81 +1424,82 @@
 }
 
 void whpgDisplay(bool force, bool showButtons){ 
-    unsigned short maxVal, minVal, maxGid, minGid, avg, i, j;
+    unsigned short maxVal, minVal, maxGid, minGid, i, j, y;
+    float avg;
+    static unsigned short lmg;
+
+    tt.foreground(White);
+    tt.background(Navy);
+    tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
+
+    maxVal=0;
+    minVal=9999;
+    maxGid=0;
+    minGid=9999;
+    avg=0;
 
     if(force){
-        tt.foreground(White);
-        tt.background(Navy);
-        tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
-        maxVal=0;
-        minVal=9999;
-        maxGid=0;
-        minGid=9999;
-        avg=0;
-       
-        // find max/min/avg
-        for(i=0; i<300; i++){
-            j=whpg[i];
-            if(j<255){
-                avg+=j;
-                if(j>maxVal) maxVal=j;
-                if(j<minVal) minVal=j;
-                if(i>maxGid) maxGid=i;
-                if(i<minGid) minGid=i;
-            }
+        lmg=281;
+    }
+
+    // find max/min/avg
+    for(i=0; i<300; i++){
+        if(whpg[i]>0){
+            j=(whpg[i]-whpg[i+1]);
+            avg+=j;
+            if(j>maxVal) maxVal=j;
+            if(j<minVal) minVal=j;
+            if(i>maxGid) maxGid=i;
+            if(i<minGid) minGid=i;
         }
-        avg /= (maxGid-minGid+1);
+    }
+    avg /= (maxGid-minGid+1);
 
+    if (minGid<lmg){ //update if new data
+        lmg = minGid;
         //------------------
         tt.cls();
 
         // show as vertical bar plot
         int xWinMin = 26;
         int xWinMax = 316;
-        int yWinMin = 50;
-        int yWinMax = 150;
-        // draw the Bar Graph Frame, 2 pixels wide
-        tt.rect( xWinMin-1,yWinMin-1, xWinMax+1,yWinMax+1,Red);
-        tt.rect( xWinMin-2,yWinMin-2, xWinMax+2,yWinMax+2,Green);
-        
-        // bar heights
-        int height = yWinMax - yWinMin ;
-        int iBarValMax = maxVal - minVal ; // zero to N
+        int yWinMin = 20;
+        int yWinMax = 200;
+        // draw the Frame, 2 pixels wide
+        tt.rect( xWinMin-1,yWinMin-1, xWinMax+1,yWinMax+1, Red);
+        tt.rect( xWinMin-2,yWinMin-2, xWinMax+2,yWinMax+2, Green);
         
         //----------------
-        if( iBarValMax > 0 ) {
+        if( maxVal > minVal ) {
 
             // label the Y axis
-            tt.locate( 2, yWinMin-14 ); printf("%02d\n", maxVal );
-            tt.locate( 2, yWinMax+5); printf("%02d\n", minVal );
-            tt.locate( 2, (yWinMax+yWinMin)/2); printf("%02d avg\n", avg );
-                   
-            //---------------
-            // show the bars
-            int nBarWidth = 1 ;
-            int nBarSpace = 0 ; // 1 for testing
-            
-            int xPos = xWinMin + 2 ; // start one from the left
-             
-            for( int i=minGid; i<=maxGid; i++) {
-                height = whpg[i] ;
-                if( height > 100 ) height = 100 ; // clip tops
-                
-                // draw the bar, is always inside x-window
-                tt.fillrect( xPos,yWinMax-height, xPos+nBarWidth-1,yWinMax, Green);  
-                // tic mark the x axis each 10
-                if(i%10 == 9){
-                   tt.line( xPos,yWinMax+2, xPos,yWinMax+5, White);  // a white tick mark
-                   tt.line( xPos+1,yWinMax+2, xPos+1,yWinMax+5, White);  // a white tick mark, to widen
+            tt.locate( 2, yWinMin-14); printf("200\n");
+            tt.locate( 2, yWinMax+5); printf("0\n");
+            tt.locate( 2, yWinMax-avg-18); printf("%3.1f\n", avg );
+            tt.locate( 2, yWinMax-avg+2); printf("avg\n");
+
+            for( i=4; i<=286; i++) {
+                if((whpg[i-4]>0)&&(whpg[i+4]>0)){
+                    y = (whpg[i-4]-whpg[i+4])/4;
+                }else{
+                    y =  whpg[i];
+                }
+                if( (y>20) && (y<180) && whpg[i]>0){
+                    tt.fillcircle(i+xWinMin,yWinMax-y,2,Green);
                 }
-                // label the x axis each 20
-                if(i%20 == 19){
-                   tt.locate( xPos-6, yWinMax+8 );
-                   printf("%02d\n", i+1 );
+                if(i%40 == 0){
+                    // label the x axis each 40
+                    tt.line( i+xWinMin,yWinMax+2, i+xWinMin,yWinMax+8, White );  // a white tick mark
+                    if(i<10){
+                        tt.locate( i+xWinMin-12, yWinMax+8 );
+                    }else{
+                        tt.locate( i+xWinMin-20, yWinMax+8 );
+                    }
+                    printf("%d\n", i );
+                } else if(i%10 == 0){
+                    // tic mark the x axis each 10
+                    tt.line( i+xWinMin,yWinMax+2, i+xWinMin,yWinMax+4, White );  // a white tick mark
                 }
-                
-                // step to the next bar position
-                xPos += nBarWidth + nBarSpace ; 
             }
         }
     }
--- a/displayModes.h	Wed Mar 19 14:08:56 2014 +0000
+++ b/displayModes.h	Sun Mar 23 22:04:44 2014 +0000
@@ -15,7 +15,7 @@
 extern char displayLog[20][40];
 extern unsigned char displayLoc;
 extern char indexLastMsg[0x800];
-extern unsigned char whpg[300];
+extern unsigned short whpg[300];
 
 //extern unsigned char battData[256]; // 
 extern unsigned char battData[BatDataBufMax]; // BatDataBufMax
--- a/main.cpp	Wed Mar 19 14:08:56 2014 +0000
+++ b/main.cpp	Sun Mar 23 22:04:44 2014 +0000
@@ -11,8 +11,7 @@
 // * Be more efficient with write buffer (use msgLen instead of always storing 8 bytes)
 
 
-// rev178
-// Added Wh/gid display
+// rev179
 
 #include "mbed.h"
 #include "CAN.h"
@@ -23,7 +22,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "178";
+char revStr[7] = "179";
 unsigned long maxTarget = 1000;
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -182,13 +181,15 @@
 unsigned long fbScalar = 132;
 int effCheckTime = 3;
 bool ignoreDayData = true;
-unsigned short cgids,lgids;
-unsigned char whpg[300];
+unsigned short cgids,lgids=0;
+unsigned short whpg[300]={0};
+unsigned short wh[300]={0};
 
 int main() {
     char sTemp[40];
     unsigned long secs;
-    unsigned char i,j,display=0,lwt=0;
+    unsigned short i,j;
+    unsigned char display=0,lwt=0;
     point lastTouch;
     float average;
     tt.set_orientation(1);
@@ -233,6 +234,10 @@
     sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
     printMsg(sTemp); // revision
 
+    for(i=0;i<300;i++){ // initialize wh lookup
+        wh[i]=i*80;
+    }
+
     //read efficiency history data
     hfile = fopen("/local/ehist.cny", "r");
     if (hfile!=NULL){ // found a efficiency history file
@@ -258,6 +263,11 @@
         if(!feof(hfile)){
             fscanf(hfile,"%f %f\r\n",&maxTripCCkWh,&minTripCCkWh);
         }
+        if(!feof(hfile)){
+            for(i=0;i<300;i++){
+                fscanf(hfile,"%d\r\n",&wh[i]);
+            }
+        }
         fclose(hfile);
         printMsg("History Loaded.\n"); // History loaded
     } else { // create initial file
@@ -892,6 +902,9 @@
                     CCkWh_trip[3]=0;
                     ignoreDayData=false;
                     lt=t; // Remember when counters were cleared
+                    for(i=0;i<300;i++){ // Clear whpg array
+                        whpg[i]=0;
+                    }
                 }
                 wait5secs=5;
                 if (showHealth&&!playbackOpen){
@@ -924,17 +937,16 @@
                 if((startGids>0)&&(startGids<300)){ // Ignore bogus values at startup
                     getGids=false;
                     lgids=startGids; // initialize wh/gid array
-                    for(i=0;i<299;i++){
-                        whpg[i]=255;
-                    }
                 }
             }
-            if(cgids<lgids){
-                whpg[lgids]= (unsigned char) (1000*kWh_trip[3]); // Save kWh for each gid since last charge
-                lgids=cgids;
-            }else if(cgids>lgids){
-                whpg[cgids]= (unsigned char) (1000*kWh_trip[3]); // Save kWh for each gid since last charge
-                lgids=cgids;
+            if((cgids>0)&&(cgids<300)){
+                if(cgids<lgids){
+                    whpg[cgids] = (unsigned short) (1000*kWh_trip[3]); // Save kWh for each gid since last charge
+                    lgids=cgids;
+                }else if(cgids>lgids){
+                    whpg[cgids] = (unsigned short) (1000*kWh_trip[3]); // Save kWh for each gid since last charge
+                    lgids=cgids;
+                }
             }
             if(wait5secs>0){ // Wait a few seconds after poweron to give BMS time to measure CP's
                 wait5secs-=1;
@@ -1005,7 +1017,7 @@
                 miles_trip[0]+=mph[0]/3600; // per trip
                 miles_trip[1]+=mph[0]/3600; // per charge
                 miles_trip[2]+=mph[0]/3600; // user
-                miles_trip[3]+=mph[0]/3600; // user
+                miles_trip[3]+=mph[0]/3600; // per day/roundtrip
                 kWh_trip[0]+=kW[0]/3600;
                 kWh_trip[1]+=kW[0]/3600;
                 kWh_trip[2]+=kW[0]/3600;