test fork

Dependencies:   SPI_TFTx2 SPI_TFTx2_ILI9341 TFT_fonts TOUCH_TFTx2 mbed

Fork of CANary_9341 by Tick Tock

displayModes.cpp

Committer:
TickTock
Date:
2013-03-23
Revision:
36:dbd39c315258
Parent:
35:5acbd8a64a89
Child:
37:fea2c1d52c5f

File content as of revision 36:dbd39c315258:

//displayModes.cpp

#include "displayModes.h"
char sTemp1[40];
char sTemp2[16];

void printLast (bool force){
    CANMessage msg;
    tt.locate(0,6);
    tt.foreground(Red);
    tt.background(Yellow);
    if(force) tt.cls(); // Just clear screen if forced - always update display
    tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
    for(int i=0; i<19; i++){
        msg = lastMsg[i+indexOffset];
        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]);
    }
    if(sMode==1){
        tt.foreground(Yellow);
        tt.background(DarkCyan);
        tt.set_font((unsigned char*) Arial12x12);
        sprintf(sTemp1,"<up>");
        sprintf(sTemp2,"");
        showButton(0,0,sTemp1,sTemp2,3,3);
        tt.foreground(Yellow);
        tt.background(DarkCyan);
        tt.set_font((unsigned char*) Arial12x12);
        sprintf(sTemp1,"<down>");
        showButton(2,0,sTemp1,sTemp2,3,3);
    }
}

void printChanged (bool force){
    CANMessage msg;
    unsigned char i,j;
    tt.locate(0,6);
    tt.foreground(Red);
    tt.background(Yellow);
    if(force) tt.cls(); // Just clear screen if forced - always update display
    tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
    i=0;
    j=indexOffset;
    do{
        j=j<99?j+1:j;
        if(msgChanged[j]>0){
            msg = lastMsg[j];
            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]);
            i++;
        }// if changed
    }while(i<19&&j<99);
}

void printLog (bool force){
    static unsigned char lastDisplayLoc = 0;
    if(force||displayLoc!=lastDisplayLoc){ //only update if changed
        tt.foreground(Amber);
        tt.background(Black);
        tt.cls();
        tt.locate(0,6);
        tt.set_font((unsigned char*) Arial12x12);
        for(int i=0; i<19; i++){
            printf("%s",displayLog[displayLoc]);
            displayLoc=displayLoc>17?0:displayLoc+1;
        }
    }
    lastDisplayLoc=displayLoc;
}

void printDTE (bool force){
    unsigned short gids, SOC, packV;
    static unsigned short lgids=0, lSOC=0, lpackV=0;
    CANMessage msg;

    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);

    tt.background(Navy);
    if(force) tt.cls();
    if(force||gids!=lgids){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(10,10);
        printf("%4d gids\n",gids);
        tt.locate(20,40);
        printf("%4.1f kWh\n",(float)gids*0.08);
        tt.set_font((unsigned char*) SCProSB31x55);
        tt.foreground(Green);
        tt.locate(60,96);
        printf("%4.1f mi  \n",(float)(gids-5)*0.31); // Approx for now
        lgids=gids;
    }
    if(force||SOC!=lSOC){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(200,10);
        printf("%4.1f%s\n",(float)SOC/10,"%");
        lSOC=SOC;
    }
    if(force||packV!=lpackV){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(200,200);
        printf("%4.1fV\n",(float)packV/2);
        lpackV=packV;
    }
    if(force||tick){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(20,200);
        printf("%4.2fV  \n",accV);
    }
}

