Attempts to merge SPI_TFT2 & SPI_TFT_ILI9341

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 mbed

Fork of CANary by Tick Tock

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Sat Apr 20 17:08:40 2013 +0000
Parent:
87:46ac3f2519d6
Child:
89:2263d349b484
Commit message:
Made logCan reentry safe so logTS & logEven can be safely interrupted by logCan.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
utility.cpp Show annotated file Show diff for this revision Revisions of this file
utility.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Apr 19 02:06:59 2013 +0000
+++ b/main.cpp	Sat Apr 20 17:08:40 2013 +0000
@@ -160,11 +160,11 @@
     }
     t = *localtime(&seconds) ;
     strftime(sTemp, 32, "%a %m/%d/%Y %X\n", &t);
-    logMsg(sTemp); // record RTC
+    printMsg(sTemp); // record RTC
     
     // revision
     sprintf(sTemp,"CANary firmware rev%s\n", revStr); // gg - for Logging the revision
-    logMsg(sTemp); // revision
+    printMsg(sTemp); // revision
 
     // Look for new binary on thumbdrive
     // Can't make this work right now since USB doesn't attach the right timestamp (so new binary isn't loaded)
@@ -172,11 +172,11 @@
     lastDMode[whichTouched]=99;//force refresh
     if (cfile!=NULL){ //found a new binary on the thumbdrive so copy it over
         sprintf(sTemp,"New binary found.\n");
-        logMsg(sTemp); // new binary
+        printMsg(sTemp); // new binary
         file = fopen("/local/CANary.bin", "wb");
         if (file==NULL){ //failed to open destination
             sprintf(sTemp,"Unable to open destination file.\n");
-            logMsg(sTemp); // cannot open CANary.bin
+            printMsg(sTemp); // cannot open CANary.bin
         } else {
             tt.set_display(2);
             tt.foreground(White);
@@ -214,10 +214,10 @@
         }
         fclose(cfile);
         sprintf(sTemp,"History Loaded.\n");
-        logMsg(sTemp); // History loaded
+        printMsg(sTemp); // History loaded
     } else { // create initial file
         sprintf(sTemp,"History not found.  Created.\n");
-        logMsg(sTemp); // history not found, created
+        printMsg(sTemp); // history not found, created
         for(i=0;i<39;i++){
             // Pre-load with 4 mpkWh @ 40 mph
             mph[i]=40*timeConstant[i];
@@ -251,24 +251,24 @@
                 t = *localtime(&seconds) ;
                 strftime(fileName, 32, "/usb/%m%d%H%M.alc", &t); //mmddhhmm.alc
                 //sprintf(sTemp,"Using file %s\n",fileName);
-                //logMsg(sTemp); // using alc file ...
+                //printMsg(sTemp); // using alc file ...
                 file = fopen(fileName, "ab");
                 lastDMode[whichTouched]=99;//force refresh
                 if(file==NULL){
                     sprintf(sTemp,"\nUnable to open %s\n\n\n\n",fileName);
-                    logMsg(sTemp); // cannot open alc file
+                    printMsg(sTemp); // cannot open alc file
                     logEn=false;
                     spkr.beep(1000,0.25);
                 } else {
                     logOpen = true;
                     readPointer=writePointer;
                     sprintf(sTemp,"Starting Can Log %s\n",fileName);
-                    logMsg(sTemp); // starting alc log file 
+                    printMsg(sTemp); // starting alc log file 
                     
                     logTS(); // Date Time at start
-                    logErrMsg("Starting"); // Log startup msg for testing
+                    logEventMsg("Starting"); // Log startup msg for testing
                     sprintf(sTemp,"Cr%s",revStr);
-                    logErrMsg(sTemp); // gg - log firmware version
+                    logEventMsg(sTemp); // gg - log firmware version
                     
                     spkr.beep(2000,0.25);
                 }
@@ -280,7 +280,7 @@
                 if (file == NULL) {
                     logOpen = false;
                     sprintf(sTemp,"Failed to append log file.\n");
-                    logMsg(sTemp); // failed to append 
+                    printMsg(sTemp); // failed to append 
                     spkr.beep(1000,0.25);
                     logEn=false;
                 } else {
@@ -297,7 +297,7 @@
             } // if > 1/16 full, canbus has stopped, or logging stopped
             if (!logEn) {
                 sprintf(sTemp,"Stopping Can Log %s\n",fileName);
-                logMsg(sTemp); // stopping alc log file 
+                printMsg(sTemp); // stopping alc log file 
                 fclose(file);
                 logOpen=false;
                 pointerSep=0;
@@ -310,7 +310,7 @@
             seconds = time(NULL);
             t = *localtime(&seconds) ;
             strftime(sTemp, 40, "Sleeping: %a %m/%d/%Y %X\n", &t);
-            logMsg(sTemp); // sleeping date time
+            printMsg(sTemp); // sleeping date time
             updateDisplay(0); //Added for turbo3 who has a display override and wants to see the sleep message before going to sleep
             updateDisplay(1);
             //LPC_RTC->CIIR=0x00; // block RTC interrupts
@@ -333,13 +333,13 @@
             seconds = time(NULL);
             t = *localtime(&seconds) ;
             strftime(sTemp, 40, "Waking: %a %m/%d/%Y %X\n", &t);
-            logMsg(sTemp); // wakeup date time
+            printMsg(sTemp); // wakeup date time
             if (time(NULL)>(secs+1800)) {
                 logOpen = false; // Start new file if asleep for more than 30 minutes
                 if (secsNoTouch>100) secsNoTouch = 100; // also mostly reset user Idle counter
             } else if (logOpen){ // insert timestamp on each wake if logging enabled (disabled for now)
                 file = fopen(fileName, "ab");
-                logErrMsg("WakingUp"); // gg - use messeges
+                logEventMsg("WakingUp"); // gg - use messeges
                 logTS(); // Date-Time at wakeup
             }
         } // if idle
@@ -366,7 +366,7 @@
                 sMode = 1;
             }
             //sprintf(sTemp,"%d,%d ",lastTouch.x,lastTouch.y);
-            //logMsg(sTemp); // touch x,y - for debug
+            //printMsg(sTemp); // touch x,y - for debug
             touched = false; // clear interrupt flag
         }
         //---------------
