Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers displayModes.cpp Source File

displayModes.cpp

00001 //displayModes.cpp
00002 #include "displayModes.h"
00003 
00004 char sTemp1[40];
00005 char sTemp2[16];
00006 
00007 void mainDisplay (bool force, bool showButtons){
00008     unsigned short gids, SOC_x10, packV_x2, tireP;
00009     static unsigned short lgids=0, lSOC=0, lSOH=0, lpackV_x2=0, ltireP=0, maxPS=0;
00010     static float lmaxTemp=0;
00011     static float lkW=0, laccV=0, lmpkWh=0;
00012     static unsigned long lAh=0;
00013     CANMessage msg;
00014 
00015     msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids
00016     gids = (msg.data[0]<<2)+(msg.data[1]>>6);
00017     msg = lastMsg[indexLastMsg[0x55b]]; //Get SOC
00018     SOC_x10 = (msg.data[0]<<2)+(msg.data[1]>>6);
00019     msg = lastMsg[indexLastMsg[0x1db]]; //Get pack volts
00020     packV_x2 = (msg.data[2]<<2)+(msg.data[3]>>6);
00021     msg = lastMsg[indexLastMsg[0x385]]; //Get tire pressure
00022     tireP = msg.data[2]+msg.data[3]+msg.data[4]+msg.data[5];
00023 
00024     tt.background(Navy);
00025     tt.foreground(Yellow);
00026     tt.set_font((unsigned char*) Arial28x28);
00027     if(force) tt.cls();
00028     if(skin==ttSkin){
00029         if(force||gids!=lgids||mpkWh[dtePeriod]!=lmpkWh){
00030             tt.locate(10,10);
00031             printf("%4d gids \n",gids);
00032             if(debugMode){
00033                 if(pointerSep>maxPS){maxPS=pointerSep;}
00034                 tt.locate(10,70);
00035                 printf("%3d sep %3d max\n",pointerSep,maxPS);
00036             }
00037             tt.locate(10,40);
00038             printf("%4.1f kWh \n",(float)(gids-5)*0.075);
00039             tt.set_font((unsigned char*) SCProSB31x55);
00040             tt.foreground(Green);
00041             tt.locate(60,96);            
00042             printf("%4.1f %s  \n",convertDistance(mpkWh[dtePeriod]*((float)(gids-5)*.075)),distanceUnit()); //LM - add metric conversion
00043             lgids=gids;
00044             lmpkWh=mpkWh[dtePeriod];
00045             tt.foreground(Yellow);
00046             tt.set_font((unsigned char*) Arial28x28);
00047         }
00048         if(force||tireP!=ltireP){
00049             tt.foreground(LightGrey);
00050             if(msg.data[6]&0x80){
00051                 tt.locate(40,150);
00052                 printf("%3.1f\n",(float)msg.data[2]/4);
00053             }
00054             if(msg.data[6]&0x40){
00055                 tt.locate(114,150);
00056                 printf("%3.1f\n",(float)msg.data[3]/4);
00057             }
00058             if(msg.data[6]&0x20){
00059                 tt.locate(40,178);
00060                 printf("%3.1f\n",(float)msg.data[4]/4);
00061             }
00062             if(msg.data[6]&0x10){
00063                 tt.locate(114,178);
00064                 printf("%3.1f\n",(float)msg.data[5]/4);
00065             }
00066             tt.foreground(Yellow);
00067             ltireP=tireP;
00068         }
00069         if(force||SOC_x10!=lSOC){
00070             tt.locate(200,10);
00071             printf("%4.1f%s\n",(float)SOC_x10/10,"% ");
00072             lSOC=SOC_x10;
00073         }
00074         if(force||packV_x2!=lpackV_x2){
00075             tt.locate(200,206);
00076             printf("%4.1fV \n",(float)packV_x2/2);
00077             lpackV_x2=packV_x2;
00078         }
00079         if(force||maxTemp!=lmaxTemp){
00080             tt.locate(200,176);
00081             printf("%4.1f%s\n",convertTemperature(maxTemp),temperatureUnit());
00082             lmaxTemp=maxTemp;
00083         }
00084         if(force||accV!=laccV){
00085             tt.locate(20,206);
00086             printf("%3.1fV  \n",accV);
00087             laccV=accV;
00088         }
00089         if(force||kW[0]!=lkW){
00090             if(kW[0]<-10){ //Right justify
00091                 tt.locate(171,40);
00092                 printf("%4.2fkW\n",kW[0]);
00093             } else if (kW[0]<0){
00094                 tt.locate(171,40);
00095                 printf(" %4.2fkW\n",kW[0]);
00096             } else if (kW[0]<10){
00097                 tt.locate(165,40);
00098                 printf("  %4.2fkW\n",kW[0]);
00099             } else {
00100                 tt.locate(165,40);
00101                 printf(" %4.2fkW\n",kW[0]);
00102             }
00103             lkW=kW[0];
00104         }
00105         tt.foreground(LightGrey);
00106         if(force||Ah_x10000!=lAh){
00107             tt.locate(10,70);
00108             printf("% 4.2fAh \n",(float)Ah_x10000/10000);
00109             lAh=Ah_x10000;
00110         }
00111         if(force||SOH_x100!=lSOH){
00112             tt.locate(206,70);
00113             printf("%4.1f%s\n",(float)SOH_x100/100,"% ");
00114             lSOH=SOH_x100;
00115         }
00116     }else {//if(skin==ggSkin){
00117         if(force||gids!=lgids){
00118             tt.locate(10,10);
00119             printf("%4d GIDs \n",gids);
00120             
00121             tt.locate(40,40); // gg - add GIDs Percent of 281
00122             printf("%4.1f%s \n", (float)gids*0.355872, "% ") ;
00123             tt.locate(20,70);
00124             printf("%4.1f kwh  \n",(float)gids*0.075); // gg - closer to usable
00125             
00126             tt.set_font((unsigned char*) SCProSB31x55);
00127             tt.foreground(Green);
00128             //tt.locate(60,96);
00129             tt.locate(60,116); // gg - move down a little
00130             printf("%4.1f %s  \n",convertDistance((float)(gids-5)*0.31),distanceUnit()); // Approx for now - LM added metric
00131             lgids=gids;
00132             tt.foreground(Yellow);
00133             tt.set_font((unsigned char*) Arial28x28);
00134         }
00135         
00136         if(force||SOC_x10!=lSOC){
00137             tt.locate(200,10);
00138             printf("%4.1f%s\n",(float)SOC_x10/10,"% ");
00139             lSOC=SOC_x10;
00140         }
00141         if(force||packV_x2!=lpackV_x2){
00142             tt.locate(200,200);
00143             printf("%4.1fV \n",(float)packV_x2/2);
00144             lpackV_x2=packV_x2;
00145         }
00146         if(force||accV!=laccV){
00147             tt.locate(20,200);
00148             printf("%3.1fV  \n",accV);
00149             laccV=accV;
00150         }
00151         if(force||kW[0]!=lkW){
00152             tt.locate(160,40); // gg - move left to keep from wrap
00153             printf("%3.2fkw \n",kW[0]); // use small w to save space
00154             lkW=kW[0];
00155         }
00156     }
00157     if(led4){
00158         tt.fillcircle(310,10,6,Red);
00159     }else{
00160         tt.fillcircle(310,10,6,Navy);
00161     }
00162 }
00163 
00164 void printLast (bool force, bool showButtons){
00165     CANMessage msg;
00166     tt.locate(0,6);
00167     tt.foreground(Red);
00168     tt.background(Yellow);
00169     if(force) tt.cls(); // Just clear screen if forced - always update display
00170     tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
00171     for(int i=0; i<19; i++){
00172         msg = lastMsg[i+indexOffset];
00173         printf("%03x : %02x %02x %02x %02x %02x %02x %02x %02x    \n",msg.id,msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7]);
00174     }
00175     if(showButtons){
00176         tt.foreground(Yellow);
00177         tt.background(DarkCyan);
00178         tt.set_font((unsigned char*) Arial12x12);
00179         
00180         showButton(0,0," <up>","",4,4);
00181         showButton(2,0,"<down>","",4,4);
00182     }
00183 }
00184 
00185 void printChanged (bool force, bool showButtons){
00186     CANMessage msg;
00187     unsigned char i,j;
00188     tt.locate(0,6);
00189     tt.foreground(Red);
00190     tt.background(Yellow);
00191     if(force) tt.cls(); // Just clear screen if forced - always update display
00192     tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
00193     i=0;
00194     j=indexOffset;
00195     do{
00196         j=j<99?j+1:j;
00197         if(msgChanged[j]>0){
00198             msg = lastMsg[j];
00199             printf("%03x : %02x %02x %02x %02x %02x %02x %02x %02x    \n",msg.id,msg.data[0],msg.data[1],msg.data[2],msg.data[3],msg.data[4],msg.data[5],msg.data[6],msg.data[7]);
00200             i++;
00201         }// if changed
00202     }while(i<19&&j<99);
00203     if(showButtons){
00204         tt.foreground(Yellow);
00205         tt.background(DarkCyan);
00206         tt.set_font((unsigned char*) Arial12x12);
00207         showButton(0,0,"  <up>","",4,4);
00208         showButton(2,0," <down>","",4,4);
00209         showButton(1,0," Reset","Baseline",4,4);
00210     }
00211 }
00212 
00213 void printLog (bool force, bool showButtons){
00214     static unsigned char lastldl = 0;
00215     unsigned char ldl=displayLoc;
00216     if(force||ldl!=lastldl){ //only update if changed
00217         tt.foreground(Amber);
00218         tt.background(Black);
00219         tt.cls();
00220         tt.locate(0,6);
00221         tt.set_font((unsigned char*) Arial12x12);
00222         for(int i=0; i<19; i++){
00223             printf("%s",displayLog[ldl]);
00224             ldl=ldl>17?0:ldl+1;
00225         }
00226     }
00227     lastldl=ldl;
00228 }
00229 
00230 void tripDisplay (bool force, bool showButtons){
00231     static float lkWh=0;
00232     tt.background(White);
00233     if(force){
00234         tt.cls();
00235     }
00236     if(force||(int)(lkWh*100)!=(int)(kWh_trip[0]*100)){ //only update if changed
00237         tt.foreground(Navy);
00238         tt.set_font((unsigned char*) Arial28x28);
00239         tt.locate(6,210);
00240         printf("kWh : %s : Eff\n",distanceUnit());
00241         for(int i=0; i<3; i++){
00242             tt.locate(6,20+i*60);
00243             printf("%3.2f : %3.1f : %2.1f\n",kWh_trip[i],convertDistance(miles_trip[i]),convertDistance(miles_trip[i])/kWh_trip[i]);
00244         }
00245     }
00246     if(showButtons){
00247         tt.set_font((unsigned char*) Arial12x12);
00248         tt.background(DarkCyan);
00249         tt.foreground(Yellow);
00250         showButton(3,1," Reset","   A",4,4);
00251         showButton(3,2," Reset","   B",4,4);
00252         lkWh=kWh_trip[0];
00253     }
00254 }
00255 
00256 void healthDisplay (bool force, bool showButtons){
00257     unsigned short gids, SOC_x10;
00258     static unsigned short lgids=0, lSOC=0, lSOH=0;
00259     static float lmaxTemp=0, lresr=0, lunlV=0;
00260     static unsigned long lAh=0;
00261     CANMessage msg;
00262 
00263     msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids
00264     gids = (msg.data[0]<<2)+(msg.data[1]>>6);
00265     msg = lastMsg[indexLastMsg[0x55b]]; //Get SOC
00266     SOC_x10 = (msg.data[0]<<2)+(msg.data[1]>>6);
00267 
00268     tt.background(Blue);
00269     tt.foreground(Yellow);
00270     tt.set_font((unsigned char*) Arial28x28);
00271     if(force) tt.cls();
00272 
00273     /*if(force||tock){ // for esr debug
00274         tt.locate(10,10);
00275         printf("%d %d amps\n",Imax,Imin);
00276         tt.locate(10,40);
00277         printf(" %4.1f %4.1f\n",incRmax/2,incRmin/2);
00278         tt.locate(10,70);
00279         printf(" %4.1f %4.1f\n",redRmax/2,redRmin/2);
00280         tt.locate(10,100);
00281         printf(" %4.1f %4.1f\n",curRmax/2,curRmin/2);
00282         //tt.locate(10,130);
00283         curRmin=1000;
00284         curRmax=0;
00285         incRmin=1000;
00286         incRmax=0;
00287         redRmin=1000;
00288         redRmax=0;
00289         Imax=-1000;
00290         Imin=1000;
00291     }*/
00292     if(force||gids!=lgids){
00293         tt.locate(10,10);
00294         printf("%4d gids \n",gids);
00295         lgids=gids;
00296     }
00297     if(force||SOC_x10!=lSOC){
00298         tt.locate(10,40);
00299         printf(" %4.1f%s \n",(float)SOC_x10/10,"% SOC");
00300         lSOC=SOC_x10;
00301     }
00302     if(force||SOH_x100!=lSOH){
00303         tt.locate(10,70);
00304         printf(" %4.1f%s \n",(float)SOH_x100/100,"% SOH");
00305         lSOH=SOH_x100;
00306     }
00307     if(force||Ah_x10000!=lAh){
00308         tt.locate(10,100);
00309         printf(" %4.2f Ah cap \n",(float)Ah_x10000/10000);
00310         lAh=Ah_x10000;
00311     }
00312     if(force||maxTemp!=lmaxTemp){
00313         tt.locate(10,130);
00314         printf(" %4.1f %s (max) \n",convertTemperature(maxTemp),temperatureUnit());
00315         lmaxTemp=maxTemp;
00316     }
00317     if(force||unloadedV_x2!=lunlV){
00318         tt.locate(10,160);
00319         printf(" %4.1f V \n",unloadedV_x2/2);
00320         lunlV=unloadedV_x2;
00321     }
00322     if(force||Resr!=lresr){
00323         tt.locate(10,190);
00324         printf(" %3.0f mOhms \n",Resr*1000);
00325         lresr=Resr;
00326     }
00327 }
00328 
00329 void braking (bool force, bool showButtons, bool prdata=false){
00330     unsigned long targetBraking, regenBraking;
00331     static unsigned long maxTarget = 1000, maxRegen = 1000, tardivreg_x1000 = 1000;
00332     unsigned long temp;
00333     static unsigned char lastPressure[4] = {200,200,200,200};
00334     unsigned char i,r,t;
00335     static unsigned char lr=0, lt=0;
00336     signed short steering;
00337     unsigned short s;
00338     static unsigned short ls;
00339     unsigned char throttle;
00340     static unsigned char lthrottle;
00341     short steerOutBounds = 0 ;
00342     CANMessage msg;
00343 
00344     //---------------
00345     msg = lastMsg[indexLastMsg[0x180]]; //Get Throttle position
00346     throttle = msg.data[5];
00347     
00348     // ---- steering ----
00349     msg = lastMsg[indexLastMsg[0x002]]; //Get Steering angle
00350     steering = (msg.data[1]<<8)+msg.data[0];
00351     
00352     if(skin==ttSkin){
00353         s= (unsigned short) ((steering/10)+155)%310; // this modulo wraps display
00354     }else{// if(skin==ggSkin){    
00355         // do not go off screen left or right. gg - steering
00356         short ss = (short) ((steering/15)+160); // less gain 10 -> 15
00357         if(ss<0) { ss=0; steerOutBounds = 1; }
00358         if(ss>310) { ss=310; steerOutBounds = 1; }
00359         s = (unsigned short) ss;
00360     }
00361     
00362     //--------------
00363     msg = lastMsg[indexLastMsg[0x1ca]]; //Get brake pressure
00364     tt.background(Navy);
00365     if (force) {
00366         tt.cls();
00367         tt.rect(0,111,170,239,White);
00368         tt.line(0,207,170,207,White);
00369         tt.line(0,175,170,175,White);
00370         tt.line(0,143,170,143,White);
00371         lastPressure[0] = 200;
00372         lastPressure[1] = 200;
00373         lastPressure[2] = 200;
00374         lastPressure[3] = 200;
00375     }
00376     
00377     // display the steering position small square
00378     if (s!=ls){
00379         // steering position has moved
00380         //tt.fillrect(ls,5,ls+9,14, Navy);    // blank old position    
00381         
00382         //---- gg - steering red
00383         // box is blanked by top of Braking bar, so move up 5
00384         tt.fillrect(ls,0,ls+9,9, Navy);    // blank old position    
00385         if( steerOutBounds != 0 ) // draw out-of-bounds as a red box 
00386             tt.fillrect(s,0,s+9,9, Red);     // draw out-of-bounds position 
00387         else
00388             tt.fillrect(s,0,s+9,9, White);   // draw new in-bounds position 
00389             
00390         //----
00391         //tt.foreground(Yellow);
00392         //tt.set_font((unsigned char*) Arial28x28);
00393         //tt.locate(10,40);
00394         //printf("%d %d    \n",s,ls);
00395         ls=s;
00396     }
00397     
00398     if (throttle!=lthrottle){
00399         if (throttle>239) throttle=239;
00400         if(throttle<lthrottle){
00401             tt.fillrect(280,239-lthrottle,310,239-throttle,Navy);
00402         }else{
00403             tt.fillrect(280,239-throttle,310,239,Yellow);
00404         }
00405         lthrottle=throttle;
00406     }
00407 
00408     // plot bar graph for each wheel pressure
00409     for (i=0; i<4; i++){
00410         if (msg.data[i]<239) {
00411             if (msg.data[i]>lastPressure[i]){
00412                 tt.fillrect(10+40*i,239-msg.data[i],40+40*i,239,Red);
00413             } else if (msg.data[i]<lastPressure[i]) {
00414                 tt.fillrect(10+40*i,238-lastPressure[i],40+40*i,238-msg.data[i],Navy);
00415             }
00416             lastPressure[i]=msg.data[i];
00417         }
00418     }
00419 
00420     msg = lastMsg[indexLastMsg[0x1cb]]; //Get Target and Regen
00421     regenBraking = (msg.data[0]<<3)+(msg.data[1]>>5);
00422     targetBraking = (msg.data[2]<<3)+(msg.data[3]>>5);
00423     msg = lastMsg[indexLastMsg[0x421]]; //Get Drive Mode
00424     if (msg.data[0]==0x18) { // In Neutral
00425         regenBraking = 0;  // No regen when in Neutral
00426     }
00427 
00428     if (targetBraking<2045){
00429         if ((targetBraking>50)&&(regenBraking>50)){
00430             temp = targetBraking;
00431             temp *= 1000;
00432             temp /= regenBraking;
00433             if (temp<tardivreg_x1000) tardivreg_x1000=temp;
00434         }
00435         if (targetBraking>maxTarget) maxTarget=targetBraking;
00436         if (regenBraking>maxRegen) maxRegen=regenBraking;
00437         temp = targetBraking;
00438         temp *=200;
00439         temp /= maxTarget;
00440         t = (char) temp;
00441         if (t>200) t=200;
00442         temp = regenBraking;
00443         temp *= tardivreg_x1000;
00444         temp /= maxTarget;
00445         temp /= 5; // 1000/200=5
00446         r = (char) temp;
00447         if (r>200) r=200;
00448         if(lr!=r&&prdata){
00449             tt.foreground(Yellow);
00450             tt.set_font((unsigned char*) Arial28x28);
00451             tt.locate(100,40);
00452             printf("%d %d    \n",regenBraking,maxRegen);
00453             tt.locate(100,70);
00454             printf("%3.1f (%3.1f%s)    \n",(float)tardivreg_x1000/10,(float)regenBraking*tardivreg_x1000/targetBraking/10,"%");
00455         }    
00456         if(lt!=t&&prdata){
00457             tt.foreground(Yellow);
00458             tt.set_font((unsigned char*) Arial28x28);
00459             tt.locate(100,10);
00460             printf("%d %d    \n",targetBraking,maxTarget);
00461         }
00462         if (r>t) t=r;  //Should never happen
00463         if((lr!=r||lt!=t)&&!prdata){
00464             tt.fillrect(190,10,260,239-t,Navy);
00465             tt.fillrect(190,239-t,260,239-r,Red);
00466             tt.fillrect(190,239-r,260,239,Green);
00467         }
00468         lt=t;
00469         lr=r;
00470     }
00471 }
00472 
00473 void cpData(bool force, bool showButtons){
00474     short unsigned max, min, jv, i, bd;
00475     unsigned avg;
00476     static char step=0; // counter to allow incremental update
00477     if(force){
00478         tt.foreground(White);
00479         tt.background(Navy);
00480         tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
00481         max=0;
00482         min=9999;
00483         avg=0;
00484         for(i=0; i<96; i++){
00485            bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
00486            avg+=bd;
00487             if(bd>max) max=bd;
00488             if(bd<min) min=bd;
00489         }
00490         avg /= 96;
00491         if(min<3713) {
00492             jv=avg-(max-avg)*1.5;
00493         } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement
00494             jv=0;
00495         }
00496 
00497         char* sTemperatureUnit = temperatureUnit();
00498         switch(step){
00499             case 0:
00500                 tt.cls();
00501                 showCP=true;
00502                 break;
00503             case 1:
00504                 tt.locate(0,6);
00505                 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",
00506                     max,min,avg,jv, convertTemperature(battData[BatDataBaseG4*7+5]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+8]),sTemperatureUnit,
00507                     convertTemperature(battData[BatDataBaseG4*7+11]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+14]),sTemperatureUnit);
00508                 tt.rect(8+0*41,16,40+0*41,28,Green);
00509                 tt.rect(8+1*41,16,40+1*41,28,Yellow);
00510                 //tt.rect(8+2*41,16,40+2*41,28,White);
00511                 tt.rect(8+3*41,16,40+3*41,28,Red);
00512                 break;
00513             default:
00514                 tt.locate(0,36+(step-2)*48);
00515                 for(i=(step-2)*4; i<(step-1)*4; i++){
00516                     printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",
00517                         i*6+1,i*6+6,
00518                         (battData[BatDataBaseG2*7+i*12+3]<<8)+battData[BatDataBaseG2*7+i*12+4],(battData[BatDataBaseG2*7+i*12+5]<<8)+battData[BatDataBaseG2*7+i*12+6],
00519                         (battData[BatDataBaseG2*7+i*12+7]<<8)+battData[BatDataBaseG2*7+i*12+8],(battData[BatDataBaseG2*7+i*12+9]<<8)+battData[BatDataBaseG2*7+i*12+10],
00520                         (battData[BatDataBaseG2*7+i*12+11]<<8)+battData[BatDataBaseG2*7+i*12+12],(battData[BatDataBaseG2*7+i*12+13]<<8)+battData[BatDataBaseG2*7+i*12+14]);
00521                 }
00522                 for(i=(step-2)*24; i<(step-1)*24; i++){
00523                     bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
00524                     if(bd>0){
00525                         if(bd==max) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Green);
00526                         //if(bd==avg) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,White);
00527                         if(bd==min) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Yellow);
00528                         if(bd<jv) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Red);
00529                     }
00530                 }
00531         }
00532         step=step<5?step+1:0;
00533         if(step==0){
00534             showCP=false;
00535         }
00536     }
00537     if(showButtons){
00538         tt.foreground(Yellow);
00539         tt.background(DarkCyan);
00540         tt.set_font((unsigned char*) Arial12x12);
00541         
00542         showButton(1,0,"Request","CP Data",4,4);               
00543     }
00544 }
00545 
00546 //----------------
00547 // gg - index
00548 void showIndex(bool force, bool showButtons){ 
00549 
00550     if(force){
00551         tt.foreground(White);
00552         tt.background(Navy);
00553         //tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
00554        
00555         tt.cls();
00556         
00557         // add the buttons to GoTo to other screens
00558 
00559         tt.foreground(Yellow);
00560         tt.background(DarkCyan);
00561         tt.set_font((unsigned char*) Arial12x12);
00562         
00563          // top row
00564         showButton(0,0," GoTo"," Main",4,4);               
00565         showButton(1,0," GoTo"," Brake",4,4);               
00566         showButton(2,0," GoTo"," EFF",4,4);               
00567         showButton(3,0," GoTo","Health",4,4);     
00568         // middle row
00569         showButton(0,1," GoTo","CP Data",4,4);               
00570         showButton(1,1," GoTo","CP Hist",4,4);               
00571         showButton(2,1," GoTo","CP Bars",4,4);     
00572         showButton(3,1," GoTo"," Config",4,4);               
00573         // bottom (not Nav) row        
00574         showButton(0,2," GoTo","Playback",4,4);               
00575         showButton(1,2," GoTo","Set Time",4,4);             
00576         showButton(2,2," GoTo"," Log",4,4);     
00577         showButton(3,2," GoTo"," Trip",4,4);     
00578     
00579         showCP=false;
00580     }
00581     
00582     if(sMode==1&&showButtons){
00583         tt.foreground(Yellow);
00584         tt.background(DarkCyan);
00585         tt.set_font((unsigned char*) Arial12x12);
00586         
00587         // do nothing here?
00588     }
00589 }
00590 
00591 //----------------
00592 // gg - cpbars
00593 void cpBarPlot(bool force, bool showButtons){ 
00594     short unsigned max, min, jv, i, bd;
00595     unsigned avg;
00596     short unsigned nBar[96] ; // bar height over min
00597 
00598     if(force){
00599         tt.foreground(White);
00600         tt.background(Navy);
00601         tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
00602         max=0;
00603         min=9999;
00604         avg=0;
00605        
00606         // calc each cell-pair voltage, find max and min
00607         for(i=0; i<96; i++){
00608            bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
00609            nBar[i] = bd; // init to bar height
00610            avg+=bd;
00611             if(bd>max) max=bd;
00612             if(bd<min) min=bd;
00613         }
00614         avg /= 96;
00615         
00616         if(min<3713) {
00617             jv=avg-(max-avg)*1.5;
00618         } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement
00619             jv=0;
00620         }
00621         
00622         //------------------
00623         tt.cls();
00624             
00625         // show as vertical bar plot
00626         int xWinMin = 26;
00627         int xWinMax = 316;
00628         int yWinMin = 50;
00629         int yWinMax = 150;
00630         // draw the Bar Graph Frame, 2 pixels wide
00631         tt.rect( xWinMin-1,yWinMin-1, xWinMax+1,yWinMax+1,Red);
00632         tt.rect( xWinMin-2,yWinMin-2, xWinMax+2,yWinMax+2,Green);
00633         
00634         // bar heights
00635         int height = yWinMax - yWinMin ;
00636         int iBarValMax = max - min ; // zero to N
00637         
00638         //----------------
00639         if( iBarValMax == 0 ) {
00640             // for testing
00641             min = 3501 ; 
00642             //max = min + 95*2 ; // for tall values
00643             max = min + 95/4 ; // for small values
00644             avg = ( max + min ) / 2;
00645             iBarValMax = max - min ; // zero to N
00646             for(int i=0; i<96; i++) {
00647               //nBar[i] = i*2 + min ; // test tall values
00648               nBar[i] = i/4 + min ; // test small values
00649             }
00650         }
00651         //---------------
00652         float nBarScale = float(height) / iBarValMax ;
00653         if( nBarScale < 0.1 ) nBarScale = 0.1 ;
00654     
00655         // do the Bar-height scaling
00656         for(int i=0; i<96; i++){
00657           nBar[i] -= min ; // now, 0 to N = iBinValMax
00658           nBar[i] *= nBarScale ; // scale, as needed
00659         }
00660         
00661         // values, for now
00662         // BatDataBaseG4 * 7 = 280
00663         tt.locate( 0, yWinMax+40 );
00664         char* sTemperatureUnit = temperatureUnit();
00665         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",
00666             max,min,avg,jv, convertTemperature(battData[BatDataBaseG4*7+5]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+8]),sTemperatureUnit,
00667             convertTemperature(battData[BatDataBaseG4*7+11]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+14]),sTemperatureUnit);
00668         //printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
00669         //        max,min,avg,jv,  battData[BatDataBaseG4*7+5],battData[BatDataBaseG4*7+8],  battData[BatDataBaseG4*7+11],battData[BatDataBaseG4*7+14]);
00670         
00671         // label the X axis (approximate)
00672         tt.locate( 2, yWinMax+5); printf("%04d", min );
00673         //tt.locate( 2, yWinMin-14 ); printf("%04d = %04d from %1.4f", max, int( height / nBarScale ) + min, nBarScale );
00674         tt.locate( 2, yWinMin-14 ); printf("%04d = (%d) mv range.", max , max - min );
00675                
00676         //---------------
00677         // show the bars
00678         int nBarWidth = 2 ;
00679         int nBarSpace = 1 ; // 1 for testing
00680         
00681         int xPos = xWinMin + 2 ; // start one from the left
00682          
00683         for( int i=0; i<96; i++) {
00684             height = nBar[i] ;
00685             if( height > 100 ) height = 100 ; // clip tops
00686             
00687             // draw the bar, is always inside x-window
00688             if (shunt[i]){
00689                 tt.fillrect( xPos,yWinMax-height, xPos+nBarWidth-1,yWinMax, Red);  
00690             } else {
00691                 tt.fillrect( xPos,yWinMax-height, xPos+nBarWidth-1,yWinMax, Green);  
00692             }
00693             // tic mark the y axis each 5
00694             if(i%5 == 4){
00695                tt.line( xPos,yWinMax+2, xPos,yWinMax+5, White);  // a white tick mark
00696                tt.line( xPos+1,yWinMax+2, xPos+1,yWinMax+5, White);  // a white tick mark, to widen
00697                //tt.rect( xPos,yWinMax+2, xPos+1,yWinMax+5, White);  // a white 2-wide tick mark is SLOW
00698             }
00699             // label the y axis each 10
00700             if(i%10 == 9){
00701                tt.locate( xPos-6, yWinMax+8 );
00702                printf("%02d\n", i+1 );
00703             }
00704             
00705             // step to the next bar position
00706             xPos += nBarWidth + nBarSpace ; 
00707         }
00708         
00709         showCP=false;
00710     }
00711     
00712     // handle the button
00713     if(sMode==1&&showButtons){
00714         tt.foreground(Yellow);
00715         tt.background(DarkCyan);
00716         tt.set_font((unsigned char*) Arial12x12);
00717         
00718         showButton(1,0,"Request","CP Data",4,4);               
00719     }
00720 }
00721 
00722 //----------------
00723 // gg - hist
00724 void cpHistogram(bool force, bool showButtons){ 
00725     short unsigned max, min, jv, i, bd;
00726     unsigned avg;
00727     if(force){
00728         tt.foreground(White);
00729         tt.background(Navy);
00730         tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
00731         max=0;
00732         min=9999;
00733         avg=0;
00734         for(i=0; i<96; i++){
00735            bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
00736            avg+=bd;
00737             if(bd>max) max=bd;
00738             if(bd<min) min=bd;
00739         }
00740         avg /= 96;
00741         if(min<3713) {
00742             jv=avg-(max-avg)*1.5;
00743         } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement
00744             jv=0;
00745         }
00746         
00747         //------------------
00748         tt.cls();
00749             
00750         // show as histogram
00751         int xWinMin = 20;
00752         int xWinMax = 300;
00753         int yWinMin = 50;
00754         int yWinMax = 150;
00755         // draw the Histogram Frame, 2 pixels wide
00756         tt.rect( xWinMin-1,yWinMin-1, xWinMax+1,yWinMax+1,Red);
00757         tt.rect( xWinMin-2,yWinMin-2, xWinMax+2,yWinMax+2,Green);
00758         
00759         // binning
00760         short nBin[301] ; // bins to count Min values in nBin[0], etc.
00761         int height ;
00762         int iBinIndxMax = 300 ;
00763         int iBinValMax = max - min ; // zero to N
00764         if( iBinValMax > iBinIndxMax ) iBinValMax = iBinIndxMax ;
00765     
00766         // clean the bins
00767         for(int i=0; i<=iBinIndxMax; i++) {
00768           nBin[i] = 0;
00769         }
00770         
00771         // do the bin counting
00772         for(int i=0; i<96; i++){
00773           bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4] - min ;
00774           if( bd > iBinValMax ) bd = iBinValMax ;
00775           nBin[bd] ++ ;
00776         }
00777         
00778         //----------------
00779         if( iBinValMax == 0 ) {
00780             // for testing
00781             min = 10 ; 
00782             max = 50 ;
00783             avg = ( max + min ) / 2;
00784             iBinValMax = max - min ;
00785             for(int i=0; i<=(iBinValMax/2); i++) {
00786               nBin[i] = i ;
00787               nBin[iBinValMax-i] = i ;
00788             }
00789         }
00790         
00791         // the values, for now
00792         // BatDataBaseG4 * 7 = 280        
00793         tt.locate( 0, yWinMax+40 );
00794         char* sTemperatureUnit = temperatureUnit();
00795         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",
00796             max,min,avg,jv, convertTemperature(battData[BatDataBaseG4*7+5]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+8]),sTemperatureUnit,
00797             convertTemperature(battData[BatDataBaseG4*7+11]),sTemperatureUnit,convertTemperature(battData[BatDataBaseG4*7+14]),sTemperatureUnit);
00798         //printf(" MAX  MIN  AVG CVLI T1  T2  T3  T4\n %04d %04d %04d %04d %02dC %02dC %02dC %02dC\n\n",
00799         //        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]);
00800         
00801         //---------------
00802         // show the bars
00803         int nBarWidth = 3 ;
00804         int nBarSpace = 1 ; // 1 for testing
00805         
00806         int xPos = (xWinMin + xWinMax) / 2 ; 
00807         xPos -= (avg-min) * (nBarWidth + nBarSpace) ;
00808         
00809         for( int i=0; i<=iBinValMax; i++) {
00810             height = 4 * nBin[i] ;
00811             if( height > 100 ) height = 100 ; // clip tops
00812             
00813             // if inside the window, draw the bar
00814             if( ( xPos + nBarWidth < xWinMax ) && ( xPos > xWinMin ) )
00815               tt.fillrect( xPos,yWinMax-height, xPos+nBarWidth-1,yWinMax, Green);  
00816               
00817             // step to the next bar position
00818             xPos += nBarWidth + nBarSpace ; 
00819         }
00820         
00821         showCP=false;
00822     }
00823     
00824     // handle the button
00825     if(sMode==1&&showButtons){
00826         tt.foreground(Yellow);
00827         tt.background(DarkCyan);
00828         tt.set_font((unsigned char*) Arial12x12);
00829         
00830         showButton(1,0,"Request","CP Data",4,4);               
00831         }
00832 }
00833 
00834 //---------------
00835 void config(bool force, bool showButtons){
00836     if (force) {
00837         tt.background(Black);
00838         tt.cls();
00839     }
00840     tt.foreground(Yellow);
00841     tt.background(DarkCyan);
00842     tt.set_font((unsigned char*) Arial12x12);
00843  
00844     //-------- top row --------
00845     showButton(0,0,"Calibrate"," Touch",4,4); // gg - 4x4
00846     showButton(1,0," Reset","",4,4);
00847     showButton(2,0," Save"," Config",4,4);
00848            
00849     // a button to step to the next skin
00850     unsigned int nextSkin = skin + 1 ;
00851     if( nextSkin > maxSkin ) nextSkin = 0 ;
00852     
00853     if( nextSkin == ttSkin ) sprintf(sTemp1,"Skin TT");
00854     else if( nextSkin == ggSkin ) sprintf(sTemp1,"Skin GG");
00855     else sprintf(sTemp1,"Skin %d",nextSkin);
00856 
00857     showButton(3,0," Use",sTemp1,4,4);
00858     
00859     //------- second row -----
00860     if (logEn&&usbEn) {
00861         sprintf(sTemp1,"Disable");
00862     } else {
00863         sprintf(sTemp1,"Enable");
00864     }
00865     showButton(0,1,sTemp1,"Logging",4,4);
00866     
00867     if (repeatPoll) {
00868         sprintf(sTemp1,"Disable");
00869     } else {
00870         sprintf(sTemp1,"Enable");
00871     }
00872     showButton(1,1,sTemp1,"Auto CP",4,4);
00873     
00874     // add Enable/Disable Batt Log gg - yesBattLog
00875     if (yesBattLog) {
00876         sprintf(sTemp1,"Disable");
00877     } else {
00878         sprintf(sTemp1,"Enable");
00879     }
00880     showButton(2,1,sTemp1,"Batt Log",4,4);
00881     
00882     // add Enable/Disable Debug - debugMode
00883     if (debugMode) {
00884         sprintf(sTemp1,"Disable");
00885     } else {
00886         sprintf(sTemp1,"Enable");
00887     }
00888     showButton(3,1,sTemp1," Debug",4,4);    
00889     
00890     if(metric)
00891         showButton(0,2,"Imperial","",4,4);
00892     else
00893        showButton(0,2,"Metric","",4,4);
00894     
00895     showButton(3,2,"Update","Firmware",4,4);
00896     
00897 }
00898 
00899 void pbScreen(bool force, bool showButtons){
00900     if (force) {
00901         tt.background(Black);
00902         tt.cls();
00903     }
00904     tt.foreground(Yellow);
00905     tt.background(DarkCyan);
00906     tt.set_font((unsigned char*) Arial12x12);
00907     if(playbackOpen){
00908         showButton(0,0,"Slower"," <--",4,4);
00909         
00910         if(playbackEn){
00911             sprintf(sTemp1,"Pause");
00912         }else{
00913             sprintf(sTemp1," Run");
00914         }
00915         sprintf(sTemp2,"%4.3f ",playbackInt);
00916         showButton(1,0,sTemp1,sTemp2,4,4);
00917         
00918         showButton(2,0,"Faster","   -->",4,4);
00919     }
00920     if(playbackOpen){
00921         sprintf(sTemp1," Stop");
00922     }else{
00923         sprintf(sTemp1,"Start");
00924     }
00925     showButton(1,1,sTemp1,"Playback",4,4);
00926 }
00927 
00928 void showDateTime(bool force, bool showButtons){
00929     struct tm t; // pointer to a static tm structure
00930     time_t seconds ;
00931     tt.foreground(Yellow);
00932     tt.background(Black);
00933     if (force) {
00934         tt.cls();
00935         seconds = time(NULL);
00936         t = *localtime(&seconds) ;
00937         
00938         tt.locate(10,10);
00939         tt.set_font((unsigned char*) Arial12x12);
00940         strftime(sTemp1, 32, "%a %m/%d/%Y %X  \n", &t);
00941         printf("%s",sTemp1);
00942         if(showButtons){
00943             switch(dtMode){
00944                 case 0:
00945                     sprintf(sTemp1,"Year");
00946                     break;
00947                 case 1:
00948                     sprintf(sTemp1,"Month");
00949                     break;
00950                 case 2:
00951                     sprintf(sTemp1,"Day");
00952                     break;
00953                 case 3:
00954                     sprintf(sTemp1,"Hour");
00955                     break;
00956                 case 4:
00957                     sprintf(sTemp1,"Minute");
00958                     break;
00959                 case 5:
00960                     sprintf(sTemp1,"Second");
00961                     break;
00962                 case 6:
00963                     sprintf(sTemp1,"Select");
00964                     break;
00965                 default:
00966                     break;
00967             }
00968             tt.background(DarkCyan);
00969             showButton(0,1,sTemp1,"",4,4);                          
00970             showButton(1,1,"  UP","",4,4);               
00971             showButton(2,1," DOWN","",4,4);               
00972         }
00973     }
00974 }
00975 
00976 void dteDisplay(bool force, bool showButtons, bool showMiles){
00977     unsigned short i,x,y,lx,ly,gids,radius,color,r,t;
00978     unsigned char toVal;
00979     static unsigned short lgids=0;
00980     static unsigned char leff[39]={0};
00981     CANMessage msg;
00982     unsigned long targetBraking, regenBraking, temp;
00983     static unsigned long maxTarget = 1000, maxRegen = 1000, tardivreg_x1000 = 1000;
00984     static unsigned char lr=0, lt=0;
00985 
00986     msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids
00987     gids = (msg.data[0]<<2)+(msg.data[1]>>6);
00988     if(gids==0){
00989         gids=281; // Display new, fully charged capacity until real data obtained
00990     }
00991 
00992     tt.background(Navy);
00993     tt.foreground(Yellow);
00994     if(force){
00995         tt.set_font((unsigned char*) Arial12x12);   
00996         tt.cls();
00997         x=50+0*6;
00998         tt.locate(x-10,226);
00999         printf("sec\n");  
01000         tt.line(x,10,x,220,DarkGrey);     
01001         x=50+9*6;
01002         tt.locate(x-10,226);
01003         printf("min\n");  
01004         tt.line(x,10,x,220,DarkGrey);     
01005         x=50+18*6;
01006         tt.locate(x-10,226);
01007         printf("hour\n");  
01008         tt.line(x,10,x,220,DarkGrey);
01009         x=50+25*6;
01010         tt.locate(x-10,226);
01011         printf("day\n");  
01012         tt.line(x,10,x,220,DarkGrey);     
01013         x=50+32*6;
01014         tt.locate(x-10,226);
01015         printf("mon\n");  
01016         tt.line(x,10,x,220,DarkGrey);     
01017         x=50+38*6;
01018         //tt.locate(x-10,226);
01019         //printf("year\n");  
01020         //tt.line(x,10,x,220,DarkGrey);
01021         toVal=33;
01022     } else {
01023         toVal=24;// no need to constantly update the long tc values
01024     }
01025     if(force||lgids!=gids){ // update Y axis when kWh changes
01026         //tt.set_font((unsigned char*) Arial12x12);
01027         tt.set_font((unsigned char*) Arial24x23);
01028         //for(i=0;i<10;i++){
01029             //y=200-i*20;
01030         for(i=2;i<7;i++){
01031             y=200-(i-2)*40;
01032             tt.locate(0,y-8);
01033             if (showMiles){
01034                 printf("%3.0f\n",convertDistance(i*((float)(gids-5)*.075))); // LM - Added metric support
01035                 //printf("%2.0f  \n",i*((float)(gids-5)*.075));
01036             }else{
01037                 printf("%d.0\n",i);
01038             }
01039             tt.line(48,y,toVal*6+56,y,DarkGrey);
01040         }
01041         lgids=gids;    
01042     }
01043     if(tock||force){
01044         for(i=2;i<7;i++){
01045             y=200-(i-2)*40;
01046             tt.line(40,y,158,y,DarkGrey);
01047         }
01048 
01049         x=50+0*6; 
01050         tt.line(x,10,x,220,DarkGrey);     
01051         x=50+9*6;
01052         tt.line(x,10,x,220,DarkGrey);     
01053         x=50+18*6;
01054         tt.line(x,10,x,220,DarkGrey);     
01055         //x=50+25*6;
01056         //tt.line(x,60,x,220,DarkGrey);     
01057         //x=50+32*6;
01058         //tt.line(x,60,x,220,DarkGrey);     
01059         //x=50+38*6;
01060         //tt.line(x,60,x,220,DarkGrey);     
01061         tt.set_font((unsigned char*) SCProSB31x55);
01062         tt.foreground(Green);
01063         if (showMiles){
01064             float miles = mpkWh[dtePeriod]*((float)(gids-5)*.075);
01065             miles = convertDistance(miles); // LM - Metric support
01066             // Right justify
01067             if (miles>99.9){ //space=18; num=31; . = 23
01068                 tt.locate(161,8);
01069                 printf("%4.1f\n",miles);
01070             } else if (miles>9.9){
01071                 tt.locate(156,8);
01072                 printf("  %3.1f\n",miles);
01073             } else {
01074                 tt.locate(151,8);
01075                 printf("    %2.1f\n",miles);
01076             }
01077             tt.foreground(Cyan);
01078             tt.set_font((unsigned char*) Arial24x23);
01079             tt.locate(198,70);
01080             printf("%3.1f \n",mpkWh[dtePeriod]);
01081         } else {
01082             tt.locate(200,10);
01083             printf("%3.1f \n",mpkWh[dtePeriod]);
01084         }
01085         lx=50;
01086         ly=mpkWh[0]*40;
01087         if(dtePeriod==0){
01088             radius=6;
01089             color=Yellow;
01090         }else{
01091             radius=2;
01092             color=Green;
01093         }
01094         if(ly<100){
01095             ly=220;
01096             color=Red;
01097         }else if(ly<320) {
01098             ly=320-ly;
01099         }else{
01100             ly=0;
01101         }
01102         tt.fillcircle(lx,leff[0],radius,Navy);
01103         tt.fillcircle(lx,ly,radius,color);
01104 
01105         for(i=1;i<toVal;i++){
01106             x=50+i*6;
01107             y=mpkWh[i]*40;
01108             if(i==dtePeriod){
01109                 radius=6;
01110                 color=Yellow;
01111             }else{
01112                 radius=2;
01113                 color=Green;
01114             }
01115             if(y<60){
01116                 y=220;
01117                 color=Red;
01118             }else if(y<280) {
01119                 y=280-y;
01120             }else{
01121                 y=0;
01122             }
01123             tt.fillcircle(x,leff[i],radius,Navy);
01124             tt.line(x-6,leff[i-1],x,leff[i],Navy);
01125             leff[i-1]=ly;
01126             if(y>0){
01127                 tt.fillcircle(x,y,radius,color);
01128             }
01129             tt.line(lx,ly,x,y,White);
01130             lx=x;
01131             ly=y;
01132         }
01133         leff[i-1]=y;
01134     }
01135 
01136     msg = lastMsg[indexLastMsg[0x1cb]]; //Get Target and Regen
01137     regenBraking = (msg.data[0]<<3)+(msg.data[1]>>5);
01138     targetBraking = (msg.data[2]<<3)+(msg.data[3]>>5);
01139     msg = lastMsg[indexLastMsg[0x421]]; //Get Drive Mode
01140     if (msg.data[0]==0x18) { // In Neutral
01141         regenBraking = 0;  // No regen when in Neutral
01142     }
01143 
01144     if (targetBraking<2045){
01145         if ((targetBraking>50)&&(regenBraking>50)){
01146             temp = targetBraking;
01147             temp *= 1000;
01148             temp /= regenBraking;
01149             if (temp<tardivreg_x1000) tardivreg_x1000=temp;
01150         }
01151         if (targetBraking>maxTarget) maxTarget=targetBraking;
01152         if (regenBraking>maxRegen) maxRegen=regenBraking;
01153     
01154         temp = targetBraking;
01155         temp *=200;
01156         temp /= maxTarget;
01157         t = (char) temp;
01158         if (t>175) t=175;
01159         temp = regenBraking;
01160         temp *= tardivreg_x1000;
01161         temp /= maxTarget;
01162         temp /= 5; // 1000/200=5
01163         r = (char) temp;
01164         if (r>175) r=175;
01165         if (r>t) t=r;  //Should never happen
01166         if(lr!=r||lt!=t){
01167             tt.fillrect(264,64,310,239-t,Navy);
01168             tt.fillrect(264,239-t,310,239-r,Red);
01169             tt.fillrect(264,239-r,310,239,Green);
01170         }
01171         lt=t;
01172         lr=r;
01173     }
01174 }
01175 
01176 void updateDisplay(char display){
01177     bool changed,showButtons;
01178     changed = dMode[display]!=lastDMode[display];
01179     showButtons = (display==whichTouched)&&(sMode==1);
01180     tt.set_display(display);
01181     switch (dMode[display]) {
01182         case logScreen:
01183             printLog(changed,showButtons);
01184             break;
01185         case mainScreen:
01186             mainDisplay(changed,showButtons);
01187             break;
01188         case brakeScreen:
01189             braking(changed,showButtons);
01190             break;
01191         case dteScreen:
01192             dteDisplay(changed,showButtons,true);
01193             break;
01194          case effScreen:
01195             dteDisplay(changed,showButtons,false);
01196             break;
01197        case monitorScreen:
01198             printLast(changed,showButtons);
01199             break;
01200         case changedScreen:
01201             printChanged(changed,showButtons);
01202             break;
01203         case cpScreen:
01204             cpData(changed||showCP,showButtons);
01205             break;
01206         case configScreen:
01207             config(changed,showButtons);
01208             break;
01209         case playbackScreen:
01210             pbScreen(changed,showButtons);
01211             break;
01212         case dateScreen:
01213             showDateTime(changed,showButtons);
01214             break;
01215         case cpHistScreen: // gg - hist
01216             cpHistogram(changed||showCP,showButtons);
01217             break;
01218         case cpBarScreen: // gg - cpbars
01219             cpBarPlot(changed||showCP,showButtons);
01220             break;
01221         case indexScreen:
01222             showIndex(changed,showButtons);
01223             break;
01224         case tripScreen:
01225             tripDisplay(changed,showButtons);
01226             break;
01227         case healthScreen:
01228             healthDisplay(changed,showButtons);
01229             break;
01230         default:
01231             if (changed){
01232                 tt.background(Black);
01233                 tt.cls();
01234             }
01235             break;
01236     }
01237     lastDMode[display]=dMode[display];
01238 
01239     if(display==whichTouched){
01240         switch (sMode) {
01241             case 1: // Select screens
01242                 tt.foreground(Yellow);
01243                 tt.background(DarkCyan);
01244                 tt.set_font((unsigned char*) Arial12x12);   
01245                            
01246                 showButton(0,tNavRow," <-Prev","",4,4); // gg - 4x4
01247                 // col 1 see below
01248                 showButton(2,tNavRow," Go To"," Index",4,4); // gg - index
01249                 showButton(3,tNavRow," Next->","",4,4); // gg - move next
01250                 
01251                 // col 1 in Nav row                              
01252                 switch (dMode[display]) {
01253                     case offScreen:
01254                         sprintf(sTemp2,"  Off");
01255                         break;
01256                     case logScreen:
01257                         sprintf(sTemp2,"  Log");
01258                         break;
01259                     case mainScreen:
01260                         sprintf(sTemp2,"  Main");
01261                         break;
01262                     case brakeScreen:
01263                         sprintf(sTemp2,"Braking");
01264                         break;
01265                     case dteScreen:
01266                         sprintf(sTemp2,"  DTE");
01267                         break;
01268                     case effScreen:
01269                         sprintf(sTemp2,"  Eff");
01270                         break;
01271                     case monitorScreen:
01272                         sprintf(sTemp2," Monitor");
01273                         break;
01274                     case changedScreen:
01275                         sprintf(sTemp2,"DeltaMon");
01276                         break;
01277                     case cpScreen:
01278                         sprintf(sTemp2,"CP Data");
01279                         break;
01280                     case configScreen:
01281                         sprintf(sTemp2," Config");
01282                         break;          
01283                     case playbackScreen:
01284                         sprintf(sTemp2,"Playback");
01285                         break;
01286                     case dateScreen:
01287                         sprintf(sTemp2,"Set Time");
01288                         break;
01289                     case cpHistScreen: // gg - hist
01290                         sprintf(sTemp2,"CP Hist");
01291                         break;
01292                     case cpBarScreen: // gg - cpbars
01293                         sprintf(sTemp2,"CP Bars");
01294                         break;
01295                     case tripScreen:
01296                         sprintf(sTemp2," Trip");
01297                         break;
01298                     case healthScreen:
01299                         sprintf(sTemp2,"Health");
01300                         break;
01301                     case indexScreen: // gg - index
01302                         sprintf(sTemp2," Index");
01303                         break;
01304                 }
01305                 showButton(1,tNavRow," Select",sTemp2,4,4);
01306                 
01307                 wait_ms(100); //  pause a moment to reduce flicker
01308                 break;
01309                 
01310             case 2: // numpad
01311                 tt.foreground(Yellow);
01312                 tt.background(DarkCyan);
01313                 tt.set_font((unsigned char*) Arial24x23);
01314 
01315                 sprintf(sTemp2,"");
01316                 showButton(0,0," 1",sTemp2,4,4);
01317                 showButton(1,0," 2",sTemp2,4,4);
01318                 showButton(2,0," 3",sTemp2,4,4);
01319                 showButton(0,1," 4",sTemp2,4,4);
01320                 showButton(1,1," 5",sTemp2,4,4);
01321                 showButton(2,1," 6",sTemp2,4,4);
01322                 showButton(0,2," 7",sTemp2,4,4);
01323                 showButton(1,2," 8",sTemp2,4,4);
01324                 showButton(2,2," 9",sTemp2,4,4);
01325                 showButton(1,3," 0",sTemp2,4,4);
01326                 
01327                 showButton(0,3,"<--",sTemp2,4,4);
01328                 showButton(2,3,"-->",sTemp2,4,4);
01329                 showButton(3,3,"return",sTemp2,4,4);
01330             case 3:
01331                 break;
01332             default:
01333                 break;
01334         }
01335     }
01336     tock=false;
01337 } // updateDisplay
01338 
01339 //---------------------
01340 // gg - highlight
01341 void highlightButton(unsigned char column, unsigned char row, unsigned char tScn, unsigned char columns, unsigned char rows){
01342 
01343     unsigned short x1,x2,y1,y2;
01344     
01345     x1=column*(320/columns)+btnGap/2;
01346     x2=(column+1)*(320/columns)-btnGap/2;
01347     y1=row*(240/rows)+btnGap/2;
01348     y2=(row+1)*(240/rows)-btnGap/2;
01349     
01350     tt.set_display(tScn);
01351 
01352     if( skin == ggSkin ){
01353         // paint the whole button box, for a better visual effect
01354         //  especially on a screen with a yellow background
01355         if( tScn == 0 )
01356             tt.fillrect(x1,y1,x2,y2,White); // DarkCyan);
01357         else
01358             tt.fillrect(x1,y1,x2,y2,Green); // DarkCyan);
01359     } else {
01360         tt.fillrect(x1,y1,x2,y2,Green); // DarkCyan);
01361     }
01362 
01363     // paint the outer pixel as a yellow frame
01364     tt.rect(x1,y1,x2,y2,Yellow) ; // DarkCyan);
01365 }
01366 
01367 //---------------------
01368 void showButton(unsigned char column, unsigned char row, char * text1, char * text2, unsigned char columns, unsigned char rows){
01369     unsigned short x1,x2,y1,y2;
01370     
01371     x1=column*(320/columns)+btnGap/2;
01372     x2=(column+1)*(320/columns)-btnGap/2;
01373     y1=row*(240/rows)+btnGap/2;
01374     y2=(row+1)*(240/rows)-btnGap/2;
01375     tt.fillrect(x1,y1,x2,y2,DarkCyan);
01376     
01377     // adapt formatting of text to the smaller 4x4 box
01378     tt.locate(x1+btnGap/2,y1+btnGap); // gg - 4x4
01379     printf("%s\n",text1);
01380     
01381     tt.locate(x1+btnGap/2,y1+btnGap+20);
01382     printf("%s\n",text2);
01383 }
01384 
01385 
01386 //The temps are stored as metric, distances as imperial... I'm assuming the input based on that - LM
01387 float convertTemperature(float input) 
01388 {
01389     if (!metric) {
01390         //convert!
01391         float output = input *1.8f;
01392         output += 32.0f;
01393         return output;   
01394     }
01395     return input;
01396 }
01397 float convertDistance(float input)
01398 {
01399     if (metric) {
01400         return input / 0.62137f;
01401     }
01402     return input;
01403 }
01404 char* distanceUnit()
01405 {
01406     if(metric)
01407         return "km";
01408     return "mi";
01409 }
01410 char* temperatureUnit()
01411 {
01412     if(metric)
01413         return "C";
01414     return "F";
01415 }