void braking (bool force, bool prdata=false){
    unsigned long targetBraking, regenBraking;
    static unsigned long maxTarget = 1000, maxRegen = 1000, tarDivReg = 1000;
    unsigned long temp;
    static unsigned char lastPressure[4] = {200,200,200,200};
    unsigned char i,r,t;
    static unsigned char lr, lt;
    CANMessage msg;

    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);
    if ((targetBraking>50)&&(regenBraking>50)){
        temp = targetBraking;
        temp *= 1000;
        temp /= regenBraking;
        if (temp<tarDivReg) tarDivReg=temp;
    }
    if (targetBraking>maxTarget) maxTarget=targetBraking;
    if (regenBraking>maxRegen) maxRegen=regenBraking;

    msg = lastMsg[indexLastMsg[0x1ca]]; //Get brake pressure
    tt.background(Navy);
    if (force) {
        tt.cls();
        tt.rect(0,111,170,239,White);
        tt.line(0,207,170,207,White);
        tt.line(0,175,170,175,White);
        tt.line(0,143,170,143,White);
        lastPressure[0] = 200;
        lastPressure[1] = 200;
        lastPressure[2] = 200;
        lastPressure[3] = 200;
    }
    // plot bar graph for each wheel pressure
    for (i=0; i<4; i++){
        if (msg.data[i]<239) {
            if (msg.data[i]>lastPressure[i]){
                tt.fillrect(10+40*i,239-msg.data[i],40+40*i,239,Red);
            } else if (msg.data[i]<lastPressure[i]) {
                tt.fillrect(10+40*i,238-lastPressure[i],40+40*i,238-msg.data[i],Navy);
            }
            lastPressure[i]=msg.data[i];
        }
    }

    temp = targetBraking;
    temp *=200;
    temp /= maxTarget;
    t = (char) temp;
    if (t>200) t=200;
    temp = regenBraking;
    temp *= tarDivReg;
    temp /= maxTarget;
    temp /= 5;
    r = (char) temp;
    if (r>200) r=200;
    if(lr!=r&&prdata){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(100,40);
        printf("%d %d    \n",regenBraking,maxRegen);
        tt.locate(100,70);
        printf("%3.1f (%3.1f%s)    \n",(float)tarDivReg/10,(float)regenBraking*tarDivReg/targetBraking/10,"%");
    }    
    if(lt!=t&&prdata){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(100,10);
        printf("%d %d    \n",targetBraking,maxTarget);
    }
    if (r>t) t=r;  //Should never happen
    if((lr!=r||lt!=t)&&!prdata){
        tt.fillrect(200,10,300,239-t,Navy);
        tt.fillrect(200,239-t,300,239-r,Red);
        tt.fillrect(200,239-r,300,239,Green);
    }
    lt=t;
    lr=r;
}

void cpData(bool force){
    short unsigned max, min, jv, i, bd;
    unsigned avg;
    if(force){
        tt.foreground(White);
        tt.background(Navy);
        tt.set_font((unsigned char*) Arial12x12_prop);  // select the font
        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;
        }
        tt.cls();
        tt.locate(0,6);
        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[224+5],battData[224+8],battData[224+11],battData[224+14]);
        tt.locate(0,36);
        for(i=0; i<16; i++){
            printf("%02d-%02d : %04d %04d %04d %04d %04d %04d\n",i*6+1,i*6+6,(battData[i*12+3]<<8)+battData[i*12+4],(battData[i*12+5]<<8)+battData[i*12+6],(battData[i*12+7]<<8)+battData[i*12+8],(battData[i*12+9]<<8)+battData[i*12+10],(battData[i*12+11]<<8)+battData[i*12+12],(battData[i*12+13]<<8)+battData[i*12+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);
        tt.rect(8+3*41,16,40+3*41,28,Red);
        for(i=0; i<96; i++){
            bd=(battData[i*2+3]<<8)+battData[i*2+4];
            if(bd>0){
                if(bd==max) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Green);
                //if(bd==avg) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,White);
                if(bd==min) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Yellow);
                if(bd<jv) tt.rect(58+(i%6)*41,34+(int)(i/6)*12,90+(i%6)*41,46+(int)(i/6)*12,Red);
            }
        }
        showCP=false;
    }
    if(sMode==1){
        tt.foreground(Yellow);
        tt.background(DarkCyan);
        tt.set_font((unsigned char*) Arial12x12);
        sprintf(sTemp1,"Request");
        sprintf(sTemp2,"CP data");
        showButton(1,0,sTemp1,sTemp2,3,3);               
    }
}

void config1(bool force){
    if (force) {
        tt.background(Black);
        tt.cls();
    }
    tt.foreground(Yellow);
    tt.background(DarkCyan);
    tt.set_font((unsigned char*) Arial12x12);
    sprintf(sTemp1," Calibrate");
    sprintf(sTemp2,"  Touch");
    showButton(0,0,sTemp1,sTemp2,3,3);
    sprintf(sTemp1,"  Reset");
    sprintf(sTemp2,"");
    showButton(1,0,sTemp1,sTemp2,3,3);
    sprintf(sTemp1,"  Save");
    sprintf(sTemp2,"  Config");
    showButton(2,0,sTemp1,sTemp2,3,3);
    if (logEn) {
        sprintf(sTemp1," Disable");
    } else {
        sprintf(sTemp1," Enable");
    }
    sprintf(sTemp2," Logging");
    showButton(0,1,sTemp1,sTemp2,3,3);
    if (repeatPoll) {
        sprintf(sTemp1," Disable");
    } else {
        sprintf(sTemp1," Enable");
    }
    sprintf(sTemp2," Auto CP");
    showButton(1,1,sTemp1,sTemp2,3,3);
    sprintf(sTemp1," Start");
    sprintf(sTemp2,"Playback");
    showButton(2,1,sTemp1,sTemp2,3,3);
}