@@ -380,7 +380,7 @@
                 secsNoTouch +=2; // increment to prevent double touch
                 sMode = 1;
                 //sprintf(sTemp,"button %d %d,%d %d\n",i,buttonX(lastTouch.x,3),buttonY(lastTouch.y,3),lastTouch.x);
-                //logMsg(sTemp); // button parms - for debug
+                //printMsg(sTemp); // button parms - for debug
                 switch (sMode) {
                     case 0: // no select
                         break;
@@ -457,7 +457,7 @@
                                     sMode=0;
                                 } else if (dMode[whichTouched]==config1Screen) {
                                     sprintf(sTemp,"Saving config file.\n");
-                                    logMsg(sTemp); // saving config
+                                    printMsg(sTemp); // saving config
                                     saveConfig();
                                     spkr.beep(2000,0.25);
                                 } else if (dMode[whichTouched]==playbackScreen) { // faster
@@ -487,7 +487,7 @@
                                     lastDMode[whichTouched ^ 1]=99; // repaint other screen (^ = XOR)
                                     
                                     //sprintf(sTemp,"Changed Skin to %d.\n");
-                                    //logMsg(sTemp); /// changed skin - for debug
+                                    //printMsg(sTemp); /// changed skin - for debug
                                     //saveConfig();
                                     //spkr.beep(2000,0.25);
                                     
@@ -539,21 +539,21 @@
                                             lastDMode[whichTouched]=99;//force refresh
                                             if(file==NULL){
                                                 sprintf(sTemp,"Unable to open /usb/playback.alc\n");
-                                                logMsg(sTemp); // no playback.alc
+                                                printMsg(sTemp); // no playback.alc
                                                 spkr.beep(1000,0.25);
                                             } else {
                                                 playbackOpen = true;
                                                 playbackEn=true;
                                                 playback.attach(&playbackISR,playbackInt);
                                                 sprintf(sTemp,"Starting playback\n");
-                                                logMsg(sTemp); // start playback
+                                                printMsg(sTemp); // start playback
                                                 spkr.beep(2000,0.25);
                                                 can1.attach(&doNothing);// Stop recieving CAN data
                                                 can2.attach(&doNothing);
                                             }
                                         } else {
                                             sprintf(sTemp,"Must stop logging first\n");
-                                            logMsg(sTemp); // Must stop logging first
+                                            printMsg(sTemp); // Must stop logging first
                                         }
                                     } else {
                                         playback.detach();
@@ -691,7 +691,7 @@
             laccOn=accOn;
             if(!accOn&&!logEn&&userIdle&&!playbackEn){
                 //sprintf(sTemp,"Display Off %4.2f\n",accV);
-                //logMsg(sTemp); // display off - for debug
+                //printMsg(sTemp); // display off - for debug
                 
                 dled = 0; // turn off display if car off and logging disabled and no user activity
             }else if(!headlights){
--- a/utility.cpp	Fri Apr 19 02:06:59 2013 +0000
+++ b/utility.cpp	Sat Apr 20 17:08:40 2013 +0000
@@ -21,7 +21,7 @@
     NVIC_EnableIRQ( RTC_IRQn );
 }
 
-void logMsg (char *msg) {
+void printMsg (char *msg) {
     strcpy(displayLog[displayLoc],msg);
     displayLoc=displayLoc>17?0:displayLoc+1;
 }
@@ -137,7 +137,7 @@
                     if( nLost > 0 ) {
                         // We previously lost messages that did not get into the buffer
                         sprintf(sTemp,"-- Lost %d Messages.\n", nLost);
-                        logMsg(sTemp); // write buffer overrun
+                        printMsg(sTemp); // write buffer overrun
                         //spkr.beep(500,0.25);
                         
                         nLost = 0 ;
@@ -146,37 +146,41 @@
                 }
             }
         }
