test fork
Dependencies: SPI_TFTx2 SPI_TFTx2_ILI9341 TFT_fonts TOUCH_TFTx2 mbed
Fork of CANary_9341 by
Diff: main.cpp
- Revision:
- 5:ebf6fa994b78
- Parent:
- 4:8d7759f4fe7a
- Child:
- 7:17bf9ceaf0aa
--- a/main.cpp Sun Feb 17 16:44:59 2013 +0000 +++ b/main.cpp Tue Feb 19 18:19:47 2013 +0000 @@ -1,5 +1,8 @@ //To Do: -// USB device detec +// USB device detect +// config file on local fs +// user programmable message decode +// brake trainer #include "CANary.h" // write and read the Mode Data LocalFileSystem local("local"); // test the local file system to write files @@ -33,19 +36,19 @@ char writeBuffer[maxBufLen][13]; // buffer for USB write char indexLastMsg[0x800]={0}; // index table for last message CANMessage lastMsg[100]; // table to store last message of eachtype -char ii = 0; // indexindex char c; volatile int writePointer = 0; volatile int secsNoMsg = 0, secsNoTouch = 0; volatile bool canIdle = false, userIdle = false; point lastTouch; char counter = 0; -int dMode[2] = {1,6}; //display mode +int dMode[2] = {4,2}; //display mode int sMode = 0; // setup mode int lastDMode[2]; //last screen mode int lastSMode = 0; char displayLog[19][40]; int displayLoc = 0; +int indexOffset = 1; TOUCH_TFTx2 tt(p16, p17, p19, p20, p11, p12, p13, p6, p7, p5, "TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs0, cs1, reset @@ -70,38 +73,10 @@ int isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC return ((isecs<<10)+msec) ; // return the two byte time stamp } - -void printLast (){ - int i; - CANMessage msg; - tt.locate(0,6); - tt.foreground(Red); - tt.set_font((unsigned char*) Arial12x12_prop); // select the font - for(i=0; i<19; i++){ - msg = lastMsg[i+1]; - 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]); - } -} - -void printLog (){ - int i; - tt.locate(0,6); - tt.foreground(Amber); - tt.cls(); - for(i=0; i<19; i++){ - printf("%s",displayLog[displayLoc]); - displayLoc=displayLoc>17?0:displayLoc+1; - } -} - -void logMsg (char *msg) { - strcpy(displayLog[displayLoc],msg); - displayLoc=displayLoc>17?0:displayLoc+1; -} - void logCan (char mtype, CANMessage canRXmsg) { unsigned short ts = getTimeStamp(); unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 + static char ii = 0; // indexindex if(canRXmsg.id>0) { writeBuffer[writePointer][0]=mtype; writeBuffer[writePointer][1]=((secs%60)<<2)+((ts&0x300)>>8); @@ -174,47 +149,187 @@ led2 = !led2; } +void logMsg (char *msg) { + strcpy(displayLog[displayLoc],msg); + displayLoc=displayLoc>17?0:displayLoc+1; +} + void touched(){ LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF); lastTouch = tt.get_touch(); lastTouch = tt.to_pixel(lastTouch); // convert to pixel pos - //printf("X=%d Y=%d\n",lastTouch.x,lastTouch.y); - /*if (lastTouch.x > tt.width()){ - tt.set_display(1); - tt.fillcircle(lastTouch.x-tt.width(),lastTouch.y,10,Blue); - }else{ - tt.set_display(0); - tt.fillcircle(lastTouch.x,lastTouch.y,10,Blue); - }*/ secsNoTouch = 0; } -void updateDisplay(){ +void printLast (bool force){ + CANMessage msg; + if(force) tt.cls(); + 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]); + } +} + +void printLog (bool force){ static int 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 dte (bool force){ + unsigned gids, SOC, packV; + static unsigned 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(Amber); + tt.set_font((unsigned char*) Arial28x28); + tt.locate(10,10); + printf("%4d gids\n",gids); + tt.locate(10,200); + 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*0.33); // Approx for now + lgids=gids; + } + if(force||SOC!=lSOC){ + tt.foreground(Amber); + 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(Amber); + tt.set_font((unsigned char*) Arial28x28); + tt.locate(200,200); + printf("%4.1fV\n",(float)packV/2); + lpackV=packV; + } +} + +void braking (bool force, bool prdata){ + unsigned short targetBraking, regenBraking, speed; + static unsigned short maxTarget = 0, maxRegen = 0, tarDivReg = 0; + 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); + if (targetBraking>maxTarget) maxTarget=targetBraking; + if (regenBraking>maxRegen) maxRegen=regenBraking; + if (regenBraking>50) { + temp = 1000*targetBraking; + temp /= regenBraking; + if (temp>tarDivReg) tarDivReg=temp; + } + 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 + 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]; + } + } + + if(targetBraking>50){ + targetBraking *= speed; + regenBraking *= speed; + temp = 200*targetBraking/maxTarget; + t = (char) temp; + temp = 200*regenBraking*tarDivReg/maxTarget; + r = (char) temp; + if(lr!=r&&prdata){ + tt.foreground(Amber); + tt.set_font((unsigned char*) Arial28x28); + tt.locate(100,50); + printf("%d %d \n",regenBraking,maxRegen); + tt.locate(100,90); + printf("%3.1f (%3.1f%s) \n",(float)tarDivReg/1000,(float)regenBraking*tarDivReg/targetBraking/1000,"%"); + } + if(lt!=t&&prdata){ + tt.foreground(Amber); + tt.set_font((unsigned char*) Arial28x28); + tt.locate(100,10); + printf("%d %d \n",targetBraking,maxTarget); + } + if((lr!=r||lt!=t)&&!prdata){ + tt.fillrect(200,239-t,300,238-r,Red); + tt.fillrect(200,239-r,300,239,Green); + lt=t; + lr=r; + } + } +} + +void updateDisplay(){ + bool changed; for (int i=0; i<2; i++){ + changed = dMode[i]!=lastDMode[i]||(lastSMode>0&&sMode==0); tt.set_display(i); switch (dMode[i]) { case logMode: - tt.background(Black); - if(dMode[i]!=lastDMode[i]||(lastSMode>0&&sMode==0)||(lastDisplayLoc!=displayLoc)){ - printLog(); - lastDisplayLoc=displayLoc; - } + printLog(changed); break; case dteMode: - tt.background(Black); - if(dMode[i]!=lastDMode[i]||(lastSMode>0&&sMode==0)) tt.cls(); - tt.fillrect(10,10,300,50,Blue); - break; + dte(changed); + break; case brakeMode: - tt.background(Black); - if(dMode[i]!=lastDMode[i]||(lastSMode>0&&sMode==0)) tt.cls(); - tt.fillcircle(100,100,15,Red); + braking(changed,true); break; case powerMode: - tt.background(Black); - if(dMode[i]!=lastDMode[i]||(lastSMode>0&&sMode==0)) tt.cls(); - tt.fillrect(10,10,300,50,Green); + braking(changed,false); break; case controlMode: tt.background(Black); @@ -222,9 +337,7 @@ tt.fillcircle(100,100,15,Orange); break; case monitorMode: - tt.background(Yellow); - if(dMode[i]!=lastDMode[i]||(lastSMode>0&&sMode==0)) tt.cls(); - printLast(); + printLast(changed); break; default: tt.background(Black); @@ -278,7 +391,7 @@ can1_SleepMode = 1; // Turn on Monitor_only Mode can2_SleepMode = 1; // Turn on Monitor_only Mode //ticker.attach(&send1, 0.5); - ticker.attach(&updateDisplay, 0.25); // Display messages + ticker.attach(&updateDisplay, 0.5); // Display messages can1.attach(&recieve1); can2.attach(&recieve2); @@ -453,7 +566,6 @@ if (!userIdle) { if (secsNoTouch<2) {// Recently touched secsNoTouch +=2; // increment to prevent double touch - sMode=1; if (lastTouch.x>320){ i=1; lastTouch.x-=320; @@ -461,16 +573,25 @@ i=0; } if (lastTouch.y>btn11y1 && lastTouch.y<btn11y2){ - if (lastTouch.x>btn31x1 && lastTouch.x<btn31x2) { - dMode[i]=dMode[i]>0?dMode[i]-1:maxModes; - } else if (lastTouch.x>btn33x1 && lastTouch.x<btn33x2) { - dMode[i]=dMode[i]<maxModes?dMode[i]+1:0; + if(sMode==1){ + if (lastTouch.x>btn31x1 && lastTouch.x<btn31x2) { + dMode[i]=dMode[i]>0?dMode[i]-1:maxModes; + } else if (lastTouch.x>btn32x1 && lastTouch.x<btn32x2) { + secsNoTouch = userTimeout; // immediately exit config mode + } else if (lastTouch.x>btn33x1 && lastTouch.x<btn33x2) { + dMode[i]=dMode[i]<maxModes?dMode[i]+1:0; + } + } else sMode=1; + } else { + if (dMode[i]==monitorMode) { + if (lastTouch.x>btn31x1 && lastTouch.x<btn31x2) + indexOffset=indexOffset>4?indexOffset-4:1; + else if (lastTouch.x>btn33x1 && lastTouch.x<btn33x2) + indexOffset=indexOffset<77?indexOffset+4:80; } - } else { - sprintf(sTemp,"missed button\n"); - logMsg(sTemp); } } + secsNoMsg = 0; } else { // userIdle sMode=0; }