void config2(bool force){
    if (force) {
        tt.background(Black);
        tt.cls();
    }
    tt.foreground(Yellow);
    tt.background(DarkCyan);
    tt.set_font((unsigned char*) Arial12x12);
    if(playbackOpen){
        sprintf(sTemp1,"Slower");
        sprintf(sTemp2," <--");
        showButton(0,0,sTemp1,sTemp2,3,3);
        if(playbackEn){
            sprintf(sTemp1," Pause");
        }else{
            sprintf(sTemp1,"  Run");
        }
        sprintf(sTemp2," %4.3f ",playbackInt);
        showButton(1,0,sTemp1,sTemp2,3,3);
        sprintf(sTemp1,"Faster");
        sprintf(sTemp2,"     -->");
        showButton(2,0,sTemp1,sTemp2,3,3);
    }
    if(playbackOpen){
        sprintf(sTemp1," Stop");
    }else{
        sprintf(sTemp1," Start");
    }
    sprintf(sTemp2,"Playback");
    showButton(1,1,sTemp1,sTemp2,3,3);
}

void showDateTime(bool force){
    struct tm t; // pointer to a static tm structure
    time_t seconds ;
    tt.foreground(Yellow);
    tt.background(Navy);
    if (force) {
        tt.cls();
        seconds = time(NULL);
        t = *localtime(&seconds) ;
        
        tt.locate(10,10);
        strftime(sTemp1, 32, "%a %m/%d/%Y %X  \n", &t);
        printf("%s",sTemp1);
        if(sMode==1){
            tt.foreground(Yellow);
            tt.background(DarkCyan);
            tt.set_font((unsigned char*) Arial12x12);
            sprintf(sTemp2,"");
            switch(dtMode){
                case 0:
                    sprintf(sTemp1,"Year");
                    break;
                case 1:
                    sprintf(sTemp1,"Month");
                    break;
                case 2:
                    sprintf(sTemp1,"Day");
                    break;
                case 3:
                    sprintf(sTemp1,"Hour");
                    break;
                case 4:
                    sprintf(sTemp1,"Minute");
                    break;
                case 5:
                    sprintf(sTemp1,"Second");
                    break;
                case 6:
                    sprintf(sTemp1,"Select");
                    break;
                default:
                    break;
            }
            showButton(0,1,sTemp1,sTemp2,3,3);               
            sprintf(sTemp1,"   UP");
            showButton(1,1,sTemp1,sTemp2,3,3);               
            sprintf(sTemp1,"  DOWN");
            showButton(2,1,sTemp1,sTemp2,3,3);               
        }
    }
}