-    }else{ // not debugMode - keep code short
+    } else { // not debugMode - keep code short
         if(logOpen){
-            if(canRXmsg.id>0) {
-                ts=getTimeStamp(); // only use
-                writeBuffer[writePointer][0]=mType;
-                writeBuffer[writePointer][1]=(ts&0xff00)>>8;
-                writeBuffer[writePointer][2]=(ts&0x00ff);
-                writeBuffer[writePointer][3]=canRXmsg.id&0xff;
-                writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
-                for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
-                    writeBuffer[writePointer][i]=canRXmsg.data[i-5];
-                }
-                if (++writePointer >= maxBufLen) {
-                    writePointer = 0;
-                    led3 = !led3;
-                }
-                if (writePointer==readPointer) {
-                    // Just overwrote an entry that hasn't been sent to thumbdrive
-                    sprintf(sTemp,"Write buffer overrun.\n");
-                    logMsg(sTemp); // write buffer overrun
-                    spkr.beep(500,0.25);
-                }
+            int localWritePointer = writePointer++; // create local copy to make logCan reentrant
+            // note that the static variables above do not prevent safe reentry
+            // since they are only used for msgId<0x800 which will never interrupt
+            // another msgId<0x800 (both CANbusses are same priority)
+            if (writePointer >= maxBufLen) {
+                writePointer = 0;
+                led3 = !led3;
+            }
+            if (localWritePointer >= maxBufLen) { //Have to test local, too, just in case interrupted just after increment (got double incremented)
+                localWritePointer = 0;
+            }
+            ts=getTimeStamp(); // only use
+            writeBuffer[localWritePointer][0]=mType;
+            writeBuffer[localWritePointer][1]=(ts&0xff00)>>8;
+            writeBuffer[localWritePointer][2]=(ts&0x00ff);
+            writeBuffer[localWritePointer][3]=canRXmsg.id&0xff;
+            writeBuffer[localWritePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
+            for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[localWritePointer][i]=canRXmsg.data?)
+                writeBuffer[localWritePointer][i]=canRXmsg.data[i-5];
+            }
+            if (writePointer==readPointer) {
+                // Just caught up to read pointer
+                sprintf(sTemp,"Write buffer overrun.\n");
+                printMsg(sTemp); // write buffer overrun
+                spkr.beep(500,0.25);
             }
         }
     }
 
     if(canRXmsg.id<0x800){ // Block FFE and FFF messages
+
         if(indexLastMsg[canRXmsg.id]==0) { //Check if no entry
-            //ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used
             if(ii<99) {
-                //indexLastMsg[canRXmsg.id]=ii; //Create entry if first message
                 indexLastMsg[canRXmsg.id]=++ii; //Create entry for first MsgID occurance
                 // ii max is 99 here
             } else {
@@ -184,7 +188,7 @@
                 if(ii==99) {
                     ii++; // step to 100 to log only one error
                     sprintf(sTemp,"MsgID buffer overrun.\n");
-                    logMsg(sTemp); // write buffer overrun
+                    printMsg(sTemp); // write buffer overrun
                 }
             }
         }
@@ -213,27 +217,27 @@
                 if(canRXmsg.data[3]==2){//Group 2 = cellpair data
                     bdi=BatDataBaseG2; // index offset for CP data (uses 00 - 1C)
                     sprintf(sTemp,"  Getting cell pair data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==4){//Group 4 = temperature data
                     bdi=BatDataBaseG4; // index offset for Temperature data (uses 20 - 22)
                     sprintf(sTemp,"  Getting temperature data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==1){//Group 1 data
                     bdi=BatDataBaseG1; // index offset for Group 1 data (uses 20 - 22)
                     sprintf(sTemp,"Getting Group 1 data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==3){//Group 3 data
                     bdi=BatDataBaseG3; // index offset for Group 3 data (uses 20 - 22)
                     sprintf(sTemp,"  Getting Group 3 data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==5){//Group 5 data
                     bdi=BatDataBaseG5; // index offset for Group 5 data (uses 20 - 22)
                     sprintf(sTemp,"  Getting Group 5 data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else bdi=0xff; // ignore other messages (for now)
                 lasti=0;
@@ -307,16 +311,16 @@
     logCan(0,tsMsg); // Date-Time
 }
 
-void logErrMsg (char * errMsg) {
+void logEventMsg (char * eventMsg) {
     // log CAN-Do 8-character Pseudo Message
     CANMessage tsMsg;
     tsMsg.id=0xffe; // pseudo Message to CAN-Do log
     tsMsg.len=0xf;
-    int iMsgLen = strlen(errMsg);
+    int iMsgLen = strlen(eventMsg);
     // 8 character message compatible with CAN-Do
     for(int i=0; i<8; i++){
       tsMsg.data[i]=' '; 
-      if( i < iMsgLen ) tsMsg.data[i]=errMsg[i];
+      if( i < iMsgLen ) tsMsg.data[i]=eventMsg[i];
     }
     logCan(0,tsMsg); // FFE Comment Message
 }
@@ -369,52 +373,6 @@
     }
 }
 
-/*void sendCPreq() {
-    char i;
-    char data[8] = {0x02, 0x21, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff};
-    can1.monitor(false); // set to active mode
-    can1SleepMode = 0; // enable TX
-    can1.write(CANMessage(0x79b, data, 8));
-    
-    if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
-    
-    logCan(1,CANMessage(0x79b, data, 8)); // Group 2 Request on EV
-    data[0]=0x30; //change to request next line message
-    data[1]=0x01;
-    data[2]=0x00;
-    for(i=0;i<29;i++){
-        wait_ms(16); //wait 16ms
-        can1.write(CANMessage(0x79b, data, 8));
-    }
-    can1SleepMode = 1; // disable TX
-    can1.monitor(true); // set to snoop mode
-}
-
-void sendTreq() {
-    char i;
-    char data[8] = {0x02, 0x21, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff};
-    can1.monitor(false); // set to active mode
-    can1SleepMode = 0; // enable TX
-    can1.write(CANMessage(0x79b, data, 8));
-    
-    if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
-    
-    logCan(1,CANMessage(0x79b, data, 8)); // Group 4 request on EV
-    data[0]=0x30; //change to request next line message
-    data[1]=0x01;
-    data[2]=0x00;
-    for(i=0;i<3;i++){
-        wait_ms(16); //wait 16ms
-        can1.write(CANMessage(0x79b, data, 8));
-    }
-    can1SleepMode = 1; // disable TX
-    can1.monitor(true); // set to snoop mode
-}
-
-void autoPollISR() {  //This is the ticker ISR for auto-polling
-    pollCP=true;    //Set a flag to do in main loop instead of here
-}                   //since ticker blocks other interrupts*/
-
 void autoPollISR(){
     reqMsgCnt = 0; //reset message counter
     msgReq.attach(&sendReq,0.025);
@@ -496,9 +454,9 @@
     cfile = fopen("/local/config.txt", "r");
     if (cfile==NULL){ // if doesn't exist --> create
         sprintf(sTemp,"No config file found.\n");
-        logMsg(sTemp); // no config file
+        printMsg(sTemp); // no config file
         sprintf(sTemp,"Calibrating touch screen.\n");
-        logMsg(sTemp); // calibrating
+        printMsg(sTemp); // calibrating
         //tt.setcal(5570, 34030, 80, 108, 33700, 5780, 82, 108, 32500);// bypass calibration using my values
         tt.calibrate();   // run touchscreen calibration routine
         // NOTE: calibrates screen 1 first, then screen 0.
@@ -542,10 +500,10 @@
         if(ff<4){//If not latest format, save as latest format
             saveConfig();
             sprintf(sTemp,"Config file format updated.\n");
-            logMsg(sTemp); // config forat updates
+            printMsg(sTemp); // config forat updates
         }
         sprintf(sTemp,"Config file loaded.\n");
-        logMsg(sTemp); // config file loaded
+        printMsg(sTemp); // config file loaded
     }
 }
 
--- a/utility.h	Fri Apr 19 02:06:59 2013 +0000
+++ b/utility.h	Sat Apr 20 17:08:40 2013 +0000
@@ -58,14 +58,12 @@
     void mbed_reset();
     void RTC_IRQHandler();
     void RTC_Init (void);    
-    void logMsg (char *msg);
+    void printMsg (char *msg);
     void touch_ISR();
     unsigned short getTimeStamp();
     void logCan (char mType, CANMessage canRXmsg);
     void logTS ();    
-    void logErrMsg (char * errMsg); // gg - messeges
-    //void sendCPreq();    
-    //void sendTreq();    
+    void logEventMsg (char * eventMsg); // gg - messeges
     void sendReq();    
     void autoPollISR();
     void playbackISR();