Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed

Fork of CANary by Tick Tock

Committer:
TickTock
Date:
Sat Jun 15 18:47:38 2013 +0000
Branch:
Metric
Revision:
99:c05abf8e1cdc
Parent:
98:9f8bab96edff
Parent:
97:a25940fd7b5b
Child:
100:63a7456fc972
Merged and moved check for firmware.bin *before* deleting old *.bin.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TickTock 13:62e0f7f39ff5 1 // main.cpp
TickTock 13:62e0f7f39ff5 2
TickTock 4:8d7759f4fe7a 3 //To Do:
TickTock 33:a277743ebdeb 4 // * USB device detect
TickTock 33:a277743ebdeb 5 // * Ability to update binary from the thumb-drive (requires file timestamp)
TickTock 33:a277743ebdeb 6 // * Audible friction brake feedback
TickTock 33:a277743ebdeb 7 // * User-configurable watchpoint
TickTock 33:a277743ebdeb 8 // * Add 50% charge option
TickTock 33:a277743ebdeb 9 // * Tire Pressure Sensor display
TickTock 77:7c136766466c 10 // * Fix bug in playback while connected to canbus (hangs)
TickTock 97:a25940fd7b5b 11 // * Fix no power displayed while charging
TickTock 97:a25940fd7b5b 12 // * Force regen display to zero when in neutral
TickTock 13:62e0f7f39ff5 13
TickTock 12:8e42d7ba8468 14 #include "mbed.h"
TickTock 12:8e42d7ba8468 15 #include "CAN.h"
TickTock 12:8e42d7ba8468 16 #include "beep.h"
TickTock 12:8e42d7ba8468 17 #include "MSCFileSystem.h"
TickTock 12:8e42d7ba8468 18 #include "PowerControl.h"
TickTock 12:8e42d7ba8468 19 #include "EthernetPowerControl.h"
TickTock 12:8e42d7ba8468 20 #include "utility.h"
TickTock 12:8e42d7ba8468 21 #include "displayModes.h"
TickTock 23:cd03f9c3395e 22 #include "TOUCH_TFTx2.h"
TickTock 4:8d7759f4fe7a 23
TickTock 99:c05abf8e1cdc 24 char revStr[7] = "100";
TickTock 99:c05abf8e1cdc 25
TickTock 13:62e0f7f39ff5 26 LocalFileSystem local("local");
TickTock 13:62e0f7f39ff5 27
TickTock 13:62e0f7f39ff5 28 // to write to USB Flash Drives, or equivalent (SD card in Reader/Writer)
TickTock 35:5acbd8a64a89 29 MSCFileSystem fs("usb"); // to write to a USB Flash Drive
TickTock 13:62e0f7f39ff5 30
TickTock 13:62e0f7f39ff5 31 time_t seconds ;
TickTock 13:62e0f7f39ff5 32
TickTock 36:dbd39c315258 33 Ticker autoPoll;
TickTock 36:dbd39c315258 34 Ticker playback;
TickTock 78:a383971fe02f 35 Ticker msgReq;
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 35:5acbd8a64a89 51 Beep spkr(p21);
TickTock 13:62e0f7f39ff5 52
garygid 69:6bfdfc002036 53 // revision string, max 6 characters
garygid 71:7e87256d6c9a 54 // gg - revStr is used in 2 places
garygid 71:7e87256d6c9a 55 // gg - and is easy to edit here
garygid 71:7e87256d6c9a 56 // gg - added ZeroSecTick and revStr
garygid 69:6bfdfc002036 57
garygid 87:46ac3f2519d6 58 bool debugMode = false;
TickTock 48:d1ce92104a1f 59 bool logEn = false, logOpen = false;
TickTock 48:d1ce92104a1f 60 bool yesBattLog = false ; // gg - Batt Log
garygid 54:6ce808d0995e 61 unsigned char tNavRow = 3 ; // gg - 4x4 touch
garygid 54:6ce808d0995e 62
TickTock 97:a25940fd7b5b 63 FILE *hfile; // config file
TickTock 97:a25940fd7b5b 64 FILE *lfile; // log file
TickTock 13:62e0f7f39ff5 65 char fileName[35] = "" ;
TickTock 84:fd21e5d32dab 66 char writeBuffer[maxBufLen][13] __attribute__ ((section("AHBSRAM1"))); // buffer for USB write
TickTock 13:62e0f7f39ff5 67 char indexLastMsg[0x800]={0}; // index table for last message
TickTock 13:62e0f7f39ff5 68 CANMessage lastMsg[100]; // table to store last message of eachtype
garygid 87:46ac3f2519d6 69
garygid 87:46ac3f2519d6 70 //unsigned char battData[256]={0}; // 7 * 0x3D = BatDataBufMax
garygid 87:46ac3f2519d6 71 unsigned char battData[BatDataBufMax]={0}; // 7 * 0x3D = BatDataBufMax
garygid 87:46ac3f2519d6 72
TickTock 13:62e0f7f39ff5 73 unsigned char msgChanged[100]; // inidcates which bytes changed
TickTock 13:62e0f7f39ff5 74 char c;
TickTock 76:fb6779d0963e 75 volatile int writePointer = 0;
TickTock 76:fb6779d0963e 76 int readPointer=0;
TickTock 75:77bd26829dca 77 volatile unsigned short secsNoMsg = 0;
TickTock 75:77bd26829dca 78 volatile unsigned short secsNoTouch = 0;
TickTock 20:3bf176d14b14 79 volatile bool canIdle;
TickTock 20:3bf176d14b14 80 volatile bool userIdle;
TickTock 26:462ccb580472 81 bool touched=false; //flag to read touchscreen
TickTock 42:4533b13b297d 82 unsigned char whichTouched = 0;
TickTock 13:62e0f7f39ff5 83 char counter = 0;
TickTock 41:8d4609ea7259 84 unsigned char dMode[2] = {mainScreen,brakeScreen}; //display mode
TickTock 13:62e0f7f39ff5 85 unsigned char sMode = 0; // setup mode
TickTock 13:62e0f7f39ff5 86 unsigned char lastDMode[2] = {0,0}; //last screen mode
TickTock 25:ddf0ec209f03 87 unsigned char dtMode = 6;
TickTock 13:62e0f7f39ff5 88 char displayLog[20][40];
TickTock 13:62e0f7f39ff5 89 unsigned char displayLoc = 0;
leafman 98:9f8bab96edff 90 unsigned int fwCount=1;
TickTock 13:62e0f7f39ff5 91 unsigned char indexOffset = 1;
TickTock 13:62e0f7f39ff5 92 bool showCP = false;
TickTock 78:a383971fe02f 93 //bool pollCP = false;
TickTock 39:eef8beac7411 94 bool logCP = false; //Turbo3
TickTock 33:a277743ebdeb 95 bool repeatPoll = false;
TickTock 34:4751a8259b18 96 bool headlights = false;
TickTock 34:4751a8259b18 97 bool tick = false;
garygid 69:6bfdfc002036 98 bool ZeroSecTick = false;
TickTock 35:5acbd8a64a89 99 float ledHi = 0.8; // Bright LED value (until config file read)
TickTock 35:5acbd8a64a89 100 float ledLo = 0.1; // Dim LED value (until config file read)
TickTock 35:5acbd8a64a89 101 unsigned short pollInt = 300; // polling interval=5 minutes (until config file read)
TickTock 35:5acbd8a64a89 102 bool accOn = false; // Accessories on
TickTock 49:a3d2c5bb3cfa 103 bool laccOn = false;
TickTock 35:5acbd8a64a89 104 float scale12V = 16.2; // R1:R2 ratio
TickTock 40:0e6e71a7323f 105 signed long mWs_x4 = 0;
TickTock 40:0e6e71a7323f 106 unsigned short numWsamples = 0;
TickTock 41:8d4609ea7259 107 unsigned short numSsamples = 0;
TickTock 35:5acbd8a64a89 108 float accV = 0;
TickTock 36:dbd39c315258 109 bool playbackEn = false;
TickTock 36:dbd39c315258 110 bool playbackOpen = false;
TickTock 49:a3d2c5bb3cfa 111 //float playbackInt = 0.05; //read messages every 50 ms
TickTock 78:a383971fe02f 112 float playbackInt = 0.005; //read messages every 5 ms
TickTock 36:dbd39c315258 113 bool step = false;
TickTock 37:fea2c1d52c5f 114 char header[5];
TickTock 36:dbd39c315258 115 char data[8];
TickTock 41:8d4609ea7259 116 signed long motorRPM;
garygid 65:821fc79cd7fe 117 unsigned char skin = ttSkin ;
TickTock 50:83d5864c64a0 118 unsigned char dtePeriod = 14; //ten minute averaging interval
TickTock 49:a3d2c5bb3cfa 119 float mph[39]={0};
TickTock 49:a3d2c5bb3cfa 120 float kW[39]={0};
TickTock 49:a3d2c5bb3cfa 121 float mpkWh[39]={0};
TickTock 49:a3d2c5bb3cfa 122 // Logarithmic division scale (roughly - snapped to common units of time)
TickTock 49:a3d2c5bb3cfa 123 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 124 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 125 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 126 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 127 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 49:a3d2c5bb3cfa 128 bool updateDTE = false;
TickTock 73:62ee8eae3a84 129 unsigned short pointerSep;
TickTock 78:a383971fe02f 130 unsigned char reqMsgCnt = 99;
TickTock 96:a6c6a6fd1d28 131 unsigned char battTemp_x4 = 0;
leafman 98:9f8bab96edff 132 bool metric = false;
leafman 98:9f8bab96edff 133
TickTock 0:1596b8644523 134 int main() {
TickTock 4:8d7759f4fe7a 135 char sTemp[40];
TickTock 2:71b1999a8ea5 136 unsigned long secs;
TickTock 49:a3d2c5bb3cfa 137 unsigned char i,j,display=0,lwt=0;
TickTock 22:a43df3905863 138 point lastTouch;
TickTock 52:d5385fbf4ea1 139 float average;
TickTock 7:17bf9ceaf0aa 140
TickTock 3:3e879b043bc5 141 tt.set_orientation(1);
TickTock 4:8d7759f4fe7a 142 tt.background(Black);
TickTock 19:d576298c46f3 143 tt.set_display(2); // select both displays
TickTock 3:3e879b043bc5 144 tt.cls();
TickTock 1:9dcd70c32180 145 tt.claim(stdout); // send stdout to the TFT display
TickTock 12:8e42d7ba8468 146 touchpad.rise(&touch_ISR);
TickTock 4:8d7759f4fe7a 147 tt.wfi(); // enable interrupt on touch
TickTock 7:17bf9ceaf0aa 148 dled = 0.8; // turn on display LED 80%
TickTock 26:462ccb580472 149
TickTock 2:71b1999a8ea5 150 timer.start() ;
TickTock 2:71b1999a8ea5 151 RTC_Init(); // start the RTC Interrupts that sync the timer
TickTock 1:9dcd70c32180 152 struct tm t; // pointer to a static tm structure
TickTock 93:c2402e8cd0e2 153 NVIC_SetPriority(CAN_IRQn, 2); //set can priority just below RTC
TickTock 93:c2402e8cd0e2 154 NVIC_SetPriority(TIMER3_IRQn, 3); //set ticker priority just below can
TickTock 33:a277743ebdeb 155
TickTock 1:9dcd70c32180 156 seconds = time(NULL);
TickTock 1:9dcd70c32180 157 t = *localtime(&seconds) ;
TickTock 1:9dcd70c32180 158 // is it a date before 2012 ?
TickTock 26:462ccb580472 159 if ((t.tm_year + 1900) < 2012 ) {
TickTock 26:462ccb580472 160 // before 2013 so update year to make date entry easier
TickTock 26:462ccb580472 161 t.tm_year = 2013 - 1900;
TickTock 1:9dcd70c32180 162 // set the RTC
TickTock 1:9dcd70c32180 163 set_time(mktime(&t));
TickTock 1:9dcd70c32180 164 seconds = time(NULL);
TickTock 26:462ccb580472 165 }
TickTock 20:3bf176d14b14 166 t = *localtime(&seconds) ;
TickTock 20:3bf176d14b14 167 strftime(sTemp, 32, "%a %m/%d/%Y %X\n", &t);
TickTock 93:c2402e8cd0e2 168 printMsg(sTemp); // record RTC
garygid 54:6ce808d0995e 169
TickTock 62:ffd15edb5431 170 // revision
garygid 70:0700b769ac15 171 sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
TickTock 93:c2402e8cd0e2 172 printMsg(sTemp); // revision
TickTock 15:a359fecf85ba 173
TickTock 26:462ccb580472 174 // Look for new binary on thumbdrive
TickTock 17:e32324a2678d 175 // Can't make this work right now since USB doesn't attach the right timestamp (so new binary isn't loaded)
TickTock 35:5acbd8a64a89 176 /*cfile = fopen("/usb/CANary.bin", "rb");
TickTock 50:83d5864c64a0 177 lastDMode[whichTouched]=99;//force refresh
TickTock 17:e32324a2678d 178 if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over
TickTock 17:e32324a2678d 179 sprintf(sTemp,"New binary found.\n");
TickTock 93:c2402e8cd0e2 180 printMsg(sTemp); // new binary
TickTock 97:a25940fd7b5b 181 lfile = fopen("/local/CANary.bin", "wb");
TickTock 97:a25940fd7b5b 182 if (lfile==NULL){ //failed to open destination
TickTock 17:e32324a2678d 183 sprintf(sTemp,"Unable to open destination file.\n");
TickTock 93:c2402e8cd0e2 184 printMsg(sTemp); // cannot open CANary.bin
TickTock 17:e32324a2678d 185 } else {
TickTock 18:999401f359a5 186 tt.set_display(2);
TickTock 18:999401f359a5 187 tt.foreground(White);
TickTock 18:999401f359a5 188 tt.background(Black);
TickTock 18:999401f359a5 189 tt.cls();
TickTock 18:999401f359a5 190 tt.locate(1,40);
TickTock 18:999401f359a5 191 printf("%s\n","Copying binary - Do no remove power.");
TickTock 18:999401f359a5 192 tt.locate(1,80);
TickTock 18:999401f359a5 193 printf("CANary will reset when complete.\n");
TickTock 18:999401f359a5 194 wait(1); //Wait 1 sec for display DMA to finish before writing file
TickTock 17:e32324a2678d 195 while ( int size = fread( writeBuffer, sizeof(char), maxBufLen*13, cfile )){
TickTock 97:a25940fd7b5b 196 fwrite( writeBuffer, sizeof(char), size, lfile );
TickTock 18:999401f359a5 197 led4=led3;
TickTock 18:999401f359a5 198 led3=led2;
TickTock 18:999401f359a5 199 led2=led1;
TickTock 18:999401f359a5 200 led1=!led4;
TickTock 17:e32324a2678d 201 }
TickTock 17:e32324a2678d 202 fclose(cfile);
TickTock 97:a25940fd7b5b 203 fclose(lfile);
TickTock 35:5acbd8a64a89 204 remove("/usb/CANary.bin"); // delete original
TickTock 17:e32324a2678d 205 mbed_reset(); //restart
TickTock 17:e32324a2678d 206 }
TickTock 18:999401f359a5 207 }*/
TickTock 17:e32324a2678d 208
TickTock 26:462ccb580472 209 secsNoMsg = 0;
TickTock 43:e7f6f80590e3 210
TickTock 49:a3d2c5bb3cfa 211 //read efficiency history data
TickTock 97:a25940fd7b5b 212 hfile = fopen("/local/ehist.cny", "r");
TickTock 97:a25940fd7b5b 213 if (hfile!=NULL){ // found a efficiency history file
TickTock 92:935adef49ea4 214 for(i=0;i<39;i++){
TickTock 97:a25940fd7b5b 215 if(!feof(hfile)){
TickTock 97:a25940fd7b5b 216 fscanf(hfile,"%f %f\r\n",&mph[i],&kW[i]);
TickTock 49:a3d2c5bb3cfa 217 mpkWh[i]=mph[i]/kW[i];
TickTock 49:a3d2c5bb3cfa 218 }
TickTock 49:a3d2c5bb3cfa 219 }
TickTock 97:a25940fd7b5b 220 fclose(hfile);
TickTock 49:a3d2c5bb3cfa 221 sprintf(sTemp,"History Loaded.\n");
TickTock 93:c2402e8cd0e2 222 printMsg(sTemp); // History loaded
TickTock 49:a3d2c5bb3cfa 223 } else { // create initial file
TickTock 49:a3d2c5bb3cfa 224 sprintf(sTemp,"History not found. Created.\n");
TickTock 93:c2402e8cd0e2 225 printMsg(sTemp); // history not found, created
TickTock 92:935adef49ea4 226 for(i=0;i<39;i++){
TickTock 49:a3d2c5bb3cfa 227 // Pre-load with 4 mpkWh @ 40 mph
TickTock 49:a3d2c5bb3cfa 228 mph[i]=40*timeConstant[i];
TickTock 49:a3d2c5bb3cfa 229 kW[i]=10*timeConstant[i];
TickTock 49:a3d2c5bb3cfa 230 mpkWh[i]=4;
TickTock 49:a3d2c5bb3cfa 231 }
TickTock 49:a3d2c5bb3cfa 232 }
TickTock 49:a3d2c5bb3cfa 233
TickTock 46:73e8716f976f 234 // Read config file
TickTock 46:73e8716f976f 235 readConfig();
TickTock 43:e7f6f80590e3 236
TickTock 77:7c136766466c 237 // Start monitors
TickTock 77:7c136766466c 238 can1.monitor(true); // set to snoop mode
TickTock 77:7c136766466c 239 can2.monitor(true); // set to snoop mode
TickTock 77:7c136766466c 240 can1.frequency(500000);
TickTock 77:7c136766466c 241 can2.frequency(500000);
TickTock 77:7c136766466c 242 can1SleepMode = 1; // Turn on Monitor_only Mode
TickTock 77:7c136766466c 243 can2SleepMode = 1; // Turn on Monitor_only Mode
TickTock 77:7c136766466c 244 can1.attach(&recieve1);
TickTock 77:7c136766466c 245 can2.attach(&recieve2);
TickTock 77:7c136766466c 246
TickTock 26:462ccb580472 247 touched=false;
TickTock 26:462ccb580472 248 secsNoTouch=2;
TickTock 2:71b1999a8ea5 249 while (true) {
TickTock 4:8d7759f4fe7a 250 if (!logOpen) { // Open new file if one is not already open
TickTock 7:17bf9ceaf0aa 251 if(logEn){ //logging enable
TickTock 7:17bf9ceaf0aa 252 seconds = time(NULL);
TickTock 7:17bf9ceaf0aa 253 t = *localtime(&seconds) ;
TickTock 35:5acbd8a64a89 254 strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
TickTock 16:2a6ca248e1cb 255 //sprintf(sTemp,"Using file %s\n",fileName);
TickTock 93:c2402e8cd0e2 256 //printMsg(sTemp); // using alc file ...
TickTock 97:a25940fd7b5b 257 lfile = fopen(fileName, "ab");
TickTock 94:c3a14b3975d6 258 lastDMode[0]=99;//force refresh
TickTock 94:c3a14b3975d6 259 lastDMode[1]=99;//force refresh
TickTock 97:a25940fd7b5b 260 if(lfile==NULL){
TickTock 7:17bf9ceaf0aa 261 sprintf(sTemp,"\nUnable to open %s\n\n\n\n",fileName);
TickTock 93:c2402e8cd0e2 262 printMsg(sTemp); // cannot open alc file
TickTock 7:17bf9ceaf0aa 263 logEn=false;
TickTock 26:462ccb580472 264 spkr.beep(1000,0.25);
TickTock 7:17bf9ceaf0aa 265 } else {
TickTock 7:17bf9ceaf0aa 266 logOpen = true;
TickTock 7:17bf9ceaf0aa 267 readPointer=writePointer;
TickTock 7:17bf9ceaf0aa 268 sprintf(sTemp,"Starting Can Log %s\n",fileName);
TickTock 93:c2402e8cd0e2 269 printMsg(sTemp); // starting alc log file
garygid 69:6bfdfc002036 270
garygid 69:6bfdfc002036 271 logTS(); // Date Time at start
TickTock 93:c2402e8cd0e2 272 logEvent("Starting"); // Log startup msg for testing
garygid 69:6bfdfc002036 273 sprintf(sTemp,"Cr%s",revStr);
TickTock 93:c2402e8cd0e2 274 logEvent(sTemp); // gg - log firmware version
garygid 69:6bfdfc002036 275
TickTock 26:462ccb580472 276 spkr.beep(2000,0.25);
TickTock 7:17bf9ceaf0aa 277 }
TickTock 7:17bf9ceaf0aa 278 }//logging enabled
TickTock 15:a359fecf85ba 279 } else { // if (logOpen)
TickTock 73:62ee8eae3a84 280 pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
TickTock 73:62ee8eae3a84 281 if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
TickTock 7:17bf9ceaf0aa 282 // Dump buffer if > 1/16 full or canbus has stopped
TickTock 97:a25940fd7b5b 283 if (lfile == NULL) {
TickTock 7:17bf9ceaf0aa 284 logOpen = false;
TickTock 37:fea2c1d52c5f 285 sprintf(sTemp,"Failed to append log file.\n");
TickTock 93:c2402e8cd0e2 286 printMsg(sTemp); // failed to append
TickTock 26:462ccb580472 287 spkr.beep(1000,0.25);
TickTock 7:17bf9ceaf0aa 288 logEn=false;
TickTock 7:17bf9ceaf0aa 289 } else {
TickTock 7:17bf9ceaf0aa 290 while (readPointer != writePointer) {
TickTock 92:935adef49ea4 291 for (j = 0; j<13; j++){
TickTock 97:a25940fd7b5b 292 fprintf(lfile,"%c",writeBuffer[readPointer][j]);
TickTock 4:8d7759f4fe7a 293 }
TickTock 86:d1c9e8ac1c4b 294 if(++readPointer >= maxBufLen){
TickTock 7:17bf9ceaf0aa 295 readPointer=0;
TickTock 86:d1c9e8ac1c4b 296 led4 = !led4;
TickTock 86:d1c9e8ac1c4b 297 }
TickTock 4:8d7759f4fe7a 298 }
TickTock 7:17bf9ceaf0aa 299 }
TickTock 30:e633a63eb257 300 } // if > 1/16 full, canbus has stopped, or logging stopped
TickTock 30:e633a63eb257 301 if (!logEn) {
TickTock 77:7c136766466c 302 sprintf(sTemp,"Stopping Can Log %s\n",fileName);
TickTock 93:c2402e8cd0e2 303 printMsg(sTemp); // stopping alc log file
TickTock 97:a25940fd7b5b 304 fclose(lfile);
TickTock 30:e633a63eb257 305 logOpen=false;
TickTock 77:7c136766466c 306 pointerSep=0;
TickTock 30:e633a63eb257 307 }
TickTock 4:8d7759f4fe7a 308 } // if logOpen
TickTock 36:dbd39c315258 309 if (canIdle&&userIdle&&!playbackEn) { // canbus idle --> sleep to save power
TickTock 95:248b3c25a7df 310 if (repeatPoll) { // stop autopolling if enabled
TickTock 95:248b3c25a7df 311 autoPoll.detach();
TickTock 95:248b3c25a7df 312 }
TickTock 26:462ccb580472 313 if (logOpen){
TickTock 97:a25940fd7b5b 314 fclose(lfile);
TickTock 7:17bf9ceaf0aa 315 } // if (logOpen)*/
TickTock 20:3bf176d14b14 316 seconds = time(NULL);
TickTock 20:3bf176d14b14 317 t = *localtime(&seconds) ;
TickTock 21:22bdce9efcb5 318 strftime(sTemp, 40, "Sleeping: %a %m/%d/%Y %X\n", &t);
TickTock 93:c2402e8cd0e2 319 printMsg(sTemp); // sleeping date time
TickTock 25:ddf0ec209f03 320 updateDisplay(0); //Added for turbo3 who has a display override and wants to see the sleep message before going to sleep
TickTock 25:ddf0ec209f03 321 updateDisplay(1);
TickTock 4:8d7759f4fe7a 322 //LPC_RTC->CIIR=0x00; // block RTC interrupts
TickTock 4:8d7759f4fe7a 323 led1=0;
TickTock 4:8d7759f4fe7a 324 led2=0;
TickTock 4:8d7759f4fe7a 325 led3=0;
TickTock 4:8d7759f4fe7a 326 led4=0;
TickTock 4:8d7759f4fe7a 327 dled=0; // turn off display
TickTock 4:8d7759f4fe7a 328 secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900
TickTock 22:a43df3905863 329 while (secsNoMsg>canTimeout && !touched) {
TickTock 4:8d7759f4fe7a 330 //DeepPowerDown();
TickTock 12:8e42d7ba8468 331 tt.wfi(); //enable touch interrupt
TickTock 21:22bdce9efcb5 332 //__wfi(); // freeze CPU and wait for interrupt (from canbus or touch)
TickTock 21:22bdce9efcb5 333 Sleep();
TickTock 4:8d7759f4fe7a 334 }
TickTock 97:a25940fd7b5b 335 lastDMode[0]=99;
TickTock 97:a25940fd7b5b 336 lastDMode[1]=99;
TickTock 43:e7f6f80590e3 337 secsNoTouch=2;
TickTock 4:8d7759f4fe7a 338 canIdle=secsNoMsg>canTimeout;
TickTock 7:17bf9ceaf0aa 339 dled=0.8; // turn on display LED
TickTock 20:3bf176d14b14 340 seconds = time(NULL);
TickTock 20:3bf176d14b14 341 t = *localtime(&seconds) ;
TickTock 21:22bdce9efcb5 342 strftime(sTemp, 40, "Waking: %a %m/%d/%Y %X\n", &t);
TickTock 93:c2402e8cd0e2 343 printMsg(sTemp); // wakeup date time
TickTock 4:8d7759f4fe7a 344 if (time(NULL)>(secs+1800)) {
TickTock 4:8d7759f4fe7a 345 logOpen = false; // Start new file if asleep for more than 30 minutes
TickTock 4:8d7759f4fe7a 346 if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter
TickTock 43:e7f6f80590e3 347 } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now)
TickTock 97:a25940fd7b5b 348 lfile = fopen(fileName, "ab");
TickTock 93:c2402e8cd0e2 349 logEvent("WakingUp"); // gg - use messeges
garygid 69:6bfdfc002036 350 logTS(); // Date-Time at wakeup
TickTock 4:8d7759f4fe7a 351 }
TickTock 95:248b3c25a7df 352 if (repeatPoll) { // re-enable autopolling if enabled
TickTock 95:248b3c25a7df 353 autoPoll.attach(&autoPollISR,pollInt);
TickTock 95:248b3c25a7df 354 }
TickTock 4:8d7759f4fe7a 355 } // if idle
TickTock 4:8d7759f4fe7a 356
TickTock 15:a359fecf85ba 357 if(touched){ // call touchscreen procedure if touch interrupt detected
TickTock 7:17bf9ceaf0aa 358 lastTouch = tt.get_touch();
TickTock 7:17bf9ceaf0aa 359 lastTouch = tt.to_pixel(lastTouch); // convert to pixel pos
TickTock 40:0e6e71a7323f 360 if((lastTouch.x!=639)&&(lastTouch.x!=319)&&(lastTouch.y!=239)){ // filter phantom touches
TickTock 43:e7f6f80590e3 361 if (userIdle) {
TickTock 43:e7f6f80590e3 362 secsNoTouch=2; // Ignore first touch if user idle
TickTock 43:e7f6f80590e3 363 userIdle=false;
TickTock 40:0e6e71a7323f 364 } else {
TickTock 43:e7f6f80590e3 365 secsNoTouch=0;
TickTock 40:0e6e71a7323f 366 }
TickTock 43:e7f6f80590e3 367 if (lastTouch.x>320){
TickTock 43:e7f6f80590e3 368 whichTouched=1;
TickTock 43:e7f6f80590e3 369 } else {
TickTock 43:e7f6f80590e3 370 whichTouched=0;
TickTock 43:e7f6f80590e3 371 }
TickTock 43:e7f6f80590e3 372 if (whichTouched!=lwt){
TickTock 43:e7f6f80590e3 373 lastDMode[lwt]=99; // Repaint lastTouched
TickTock 43:e7f6f80590e3 374 lwt=whichTouched;
TickTock 43:e7f6f80590e3 375 }
TickTock 43:e7f6f80590e3 376 sMode = 1;
TickTock 22:a43df3905863 377 }
TickTock 22:a43df3905863 378 //sprintf(sTemp,"%d,%d ",lastTouch.x,lastTouch.y);
TickTock 93:c2402e8cd0e2 379 //printMsg(sTemp); // touch x,y - for debug
TickTock 12:8e42d7ba8468 380 touched = false; // clear interrupt flag
TickTock 7:17bf9ceaf0aa 381 }
garygid 54:6ce808d0995e 382 //---------------
garygid 54:6ce808d0995e 383 // gg - 4x4 touch
TickTock 62:ffd15edb5431 384 //unsigned char tScrn = 0 ; // screen 0
garygid 54:6ce808d0995e 385 unsigned char tCol ;
garygid 54:6ce808d0995e 386 unsigned char tRow ;
garygid 54:6ce808d0995e 387
TickTock 4:8d7759f4fe7a 388 if (!userIdle) {
TickTock 4:8d7759f4fe7a 389 if (secsNoTouch<2) {// Recently touched
TickTock 4:8d7759f4fe7a 390 secsNoTouch +=2; // increment to prevent double touch
TickTock 43:e7f6f80590e3 391 sMode = 1;
TickTock 22:a43df3905863 392 //sprintf(sTemp,"button %d %d,%d %d\n",i,buttonX(lastTouch.x,3),buttonY(lastTouch.y,3),lastTouch.x);
TickTock 93:c2402e8cd0e2 393 //printMsg(sTemp); // button parms - for debug
TickTock 23:cd03f9c3395e 394 switch (sMode) {
TickTock 23:cd03f9c3395e 395 case 0: // no select
TickTock 23:cd03f9c3395e 396 break;
TickTock 63:aa3bf6a33635 397 case 1: // select screen
garygid 54:6ce808d0995e 398 //--------------
garygid 54:6ce808d0995e 399 // gg - 4x4 touch
garygid 54:6ce808d0995e 400 tCol = buttonX(lastTouch.x,4) ;
TickTock 62:ffd15edb5431 401 if( tCol >= 4 ){ tCol -= 4; } // touch is on screen 1
garygid 54:6ce808d0995e 402
garygid 54:6ce808d0995e 403 tRow = buttonY(lastTouch.y,4) ;
garygid 54:6ce808d0995e 404
TickTock 63:aa3bf6a33635 405 highlightButton( tCol,tRow, whichTouched, 4,4) ; // gg - highlight
garygid 58:4d06288d75a2 406
TickTock 63:aa3bf6a33635 407 if( tRow == tNavRow ) tRow = 7 ; // gg
garygid 54:6ce808d0995e 408 switch ( (tCol*10) + tRow ) {
garygid 54:6ce808d0995e 409 //---------------------------------
garygid 67:2022fce701d0 410 case 00: // top row, left button on screen 0 or 1
TickTock 42:4533b13b297d 411 if (dMode[whichTouched]==monitorScreen||dMode[whichTouched]==changedScreen) {
garygid 67:2022fce701d0 412 indexOffset=indexOffset>4?indexOffset-4:1;
garygid 67:2022fce701d0 413 } else if (dMode[whichTouched] == indexScreen) { // gg - index
garygid 67:2022fce701d0 414 dMode[whichTouched] = mainScreen ; // GoTo Main Screen
TickTock 73:62ee8eae3a84 415 sMode=0;
leafman 98:9f8bab96edff 416 } else if (dMode[whichTouched]==configScreen) {
TickTock 23:cd03f9c3395e 417 wait_ms(500);
TickTock 37:fea2c1d52c5f 418 tt.background(Black);
TickTock 23:cd03f9c3395e 419 tt.calibrate();
TickTock 42:4533b13b297d 420 } else if (dMode[whichTouched]==playbackScreen) { // slower
TickTock 36:dbd39c315258 421 playbackInt *=2;
TickTock 36:dbd39c315258 422 if(playbackEn){
TickTock 36:dbd39c315258 423 playback.detach();
TickTock 36:dbd39c315258 424 playback.attach(&playbackISR,playbackInt);
TickTock 36:dbd39c315258 425 }
TickTock 62:ffd15edb5431 426 } else {
TickTock 62:ffd15edb5431 427 lastDMode[whichTouched]=99;//repaint to clear highlight
TickTock 23:cd03f9c3395e 428 }
TickTock 23:cd03f9c3395e 429 break;
garygid 54:6ce808d0995e 430 //-----------------------------------------------
garygid 67:2022fce701d0 431 case 10: // 1,0 (col left of center,top row) on screen 0 or 1
TickTock 42:4533b13b297d 432 if (dMode[whichTouched]==changedScreen) {
TickTock 92:935adef49ea4 433 for(j=0;j<100;j++) msgChanged[j]=0; // clear changed data
TickTock 42:4533b13b297d 434 lastDMode[whichTouched]=99;//force refresh
garygid 67:2022fce701d0 435 } else if (dMode[whichTouched] == indexScreen) { // gg - index
TickTock 73:62ee8eae3a84 436 sMode=0;
garygid 67:2022fce701d0 437 dMode[whichTouched] = brakeScreen ; // GoTo Brake Screen
TickTock 42:4533b13b297d 438 } else if (dMode[whichTouched]==cpScreen) {
TickTock 78:a383971fe02f 439 reqMsgCnt=0;
TickTock 94:c3a14b3975d6 440 msgReq.attach(&sendReq,0.015);
TickTock 48:d1ce92104a1f 441 } else if (dMode[whichTouched]==cpHistScreen) { // gg - hist
TickTock 78:a383971fe02f 442 reqMsgCnt=0;
TickTock 94:c3a14b3975d6 443 msgReq.attach(&sendReq,0.015);
TickTock 48:d1ce92104a1f 444 } else if (dMode[whichTouched]==cpBarScreen) { // gg - cpbars
TickTock 78:a383971fe02f 445 reqMsgCnt=0;
TickTock 94:c3a14b3975d6 446 msgReq.attach(&sendReq,0.015);
leafman 98:9f8bab96edff 447 } else if (dMode[whichTouched]==configScreen) {
TickTock 23:cd03f9c3395e 448 mbed_reset();
TickTock 42:4533b13b297d 449 } else if (dMode[whichTouched]==playbackScreen) { // pause/unpause
TickTock 36:dbd39c315258 450 playbackEn=!playbackEn;
TickTock 36:dbd39c315258 451 if(playbackEn){
TickTock 36:dbd39c315258 452 playback.attach(&playbackISR,playbackInt);
TickTock 62:ffd15edb5431 453 } else {
TickTock 36:dbd39c315258 454 playback.detach();
TickTock 36:dbd39c315258 455 }
TickTock 62:ffd15edb5431 456 } else {
TickTock 62:ffd15edb5431 457 lastDMode[whichTouched]=99;//repaint to clear highlight
TickTock 23:cd03f9c3395e 458 }
TickTock 62:ffd15edb5431 459
TickTock 23:cd03f9c3395e 460 break;
garygid 54:6ce808d0995e 461 //--------------------------------------
garygid 54:6ce808d0995e 462 case 20: // col 2 and row 0 on either screen 0 or 1
TickTock 42:4533b13b297d 463 if (dMode[whichTouched]==monitorScreen||dMode[whichTouched]==changedScreen) {
TickTock 23:cd03f9c3395e 464 indexOffset=indexOffset<77?indexOffset+4:80;
garygid 67:2022fce701d0 465 } else if (dMode[whichTouched] == indexScreen) { // gg - index
garygid 67:2022fce701d0 466 dMode[whichTouched] = effScreen ; // GoTo EFF Screen
TickTock 73:62ee8eae3a84 467 sMode=0;
leafman 98:9f8bab96edff 468 } else if (dMode[whichTouched]==configScreen) {
TickTock 23:cd03f9c3395e 469 sprintf(sTemp,"Saving config file.\n");
TickTock 93:c2402e8cd0e2 470 printMsg(sTemp); // saving config
TickTock 25:ddf0ec209f03 471 saveConfig();
TickTock 26:462ccb580472 472 spkr.beep(2000,0.25);
TickTock 42:4533b13b297d 473 } else if (dMode[whichTouched]==playbackScreen) { // faster
TickTock 36:dbd39c315258 474 if(playbackInt>.002){
TickTock 36:dbd39c315258 475 playbackInt/=2;
TickTock 36:dbd39c315258 476 if(playbackEn){
TickTock 36:dbd39c315258 477 playback.detach();
TickTock 36:dbd39c315258 478 playback.attach(&playbackISR,playbackInt);
TickTock 36:dbd39c315258 479 }
TickTock 36:dbd39c315258 480 }
TickTock 86:d1c9e8ac1c4b 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 65:821fc79cd7fe 486
garygid 67:2022fce701d0 487 case 30: // right-most on top row
garygid 65:821fc79cd7fe 488
leafman 98:9f8bab96edff 489 if (dMode[whichTouched]==configScreen) {
garygid 65:821fc79cd7fe 490 // step through skins
garygid 65:821fc79cd7fe 491 if( skin < maxSkin ) skin += 1 ;
garygid 65:821fc79cd7fe 492 else skin = 0 ;
garygid 65:821fc79cd7fe 493
garygid 65:821fc79cd7fe 494 // repaint both screens, I think
garygid 65:821fc79cd7fe 495 lastDMode[whichTouched]=99;//repaint to clear highlight
garygid 65:821fc79cd7fe 496 // and re-paint the other screen too, to see new skin there
garygid 65:821fc79cd7fe 497 lastDMode[whichTouched ^ 1]=99; // repaint other screen (^ = XOR)
garygid 65:821fc79cd7fe 498
garygid 65:821fc79cd7fe 499 //sprintf(sTemp,"Changed Skin to %d.\n");
TickTock 93:c2402e8cd0e2 500 //printMsg(sTemp); /// changed skin - for debug
garygid 65:821fc79cd7fe 501 //saveConfig();
garygid 65:821fc79cd7fe 502 //spkr.beep(2000,0.25);
garygid 69:6bfdfc002036 503
garygid 67:2022fce701d0 504 } else if (dMode[whichTouched] == indexScreen) { // gg - index
garygid 67:2022fce701d0 505 dMode[whichTouched] = dteScreen ; // GoTo DTE Screen
TickTock 73:62ee8eae3a84 506 sMode=0;
TickTock 86:d1c9e8ac1c4b 507 } else {
garygid 65:821fc79cd7fe 508 lastDMode[whichTouched]=99;//repaint to clear highlight
garygid 65:821fc79cd7fe 509 }
garygid 65:821fc79cd7fe 510
garygid 65:821fc79cd7fe 511 break;
garygid 54:6ce808d0995e 512 //----------------------------------
garygid 54:6ce808d0995e 513 //----------------------------------
garygid 67:2022fce701d0 514 case 01: // left col middle row
leafman 98:9f8bab96edff 515 if (dMode[whichTouched]==configScreen) {
TickTock 23:cd03f9c3395e 516 logEn = !logEn;
TickTock 78:a383971fe02f 517 if (!logEn) {
TickTock 78:a383971fe02f 518 repeatPoll=false; // disable auto polling, too
TickTock 78:a383971fe02f 519 autoPoll.detach();
TickTock 78:a383971fe02f 520 }
garygid 67:2022fce701d0 521 } else if (dMode[whichTouched] == indexScreen) { // gg - index
TickTock 73:62ee8eae3a84 522 dMode[whichTouched] = cpScreen ; // GoTo CP Data Screen
TickTock 73:62ee8eae3a84 523 sMode=0;
TickTock 42:4533b13b297d 524 } else if (dMode[whichTouched]==dateScreen){
TickTock 25:ddf0ec209f03 525 dtMode=(dtMode<6)?dtMode+1:0;
TickTock 42:4533b13b297d 526 lastDMode[whichTouched]=99;
TickTock 62:ffd15edb5431 527 } else {
TickTock 62:ffd15edb5431 528 lastDMode[whichTouched]=99;//repaint to clear highlight
TickTock 23:cd03f9c3395e 529 }
TickTock 62:ffd15edb5431 530
TickTock 23:cd03f9c3395e 531 break;
garygid 54:6ce808d0995e 532 //------------------------------
TickTock 23:cd03f9c3395e 533 case 11:
leafman 98:9f8bab96edff 534 if (dMode[whichTouched]==configScreen){
TickTock 77:7c136766466c 535 repeatPoll = !repeatPoll;
TickTock 33:a277743ebdeb 536 if (repeatPoll) {
TickTock 36:dbd39c315258 537 autoPoll.attach(&autoPollISR,pollInt);
TickTock 33:a277743ebdeb 538 } else {
TickTock 36:dbd39c315258 539 autoPoll.detach();
TickTock 33:a277743ebdeb 540 }
garygid 67:2022fce701d0 541 } else if (dMode[whichTouched] == indexScreen) { // gg - index
garygid 67:2022fce701d0 542 dMode[whichTouched] = cpHistScreen ; // GoTo CP Hist Screen
TickTock 73:62ee8eae3a84 543 sMode=0;
TickTock 42:4533b13b297d 544 } else if (dMode[whichTouched]==playbackScreen) {
TickTock 36:dbd39c315258 545 // Start/stop playback
TickTock 36:dbd39c315258 546 if(!playbackOpen){
TickTock 36:dbd39c315258 547 if(!logOpen){
TickTock 97:a25940fd7b5b 548 lfile = fopen("/usb/playback.alc", "rb");
TickTock 50:83d5864c64a0 549 lastDMode[whichTouched]=99;//force refresh
TickTock 97:a25940fd7b5b 550 if(lfile==NULL){
TickTock 36:dbd39c315258 551 sprintf(sTemp,"Unable to open /usb/playback.alc\n");
TickTock 93:c2402e8cd0e2 552 printMsg(sTemp); // no playback.alc
TickTock 36:dbd39c315258 553 spkr.beep(1000,0.25);
TickTock 36:dbd39c315258 554 } else {
TickTock 36:dbd39c315258 555 playbackOpen = true;
TickTock 50:83d5864c64a0 556 playbackEn=true;
TickTock 50:83d5864c64a0 557 playback.attach(&playbackISR,playbackInt);
TickTock 36:dbd39c315258 558 sprintf(sTemp,"Starting playback\n");
TickTock 93:c2402e8cd0e2 559 printMsg(sTemp); // start playback
TickTock 36:dbd39c315258 560 spkr.beep(2000,0.25);
TickTock 36:dbd39c315258 561 can1.attach(&doNothing);// Stop recieving CAN data
TickTock 36:dbd39c315258 562 can2.attach(&doNothing);
TickTock 36:dbd39c315258 563 }
TickTock 36:dbd39c315258 564 } else {
TickTock 36:dbd39c315258 565 sprintf(sTemp,"Must stop logging first\n");
TickTock 93:c2402e8cd0e2 566 printMsg(sTemp); // Must stop logging first
TickTock 36:dbd39c315258 567 }
TickTock 36:dbd39c315258 568 } else {
TickTock 36:dbd39c315258 569 playback.detach();
TickTock 97:a25940fd7b5b 570 fclose(lfile);
TickTock 36:dbd39c315258 571 playbackOpen=false;
TickTock 50:83d5864c64a0 572 playbackEn=false;
TickTock 36:dbd39c315258 573 can1.attach(&recieve1);// Restore CAN data recieve
TickTock 36:dbd39c315258 574 can2.attach(&recieve2);
TickTock 42:4533b13b297d 575 lastDMode[whichTouched]=99;
TickTock 36:dbd39c315258 576 }
TickTock 42:4533b13b297d 577 } else if (dMode[whichTouched]==dateScreen){
TickTock 25:ddf0ec209f03 578 upDate(dtMode,true);
TickTock 42:4533b13b297d 579 lastDMode[whichTouched]=99;
TickTock 62:ffd15edb5431 580 } else {
TickTock 62:ffd15edb5431 581 lastDMode[whichTouched]=99;//repaint to clear highlight
TickTock 25:ddf0ec209f03 582 }
TickTock 62:ffd15edb5431 583
TickTock 23:cd03f9c3395e 584 break;
garygid 54:6ce808d0995e 585 //---------------------------------
garygid 54:6ce808d0995e 586 case 21: // col 2 row 1
leafman 98:9f8bab96edff 587 if (dMode[whichTouched]==configScreen) { // gg - Batt Log Enable Button
TickTock 48:d1ce92104a1f 588 yesBattLog = !yesBattLog;
garygid 67:2022fce701d0 589 } else if (dMode[whichTouched] == indexScreen) { // gg - index
garygid 67:2022fce701d0 590 dMode[whichTouched] = cpBarScreen ; // GoTo CP Bars Screen
TickTock 73:62ee8eae3a84 591 sMode=0;
TickTock 48:d1ce92104a1f 592 } else if (dMode[whichTouched]==dateScreen){
TickTock 25:ddf0ec209f03 593 upDate(dtMode,false);
TickTock 42:4533b13b297d 594 lastDMode[whichTouched]=99;
TickTock 62:ffd15edb5431 595 } else {
TickTock 62:ffd15edb5431 596 lastDMode[whichTouched]=99;//repaint to clear highlight
TickTock 25:ddf0ec209f03 597 }
TickTock 62:ffd15edb5431 598
TickTock 23:cd03f9c3395e 599 break;
garygid 54:6ce808d0995e 600
garygid 87:46ac3f2519d6 601 case 31: // col 3 row 1
leafman 98:9f8bab96edff 602 if (dMode[whichTouched]==configScreen) { // gg - Batt Log Enable Button
garygid 87:46ac3f2519d6 603 debugMode = !debugMode;
leafman 98:9f8bab96edff 604 } else if (dMode[whichTouched] == indexScreen) { // gg - index
leafman 98:9f8bab96edff 605 dMode[whichTouched] = configScreen ; // GoTo Config Screen
garygid 87:46ac3f2519d6 606 } else {
garygid 87:46ac3f2519d6 607 lastDMode[whichTouched]=99;//repaint to clear highlight
garygid 87:46ac3f2519d6 608 }
garygid 87:46ac3f2519d6 609 break;
garygid 87:46ac3f2519d6 610
garygid 54:6ce808d0995e 611 //-----------------------------------
garygid 67:2022fce701d0 612 case 02: // left col, bottom row (not nav)
garygid 67:2022fce701d0 613 if (dMode[whichTouched] == indexScreen) { // gg - index
leafman 98:9f8bab96edff 614 dMode[whichTouched] = playbackScreen ; // GoTo Playback Screen
leafman 98:9f8bab96edff 615 } else if (dMode[whichTouched]==configScreen) {
leafman 98:9f8bab96edff 616 metric = !metric; // toggle metric/imperial display
garygid 87:46ac3f2519d6 617 } else {
garygid 87:46ac3f2519d6 618 lastDMode[whichTouched]=99;//repaint to clear highlight
garygid 87:46ac3f2519d6 619 }
garygid 67:2022fce701d0 620 break;
garygid 67:2022fce701d0 621
garygid 67:2022fce701d0 622 case 12: // left-middle col, bottom row (not nav)
garygid 67:2022fce701d0 623 if (dMode[whichTouched] == indexScreen) { // gg - index
leafman 98:9f8bab96edff 624 dMode[whichTouched] = dateScreen ; // GoTo Set Date/Time Screen
garygid 87:46ac3f2519d6 625 } else {
garygid 87:46ac3f2519d6 626 lastDMode[whichTouched]=99;//repaint to clear highlight
garygid 87:46ac3f2519d6 627 }
garygid 67:2022fce701d0 628 break;
garygid 67:2022fce701d0 629
garygid 67:2022fce701d0 630 case 22: // right-middle col, bottom row (not nav)
garygid 67:2022fce701d0 631 if (dMode[whichTouched] == indexScreen) { // gg - index
leafman 98:9f8bab96edff 632 dMode[whichTouched] = logScreen ;
garygid 87:46ac3f2519d6 633 } else {
garygid 87:46ac3f2519d6 634 lastDMode[whichTouched]=99;//repaint to clear highlight
garygid 87:46ac3f2519d6 635 }
garygid 67:2022fce701d0 636 break;
TickTock 72:f9a6faa28873 637
leafman 98:9f8bab96edff 638 case 32: // right col, bottom row (not nav)
leafman 98:9f8bab96edff 639 if (dMode[whichTouched] == configScreen) {
leafman 98:9f8bab96edff 640 updateFirmware();
leafman 98:9f8bab96edff 641 } else {
garygid 87:46ac3f2519d6 642 lastDMode[whichTouched]=99;//repaint to clear highlight
leafman 98:9f8bab96edff 643 }
TickTock 72:f9a6faa28873 644 break;
TickTock 72:f9a6faa28873 645
garygid 67:2022fce701d0 646 //-----------------------------------
garygid 54:6ce808d0995e 647 //-----------------------------------
garygid 54:6ce808d0995e 648 // Prev Navigation
garygid 54:6ce808d0995e 649 case 07: // col 0 row tNavRow
TickTock 42:4533b13b297d 650 dMode[whichTouched]=dMode[whichTouched]>0?dMode[whichTouched]-1:maxScreens;
TickTock 22:a43df3905863 651 break;
garygid 54:6ce808d0995e 652 //-----------------------------------
garygid 54:6ce808d0995e 653 // Select Screen Navigation
garygid 54:6ce808d0995e 654 case 17:
TickTock 43:e7f6f80590e3 655 //secsNoTouch = userTimeout; // immediately exit config mode
TickTock 43:e7f6f80590e3 656 sMode=0;
TickTock 43:e7f6f80590e3 657 lastDMode[whichTouched]=99; // Repaint
TickTock 22:a43df3905863 658 break;
garygid 54:6ce808d0995e 659 //-----------------------------------
garygid 54:6ce808d0995e 660 // Index Navigation
garygid 54:6ce808d0995e 661 case 27: // col 2 row tNavRow
garygid 67:2022fce701d0 662 dMode[whichTouched]= indexScreen ; // gg - index
garygid 54:6ce808d0995e 663 break;
garygid 54:6ce808d0995e 664 //------------------------------------
garygid 54:6ce808d0995e 665 // Next Navigation
garygid 54:6ce808d0995e 666 case 37: // lower right on Nav Line gg - move next
TickTock 42:4533b13b297d 667 dMode[whichTouched]=dMode[whichTouched]<maxScreens?dMode[whichTouched]+1:0;
TickTock 22:a43df3905863 668 break;
garygid 54:6ce808d0995e 669 //------------------------------------
garygid 54:6ce808d0995e 670 //------------------------------------
TickTock 22:a43df3905863 671 default:
TickTock 62:ffd15edb5431 672 lastDMode[whichTouched]=99;//repaint to clear highlight
TickTock 25:ddf0ec209f03 673 break;
TickTock 25:ddf0ec209f03 674 }
TickTock 23:cd03f9c3395e 675 break;
TickTock 23:cd03f9c3395e 676 case 2: // numpad
TickTock 23:cd03f9c3395e 677 break;
TickTock 23:cd03f9c3395e 678 case 3:
TickTock 23:cd03f9c3395e 679 break;
TickTock 23:cd03f9c3395e 680 default:
TickTock 23:cd03f9c3395e 681 break;
TickTock 23:cd03f9c3395e 682 } // case sMode
TickTock 22:a43df3905863 683 } //recently touched
TickTock 4:8d7759f4fe7a 684 } else { // userIdle
TickTock 8:67eed72f3e10 685 if(sMode==1){
TickTock 8:67eed72f3e10 686 sMode=0;
TickTock 43:e7f6f80590e3 687 lastDMode[whichTouched]=99;
TickTock 8:67eed72f3e10 688 }
TickTock 4:8d7759f4fe7a 689 }
TickTock 34:4751a8259b18 690
TickTock 34:4751a8259b18 691 if(tick){ // Executes once a second
TickTock 93:c2402e8cd0e2 692 headlights = (lastMsg[indexLastMsg[0x358]].data[1]&0x80)?true:false; // headlight/turn signal indicator
TickTock 41:8d4609ea7259 693 accV=floor(mon12V*scale12V*10+0.5)/10; //Round to nearest 10th
TickTock 35:5acbd8a64a89 694 accOn=(accV>5)?true:false;
TickTock 49:a3d2c5bb3cfa 695 if(laccOn&&!accOn){ // Car turned off
TickTock 49:a3d2c5bb3cfa 696 //write efficiency history data
TickTock 97:a25940fd7b5b 697 hfile = fopen("/local/ehist.cny", "w");
TickTock 97:a25940fd7b5b 698 if (hfile!=NULL){ // found a efficiency history file
TickTock 92:935adef49ea4 699 for(i=0;i<39;i++){
TickTock 97:a25940fd7b5b 700 fprintf(hfile,"%f %f\r\n",mph[i],kW[i]);
TickTock 49:a3d2c5bb3cfa 701 }
TickTock 97:a25940fd7b5b 702 fclose(hfile);
TickTock 49:a3d2c5bb3cfa 703 }
TickTock 49:a3d2c5bb3cfa 704 }
TickTock 49:a3d2c5bb3cfa 705 laccOn=accOn;
TickTock 97:a25940fd7b5b 706 if(!accOn&&!logEn&&userIdle&&!playbackEn){
TickTock 35:5acbd8a64a89 707 dled = 0; // turn off display if car off and logging disabled and no user activity
TickTock 35:5acbd8a64a89 708 }else if(!headlights){
TickTock 35:5acbd8a64a89 709 dled = ledHi;
TickTock 86:d1c9e8ac1c4b 710 } else {
TickTock 35:5acbd8a64a89 711 dled = ledLo;
TickTock 34:4751a8259b18 712 }
TickTock 49:a3d2c5bb3cfa 713
TickTock 49:a3d2c5bb3cfa 714 //compute historic efficiency
TickTock 41:8d4609ea7259 715 if(numSsamples>0){ // Avoid div0
TickTock 49:a3d2c5bb3cfa 716 mph[0]=((float) motorRPM)/numSsamples/215; // Empirically derived - may change car to car
TickTock 86:d1c9e8ac1c4b 717 } else {
TickTock 49:a3d2c5bb3cfa 718 mph[0]=0;
TickTock 36:dbd39c315258 719 }
TickTock 49:a3d2c5bb3cfa 720 if(mph[0]>99){
TickTock 49:a3d2c5bb3cfa 721 mph[0]=0;
TickTock 49:a3d2c5bb3cfa 722 }
TickTock 49:a3d2c5bb3cfa 723 mpkWh[0]=mph[0];
TickTock 41:8d4609ea7259 724 if(numWsamples>0){ // Avoid div0
TickTock 49:a3d2c5bb3cfa 725 kW[0]=((float) mWs_x4)/numWsamples/4e3;
TickTock 49:a3d2c5bb3cfa 726 mpkWh[0]/=kW[0];
TickTock 49:a3d2c5bb3cfa 727 if (mpkWh[0]<0) {
TickTock 49:a3d2c5bb3cfa 728 mpkWh[0]=99;// negative means inf.
TickTock 49:a3d2c5bb3cfa 729 }
TickTock 86:d1c9e8ac1c4b 730 } else {
TickTock 49:a3d2c5bb3cfa 731 kW[0]=0;
TickTock 49:a3d2c5bb3cfa 732 mpkWh[0]=0;
TickTock 49:a3d2c5bb3cfa 733 }
TickTock 49:a3d2c5bb3cfa 734 //mpkWh[0]=floor(mpkWh[0]*10+0.5)/10; // Round to nearest 10th
TickTock 49:a3d2c5bb3cfa 735 motorRPM=0;
TickTock 41:8d4609ea7259 736 numSsamples=0;
TickTock 40:0e6e71a7323f 737 mWs_x4=0;
TickTock 40:0e6e71a7323f 738 numWsamples=0;
TickTock 53:8a51361434ae 739 if(accOn||playbackEn){
TickTock 92:935adef49ea4 740 for(i=1;i<39;i++){
TickTock 49:a3d2c5bb3cfa 741 average=mph[i]/timeConstant[i];
TickTock 49:a3d2c5bb3cfa 742 mph[i]-=average;
TickTock 52:d5385fbf4ea1 743 mph[i]+=mph[0];
TickTock 52:d5385fbf4ea1 744 mpkWh[i]=average;
TickTock 49:a3d2c5bb3cfa 745 average=kW[i]/timeConstant[i];
TickTock 97:a25940fd7b5b 746 if((mph[0]>0)||(kW[0]>0)){ //Not charging - so include in efficiency data
TickTock 97:a25940fd7b5b 747 kW[i]-=average;
TickTock 97:a25940fd7b5b 748 kW[i]+=kW[0];
TickTock 97:a25940fd7b5b 749 }
TickTock 52:d5385fbf4ea1 750 mpkWh[i]/=average;
TickTock 49:a3d2c5bb3cfa 751 if (mpkWh[i]<0) {
TickTock 49:a3d2c5bb3cfa 752 mpkWh[i]=99;// negative means inf.
TickTock 49:a3d2c5bb3cfa 753 }
TickTock 49:a3d2c5bb3cfa 754 //mpkWh[i]=floor(mpkWh[i]*10+0.5)/10; // Round to nearest 10th
TickTock 52:d5385fbf4ea1 755 }
TickTock 49:a3d2c5bb3cfa 756 }
TickTock 49:a3d2c5bb3cfa 757 updateDTE=true;
TickTock 40:0e6e71a7323f 758 if(logCP)
garygid 69:6bfdfc002036 759 logPackVoltages(); // Turbo3, only call
TickTock 34:4751a8259b18 760 tick=false;
TickTock 92:935adef49ea4 761 }
TickTock 36:dbd39c315258 762
TickTock 39:eef8beac7411 763 display=display<1?display+1:0; // toggle display
TickTock 39:eef8beac7411 764 updateDisplay(display);
TickTock 39:eef8beac7411 765
TickTock 36:dbd39c315258 766 if(step){ // playback
TickTock 36:dbd39c315258 767 if(playbackOpen&&playbackEn){
TickTock 92:935adef49ea4 768 for(i=0;i<120;i++){
TickTock 97:a25940fd7b5b 769 if(!feof(lfile)){
TickTock 97:a25940fd7b5b 770 fscanf(lfile,"%5c%8c",&header,&data);
garygid 69:6bfdfc002036 771 logCan(header[0],CANMessage(0x7ff&((header[4]<<8)+header[3]), data, 8)); // Playback
TickTock 86:d1c9e8ac1c4b 772 } else {
TickTock 97:a25940fd7b5b 773 fclose(lfile); // restart
TickTock 97:a25940fd7b5b 774 lfile = fopen("/usb/playback.alc", "rb");
TickTock 50:83d5864c64a0 775 lastDMode[whichTouched]=99;//force refresh
TickTock 36:dbd39c315258 776 spkr.beep(2000,0.25);
TickTock 36:dbd39c315258 777 }
TickTock 36:dbd39c315258 778 }
TickTock 36:dbd39c315258 779 }
TickTock 36:dbd39c315258 780 step=false;
TickTock 36:dbd39c315258 781 }
TickTock 37:fea2c1d52c5f 782
TickTock 2:71b1999a8ea5 783 } //while (true)
TickTock 0:1596b8644523 784 }