void updateDisplay(char display){
    bool changed;
    changed = dMode[display]!=lastDMode[display];
    tt.set_display(display);
    switch (dMode[display]) {
        case logScreen:
            printLog(changed);
            break;
        case dteScreen:
            printDTE(changed);
            break;
        case brakeScreen:
            braking(changed);
            break;
        case powerScreen:
            //braking(changed, true);
            //break;
        case monitorScreen:
            printLast(changed);
            break;
        case changedScreen:
            printChanged(changed);
            break;
        case cpScreen:
            cpData(changed||showCP);
            break;
        case config1Screen:
            config1(changed);
            break;
        case config2Screen:
            config2(changed);
            break;
        case dateScreen:
            showDateTime(changed);
            break;
        default:
            tt.background(Black);
            tt.cls();
            break;
    }
    lastDMode[display]=dMode[display];

    switch (sMode) {
        case 1: // Select screens
            tt.foreground(Yellow);
            tt.background(DarkCyan);
            tt.set_font((unsigned char*) Arial12x12);
            sprintf(sTemp1,"<-Prev");
            sprintf(sTemp2,"");
            showButton(0,2,sTemp1,sTemp2,3,3);
            sprintf(sTemp1," Select %d",dMode[display]);
            showButton(1,2,sTemp1,sTemp2,3,3);
            sprintf(sTemp1,"   Next->");
            showButton(2,2,sTemp1,sTemp2,3,3);
            break;
        case 2: // numpad
            sprintf(sTemp2,"");
            tt.foreground(Yellow);
            tt.background(DarkCyan);
            tt.set_font((unsigned char*) Arial24x23);
            sprintf(sTemp1," 1");
            showButton(0,0,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 2");
            showButton(1,0,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 3");
            showButton(2,0,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 4");
            showButton(0,1,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 5");
            showButton(1,1,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 6");
            showButton(2,1,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 7");
            showButton(0,2,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 8");
            showButton(1,2,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 9");
            showButton(2,2,sTemp1,sTemp2,4,4);
            sprintf(sTemp1," 0");
            showButton(1,3,sTemp1,sTemp2,4,4);
            sprintf(sTemp1,"<--");
            showButton(0,3,sTemp1,sTemp2,4,4);
            sprintf(sTemp1,"-->");
            showButton(2,3,sTemp1,sTemp2,4,4);
            sprintf(sTemp1,"return");
            showButton(3,3,sTemp1,sTemp2,4,4);
        case 3:
            break;
        default:
            break;
    }
}

void showButton(unsigned char column, unsigned char row, char * text1, char * text2, unsigned char columns, unsigned char rows){
    unsigned short x1,x2,y1,y2;
    x1=column*(320/columns)+btnGap/2;
    x2=(column+1)*(320/columns)-btnGap/2;
    y1=row*(240/rows)+btnGap/2;
    y2=(row+1)*(240/rows)-btnGap/2;
    tt.fillrect(x1,y1,x2,y2,DarkCyan);
    tt.locate(x1+btnGap,y1+btnGap);
    printf("%s\n",text1);
    tt.locate(x1+btnGap,y1+btnGap+30);
    printf("%s\n",text2);
}

/*void braking (bool force, bool prdata=false){
    unsigned long targetBraking, regenBraking, speed;
    static unsigned long maxTarget = 20000, maxRegen = 20000, tarDivReg = 1000;
    short rpm;
    unsigned long temp;
    static unsigned char lastPressure[4] = {200,200,200,200};
    unsigned char i,r,t;
    static unsigned char lr, lt;
    CANMessage msg;

    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 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
    if ((targetBraking>2039)||(speed>200)) { //Filter weird messages
        targetBraking = 0;
        regenBraking = 0;
    } else {
        if ((targetBraking>50)&&(regenBraking>50)){
            temp = targetBraking;
            temp *= 1000;
            temp /= regenBraking;
            if (temp<tarDivReg) tarDivReg=temp;
        }
        targetBraking *= speed;
        regenBraking *= speed;
        if (targetBraking>maxTarget) maxTarget=targetBraking;
        if (regenBraking>maxRegen) maxRegen=regenBraking;
    }

    msg = lastMsg[indexLastMsg[0x1ca]]; //Get brake pressure
    tt.background(Navy);
    if (force) {
        tt.cls();
        tt.rect(0,111,170,239,White);
        tt.line(0,207,170,207,White);
        tt.line(0,175,170,175,White);
        tt.line(0,143,170,143,White);
        lastPressure[0] = 200;
        lastPressure[1] = 200;
        lastPressure[2] = 200;
        lastPressure[3] = 200;
    }
    // plot bar graph for each wheel pressure
    for (i=0; i<4; i++){
        if (msg.data[i]<239) {
            if (msg.data[i]>lastPressure[i]){
                tt.fillrect(10+40*i,239-msg.data[i],40+40*i,239,Red);
            } else if (msg.data[i]<lastPressure[i]) {
                tt.fillrect(10+40*i,238-lastPressure[i],40+40*i,238-msg.data[i],Navy);
            }
            lastPressure[i]=msg.data[i];
        }
    }

    temp = targetBraking;
    temp *=200;
    temp /= maxTarget;
    t = (char) temp;
    if (t>200) t=200;
    temp = regenBraking;
    temp *= tarDivReg;
    temp /= maxTarget;
    temp /= 5;
    r = (char) temp;
    if (r>200) r=200;
    if(lr!=r&&prdata){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(100,40);
        printf("%d %d    \n",regenBraking,maxRegen);
        tt.locate(100,70);
        printf("%3.1f (%3.1f%s)    \n",(float)tarDivReg/10,(float)regenBraking*tarDivReg/targetBraking/10,"%");
    }    
    if(lt!=t&&prdata){
        tt.foreground(Yellow);
        tt.set_font((unsigned char*) Arial28x28);
        tt.locate(100,10);
        printf("%d %d    \n",targetBraking,maxTarget);
    }
    if (r>t) t=r;  //Should never happen
    if((lr!=r||lt!=t)&&!prdata){
        tt.fillrect(200,10,300,239-t,Navy);
        tt.fillrect(200,239-t,300,239-r,Red);
        tt.fillrect(200,239-r,300,239,Green);
    }
    lt=t;
    lr=r;
}*/