Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341
Dependencies: SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed
Fork of CANary by
Diff: main.cpp
- Branch:
- Metric
- Revision:
- 99:c05abf8e1cdc
- Parent:
- 98:9f8bab96edff
- Parent:
- 97:a25940fd7b5b
- Child:
- 100:63a7456fc972
--- a/main.cpp Mon Jun 10 12:00:48 2013 +0000 +++ b/main.cpp Sat Jun 15 18:47:38 2013 +0000 @@ -8,7 +8,8 @@ // * Add 50% charge option // * Tire Pressure Sensor display // * Fix bug in playback while connected to canbus (hangs) -// * Add temperature to Main screen +// * Fix no power displayed while charging +// * Force regen display to zero when in neutral #include "mbed.h" #include "CAN.h" @@ -20,6 +21,8 @@ #include "displayModes.h" #include "TOUCH_TFTx2.h" +char revStr[7] = "100"; + LocalFileSystem local("local"); // to write to USB Flash Drives, or equivalent (SD card in Reader/Writer) @@ -51,15 +54,14 @@ // gg - revStr is used in 2 places // gg - and is easy to edit here // gg - added ZeroSecTick and revStr -char revStr[7] = "97"; bool debugMode = false; bool logEn = false, logOpen = false; bool yesBattLog = false ; // gg - Batt Log unsigned char tNavRow = 3 ; // gg - 4x4 touch -FILE *cfile; -FILE *file; +FILE *hfile; // config file +FILE *lfile; // log file char fileName[35] = "" ; char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write char indexLastMsg[0x800]={0}; // index table for last message @@ -176,8 +178,8 @@ if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over sprintf(sTemp,"New binary found.\n"); printMsg(sTemp); // new binary - file = fopen("/local/CANary.bin", "wb"); - if (file==NULL){ //failed to open destination + lfile = fopen("/local/CANary.bin", "wb"); + if (lfile==NULL){ //failed to open destination sprintf(sTemp,"Unable to open destination file.\n"); printMsg(sTemp); // cannot open CANary.bin } else { @@ -191,14 +193,14 @@ printf("CANary will reset when complete.\n"); wait(1); //Wait 1 sec for display DMA to finish before writing file while ( int size = fread( writeBuffer, sizeof(char), maxBufLen*13, cfile )){ - fwrite( writeBuffer, sizeof(char), size, file ); + fwrite( writeBuffer, sizeof(char), size, lfile ); led4=led3; led3=led2; led2=led1; led1=!led4; } fclose(cfile); - fclose(file); + fclose(lfile); remove("/usb/CANary.bin"); // delete original mbed_reset(); //restart } @@ -207,15 +209,15 @@ secsNoMsg = 0; //read efficiency history data - cfile = fopen("/local/ehist.cny", "r"); - if (cfile!=NULL){ // found a efficiency history file + hfile = fopen("/local/ehist.cny", "r"); + if (hfile!=NULL){ // found a efficiency history file for(i=0;i<39;i++){ - if(!feof(cfile)){ - fscanf(cfile,"%f %f\r\n",&mph[i],&kW[i]); + if(!feof(hfile)){ + fscanf(hfile,"%f %f\r\n",&mph[i],&kW[i]); mpkWh[i]=mph[i]/kW[i]; } } - fclose(cfile); + fclose(hfile); sprintf(sTemp,"History Loaded.\n"); printMsg(sTemp); // History loaded } else { // create initial file @@ -252,10 +254,10 @@ strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc //sprintf(sTemp,"Using file %s\n",fileName); //printMsg(sTemp); // using alc file ... - file = fopen(fileName, "ab"); + lfile = fopen(fileName, "ab"); lastDMode[0]=99;//force refresh lastDMode[1]=99;//force refresh - if(file==NULL){ + if(lfile==NULL){ sprintf(sTemp,"\nUnable to open %s\n\n\n\n",fileName); printMsg(sTemp); // cannot open alc file logEn=false; @@ -278,7 +280,7 @@ pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen; if (pointerSep>(maxBufLen/16)||canIdle||!logEn) { // Dump buffer if > 1/16 full or canbus has stopped - if (file == NULL) { + if (lfile == NULL) { logOpen = false; sprintf(sTemp,"Failed to append log file.\n"); printMsg(sTemp); // failed to append @@ -287,7 +289,7 @@ } else { while (readPointer != writePointer) { for (j = 0; j<13; j++){ - fprintf(file,"%c",writeBuffer[readPointer][j]); + fprintf(lfile,"%c",writeBuffer[readPointer][j]); } if(++readPointer >= maxBufLen){ readPointer=0; @@ -299,7 +301,7 @@ if (!logEn) { sprintf(sTemp,"Stopping Can Log %s\n",fileName); printMsg(sTemp); // stopping alc log file - fclose(file); + fclose(lfile); logOpen=false; pointerSep=0; } @@ -309,7 +311,7 @@ autoPoll.detach(); } if (logOpen){ - fclose(file); + fclose(lfile); } // if (logOpen)*/ seconds = time(NULL); t = *localtime(&seconds) ; @@ -330,6 +332,8 @@ //__wfi(); // freeze CPU and wait for interrupt (from canbus or touch) Sleep(); } + lastDMode[0]=99; + lastDMode[1]=99; secsNoTouch=2; canIdle=secsNoMsg>canTimeout; dled=0.8; // turn on display LED @@ -341,7 +345,7 @@ logOpen = false; // Start new file if asleep for more than 30 minutes if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now) - file = fopen(fileName, "ab"); + lfile = fopen(fileName, "ab"); logEvent("WakingUp"); // gg - use messeges logTS(); // Date-Time at wakeup } @@ -541,9 +545,9 @@ // Start/stop playback if(!playbackOpen){ if(!logOpen){ - file = fopen("/usb/playback.alc", "rb"); + lfile = fopen("/usb/playback.alc", "rb"); lastDMode[whichTouched]=99;//force refresh - if(file==NULL){ + if(lfile==NULL){ sprintf(sTemp,"Unable to open /usb/playback.alc\n"); printMsg(sTemp); // no playback.alc spkr.beep(1000,0.25); @@ -563,7 +567,7 @@ } } else { playback.detach(); - fclose(file); + fclose(lfile); playbackOpen=false; playbackEn=false; can1.attach(&recieve1);// Restore CAN data recieve @@ -690,19 +694,16 @@ accOn=(accV>5)?true:false; if(laccOn&&!accOn){ // Car turned off //write efficiency history data - cfile = fopen("/local/ehist.cny", "w"); - if (cfile!=NULL){ // found a efficiency history file + hfile = fopen("/local/ehist.cny", "w"); + if (hfile!=NULL){ // found a efficiency history file for(i=0;i<39;i++){ - fprintf(cfile,"%f %f\r\n",mph[i],kW[i]); + fprintf(hfile,"%f %f\r\n",mph[i],kW[i]); } - fclose(cfile); + fclose(hfile); } } laccOn=accOn; - if(!accOn&&!logEn&&userIdle&&!playbackEn){ - //sprintf(sTemp,"Display Off %4.2f\n",accV); - //printMsg(sTemp); // display off - for debug - + if(!accOn&&!logEn&&userIdle&&!playbackEn){ dled = 0; // turn off display if car off and logging disabled and no user activity }else if(!headlights){ dled = ledHi; @@ -742,8 +743,10 @@ mph[i]+=mph[0]; mpkWh[i]=average; average=kW[i]/timeConstant[i]; - kW[i]-=average; - kW[i]+=kW[0]; + if((mph[0]>0)||(kW[0]>0)){ //Not charging - so include in efficiency data + kW[i]-=average; + kW[i]+=kW[0]; + } mpkWh[i]/=average; if (mpkWh[i]<0) { mpkWh[i]=99;// negative means inf. @@ -759,23 +762,16 @@ display=display<1?display+1:0; // toggle display updateDisplay(display); - - /*if(pollCP){ // We do this inside main loop instead of ticker so CAN RX will not be blocked - sendCPreq(); // send cellpair data request. - wait_ms(16); - sendTreq(); //send temperature request - pollCP=false; - }*/ if(step){ // playback if(playbackOpen&&playbackEn){ for(i=0;i<120;i++){ - if(!feof(file)){ - fscanf(file,"%5c%8c",&header,&data); + if(!feof(lfile)){ + fscanf(lfile,"%5c%8c",&header,&data); logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8)); // Playback } else { - fclose(file); // restart - file = fopen("/usb/playback.alc", "rb"); + fclose(lfile); // restart + lfile = fopen("/usb/playback.alc", "rb"); lastDMode[whichTouched]=99;//force refresh spkr.beep(2000,0.25); }