![](/media/cache/profiles/0df840aed5f513a02964d0c27bc4f277.jpg.50x50_q85.jpg)
Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341
Dependencies: SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed
Fork of CANary by
main.cpp@164:46ed06263b0e, 2013-11-28 (annotated)
- Committer:
- TickTock
- Date:
- Thu Nov 28 03:23:30 2013 +0000
- Revision:
- 164:46ed06263b0e
- Parent:
- 163:3b6fab958773
- Child:
- 165:4daa921730dd
// Turn off noCC DTE when CC is off
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TickTock | 13:62e0f7f39ff5 | 1 | // main.cpp |
TickTock | 108:29b5a760adc2 | 2 | // |
TickTock | 4:8d7759f4fe7a | 3 | //To Do: |
TickTock | 33:a277743ebdeb | 4 | // * Add 50% charge option |
TickTock | 152:a4d66901785d | 5 | // * Add linear efficiency graph with 10 minute values |
TickTock | 146:88f7bda79d8e | 6 | // * Add in-device config editor |
TickTock | 149:e9739523109f | 7 | // * Change pack volt color when CVLI fails |
TickTock | 149:e9739523109f | 8 | // * Add tire pressure cal (40psi for me = FR 38, RR 38.2, FL 37.8, RL 38 - maybe 2psi error on my tire gauge?) |
TickTock | 164:46ed06263b0e | 9 | // * Add on screen messages for heater on, etc, and use refresh feature above to clear in x seconds |
TickTock | 157:1a100a5ed06f | 10 | |
TickTock | 164:46ed06263b0e | 11 | // rev164 |
TickTock | 164:46ed06263b0e | 12 | // Turn off noCC DTE when CC is off |
TickTock | 134:ad243e4f1a17 | 13 | |
TickTock | 12:8e42d7ba8468 | 14 | #include "mbed.h" |
TickTock | 12:8e42d7ba8468 | 15 | #include "CAN.h" |
TickTock | 109:3e6f0e8fca0d | 16 | #include "ff.h" |
TickTock | 12:8e42d7ba8468 | 17 | #include "PowerControl.h" |
TickTock | 12:8e42d7ba8468 | 18 | #include "EthernetPowerControl.h" |
TickTock | 12:8e42d7ba8468 | 19 | #include "utility.h" |
TickTock | 12:8e42d7ba8468 | 20 | #include "displayModes.h" |
TickTock | 23:cd03f9c3395e | 21 | #include "TOUCH_TFTx2.h" |
TickTock | 164:46ed06263b0e | 22 | char revStr[7] = "164"; // gg - revision string, max 6 characters |
TickTock | 110:ffddff3ad2f2 | 23 | |
TickTock | 109:3e6f0e8fca0d | 24 | FATFS USBdrive; |
TickTock | 13:62e0f7f39ff5 | 25 | LocalFileSystem local("local"); |
TickTock | 115:549410af477d | 26 | unsigned char wait5secs = 5; |
TickTock | 13:62e0f7f39ff5 | 27 | // to write to USB Flash Drives, or equivalent (SD card in Reader/Writer) |
TickTock | 119:0e2b641e98a2 | 28 | // class10 SDcard in Reader/Writer recommended |
TickTock | 111:d1559bb25c43 | 29 | FRESULT mfr = f_mount(0,&USBdrive); |
TickTock | 13:62e0f7f39ff5 | 30 | time_t seconds ; |
TickTock | 13:62e0f7f39ff5 | 31 | |
TickTock | 36:dbd39c315258 | 32 | Ticker autoPoll; |
TickTock | 36:dbd39c315258 | 33 | Ticker playback; |
TickTock | 78:a383971fe02f | 34 | Ticker msgReq; |
TickTock | 140:ab3e94eb0227 | 35 | Ticker geiger; |
TickTock | 13:62e0f7f39ff5 | 36 | Timer timer; |
TickTock | 13:62e0f7f39ff5 | 37 | |
TickTock | 13:62e0f7f39ff5 | 38 | DigitalOut led1(LED1); |
TickTock | 13:62e0f7f39ff5 | 39 | DigitalOut led2(LED2); |
TickTock | 13:62e0f7f39ff5 | 40 | DigitalOut led3(LED3); |
TickTock | 13:62e0f7f39ff5 | 41 | DigitalOut led4(LED4); |
TickTock | 13:62e0f7f39ff5 | 42 | |
TickTock | 13:62e0f7f39ff5 | 43 | InterruptIn touchpad(p17); |
TickTock | 13:62e0f7f39ff5 | 44 | CAN can1(p9, p10); // CAN1 (EV) uses pins 9 and 10 (rx, tx) and pin 8 (rs) |
TickTock | 13:62e0f7f39ff5 | 45 | DigitalOut can1SleepMode(p8); // Use pin 8 to control the sleep mode of can2 |
TickTock | 13:62e0f7f39ff5 | 46 | CAN can2(p30, p29); // CAN2 (CAR) uses pins 30 and 29 (rx, tx) and pin 28 (rs) |
TickTock | 13:62e0f7f39ff5 | 47 | DigitalOut can2SleepMode(p28); // Use pin 28 to control the sleep mode of can1 |
TickTock | 35:5acbd8a64a89 | 48 | AnalogIn mon12V(p15); |
TickTock | 13:62e0f7f39ff5 | 49 | TOUCH_TFTx2 tt(p16, p17, p19, p20, p11, p12, p13, p6, p7, p5, "TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs0, cs1, reset |
TickTock | 35:5acbd8a64a89 | 50 | PwmOut dled(p23); |
TickTock | 155:f31006516956 | 51 | PwmOut spkr(p21); |
TickTock | 13:62e0f7f39ff5 | 52 | |
garygid | 87:46ac3f2519d6 | 53 | bool debugMode = false; |
TickTock | 108:29b5a760adc2 | 54 | bool usbEn = false; |
TickTock | 131:0d926c080a65 | 55 | bool logEn = false; |
TickTock | 108:29b5a760adc2 | 56 | bool logOpen = false; |
TickTock | 115:549410af477d | 57 | bool yesBattLog = true; // gg - Batt Log |
TickTock | 108:29b5a760adc2 | 58 | unsigned char tNavRow = 3; // gg - 4x4 touch |
TickTock | 140:ab3e94eb0227 | 59 | bool brakeMon = false; // disable until desired value read from config |
TickTock | 155:f31006516956 | 60 | bool regenMon = false; |
TickTock | 159:577e9d015693 | 61 | bool heaterMon = false; |
TickTock | 152:a4d66901785d | 62 | bool autoSync = false; // auto clock sync on powerup |
TickTock | 152:a4d66901785d | 63 | bool syncDone = true; |
TickTock | 159:577e9d015693 | 64 | bool heaterOn = false; |
TickTock | 159:577e9d015693 | 65 | bool lHeaterOn = false; |
TickTock | 164:46ed06263b0e | 66 | bool CCon = false; |
TickTock | 164:46ed06263b0e | 67 | bool lCCon = false; |
garygid | 54:6ce808d0995e | 68 | |
TickTock | 109:3e6f0e8fca0d | 69 | FILE *hfile; // history file |
TickTock | 109:3e6f0e8fca0d | 70 | FIL efile; // external usb file |
TickTock | 109:3e6f0e8fca0d | 71 | FRESULT efr; // external file access flags |
TickTock | 109:3e6f0e8fca0d | 72 | unsigned int bytesRW; |
TickTock | 108:29b5a760adc2 | 73 | char fileName[35] = ""; |
TickTock | 84:fd21e5d32dab | 74 | char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write |
TickTock | 13:62e0f7f39ff5 | 75 | char indexLastMsg[0x800]={0}; // index table for last message |
TickTock | 13:62e0f7f39ff5 | 76 | CANMessage lastMsg[100]; // table to store last message of eachtype |
garygid | 87:46ac3f2519d6 | 77 | |
TickTock | 124:0d622967b764 | 78 | unsigned char battData[BatDataBufMax]={0}; // 7 * 0x3D = BatDataBufMax |
garygid | 87:46ac3f2519d6 | 79 | |
TickTock | 13:62e0f7f39ff5 | 80 | unsigned char msgChanged[100]; // inidcates which bytes changed |
TickTock | 13:62e0f7f39ff5 | 81 | char c; |
TickTock | 76:fb6779d0963e | 82 | volatile int writePointer = 0; |
TickTock | 76:fb6779d0963e | 83 | int readPointer=0; |
TickTock | 141:cf13a632ddb5 | 84 | volatile unsigned short secsNoCarCanMsg = canTimeout; |
TickTock | 141:cf13a632ddb5 | 85 | volatile unsigned short secsNoEvCanMsg = canTimeout; |
TickTock | 75:77bd26829dca | 86 | volatile unsigned short secsNoTouch = 0; |
TickTock | 132:08748a67280a | 87 | volatile bool carCanIdle,evCanIdle,userIdle; |
TickTock | 26:462ccb580472 | 88 | bool touched=false; //flag to read touchscreen |
TickTock | 42:4533b13b297d | 89 | unsigned char whichTouched = 0; |
TickTock | 13:62e0f7f39ff5 | 90 | char counter = 0; |
TickTock | 41:8d4609ea7259 | 91 | unsigned char dMode[2] = {mainScreen,brakeScreen}; //display mode |
TickTock | 13:62e0f7f39ff5 | 92 | unsigned char sMode = 0; // setup mode |
TickTock | 13:62e0f7f39ff5 | 93 | unsigned char lastDMode[2] = {0,0}; //last screen mode |
TickTock | 131:0d926c080a65 | 94 | unsigned char dtMode = 0; |
TickTock | 13:62e0f7f39ff5 | 95 | char displayLog[20][40]; |
TickTock | 13:62e0f7f39ff5 | 96 | unsigned char displayLoc = 0; |
leafman | 98:9f8bab96edff | 97 | unsigned int fwCount=1; |
TickTock | 13:62e0f7f39ff5 | 98 | unsigned char indexOffset = 1; |
TickTock | 13:62e0f7f39ff5 | 99 | bool showCP = false; |
TickTock | 39:eef8beac7411 | 100 | bool logCP = false; //Turbo3 |
TickTock | 108:29b5a760adc2 | 101 | bool logOnce = false; |
TickTock | 103:1389e9efe8c3 | 102 | bool repeatPoll = true; |
TickTock | 34:4751a8259b18 | 103 | bool headlights = false; |
TickTock | 140:ab3e94eb0227 | 104 | bool miles_kmbar = true; |
TickTock | 34:4751a8259b18 | 105 | bool tick = false; |
garygid | 69:6bfdfc002036 | 106 | bool ZeroSecTick = false; |
TickTock | 35:5acbd8a64a89 | 107 | float ledHi = 0.8; // Bright LED value (until config file read) |
TickTock | 35:5acbd8a64a89 | 108 | float ledLo = 0.1; // Dim LED value (until config file read) |
TickTock | 35:5acbd8a64a89 | 109 | unsigned short pollInt = 300; // polling interval=5 minutes (until config file read) |
TickTock | 35:5acbd8a64a89 | 110 | bool accOn = false; // Accessories on |
TickTock | 49:a3d2c5bb3cfa | 111 | bool laccOn = false; |
TickTock | 35:5acbd8a64a89 | 112 | float scale12V = 16.2; // R1:R2 ratio |
TickTock | 40:0e6e71a7323f | 113 | signed long mWs_x4 = 0; |
TickTock | 40:0e6e71a7323f | 114 | unsigned short numWsamples = 0; |
TickTock | 41:8d4609ea7259 | 115 | unsigned short numSsamples = 0; |
TickTock | 35:5acbd8a64a89 | 116 | float accV = 0; |
TickTock | 143:88b5155622a5 | 117 | float accV2 = 0; |
TickTock | 161:71ac85d11f03 | 118 | float CCkW = 0; |
TickTock | 36:dbd39c315258 | 119 | bool playbackEn = false; |
TickTock | 36:dbd39c315258 | 120 | bool playbackOpen = false; |
TickTock | 49:a3d2c5bb3cfa | 121 | //float playbackInt = 0.05; //read messages every 50 ms |
TickTock | 78:a383971fe02f | 122 | float playbackInt = 0.005; //read messages every 5 ms |
TickTock | 36:dbd39c315258 | 123 | bool step = false; |
TickTock | 37:fea2c1d52c5f | 124 | char header[5]; |
TickTock | 36:dbd39c315258 | 125 | char data[8]; |
TickTock | 160:905fe45ed54b | 126 | signed long motorRPM_x2; |
garygid | 65:821fc79cd7fe | 127 | unsigned char skin = ttSkin ; |
TickTock | 50:83d5864c64a0 | 128 | unsigned char dtePeriod = 14; //ten minute averaging interval |
TickTock | 107:e9be732c1ad4 | 129 | float kWh_trip[3]={0}; |
TickTock | 107:e9be732c1ad4 | 130 | float miles_trip[3]={0}; |
TickTock | 144:b0c9d30dd346 | 131 | float curEff = 0; |
TickTock | 158:059abeb44ba1 | 132 | float maxTripEff = 0; |
TickTock | 158:059abeb44ba1 | 133 | float minTripEff = 5; |
TickTock | 49:a3d2c5bb3cfa | 134 | float mph[39]={0}; |
TickTock | 49:a3d2c5bb3cfa | 135 | float kW[39]={0}; |
TickTock | 49:a3d2c5bb3cfa | 136 | float mpkWh[39]={0}; |
TickTock | 162:c6545fc0164a | 137 | float mpkWh_noCC=0; |
TickTock | 120:041edeec08f5 | 138 | float unloadedV_x2,Resr,curRmax,curRmin,redRmax,redRmin,incRmax,incRmin; |
TickTock | 121:553faf139a20 | 139 | signed short Imax, Imin; |
TickTock | 49:a3d2c5bb3cfa | 140 | // Logarithmic division scale (roughly - snapped to common units of time) |
TickTock | 49:a3d2c5bb3cfa | 141 | float timeConstant[39] = {1, 1.58, 2.51, 3.98, 6.31, 10, 15.8, 25.1, 39.8, 60, // 1 minute |
TickTock | 52:d5385fbf4ea1 | 142 | 60*1.58, 60*2.51, 60*3.98, 60*6.31, 60*10, 60*15.8, 60*25.1, 60*39.8, 60*60, // 1 hour |
TickTock | 52:d5385fbf4ea1 | 143 | 60*60*1.58, 60*60*2.51, 60*60*3.98, 60*60*6.31, 60*60*10, 60*60*15.8, 60*60*24, // 1 day |
TickTock | 52:d5385fbf4ea1 | 144 | 60*60*24*1.58, 60*60*24*2.51, 60*60*24*3.98, 60*60*24*6.31, 60*60*24*10, 60*60*24*15.8, 60*60*24*30, // 1 month |
TickTock | 52:d5385fbf4ea1 | 145 | 60*60*24*39.8, 60*60*24*63.1, 60*60*24*100, 60*60*24*158, 60*60*24*251, 60*60*24*365}; // 1 year |
TickTock | 121:553faf139a20 | 146 | bool tock = false; |
TickTock | 73:62ee8eae3a84 | 147 | unsigned short pointerSep; |
TickTock | 78:a383971fe02f | 148 | unsigned char reqMsgCnt = 99; |
TickTock | 102:fd19f777a0b4 | 149 | unsigned long Ah_x10000 = 0; |
TickTock | 102:fd19f777a0b4 | 150 | unsigned long SOC_x10000 = 0; |
TickTock | 143:88b5155622a5 | 151 | unsigned short SOH2_x100 = 0; |
TickTock | 108:29b5a760adc2 | 152 | float maxTemp = 0; |
leafman | 98:9f8bab96edff | 153 | bool metric = false; |
TickTock | 121:553faf139a20 | 154 | bool shunt[96]={0}; |
TickTock | 121:553faf139a20 | 155 | bool charging=false; |
TickTock | 131:0d926c080a65 | 156 | bool showHealth=false; |
TickTock | 131:0d926c080a65 | 157 | unsigned char saveDmode=99; |
TickTock | 131:0d926c080a65 | 158 | bool moving=false; |
TickTock | 140:ab3e94eb0227 | 159 | unsigned short chirpInt; |
TickTock | 150:ef46ce63345c | 160 | unsigned short uMsgId[8] = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}; // messages to display on debug screen msgId:byte |
TickTock | 152:a4d66901785d | 161 | unsigned short modelYear = 2011; |
TickTock | 153:e94cfe3c339c | 162 | bool idir, lidir; |
TickTock | 154:90ea16ca7475 | 163 | bool enableSound=false; |
TickTock | 158:059abeb44ba1 | 164 | bool clearTest=true; |
TickTock | 13:62e0f7f39ff5 | 165 | |
TickTock | 0:1596b8644523 | 166 | int main() { |
TickTock | 4:8d7759f4fe7a | 167 | char sTemp[40]; |
TickTock | 2:71b1999a8ea5 | 168 | unsigned long secs; |
TickTock | 49:a3d2c5bb3cfa | 169 | unsigned char i,j,display=0,lwt=0; |
TickTock | 22:a43df3905863 | 170 | point lastTouch; |
TickTock | 52:d5385fbf4ea1 | 171 | float average; |
TickTock | 3:3e879b043bc5 | 172 | tt.set_orientation(1); |
TickTock | 4:8d7759f4fe7a | 173 | tt.background(Black); |
TickTock | 19:d576298c46f3 | 174 | tt.set_display(2); // select both displays |
TickTock | 3:3e879b043bc5 | 175 | tt.cls(); |
TickTock | 137:70853cf5a30f | 176 | tt.foreground(White); |
TickTock | 137:70853cf5a30f | 177 | tt.set_font((unsigned char*) Arial12x12_prop); // select the font |
TickTock | 137:70853cf5a30f | 178 | tt.locate(0,0); |
TickTock | 1:9dcd70c32180 | 179 | tt.claim(stdout); // send stdout to the TFT display |
TickTock | 137:70853cf5a30f | 180 | if(tt.is_touched()){ // If touching screen on poweron then update firmware |
TickTock | 137:70853cf5a30f | 181 | updateFirmware(); |
TickTock | 137:70853cf5a30f | 182 | } |
TickTock | 12:8e42d7ba8468 | 183 | touchpad.rise(&touch_ISR); |
TickTock | 4:8d7759f4fe7a | 184 | tt.wfi(); // enable interrupt on touch |
TickTock | 155:f31006516956 | 185 | dled.period(0.001); |
TickTock | 154:90ea16ca7475 | 186 | dled = ledHi; // turn on display LED 80% |
TickTock | 155:f31006516956 | 187 | spkr = 0; |
TickTock | 120:041edeec08f5 | 188 | Resr = 0.075; // initial guess of Resr |
TickTock | 2:71b1999a8ea5 | 189 | timer.start() ; |
TickTock | 2:71b1999a8ea5 | 190 | RTC_Init(); // start the RTC Interrupts that sync the timer |
TickTock | 1:9dcd70c32180 | 191 | struct tm t; // pointer to a static tm structure |
TickTock | 93:c2402e8cd0e2 | 192 | NVIC_SetPriority(CAN_IRQn, 2); //set can priority just below RTC |
TickTock | 117:49883c779a74 | 193 | NVIC_SetPriority(TIMER3_IRQn, 3); //set ticker priority just below can |
TickTock | 33:a277743ebdeb | 194 | |
TickTock | 1:9dcd70c32180 | 195 | seconds = time(NULL); |
TickTock | 1:9dcd70c32180 | 196 | t = *localtime(&seconds) ; |
TickTock | 1:9dcd70c32180 | 197 | // is it a date before 2012 ? |
TickTock | 26:462ccb580472 | 198 | if ((t.tm_year + 1900) < 2012 ) { |
TickTock | 26:462ccb580472 | 199 | // before 2013 so update year to make date entry easier |
TickTock | 26:462ccb580472 | 200 | t.tm_year = 2013 - 1900; |
TickTock | 1:9dcd70c32180 | 201 | // set the RTC |
TickTock | 1:9dcd70c32180 | 202 | set_time(mktime(&t)); |
TickTock | 1:9dcd70c32180 | 203 | seconds = time(NULL); |
TickTock | 26:462ccb580472 | 204 | } |
TickTock | 20:3bf176d14b14 | 205 | t = *localtime(&seconds) ; |
TickTock | 20:3bf176d14b14 | 206 | strftime(sTemp, 32, "%a %m/%d/%Y %X\n", &t); |
TickTock | 93:c2402e8cd0e2 | 207 | printMsg(sTemp); // record RTC |
garygid | 54:6ce808d0995e | 208 | |
TickTock | 62:ffd15edb5431 | 209 | // revision |
garygid | 70:0700b769ac15 | 210 | sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision |
TickTock | 93:c2402e8cd0e2 | 211 | printMsg(sTemp); // revision |
TickTock | 17:e32324a2678d | 212 | |
TickTock | 49:a3d2c5bb3cfa | 213 | //read efficiency history data |
TickTock | 150:ef46ce63345c | 214 | hfile = fopen("/local/ehist.cny", "r"); |
TickTock | 97:a25940fd7b5b | 215 | if (hfile!=NULL){ // found a efficiency history file |
TickTock | 49:a3d2c5bb3cfa | 216 | for(i=0;i<39;i++){ |
TickTock | 97:a25940fd7b5b | 217 | if(!feof(hfile)){ |
TickTock | 97:a25940fd7b5b | 218 | fscanf(hfile,"%f %f\r\n",&mph[i],&kW[i]); |
TickTock | 49:a3d2c5bb3cfa | 219 | mpkWh[i]=mph[i]/kW[i]; |
TickTock | 49:a3d2c5bb3cfa | 220 | } |
TickTock | 49:a3d2c5bb3cfa | 221 | } |
TickTock | 134:ad243e4f1a17 | 222 | if(!feof(hfile)){ |
TickTock | 134:ad243e4f1a17 | 223 | fscanf(hfile,"%f %f\r\n",&maxTripEff,&minTripEff); |
TickTock | 134:ad243e4f1a17 | 224 | } |
TickTock | 155:f31006516956 | 225 | if(!feof(hfile)){ |
TickTock | 155:f31006516956 | 226 | fscanf(hfile,"%f\r\n",&Resr); |
TickTock | 155:f31006516956 | 227 | } |
TickTock | 97:a25940fd7b5b | 228 | fclose(hfile); |
TickTock | 116:5cd72bae7c12 | 229 | printMsg("History Loaded.\n"); // History loaded |
TickTock | 49:a3d2c5bb3cfa | 230 | } else { // create initial file |
TickTock | 116:5cd72bae7c12 | 231 | printMsg("History not found. Created.\n"); // history not found, created |
TickTock | 49:a3d2c5bb3cfa | 232 | for(i=0;i<39;i++){ |
TickTock | 49:a3d2c5bb3cfa | 233 | // Pre-load with 4 mpkWh @ 40 mph |
TickTock | 49:a3d2c5bb3cfa | 234 | mph[i]=40*timeConstant[i]; |
TickTock | 49:a3d2c5bb3cfa | 235 | kW[i]=10*timeConstant[i]; |
TickTock | 49:a3d2c5bb3cfa | 236 | mpkWh[i]=4; |
TickTock | 49:a3d2c5bb3cfa | 237 | } |
TickTock | 49:a3d2c5bb3cfa | 238 | } |
TickTock | 49:a3d2c5bb3cfa | 239 | |
TickTock | 46:73e8716f976f | 240 | // Read config file |
TickTock | 46:73e8716f976f | 241 | readConfig(); |
TickTock | 140:ab3e94eb0227 | 242 | if (brakeMon){ |
TickTock | 140:ab3e94eb0227 | 243 | geiger.attach(&chirp,0.02); |
TickTock | 140:ab3e94eb0227 | 244 | } |
TickTock | 108:29b5a760adc2 | 245 | if (repeatPoll) { // enable autopolling if enabled |
TickTock | 108:29b5a760adc2 | 246 | autoPoll.attach(&autoPollISR,pollInt); |
TickTock | 108:29b5a760adc2 | 247 | } |
TickTock | 108:29b5a760adc2 | 248 | |
TickTock | 77:7c136766466c | 249 | // Start monitors |
TickTock | 77:7c136766466c | 250 | can1.monitor(true); // set to snoop mode |
TickTock | 77:7c136766466c | 251 | can2.monitor(true); // set to snoop mode |
TickTock | 77:7c136766466c | 252 | can1.frequency(500000); |
TickTock | 77:7c136766466c | 253 | can2.frequency(500000); |
TickTock | 120:041edeec08f5 | 254 | can1SleepMode = VP230Sleep; // Turn on Monitor_only Mode |
TickTock | 120:041edeec08f5 | 255 | can2SleepMode = VP230Sleep; // Turn on Monitor_only Mode |
TickTock | 77:7c136766466c | 256 | can1.attach(&recieve1); |
TickTock | 77:7c136766466c | 257 | can2.attach(&recieve2); |
TickTock | 77:7c136766466c | 258 | |
TickTock | 26:462ccb580472 | 259 | touched=false; |
TickTock | 26:462ccb580472 | 260 | secsNoTouch=2; |
TickTock | 2:71b1999a8ea5 | 261 | while (true) { |
TickTock | 4:8d7759f4fe7a | 262 | if (!logOpen) { // Open new file if one is not already open |
TickTock | 131:0d926c080a65 | 263 | if(logEn&&usbEn){ //logging enabled and USB device detected |
TickTock | 109:3e6f0e8fca0d | 264 | strftime(fileName, 32, "%m%d%H%M.alc", &t); //mmddhhmm.alc |
TickTock | 109:3e6f0e8fca0d | 265 | efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS); |
TickTock | 7:17bf9ceaf0aa | 266 | seconds = time(NULL); |
TickTock | 7:17bf9ceaf0aa | 267 | t = *localtime(&seconds) ; |
TickTock | 94:c3a14b3975d6 | 268 | lastDMode[0]=99;//force refresh |
TickTock | 94:c3a14b3975d6 | 269 | lastDMode[1]=99;//force refresh |
TickTock | 109:3e6f0e8fca0d | 270 | if(efr != FR_OK){ |
TickTock | 109:3e6f0e8fca0d | 271 | sprintf(sTemp,"\nERR:%d Unable to open %s\n\n\n\n",efr,fileName); |
TickTock | 93:c2402e8cd0e2 | 272 | printMsg(sTemp); // cannot open alc file |
TickTock | 7:17bf9ceaf0aa | 273 | logEn=false; |
TickTock | 155:f31006516956 | 274 | beep(1000,0.25); |
TickTock | 109:3e6f0e8fca0d | 275 | wait_ms(500); |
TickTock | 155:f31006516956 | 276 | beep(1000,0.25); |
TickTock | 7:17bf9ceaf0aa | 277 | } else { |
TickTock | 7:17bf9ceaf0aa | 278 | logOpen = true; |
TickTock | 7:17bf9ceaf0aa | 279 | readPointer=writePointer; |
TickTock | 7:17bf9ceaf0aa | 280 | sprintf(sTemp,"Starting Can Log %s\n",fileName); |
TickTock | 93:c2402e8cd0e2 | 281 | printMsg(sTemp); // starting alc log file |
garygid | 69:6bfdfc002036 | 282 | |
garygid | 69:6bfdfc002036 | 283 | logTS(); // Date Time at start |
TickTock | 93:c2402e8cd0e2 | 284 | logEvent("Starting"); // Log startup msg for testing |
garygid | 69:6bfdfc002036 | 285 | sprintf(sTemp,"Cr%s",revStr); |
TickTock | 108:29b5a760adc2 | 286 | logEvent(sTemp); // gg - log firmware version |
TickTock | 155:f31006516956 | 287 | beep(2000,0.25); |
TickTock | 7:17bf9ceaf0aa | 288 | } |
TickTock | 108:29b5a760adc2 | 289 | }//logging enabled and USB detected |
TickTock | 15:a359fecf85ba | 290 | } else { // if (logOpen) |
TickTock | 73:62ee8eae3a84 | 291 | pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen; |
TickTock | 131:0d926c080a65 | 292 | if (pointerSep>(maxBufLen/16)||carCanIdle||!logEn) { |
TickTock | 7:17bf9ceaf0aa | 293 | // Dump buffer if > 1/16 full or canbus has stopped |
TickTock | 117:49883c779a74 | 294 | if (efr != FR_OK) { |
TickTock | 7:17bf9ceaf0aa | 295 | logOpen = false; |
TickTock | 117:49883c779a74 | 296 | printMsg("Failed to append log file.\n"); // failed to append |
TickTock | 155:f31006516956 | 297 | beep(3000,0.25); |
TickTock | 155:f31006516956 | 298 | beep(1500,0.25); |
TickTock | 155:f31006516956 | 299 | beep(750,0.25); |
TickTock | 155:f31006516956 | 300 | beep(375,0.25); |
TickTock | 7:17bf9ceaf0aa | 301 | logEn=false; |
TickTock | 7:17bf9ceaf0aa | 302 | } else { |
TickTock | 7:17bf9ceaf0aa | 303 | while (readPointer != writePointer) { |
TickTock | 117:49883c779a74 | 304 | efr=f_write(&efile,&writeBuffer[readPointer][0],13,&bytesRW); |
TickTock | 117:49883c779a74 | 305 | if(++readPointer >= maxBufLen){ |
TickTock | 117:49883c779a74 | 306 | readPointer=0; |
TickTock | 117:49883c779a74 | 307 | led4 = !led4; |
TickTock | 4:8d7759f4fe7a | 308 | } |
TickTock | 4:8d7759f4fe7a | 309 | } |
TickTock | 7:17bf9ceaf0aa | 310 | } |
TickTock | 30:e633a63eb257 | 311 | } // if > 1/16 full, canbus has stopped, or logging stopped |
TickTock | 30:e633a63eb257 | 312 | if (!logEn) { |
TickTock | 77:7c136766466c | 313 | sprintf(sTemp,"Stopping Can Log %s\n",fileName); |
TickTock | 93:c2402e8cd0e2 | 314 | printMsg(sTemp); // stopping alc log file |
TickTock | 109:3e6f0e8fca0d | 315 | f_close(&efile); |
TickTock | 30:e633a63eb257 | 316 | logOpen=false; |
TickTock | 77:7c136766466c | 317 | pointerSep=0; |
TickTock | 108:29b5a760adc2 | 318 | led4=false; |
TickTock | 30:e633a63eb257 | 319 | } |
TickTock | 4:8d7759f4fe7a | 320 | } // if logOpen |
TickTock | 132:08748a67280a | 321 | if (carCanIdle && (evCanIdle || !logOpen) && userIdle && !playbackEn) { // canbus idle --> sleep to save power |
TickTock | 95:248b3c25a7df | 322 | if (repeatPoll) { // stop autopolling if enabled |
TickTock | 95:248b3c25a7df | 323 | autoPoll.detach(); |
TickTock | 95:248b3c25a7df | 324 | } |
TickTock | 111:d1559bb25c43 | 325 | if (logOpen){ //close file to dump buffer |
TickTock | 111:d1559bb25c43 | 326 | f_close(&efile); |
TickTock | 134:ad243e4f1a17 | 327 | } else { //detach EVcan so only carcan will trigger wake |
TickTock | 134:ad243e4f1a17 | 328 | can1.attach(NULL); |
TickTock | 134:ad243e4f1a17 | 329 | }// if (logOpen) |
TickTock | 20:3bf176d14b14 | 330 | seconds = time(NULL); |
TickTock | 20:3bf176d14b14 | 331 | t = *localtime(&seconds) ; |
TickTock | 21:22bdce9efcb5 | 332 | strftime(sTemp, 40, "Sleeping: %a %m/%d/%Y %X\n", &t); |
TickTock | 93:c2402e8cd0e2 | 333 | printMsg(sTemp); // sleeping date time |
TickTock | 25:ddf0ec209f03 | 334 | updateDisplay(0); //Added for turbo3 who has a display override and wants to see the sleep message before going to sleep |
TickTock | 25:ddf0ec209f03 | 335 | updateDisplay(1); |
TickTock | 4:8d7759f4fe7a | 336 | //LPC_RTC->CIIR=0x00; // block RTC interrupts |
TickTock | 4:8d7759f4fe7a | 337 | led1=0; |
TickTock | 4:8d7759f4fe7a | 338 | led2=0; |
TickTock | 4:8d7759f4fe7a | 339 | led3=0; |
TickTock | 4:8d7759f4fe7a | 340 | led4=0; |
TickTock | 4:8d7759f4fe7a | 341 | dled=0; // turn off display |
TickTock | 4:8d7759f4fe7a | 342 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 132:08748a67280a | 343 | while (secsNoCarCanMsg>canTimeout && (secsNoEvCanMsg>canTimeout || !logOpen) && !touched) { |
TickTock | 4:8d7759f4fe7a | 344 | //DeepPowerDown(); |
TickTock | 12:8e42d7ba8468 | 345 | tt.wfi(); //enable touch interrupt |
TickTock | 21:22bdce9efcb5 | 346 | //__wfi(); // freeze CPU and wait for interrupt (from canbus or touch) |
TickTock | 21:22bdce9efcb5 | 347 | Sleep(); |
TickTock | 4:8d7759f4fe7a | 348 | } |
TickTock | 135:89986950d501 | 349 | if (!logOpen){ // Re-attach EVcan |
TickTock | 134:ad243e4f1a17 | 350 | can1.attach(&recieve1); |
TickTock | 134:ad243e4f1a17 | 351 | } |
TickTock | 43:e7f6f80590e3 | 352 | secsNoTouch=2; |
TickTock | 131:0d926c080a65 | 353 | carCanIdle=secsNoCarCanMsg>canTimeout; |
TickTock | 132:08748a67280a | 354 | evCanIdle=secsNoEvCanMsg>canTimeout; |
TickTock | 154:90ea16ca7475 | 355 | dled=ledHi; // turn on display LED |
TickTock | 20:3bf176d14b14 | 356 | seconds = time(NULL); |
TickTock | 20:3bf176d14b14 | 357 | t = *localtime(&seconds) ; |
TickTock | 21:22bdce9efcb5 | 358 | strftime(sTemp, 40, "Waking: %a %m/%d/%Y %X\n", &t); |
TickTock | 93:c2402e8cd0e2 | 359 | printMsg(sTemp); // wakeup date time |
TickTock | 4:8d7759f4fe7a | 360 | if (time(NULL)>(secs+1800)) { |
TickTock | 110:ffddff3ad2f2 | 361 | if (logOpen){ |
TickTock | 110:ffddff3ad2f2 | 362 | f_close(&efile); |
TickTock | 110:ffddff3ad2f2 | 363 | logOpen = false; // Start new file if asleep for more than 30 minutes |
TickTock | 110:ffddff3ad2f2 | 364 | } // if (logOpen) |
TickTock | 4:8d7759f4fe7a | 365 | if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter |
TickTock | 43:e7f6f80590e3 | 366 | } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now) |
TickTock | 111:d1559bb25c43 | 367 | efr = f_open(&efile,fileName,FA_WRITE|FA_OPEN_ALWAYS); |
TickTock | 111:d1559bb25c43 | 368 | f_lseek(&efile,0xffffffff); // goto end of file (append existing) |
TickTock | 93:c2402e8cd0e2 | 369 | logEvent("WakingUp"); // gg - use messeges |
garygid | 69:6bfdfc002036 | 370 | logTS(); // Date-Time at wakeup |
TickTock | 4:8d7759f4fe7a | 371 | } |
TickTock | 95:248b3c25a7df | 372 | if (repeatPoll) { // re-enable autopolling if enabled |
TickTock | 124:0d622967b764 | 373 | autoPoll.attach(&autoPollISR,pollInt); |
TickTock | 95:248b3c25a7df | 374 | } |
TickTock | 136:41a204105511 | 375 | wait5secs=5; // Refresh screen after 5 seconds |
TickTock | 4:8d7759f4fe7a | 376 | } // if idle |
TickTock | 4:8d7759f4fe7a | 377 | |
TickTock | 15:a359fecf85ba | 378 | if(touched){ // call touchscreen procedure if touch interrupt detected |
TickTock | 7:17bf9ceaf0aa | 379 | lastTouch = tt.get_touch(); |
TickTock | 7:17bf9ceaf0aa | 380 | lastTouch = tt.to_pixel(lastTouch); // convert to pixel pos |
TickTock | 40:0e6e71a7323f | 381 | if((lastTouch.x!=639)&&(lastTouch.x!=319)&&(lastTouch.y!=239)){ // filter phantom touches |
TickTock | 43:e7f6f80590e3 | 382 | if (userIdle) { |
TickTock | 43:e7f6f80590e3 | 383 | secsNoTouch=2; // Ignore first touch if user idle |
TickTock | 43:e7f6f80590e3 | 384 | userIdle=false; |
TickTock | 40:0e6e71a7323f | 385 | } else { |
TickTock | 43:e7f6f80590e3 | 386 | secsNoTouch=0; |
TickTock | 40:0e6e71a7323f | 387 | } |
TickTock | 43:e7f6f80590e3 | 388 | if (lastTouch.x>320){ |
TickTock | 43:e7f6f80590e3 | 389 | whichTouched=1; |
TickTock | 43:e7f6f80590e3 | 390 | } else { |
TickTock | 43:e7f6f80590e3 | 391 | whichTouched=0; |
TickTock | 43:e7f6f80590e3 | 392 | } |
TickTock | 43:e7f6f80590e3 | 393 | if (whichTouched!=lwt){ |
TickTock | 43:e7f6f80590e3 | 394 | lastDMode[lwt]=99; // Repaint lastTouched |
TickTock | 43:e7f6f80590e3 | 395 | lwt=whichTouched; |
TickTock | 43:e7f6f80590e3 | 396 | } |
TickTock | 43:e7f6f80590e3 | 397 | sMode = 1; |
TickTock | 22:a43df3905863 | 398 | } |
TickTock | 22:a43df3905863 | 399 | //sprintf(sTemp,"%d,%d ",lastTouch.x,lastTouch.y); |
TickTock | 93:c2402e8cd0e2 | 400 | //printMsg(sTemp); // touch x,y - for debug |
TickTock | 12:8e42d7ba8468 | 401 | touched = false; // clear interrupt flag |
TickTock | 7:17bf9ceaf0aa | 402 | } |
garygid | 54:6ce808d0995e | 403 | //--------------- |
garygid | 54:6ce808d0995e | 404 | // gg - 4x4 touch |
TickTock | 62:ffd15edb5431 | 405 | //unsigned char tScrn = 0 ; // screen 0 |
garygid | 54:6ce808d0995e | 406 | unsigned char tCol ; |
garygid | 54:6ce808d0995e | 407 | unsigned char tRow ; |
garygid | 54:6ce808d0995e | 408 | |
TickTock | 4:8d7759f4fe7a | 409 | if (!userIdle) { |
TickTock | 4:8d7759f4fe7a | 410 | if (secsNoTouch<2) {// Recently touched |
TickTock | 4:8d7759f4fe7a | 411 | secsNoTouch +=2; // increment to prevent double touch |
TickTock | 43:e7f6f80590e3 | 412 | sMode = 1; |
TickTock | 22:a43df3905863 | 413 | //sprintf(sTemp,"button %d %d,%d %d\n",i,buttonX(lastTouch.x,3),buttonY(lastTouch.y,3),lastTouch.x); |
TickTock | 93:c2402e8cd0e2 | 414 | //printMsg(sTemp); // button parms - for debug |
TickTock | 23:cd03f9c3395e | 415 | switch (sMode) { |
TickTock | 23:cd03f9c3395e | 416 | case 0: // no select |
TickTock | 23:cd03f9c3395e | 417 | break; |
TickTock | 63:aa3bf6a33635 | 418 | case 1: // select screen |
garygid | 54:6ce808d0995e | 419 | //-------------- |
garygid | 54:6ce808d0995e | 420 | // gg - 4x4 touch |
garygid | 54:6ce808d0995e | 421 | tCol = buttonX(lastTouch.x,4) ; |
TickTock | 62:ffd15edb5431 | 422 | if( tCol >= 4 ){ tCol -= 4; } // touch is on screen 1 |
garygid | 54:6ce808d0995e | 423 | |
garygid | 54:6ce808d0995e | 424 | tRow = buttonY(lastTouch.y,4) ; |
garygid | 54:6ce808d0995e | 425 | |
TickTock | 63:aa3bf6a33635 | 426 | highlightButton( tCol,tRow, whichTouched, 4,4) ; // gg - highlight |
garygid | 58:4d06288d75a2 | 427 | |
TickTock | 63:aa3bf6a33635 | 428 | if( tRow == tNavRow ) tRow = 7 ; // gg |
garygid | 54:6ce808d0995e | 429 | switch ( (tCol*10) + tRow ) { |
garygid | 54:6ce808d0995e | 430 | //--------------------------------- |
garygid | 67:2022fce701d0 | 431 | case 00: // top row, left button on screen 0 or 1 |
TickTock | 42:4533b13b297d | 432 | if (dMode[whichTouched]==monitorScreen||dMode[whichTouched]==changedScreen) { |
garygid | 67:2022fce701d0 | 433 | indexOffset=indexOffset>4?indexOffset-4:1; |
garygid | 67:2022fce701d0 | 434 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
garygid | 67:2022fce701d0 | 435 | dMode[whichTouched] = mainScreen ; // GoTo Main Screen |
TickTock | 73:62ee8eae3a84 | 436 | sMode=0; |
TickTock | 157:1a100a5ed06f | 437 | } else if (dMode[whichTouched]==config2Screen) { |
TickTock | 23:cd03f9c3395e | 438 | wait_ms(500); |
TickTock | 37:fea2c1d52c5f | 439 | tt.background(Black); |
TickTock | 23:cd03f9c3395e | 440 | tt.calibrate(); |
TickTock | 42:4533b13b297d | 441 | } else if (dMode[whichTouched]==playbackScreen) { // slower |
TickTock | 36:dbd39c315258 | 442 | playbackInt *=2; |
TickTock | 36:dbd39c315258 | 443 | if(playbackEn){ |
TickTock | 36:dbd39c315258 | 444 | playback.detach(); |
TickTock | 36:dbd39c315258 | 445 | playback.attach(&playbackISR,playbackInt); |
TickTock | 36:dbd39c315258 | 446 | } |
TickTock | 62:ffd15edb5431 | 447 | } else { |
TickTock | 62:ffd15edb5431 | 448 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 23:cd03f9c3395e | 449 | } |
TickTock | 23:cd03f9c3395e | 450 | break; |
garygid | 54:6ce808d0995e | 451 | //----------------------------------------------- |
garygid | 67:2022fce701d0 | 452 | case 10: // 1,0 (col left of center,top row) on screen 0 or 1 |
TickTock | 42:4533b13b297d | 453 | if (dMode[whichTouched]==changedScreen) { |
TickTock | 23:cd03f9c3395e | 454 | for(j=0;j<100;j++) msgChanged[j]=0; // clear changed data |
TickTock | 42:4533b13b297d | 455 | lastDMode[whichTouched]=99;//force refresh |
garygid | 67:2022fce701d0 | 456 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
TickTock | 73:62ee8eae3a84 | 457 | sMode=0; |
garygid | 67:2022fce701d0 | 458 | dMode[whichTouched] = brakeScreen ; // GoTo Brake Screen |
TickTock | 42:4533b13b297d | 459 | } else if (dMode[whichTouched]==cpScreen) { |
TickTock | 78:a383971fe02f | 460 | reqMsgCnt=0; |
TickTock | 79:68f0dd8d1f19 | 461 | msgReq.attach(&sendReq,0.015); |
TickTock | 48:d1ce92104a1f | 462 | } else if (dMode[whichTouched]==cpHistScreen) { // gg - hist |
TickTock | 78:a383971fe02f | 463 | reqMsgCnt=0; |
TickTock | 79:68f0dd8d1f19 | 464 | msgReq.attach(&sendReq,0.015); |
TickTock | 48:d1ce92104a1f | 465 | } else if (dMode[whichTouched]==cpBarScreen) { // gg - cpbars |
TickTock | 78:a383971fe02f | 466 | reqMsgCnt=0; |
TickTock | 79:68f0dd8d1f19 | 467 | msgReq.attach(&sendReq,0.015); |
leafman | 98:9f8bab96edff | 468 | } else if (dMode[whichTouched]==configScreen) { |
TickTock | 23:cd03f9c3395e | 469 | mbed_reset(); |
TickTock | 157:1a100a5ed06f | 470 | } else if (dMode[whichTouched]==config2Screen) { // reset DTE Max/Min |
TickTock | 158:059abeb44ba1 | 471 | maxTripEff = 0; |
TickTock | 158:059abeb44ba1 | 472 | minTripEff = 5; |
TickTock | 161:71ac85d11f03 | 473 | beep(2000,0.25); |
TickTock | 42:4533b13b297d | 474 | } else if (dMode[whichTouched]==playbackScreen) { // pause/unpause |
TickTock | 36:dbd39c315258 | 475 | playbackEn=!playbackEn; |
TickTock | 36:dbd39c315258 | 476 | if(playbackEn){ |
TickTock | 36:dbd39c315258 | 477 | playback.attach(&playbackISR,playbackInt); |
TickTock | 62:ffd15edb5431 | 478 | } else { |
TickTock | 36:dbd39c315258 | 479 | playback.detach(); |
TickTock | 36:dbd39c315258 | 480 | } |
TickTock | 62:ffd15edb5431 | 481 | } else { |
TickTock | 62:ffd15edb5431 | 482 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 23:cd03f9c3395e | 483 | } |
TickTock | 62:ffd15edb5431 | 484 | |
TickTock | 23:cd03f9c3395e | 485 | break; |
garygid | 54:6ce808d0995e | 486 | //-------------------------------------- |
garygid | 54:6ce808d0995e | 487 | case 20: // col 2 and row 0 on either screen 0 or 1 |
TickTock | 42:4533b13b297d | 488 | if (dMode[whichTouched]==monitorScreen||dMode[whichTouched]==changedScreen) { |
TickTock | 23:cd03f9c3395e | 489 | indexOffset=indexOffset<77?indexOffset+4:80; |
garygid | 67:2022fce701d0 | 490 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
garygid | 67:2022fce701d0 | 491 | dMode[whichTouched] = effScreen ; // GoTo EFF Screen |
TickTock | 73:62ee8eae3a84 | 492 | sMode=0; |
leafman | 98:9f8bab96edff | 493 | } else if (dMode[whichTouched]==configScreen) { |
TickTock | 143:88b5155622a5 | 494 | dMode[whichTouched]=mainScreen; |
TickTock | 25:ddf0ec209f03 | 495 | saveConfig(); |
TickTock | 155:f31006516956 | 496 | beep(2000,0.25); |
TickTock | 42:4533b13b297d | 497 | } else if (dMode[whichTouched]==playbackScreen) { // faster |
TickTock | 36:dbd39c315258 | 498 | if(playbackInt>.002){ |
TickTock | 36:dbd39c315258 | 499 | playbackInt/=2; |
TickTock | 36:dbd39c315258 | 500 | if(playbackEn){ |
TickTock | 36:dbd39c315258 | 501 | playback.detach(); |
TickTock | 36:dbd39c315258 | 502 | playback.attach(&playbackISR,playbackInt); |
TickTock | 36:dbd39c315258 | 503 | } |
TickTock | 36:dbd39c315258 | 504 | } |
TickTock | 86:d1c9e8ac1c4b | 505 | } else { |
TickTock | 62:ffd15edb5431 | 506 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 23:cd03f9c3395e | 507 | } |
TickTock | 62:ffd15edb5431 | 508 | |
TickTock | 23:cd03f9c3395e | 509 | break; |
garygid | 65:821fc79cd7fe | 510 | |
garygid | 67:2022fce701d0 | 511 | case 30: // right-most on top row |
garygid | 65:821fc79cd7fe | 512 | |
TickTock | 157:1a100a5ed06f | 513 | if (dMode[whichTouched]==config2Screen) { |
garygid | 65:821fc79cd7fe | 514 | // step through skins |
garygid | 65:821fc79cd7fe | 515 | if( skin < maxSkin ) skin += 1 ; |
garygid | 65:821fc79cd7fe | 516 | else skin = 0 ; |
garygid | 65:821fc79cd7fe | 517 | |
garygid | 65:821fc79cd7fe | 518 | // repaint both screens, I think |
garygid | 65:821fc79cd7fe | 519 | lastDMode[whichTouched]=99;//repaint to clear highlight |
garygid | 65:821fc79cd7fe | 520 | // and re-paint the other screen too, to see new skin there |
garygid | 65:821fc79cd7fe | 521 | lastDMode[whichTouched ^ 1]=99; // repaint other screen (^ = XOR) |
garygid | 67:2022fce701d0 | 522 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
TickTock | 122:138a40892a4c | 523 | dMode[whichTouched] = healthScreen ; // Goto health screen |
TickTock | 73:62ee8eae3a84 | 524 | sMode=0; |
TickTock | 158:059abeb44ba1 | 525 | } else if (dMode[whichTouched] == testScreen) { // gg - index |
TickTock | 158:059abeb44ba1 | 526 | clearTest=true; |
TickTock | 158:059abeb44ba1 | 527 | } else { // top-right corner always mute/unmute unless used by specific screen |
TickTock | 154:90ea16ca7475 | 528 | enableSound = !enableSound; |
TickTock | 155:f31006516956 | 529 | if(!enableSound) spkr=0; |
garygid | 65:821fc79cd7fe | 530 | } |
garygid | 65:821fc79cd7fe | 531 | |
garygid | 65:821fc79cd7fe | 532 | break; |
garygid | 54:6ce808d0995e | 533 | //---------------------------------- |
garygid | 54:6ce808d0995e | 534 | //---------------------------------- |
garygid | 67:2022fce701d0 | 535 | case 01: // left col middle row |
leafman | 98:9f8bab96edff | 536 | if (dMode[whichTouched]==configScreen) { |
TickTock | 23:cd03f9c3395e | 537 | logEn = !logEn; |
garygid | 67:2022fce701d0 | 538 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
TickTock | 73:62ee8eae3a84 | 539 | dMode[whichTouched] = cpScreen ; // GoTo CP Data Screen |
TickTock | 73:62ee8eae3a84 | 540 | sMode=0; |
TickTock | 42:4533b13b297d | 541 | } else if (dMode[whichTouched]==dateScreen){ |
TickTock | 131:0d926c080a65 | 542 | dtMode=(dtMode<5)?dtMode+1:0; |
TickTock | 42:4533b13b297d | 543 | lastDMode[whichTouched]=99; |
TickTock | 62:ffd15edb5431 | 544 | } else { |
TickTock | 62:ffd15edb5431 | 545 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 23:cd03f9c3395e | 546 | } |
TickTock | 62:ffd15edb5431 | 547 | |
TickTock | 23:cd03f9c3395e | 548 | break; |
garygid | 54:6ce808d0995e | 549 | //------------------------------ |
TickTock | 23:cd03f9c3395e | 550 | case 11: |
leafman | 98:9f8bab96edff | 551 | if (dMode[whichTouched]==configScreen){ |
TickTock | 77:7c136766466c | 552 | repeatPoll = !repeatPoll; |
TickTock | 33:a277743ebdeb | 553 | if (repeatPoll) { |
TickTock | 36:dbd39c315258 | 554 | autoPoll.attach(&autoPollISR,pollInt); |
TickTock | 33:a277743ebdeb | 555 | } else { |
TickTock | 36:dbd39c315258 | 556 | autoPoll.detach(); |
TickTock | 33:a277743ebdeb | 557 | } |
garygid | 67:2022fce701d0 | 558 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
garygid | 67:2022fce701d0 | 559 | dMode[whichTouched] = cpHistScreen ; // GoTo CP Hist Screen |
TickTock | 73:62ee8eae3a84 | 560 | sMode=0; |
TickTock | 42:4533b13b297d | 561 | } else if (dMode[whichTouched]==playbackScreen) { |
TickTock | 36:dbd39c315258 | 562 | // Start/stop playback |
TickTock | 36:dbd39c315258 | 563 | if(!playbackOpen){ |
TickTock | 131:0d926c080a65 | 564 | if(!carCanIdle){ |
TickTock | 131:0d926c080a65 | 565 | printMsg("Cannot playback while connected to canbus\n"); |
TickTock | 131:0d926c080a65 | 566 | }else if(!logOpen){ |
TickTock | 109:3e6f0e8fca0d | 567 | efr = f_open(&efile,"playback.alc",FA_READ|FA_OPEN_EXISTING); |
TickTock | 50:83d5864c64a0 | 568 | lastDMode[whichTouched]=99;//force refresh |
TickTock | 109:3e6f0e8fca0d | 569 | if(efr != FR_OK){ |
TickTock | 116:5cd72bae7c12 | 570 | printMsg("Unable to open /usb/playback.alc\n"); // no playback.alc |
TickTock | 155:f31006516956 | 571 | beep(1000,0.25); |
TickTock | 36:dbd39c315258 | 572 | } else { |
TickTock | 36:dbd39c315258 | 573 | playbackOpen = true; |
TickTock | 50:83d5864c64a0 | 574 | playbackEn=true; |
TickTock | 50:83d5864c64a0 | 575 | playback.attach(&playbackISR,playbackInt); |
TickTock | 116:5cd72bae7c12 | 576 | printMsg("Starting playback\n"); // start playback |
TickTock | 155:f31006516956 | 577 | beep(2000,0.25); |
TickTock | 138:a2f5af85ed0d | 578 | can1.attach(NULL);// Stop recieving EVCAN data |
TickTock | 138:a2f5af85ed0d | 579 | can2.attach(NULL);// Stop recieving CARCAN data |
TickTock | 36:dbd39c315258 | 580 | } |
TickTock | 36:dbd39c315258 | 581 | } else { |
TickTock | 117:49883c779a74 | 582 | printMsg("Must stop logging first\n"); |
TickTock | 36:dbd39c315258 | 583 | } |
TickTock | 36:dbd39c315258 | 584 | } else { |
TickTock | 36:dbd39c315258 | 585 | playback.detach(); |
TickTock | 109:3e6f0e8fca0d | 586 | f_close(&efile); |
TickTock | 36:dbd39c315258 | 587 | playbackOpen=false; |
TickTock | 50:83d5864c64a0 | 588 | playbackEn=false; |
TickTock | 138:a2f5af85ed0d | 589 | can1.attach(&recieve1);// Restore EVCAN data recieve |
TickTock | 138:a2f5af85ed0d | 590 | can2.attach(&recieve2);// Restore EVCAN data recieve |
TickTock | 42:4533b13b297d | 591 | lastDMode[whichTouched]=99; |
TickTock | 36:dbd39c315258 | 592 | } |
TickTock | 42:4533b13b297d | 593 | } else if (dMode[whichTouched]==dateScreen){ |
TickTock | 25:ddf0ec209f03 | 594 | upDate(dtMode,true); |
TickTock | 42:4533b13b297d | 595 | lastDMode[whichTouched]=99; |
TickTock | 62:ffd15edb5431 | 596 | } else { |
TickTock | 62:ffd15edb5431 | 597 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 25:ddf0ec209f03 | 598 | } |
TickTock | 62:ffd15edb5431 | 599 | |
TickTock | 23:cd03f9c3395e | 600 | break; |
garygid | 54:6ce808d0995e | 601 | //--------------------------------- |
garygid | 54:6ce808d0995e | 602 | case 21: // col 2 row 1 |
leafman | 98:9f8bab96edff | 603 | if (dMode[whichTouched]==configScreen) { // gg - Batt Log Enable Button |
TickTock | 48:d1ce92104a1f | 604 | yesBattLog = !yesBattLog; |
garygid | 67:2022fce701d0 | 605 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
garygid | 67:2022fce701d0 | 606 | dMode[whichTouched] = cpBarScreen ; // GoTo CP Bars Screen |
TickTock | 73:62ee8eae3a84 | 607 | sMode=0; |
TickTock | 48:d1ce92104a1f | 608 | } else if (dMode[whichTouched]==dateScreen){ |
TickTock | 25:ddf0ec209f03 | 609 | upDate(dtMode,false); |
TickTock | 42:4533b13b297d | 610 | lastDMode[whichTouched]=99; |
TickTock | 62:ffd15edb5431 | 611 | } else { |
TickTock | 62:ffd15edb5431 | 612 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 25:ddf0ec209f03 | 613 | } |
TickTock | 62:ffd15edb5431 | 614 | |
TickTock | 23:cd03f9c3395e | 615 | break; |
garygid | 54:6ce808d0995e | 616 | |
garygid | 87:46ac3f2519d6 | 617 | case 31: // col 3 row 1 |
TickTock | 157:1a100a5ed06f | 618 | if (dMode[whichTouched]==config2Screen) { // gg - Batt Log Enable Button |
garygid | 87:46ac3f2519d6 | 619 | debugMode = !debugMode; |
leafman | 98:9f8bab96edff | 620 | } else if (dMode[whichTouched] == indexScreen) { // gg - index |
TickTock | 152:a4d66901785d | 621 | dMode[whichTouched] = configScreen ; // GoTo Config Screen |
TickTock | 152:a4d66901785d | 622 | } else if ((dMode[whichTouched]==dateScreen)&&accOn){ |
TickTock | 160:905fe45ed54b | 623 | syncDone=false; // initiate clock sync |
TickTock | 152:a4d66901785d | 624 | lastDMode[whichTouched]=99; |
garygid | 87:46ac3f2519d6 | 625 | } else { |
garygid | 87:46ac3f2519d6 | 626 | lastDMode[whichTouched]=99;//repaint to clear highlight |
garygid | 87:46ac3f2519d6 | 627 | } |
garygid | 87:46ac3f2519d6 | 628 | break; |
garygid | 87:46ac3f2519d6 | 629 | |
garygid | 54:6ce808d0995e | 630 | //----------------------------------- |
garygid | 67:2022fce701d0 | 631 | case 02: // left col, bottom row (not nav) |
garygid | 67:2022fce701d0 | 632 | if (dMode[whichTouched] == indexScreen) { // gg - index |
leafman | 98:9f8bab96edff | 633 | dMode[whichTouched] = playbackScreen ; // GoTo Playback Screen |
leafman | 98:9f8bab96edff | 634 | } else if (dMode[whichTouched]==configScreen) { |
TickTock | 140:ab3e94eb0227 | 635 | brakeMon = !brakeMon; |
TickTock | 140:ab3e94eb0227 | 636 | if(brakeMon){ |
TickTock | 140:ab3e94eb0227 | 637 | geiger.attach(&chirp,0.02); |
TickTock | 140:ab3e94eb0227 | 638 | }else{ |
TickTock | 140:ab3e94eb0227 | 639 | geiger.detach(); |
TickTock | 140:ab3e94eb0227 | 640 | } |
TickTock | 158:059abeb44ba1 | 641 | } else if (dMode[whichTouched]==config2Screen) { |
TickTock | 158:059abeb44ba1 | 642 | autoSync = !autoSync; |
garygid | 87:46ac3f2519d6 | 643 | } else { |
garygid | 87:46ac3f2519d6 | 644 | lastDMode[whichTouched]=99;//repaint to clear highlight |
garygid | 87:46ac3f2519d6 | 645 | } |
garygid | 67:2022fce701d0 | 646 | break; |
garygid | 67:2022fce701d0 | 647 | |
garygid | 67:2022fce701d0 | 648 | case 12: // left-middle col, bottom row (not nav) |
TickTock | 158:059abeb44ba1 | 649 | if (dMode[whichTouched]==configScreen) { |
TickTock | 158:059abeb44ba1 | 650 | regenMon = !regenMon; |
TickTock | 158:059abeb44ba1 | 651 | } else if (dMode[whichTouched]==config2Screen) { // gg - index |
leafman | 98:9f8bab96edff | 652 | dMode[whichTouched] = dateScreen ; // GoTo Set Date/Time Screen |
garygid | 87:46ac3f2519d6 | 653 | } else { |
garygid | 87:46ac3f2519d6 | 654 | lastDMode[whichTouched]=99;//repaint to clear highlight |
garygid | 87:46ac3f2519d6 | 655 | } |
garygid | 67:2022fce701d0 | 656 | break; |
garygid | 67:2022fce701d0 | 657 | |
garygid | 67:2022fce701d0 | 658 | case 22: // right-middle col, bottom row (not nav) |
TickTock | 158:059abeb44ba1 | 659 | if (dMode[whichTouched]==indexScreen) { // gg - index |
leafman | 98:9f8bab96edff | 660 | dMode[whichTouched] = logScreen ; |
TickTock | 159:577e9d015693 | 661 | } else if (dMode[whichTouched]==configScreen) { |
TickTock | 159:577e9d015693 | 662 | heaterMon = !heaterMon; |
TickTock | 157:1a100a5ed06f | 663 | } else if (dMode[whichTouched]==config2Screen) { |
TickTock | 131:0d926c080a65 | 664 | showHealth = !showHealth; |
garygid | 87:46ac3f2519d6 | 665 | } else { |
garygid | 87:46ac3f2519d6 | 666 | lastDMode[whichTouched]=99;//repaint to clear highlight |
garygid | 87:46ac3f2519d6 | 667 | } |
garygid | 67:2022fce701d0 | 668 | break; |
TickTock | 72:f9a6faa28873 | 669 | |
leafman | 98:9f8bab96edff | 670 | case 32: // right col, bottom row (not nav) |
TickTock | 158:059abeb44ba1 | 671 | if (dMode[whichTouched]==config2Screen) { |
TickTock | 114:3f8c59a8a2b9 | 672 | logEn=false; |
leafman | 98:9f8bab96edff | 673 | updateFirmware(); |
TickTock | 158:059abeb44ba1 | 674 | } else if (dMode[whichTouched]==tripScreen) { |
TickTock | 108:29b5a760adc2 | 675 | miles_trip[2]=0; |
TickTock | 108:29b5a760adc2 | 676 | kWh_trip[2]=0; |
TickTock | 73:62ee8eae3a84 | 677 | sMode=0; |
TickTock | 108:29b5a760adc2 | 678 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 158:059abeb44ba1 | 679 | } else if (dMode[whichTouched]==indexScreen) { |
TickTock | 108:29b5a760adc2 | 680 | dMode[whichTouched] = tripScreen ; |
TickTock | 152:a4d66901785d | 681 | } else if (dMode[whichTouched]==dateScreen){ |
TickTock | 152:a4d66901785d | 682 | autoSync=!autoSync; // toggle autoSync mode |
TickTock | 152:a4d66901785d | 683 | lastDMode[whichTouched]=99; |
leafman | 98:9f8bab96edff | 684 | } else { |
garygid | 87:46ac3f2519d6 | 685 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 72:f9a6faa28873 | 686 | } |
TickTock | 72:f9a6faa28873 | 687 | break; |
TickTock | 72:f9a6faa28873 | 688 | |
garygid | 67:2022fce701d0 | 689 | //----------------------------------- |
garygid | 54:6ce808d0995e | 690 | //----------------------------------- |
garygid | 54:6ce808d0995e | 691 | // Prev Navigation |
garygid | 54:6ce808d0995e | 692 | case 07: // col 0 row tNavRow |
TickTock | 42:4533b13b297d | 693 | dMode[whichTouched]=dMode[whichTouched]>0?dMode[whichTouched]-1:maxScreens; |
TickTock | 22:a43df3905863 | 694 | break; |
garygid | 54:6ce808d0995e | 695 | //----------------------------------- |
garygid | 54:6ce808d0995e | 696 | // Select Screen Navigation |
garygid | 54:6ce808d0995e | 697 | case 17: |
TickTock | 43:e7f6f80590e3 | 698 | sMode=0; |
TickTock | 43:e7f6f80590e3 | 699 | lastDMode[whichTouched]=99; // Repaint |
TickTock | 22:a43df3905863 | 700 | break; |
garygid | 54:6ce808d0995e | 701 | //----------------------------------- |
garygid | 54:6ce808d0995e | 702 | // Index Navigation |
garygid | 54:6ce808d0995e | 703 | case 27: // col 2 row tNavRow |
garygid | 67:2022fce701d0 | 704 | dMode[whichTouched]= indexScreen ; // gg - index |
garygid | 54:6ce808d0995e | 705 | break; |
garygid | 54:6ce808d0995e | 706 | //------------------------------------ |
garygid | 54:6ce808d0995e | 707 | // Next Navigation |
garygid | 54:6ce808d0995e | 708 | case 37: // lower right on Nav Line gg - move next |
TickTock | 42:4533b13b297d | 709 | dMode[whichTouched]=dMode[whichTouched]<maxScreens?dMode[whichTouched]+1:0; |
TickTock | 22:a43df3905863 | 710 | break; |
garygid | 54:6ce808d0995e | 711 | //------------------------------------ |
garygid | 54:6ce808d0995e | 712 | //------------------------------------ |
TickTock | 22:a43df3905863 | 713 | default: |
TickTock | 62:ffd15edb5431 | 714 | lastDMode[whichTouched]=99;//repaint to clear highlight |
TickTock | 25:ddf0ec209f03 | 715 | break; |
TickTock | 25:ddf0ec209f03 | 716 | } |
TickTock | 23:cd03f9c3395e | 717 | break; |
TickTock | 23:cd03f9c3395e | 718 | case 2: // numpad |
TickTock | 23:cd03f9c3395e | 719 | break; |
TickTock | 23:cd03f9c3395e | 720 | case 3: |
TickTock | 23:cd03f9c3395e | 721 | break; |
TickTock | 23:cd03f9c3395e | 722 | default: |
TickTock | 23:cd03f9c3395e | 723 | break; |
TickTock | 23:cd03f9c3395e | 724 | } // case sMode |
TickTock | 22:a43df3905863 | 725 | } //recently touched |
TickTock | 4:8d7759f4fe7a | 726 | } else { // userIdle |
TickTock | 8:67eed72f3e10 | 727 | if(sMode==1){ |
TickTock | 8:67eed72f3e10 | 728 | sMode=0; |
TickTock | 43:e7f6f80590e3 | 729 | lastDMode[whichTouched]=99; |
TickTock | 8:67eed72f3e10 | 730 | } |
TickTock | 4:8d7759f4fe7a | 731 | } |
TickTock | 34:4751a8259b18 | 732 | |
TickTock | 153:e94cfe3c339c | 733 | // Sound tone on power reversal |
TickTock | 153:e94cfe3c339c | 734 | idir=(kW[0]>0)?true:false; |
TickTock | 155:f31006516956 | 735 | if(regenMon){ |
TickTock | 153:e94cfe3c339c | 736 | if (idir&&!lidir){ |
TickTock | 155:f31006516956 | 737 | beep(800,0.02); // Started sinking current |
TickTock | 153:e94cfe3c339c | 738 | }else if(!idir&&lidir){ |
TickTock | 155:f31006516956 | 739 | beep(3200,0.02); // Started regen |
TickTock | 153:e94cfe3c339c | 740 | } |
TickTock | 153:e94cfe3c339c | 741 | } |
TickTock | 154:90ea16ca7475 | 742 | lidir=idir; |
TickTock | 153:e94cfe3c339c | 743 | |
TickTock | 34:4751a8259b18 | 744 | if(tick){ // Executes once a second |
TickTock | 108:29b5a760adc2 | 745 | tick=false; |
TickTock | 164:46ed06263b0e | 746 | lCCon = CCon; |
TickTock | 161:71ac85d11f03 | 747 | CCkW = (lastMsg[indexLastMsg[0x510]].data[3]&0x7f)*0.125; |
TickTock | 164:46ed06263b0e | 748 | if(lastMsg[indexLastMsg[0x510]].data[3]&0x80){ |
TickTock | 164:46ed06263b0e | 749 | CCon=true; // On when button pushed |
TickTock | 164:46ed06263b0e | 750 | } else if(CCkW==0) { |
TickTock | 164:46ed06263b0e | 751 | CCon=false; // Off when power drops back to zero |
TickTock | 164:46ed06263b0e | 752 | } |
TickTock | 164:46ed06263b0e | 753 | if(!CCon && lCCon){ |
TickTock | 164:46ed06263b0e | 754 | lastDMode[0]=99;//force refresh |
TickTock | 164:46ed06263b0e | 755 | lastDMode[1]=99;//force refresh |
TickTock | 164:46ed06263b0e | 756 | } |
TickTock | 156:4e5d30535cf9 | 757 | if (miles_trip[0]>0.25) { |
TickTock | 156:4e5d30535cf9 | 758 | curEff = miles_trip[0]/kWh_trip[0]; |
TickTock | 156:4e5d30535cf9 | 759 | } else { |
TickTock | 156:4e5d30535cf9 | 760 | curEff = 0; |
TickTock | 156:4e5d30535cf9 | 761 | } |
TickTock | 93:c2402e8cd0e2 | 762 | headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false; // headlight/turn signal indicator |
TickTock | 160:905fe45ed54b | 763 | if(heaterOn){ |
TickTock | 160:905fe45ed54b | 764 | lHeaterOn=true; // Only indicate heater once per power cycle |
TickTock | 160:905fe45ed54b | 765 | } |
TickTock | 159:577e9d015693 | 766 | heaterOn =((lastMsg[indexLastMsg[0x54f]].data[5]&0x3f)>0)?true:false; |
TickTock | 159:577e9d015693 | 767 | if(heaterMon && heaterOn && !lHeaterOn){ //Heat on alarm |
TickTock | 160:905fe45ed54b | 768 | beep3(800,0.25,1200,0.25,1600,0.25); |
TickTock | 160:905fe45ed54b | 769 | //beep(1600,0.5); |
TickTock | 159:577e9d015693 | 770 | } |
TickTock | 148:6e3b9135fad2 | 771 | if(accOn&&indexLastMsg[0x355]>0){ |
TickTock | 141:cf13a632ddb5 | 772 | miles_kmbar = (lastMsg[indexLastMsg[0x355]].data[4]&0x20)?true:false; // indicates selected distance units |
TickTock | 141:cf13a632ddb5 | 773 | metric = !miles_kmbar; |
TickTock | 141:cf13a632ddb5 | 774 | } |
TickTock | 41:8d4609ea7259 | 775 | accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th |
TickTock | 35:5acbd8a64a89 | 776 | accOn=(accV>5)?true:false; |
TickTock | 131:0d926c080a65 | 777 | moving=(mph[0]>0.1); |
TickTock | 151:3047ebb3c9a8 | 778 | charging=(lastMsg[indexLastMsg[0x5bf]].data[2]>0)?true:false; // FF when charging |
TickTock | 134:ad243e4f1a17 | 779 | if (laccOn&&!accOn){ // Car turned off |
TickTock | 160:905fe45ed54b | 780 | lHeaterOn=false; |
TickTock | 134:ad243e4f1a17 | 781 | if (showHealth){ |
TickTock | 134:ad243e4f1a17 | 782 | if (saveDmode==99){ |
TickTock | 134:ad243e4f1a17 | 783 | saveDmode=dMode[0]; |
TickTock | 134:ad243e4f1a17 | 784 | } |
TickTock | 134:ad243e4f1a17 | 785 | dMode[0]=healthScreen; |
TickTock | 135:89986950d501 | 786 | secsNoTouch=2;// Keep display on a few seconds |
TickTock | 134:ad243e4f1a17 | 787 | sMode=0; |
TickTock | 134:ad243e4f1a17 | 788 | userIdle=false; |
TickTock | 134:ad243e4f1a17 | 789 | } |
TickTock | 132:08748a67280a | 790 | // Keep track of max and min trip efficiency |
TickTock | 158:059abeb44ba1 | 791 | if (miles_trip[0]>5){ // Ignore short trips |
TickTock | 144:b0c9d30dd346 | 792 | if (maxTripEff<curEff) { |
TickTock | 144:b0c9d30dd346 | 793 | maxTripEff=curEff; |
TickTock | 134:ad243e4f1a17 | 794 | } |
TickTock | 144:b0c9d30dd346 | 795 | if (minTripEff>curEff) { |
TickTock | 144:b0c9d30dd346 | 796 | minTripEff=curEff; |
TickTock | 134:ad243e4f1a17 | 797 | } |
TickTock | 132:08748a67280a | 798 | } |
TickTock | 108:29b5a760adc2 | 799 | if (repeatPoll) { // Log on shutdown if autopoll enabled |
TickTock | 108:29b5a760adc2 | 800 | tripLog(); // Write trip log on powerdown |
TickTock | 108:29b5a760adc2 | 801 | } |
TickTock | 49:a3d2c5bb3cfa | 802 | //write efficiency history data |
TickTock | 97:a25940fd7b5b | 803 | hfile = fopen("/local/ehist.cny", "w"); |
TickTock | 97:a25940fd7b5b | 804 | if (hfile!=NULL){ // found a efficiency history file |
TickTock | 49:a3d2c5bb3cfa | 805 | for(i=0;i<39;i++){ |
TickTock | 97:a25940fd7b5b | 806 | fprintf(hfile,"%f %f\r\n",mph[i],kW[i]); |
TickTock | 49:a3d2c5bb3cfa | 807 | } |
TickTock | 134:ad243e4f1a17 | 808 | fprintf(hfile,"%f %f\r\n",maxTripEff,minTripEff); // Save max and min |
TickTock | 155:f31006516956 | 809 | fprintf(hfile,"%f \r\n",Resr); // Save series resistance |
TickTock | 97:a25940fd7b5b | 810 | fclose(hfile); |
TickTock | 49:a3d2c5bb3cfa | 811 | } |
TickTock | 49:a3d2c5bb3cfa | 812 | } |
TickTock | 134:ad243e4f1a17 | 813 | if (!laccOn&&accOn){ // Car turned on |
TickTock | 160:905fe45ed54b | 814 | lHeaterOn=false; |
TickTock | 108:29b5a760adc2 | 815 | miles_trip[0]=0; |
TickTock | 108:29b5a760adc2 | 816 | kWh_trip[0]=0; |
TickTock | 115:549410af477d | 817 | wait5secs=5; |
TickTock | 134:ad243e4f1a17 | 818 | if (showHealth){ |
TickTock | 134:ad243e4f1a17 | 819 | if (saveDmode==99){ |
TickTock | 134:ad243e4f1a17 | 820 | saveDmode=dMode[0]; |
TickTock | 134:ad243e4f1a17 | 821 | } |
TickTock | 131:0d926c080a65 | 822 | dMode[0]=healthScreen; |
TickTock | 131:0d926c080a65 | 823 | } |
TickTock | 152:a4d66901785d | 824 | syncDone=!autoSync; // clear syncDone flag if autoSync enabled |
TickTock | 115:549410af477d | 825 | } |
TickTock | 49:a3d2c5bb3cfa | 826 | laccOn=accOn; |
TickTock | 135:89986950d501 | 827 | if(!accOn&&userIdle&&!playbackEn){ // Car off and no user activity - turn off screen |
TickTock | 134:ad243e4f1a17 | 828 | dled = 0; |
TickTock | 134:ad243e4f1a17 | 829 | if (saveDmode<99){ |
TickTock | 134:ad243e4f1a17 | 830 | dMode[0]=saveDmode; |
TickTock | 134:ad243e4f1a17 | 831 | saveDmode=99; |
TickTock | 134:ad243e4f1a17 | 832 | } |
TickTock | 35:5acbd8a64a89 | 833 | }else if(!headlights){ |
TickTock | 35:5acbd8a64a89 | 834 | dled = ledHi; |
TickTock | 115:549410af477d | 835 | } else { |
TickTock | 35:5acbd8a64a89 | 836 | dled = ledLo; |
TickTock | 34:4751a8259b18 | 837 | } |
TickTock | 115:549410af477d | 838 | if(wait5secs>0){ // Wait a few seconds after poweron to give BMS time to measure CP's |
TickTock | 115:549410af477d | 839 | wait5secs-=1; |
TickTock | 115:549410af477d | 840 | if (repeatPoll&&(wait5secs==0)) { // Poll on startup if autopoll enabled |
TickTock | 108:29b5a760adc2 | 841 | logOnce=true; |
TickTock | 112:b41b35401eb6 | 842 | reqMsgCnt=0; |
TickTock | 124:0d622967b764 | 843 | msgReq.attach(&sendReq,0.015); |
TickTock | 164:46ed06263b0e | 844 | lastDMode[0]=99; |
TickTock | 164:46ed06263b0e | 845 | lastDMode[1]=99; |
TickTock | 108:29b5a760adc2 | 846 | } |
TickTock | 107:e9be732c1ad4 | 847 | } |
TickTock | 134:ad243e4f1a17 | 848 | |
TickTock | 134:ad243e4f1a17 | 849 | //remove health screen once moving |
TickTock | 131:0d926c080a65 | 850 | if(moving&&(saveDmode<99)&&(wait5secs==0)){ |
TickTock | 131:0d926c080a65 | 851 | dMode[0]=saveDmode; |
TickTock | 131:0d926c080a65 | 852 | saveDmode=99; |
TickTock | 131:0d926c080a65 | 853 | } |
TickTock | 115:549410af477d | 854 | |
TickTock | 49:a3d2c5bb3cfa | 855 | //compute historic efficiency |
TickTock | 41:8d4609ea7259 | 856 | if(numSsamples>0){ // Avoid div0 |
TickTock | 160:905fe45ed54b | 857 | // calibrated to dash mph which reads slightly fast. |
TickTock | 160:905fe45ed54b | 858 | // 227 would give more accurate mph for MXV4s@40psi - 11.75" distance from center of tire to pavement |
TickTock | 160:905fe45ed54b | 859 | // but then efficiency estimation would not track miles driven as read from the odometer so |
TickTock | 160:905fe45ed54b | 860 | // making CANary have the same error as the Leaf instrumentation |
TickTock | 160:905fe45ed54b | 861 | mph[0]=((float) motorRPM_x2)/numSsamples/220; |
TickTock | 86:d1c9e8ac1c4b | 862 | } else { |
TickTock | 49:a3d2c5bb3cfa | 863 | mph[0]=0; |
TickTock | 36:dbd39c315258 | 864 | } |
TickTock | 49:a3d2c5bb3cfa | 865 | if(mph[0]>99){ |
TickTock | 49:a3d2c5bb3cfa | 866 | mph[0]=0; |
TickTock | 49:a3d2c5bb3cfa | 867 | } |
TickTock | 121:553faf139a20 | 868 | numSsamples=0; |
TickTock | 107:e9be732c1ad4 | 869 | |
TickTock | 41:8d4609ea7259 | 870 | if(numWsamples>0){ // Avoid div0 |
TickTock | 121:553faf139a20 | 871 | mpkWh[0]=mph[0]; |
TickTock | 161:71ac85d11f03 | 872 | kW[0]=((float) mWs_x4)/numWsamples/4e3; |
TickTock | 49:a3d2c5bb3cfa | 873 | mpkWh[0]/=kW[0]; |
TickTock | 49:a3d2c5bb3cfa | 874 | if (mpkWh[0]<0) { |
TickTock | 49:a3d2c5bb3cfa | 875 | mpkWh[0]=99;// negative means inf. |
TickTock | 49:a3d2c5bb3cfa | 876 | } |
TickTock | 161:71ac85d11f03 | 877 | kW[0]-=CCkW; // subtract climate control power from recorded value |
TickTock | 161:71ac85d11f03 | 878 | } else { |
TickTock | 49:a3d2c5bb3cfa | 879 | kW[0]=0; |
TickTock | 49:a3d2c5bb3cfa | 880 | mpkWh[0]=0; |
TickTock | 49:a3d2c5bb3cfa | 881 | } |
TickTock | 121:553faf139a20 | 882 | numWsamples=0; |
TickTock | 121:553faf139a20 | 883 | |
TickTock | 161:71ac85d11f03 | 884 | if((accOn||playbackEn)&&!charging){ // Calculate averages |
TickTock | 161:71ac85d11f03 | 885 | for(i=1;i<39;i++){ |
TickTock | 161:71ac85d11f03 | 886 | average=mph[i]/timeConstant[i]; |
TickTock | 161:71ac85d11f03 | 887 | mph[i]-=average; |
TickTock | 161:71ac85d11f03 | 888 | mph[i]+=mph[0]; |
TickTock | 161:71ac85d11f03 | 889 | mpkWh[i]=average; |
TickTock | 161:71ac85d11f03 | 890 | average=kW[i]/timeConstant[i]; |
TickTock | 161:71ac85d11f03 | 891 | kW[i]-=average; |
TickTock | 161:71ac85d11f03 | 892 | kW[i]+=kW[0]; |
TickTock | 162:c6545fc0164a | 893 | if(i==dtePeriod) mpkWh_noCC=mpkWh[i]/average; // compute efficiency w/o CC for dtePeriod |
TickTock | 161:71ac85d11f03 | 894 | average+=CCkW; //add climate control power back in for display |
TickTock | 161:71ac85d11f03 | 895 | mpkWh[i]/=average; |
TickTock | 161:71ac85d11f03 | 896 | if (mpkWh[i]<0) { |
TickTock | 161:71ac85d11f03 | 897 | mpkWh[i]=99;// negative means inf. |
TickTock | 161:71ac85d11f03 | 898 | } |
TickTock | 161:71ac85d11f03 | 899 | } |
TickTock | 161:71ac85d11f03 | 900 | } |
TickTock | 161:71ac85d11f03 | 901 | |
TickTock | 121:553faf139a20 | 902 | if (!charging){ |
TickTock | 151:3047ebb3c9a8 | 903 | miles_trip[0]+=mph[0]/3600; // per trip |
TickTock | 151:3047ebb3c9a8 | 904 | miles_trip[1]+=mph[0]/3600; // per charge |
TickTock | 151:3047ebb3c9a8 | 905 | miles_trip[2]+=mph[0]/3600; // user |
TickTock | 121:553faf139a20 | 906 | kWh_trip[0]+=kW[0]/3600; |
TickTock | 121:553faf139a20 | 907 | kWh_trip[1]+=kW[0]/3600; |
TickTock | 121:553faf139a20 | 908 | kWh_trip[2]+=kW[0]/3600; |
TickTock | 161:71ac85d11f03 | 909 | } else { // charging so reset per charge trip meter |
TickTock | 151:3047ebb3c9a8 | 910 | miles_trip[1]=0; |
TickTock | 151:3047ebb3c9a8 | 911 | kWh_trip[1]=0; |
TickTock | 121:553faf139a20 | 912 | } |
TickTock | 121:553faf139a20 | 913 | |
TickTock | 160:905fe45ed54b | 914 | motorRPM_x2=0; |
TickTock | 40:0e6e71a7323f | 915 | mWs_x4=0; |
TickTock | 121:553faf139a20 | 916 | |
TickTock | 121:553faf139a20 | 917 | // Compute ESR |
TickTock | 121:553faf139a20 | 918 | if((Imax-Imin)<40){ // do nothing - insufficient delta_I to measure |
TickTock | 121:553faf139a20 | 919 | unloadedV_x2 = (curRmax+curRmin)/2; |
TickTock | 154:90ea16ca7475 | 920 | }else if ((redRmax-redRmin)<(curRmax-curRmin)) { // less variation on reduced Resr |
TickTock | 120:041edeec08f5 | 921 | Resr-=0.001; |
TickTock | 120:041edeec08f5 | 922 | unloadedV_x2 = (redRmax+redRmin)/2; |
TickTock | 154:90ea16ca7475 | 923 | } else if ((incRmax-incRmin)<(curRmax-curRmin)) { // less variation on increased Resr |
TickTock | 120:041edeec08f5 | 924 | Resr+=0.001; |
TickTock | 120:041edeec08f5 | 925 | unloadedV_x2 = (incRmax+incRmin)/2; |
TickTock | 154:90ea16ca7475 | 926 | } else { // current Resr is still best answer |
TickTock | 120:041edeec08f5 | 927 | unloadedV_x2 = (curRmax+curRmin)/2; |
TickTock | 120:041edeec08f5 | 928 | } |
TickTock | 120:041edeec08f5 | 929 | curRmin=1000; |
TickTock | 120:041edeec08f5 | 930 | curRmax=0; |
TickTock | 120:041edeec08f5 | 931 | incRmin=1000; |
TickTock | 120:041edeec08f5 | 932 | incRmax=0; |
TickTock | 120:041edeec08f5 | 933 | redRmin=1000; |
TickTock | 120:041edeec08f5 | 934 | redRmax=0; |
TickTock | 121:553faf139a20 | 935 | Imax=-1000; |
TickTock | 121:553faf139a20 | 936 | Imin=1000; |
TickTock | 121:553faf139a20 | 937 | |
TickTock | 108:29b5a760adc2 | 938 | if(logCP&&usbEn){ |
TickTock | 108:29b5a760adc2 | 939 | if(logOnce){ |
TickTock | 108:29b5a760adc2 | 940 | tripLog(); |
TickTock | 108:29b5a760adc2 | 941 | logOnce=false; |
TickTock | 108:29b5a760adc2 | 942 | } |
garygid | 69:6bfdfc002036 | 943 | logPackVoltages(); // Turbo3, only call |
TickTock | 108:29b5a760adc2 | 944 | } |
TickTock | 137:70853cf5a30f | 945 | if(!usbEn){ |
TickTock | 108:29b5a760adc2 | 946 | usbEn=detectUSB(); // Keep looking if none found |
TickTock | 132:08748a67280a | 947 | if(usbEn){ // Force update to clear USB init garbage |
TickTock | 132:08748a67280a | 948 | lastDMode[0]=99; |
TickTock | 132:08748a67280a | 949 | lastDMode[1]=99; |
TickTock | 132:08748a67280a | 950 | } |
TickTock | 108:29b5a760adc2 | 951 | } |
TickTock | 152:a4d66901785d | 952 | if(!syncDone){ |
TickTock | 153:e94cfe3c339c | 953 | syncDone=syncDateTime(); |
TickTock | 152:a4d66901785d | 954 | } |
TickTock | 121:553faf139a20 | 955 | tock=true; |
TickTock | 121:553faf139a20 | 956 | } // tick |
TickTock | 39:eef8beac7411 | 957 | |
TickTock | 36:dbd39c315258 | 958 | if(step){ // playback |
TickTock | 36:dbd39c315258 | 959 | if(playbackOpen&&playbackEn){ |
TickTock | 38:155ec32c5e91 | 960 | for(i=0;i<120;i++){ |
TickTock | 109:3e6f0e8fca0d | 961 | if(!f_eof(&efile)){ |
TickTock | 109:3e6f0e8fca0d | 962 | efr=f_read(&efile,&header,5,&bytesRW); |
TickTock | 109:3e6f0e8fca0d | 963 | efr=f_read(&efile,&data,8,&bytesRW); |
garygid | 69:6bfdfc002036 | 964 | logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8)); // Playback |
TickTock | 86:d1c9e8ac1c4b | 965 | } else { |
TickTock | 109:3e6f0e8fca0d | 966 | f_close(&efile); // restart |
TickTock | 109:3e6f0e8fca0d | 967 | efr = f_open(&efile,"playback.alc",FA_READ|FA_OPEN_EXISTING); |
TickTock | 50:83d5864c64a0 | 968 | lastDMode[whichTouched]=99;//force refresh |
TickTock | 155:f31006516956 | 969 | beep(2000,0.25); |
TickTock | 36:dbd39c315258 | 970 | } |
TickTock | 36:dbd39c315258 | 971 | } |
TickTock | 36:dbd39c315258 | 972 | } |
TickTock | 36:dbd39c315258 | 973 | step=false; |
TickTock | 36:dbd39c315258 | 974 | } |
TickTock | 37:fea2c1d52c5f | 975 | |
TickTock | 140:ab3e94eb0227 | 976 | display=display<1?display+1:0; // toggle display |
TickTock | 140:ab3e94eb0227 | 977 | updateDisplay(display); |
TickTock | 2:71b1999a8ea5 | 978 | } //while (true) |
TickTock | 0:1596b8644523 | 979 | } |