Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Branch:
Metric
Revision:
117:49883c779a74
Parent:
116:5cd72bae7c12
Child:
118:02010b18efdc
--- a/main.cpp	Thu Jul 04 05:27:15 2013 +0000
+++ b/main.cpp	Thu Jul 04 18:01:52 2013 +0000
@@ -24,7 +24,7 @@
 #include "displayModes.h"
 #include "TOUCH_TFTx2.h"
 
-char revStr[7] = "116"; // gg - revision string, max 6 characters
+char revStr[7] = "117"; // gg - revision string, max 6 characters
 
 FATFS USBdrive;
 LocalFileSystem local("local");
@@ -45,8 +45,6 @@
 DigitalOut led4(LED4);
 
 InterruptIn touchpad(p17);
-InterruptIn hwInt(p24);  // Assigning p24 as both input interrupt and
-DigitalOut swInt(p24);  // digital out to create a SW interrupt
 CAN can1(p9, p10);      // CAN1 (EV) uses pins 9 and 10 (rx, tx) and pin 8 (rs)
 DigitalOut can1SleepMode(p8);     // Use pin 8 to control the sleep mode of can2
 CAN can2(p30, p29);     // CAN2 (CAR) uses pins 30 and 29 (rx, tx) and pin 28 (rs)
@@ -112,6 +110,7 @@
 float accV = 0;
 bool playbackEn = false;
 bool playbackOpen = false;
+//float playbackInt = 0.05; //read messages every 50 ms
 float playbackInt = 0.005; //read messages every 5 ms
 bool step = false;
 char header[5];
@@ -151,20 +150,15 @@
     tt.cls();
     tt.claim(stdout);        // send stdout to the TFT display
     touchpad.rise(&touch_ISR);
-    hwInt.rise(&dumpBuffer);
     tt.wfi();               // enable interrupt on touch
     dled = 0.8; // turn on display LED 80%
-    swInt = 0;
+
     timer.start() ;
     RTC_Init(); // start the RTC Interrupts that sync the timer
     struct tm t; // pointer to a static tm structure
     NVIC_SetPriority(CAN_IRQn, 2); //set can priority just below RTC
-    NVIC_SetPriority(EINT0_IRQn, 3); //set hardware interrupt
-    NVIC_SetPriority(EINT1_IRQn, 3); //not sure which InterruptIn uses
-    NVIC_SetPriority(EINT2_IRQn, 3); //so set them all
-    NVIC_SetPriority(EINT3_IRQn, 3);
-    NVIC_SetPriority(TIMER3_IRQn, 4); //set ticker priority just below can
-   
+    NVIC_SetPriority(TIMER3_IRQn, 3); //set ticker priority just below can
+
     seconds = time(NULL);
     t = *localtime(&seconds) ;
     // is it a date before 2012 ?
@@ -254,14 +248,33 @@
                 }
             }//logging enabled and USB detected
         } else { // if (logOpen)
-            if (canIdle||!logEn) {
-                swInt=1; //trigger buffer dump ISR
-            } // if canbus has stopped, or logging stopped
+            pointerSep=(writePointer+maxBufLen-readPointer)%maxBufLen;
+            if (pointerSep>(maxBufLen/16)||canIdle||!logEn) {
+                // Dump buffer if > 1/16 full or canbus has stopped
+                //if (&efile == NULL) {
+                if (efr != FR_OK) {
+                    logOpen = false;
+                    printMsg("Failed to append log file.\n"); // failed to append 
+                    spkr.beep(3000,0.25);
+                    spkr.beep(1500,0.25);
+                    spkr.beep(750,0.25);
+                    spkr.beep(375,0.25);
+                    logEn=false;
+                } else {
+                    while (readPointer != writePointer) {
+                        efr=f_write(&efile,&writeBuffer[readPointer][0],13,&bytesRW);
+                        if(++readPointer >= maxBufLen){
+                            readPointer=0;
+                            led4 = !led4;
+                        }
+                    }
+                }
+            } // if > 1/16 full, canbus has stopped, or logging stopped
             if (!logEn) {
-                logOpen=false;
                 sprintf(sTemp,"Stopping Can Log %s\n",fileName);
                 printMsg(sTemp); // stopping alc log file 
                 f_close(&efile);
+                logOpen=false;
                 pointerSep=0;
                 led4=false;
             }
@@ -513,7 +526,7 @@
                                                 can2.attach(&doNothing);
                                             }
                                         } else {
-                                            printMsg("Must stop logging first\n"); // Must stop logging first
+                                            printMsg("Must stop logging first\n");
                                         }
                                     } else {
                                         playback.detach();