.

Dependencies:   SDHCFileSystem mbed

Files at this revision

API Documentation at this revision

Comitter:
TickTock
Date:
Mon Dec 03 13:09:09 2012 +0000
Parent:
3:51134a275609
Child:
5:ddbc3d4bc266
Commit message:
logs fast. fixed rollover bug.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Dec 03 03:25:56 2012 +0000
+++ b/main.cpp	Mon Dec 03 13:09:09 2012 +0000
@@ -3,6 +3,7 @@
 #include "SDHCFileSystem.h"
 
 #define upLine "\033[1A"
+#define maxBufLen 2048
 
 //CANcan.cpp
 
@@ -76,7 +77,6 @@
 //39:       NC:5Vout (only available when connected as USB device)
 //40:   VCC3.3
 
-extern bool logOpen;
 void Log (char *message);
 void LogErr (char *message);
 
@@ -98,30 +98,32 @@
 bool logOpen = false;
 FILE *file;
 char fileName[32] = "" ;
-char writeBuffer[2048][13];
+char writeBuffer[maxBufLen][13];
 int bufPointer = 0;
 
 Serial pc(USBTX, USBRX); // tx, rx   default settings (9600 8N1)
 
 extern "C" void RTC_IRQHandler() {
+    static int i=0;
     timer.reset() ; // zero ms at the-seconds-tick
-    if ((bufPointer>1536)||((bufPointer>0)&!logOpen)) {
-        file = fopen(fileName, "a");
+    if (logOpen) {
+        file = fopen(fileName, "ab");
         if (file == NULL) {
             logOpen = false;
+            pc.printf("Failed to append log file.\n\n");
         } else {
-            int i=0;
-            while (i<bufPointer) {
+            while (i != bufPointer) {
                 for (int j = 0; j<13; j++){
                     fprintf(file,"%c",writeBuffer[i][j]);
                 }
-                i++;
+                if(++i >= maxBufLen)
+                    i=0;
             }
-            bufPointer = 0;
             led3 = !led3;
             fclose(file);
         }
     }
+
     LPC_RTC->ILR |= (1<<0); // turn on Low Bit - works
 }
 
@@ -171,7 +173,7 @@
             i=0;
         }
     }
-    pc.printf("\n\n");
+    pc.printf("\n\n\n\n");
     fclose(file);
 }
 
@@ -185,28 +187,30 @@
     for (int i = 5; i<13; i++){
         writeBuffer[bufPointer][i]=canRXmsg.data[i-5];
     }
-    if (bufPointer++>2047)
-        bufPointer=2047;
+    if (++bufPointer >= maxBufLen) {
+        bufPointer = 0;
+        led4 = !led4;
+    }
 }
 
 void send1() {
     static char counter = 0; // use for fake data
     can1.write(CANMessage(0x350, &counter, 1));
     counter++;
-    /*can1.write(CANMessage(0x351, &counter, 1));
+    can1.write(CANMessage(0x351, &counter, 1));
     counter++;
-    /*can1.write(CANMessage(0x352, &counter, 1));
-    counter++;*/
+    can1.write(CANMessage(0x352, &counter, 1));
+    counter++;
 }
 
 void send2() {
     static char counter = 0; // use for fake data
     can2.write(CANMessage(0x351, &counter, 1));
     counter++;
-    /*can2.write(CANMessage(0x351, &counter, 1));
+    can2.write(CANMessage(0x351, &counter, 1));
     counter++;
     can2.write(CANMessage(0x352, &counter, 1));
-    counter++;*/
+    counter++;
 }
 
 void recieve1() {
@@ -220,14 +224,7 @@
         }
         lastMsg1=msg1.data[0];
         if(logOpen) {
-            if((PB1==0)||(CD==0)) {
-                logOpen=false;
-                pc.printf("Stopping log\n\n");
-                wait(1.1);
-                readLog();
-            } else {
-                logCan(1, msg1);
-            }
+            logCan(1, msg1);
         }
         led1 = !led1;
     }
@@ -244,14 +241,7 @@
         }
         lastMsg2=msg2.data[0];
         if(logOpen) {
-            if((PB1==0)||(CD==0)) {
-                logOpen=false;
-                pc.printf("Stopping log\n\n");
-                wait(1.1);
-                readLog();
-            } else {
-                logCan(2, msg2);
-            }
+            logCan(2, msg2);
         }
         led2 = !led2;
     }
@@ -309,9 +299,16 @@
         pc.printf("\nNo SDRAM Inserted.\n\n\n\n");
     }
 
-
-    //.02 logs all; .01 misses some when logging;
-    //.05 gets all when not logging;.02 misses some always
     ticker1.attach(&send1, .0052); //start sending messages
     ticker2.attach(&send2, .0051); //start sending messages
+    
+    while ((PB1==1)&&(CD==1)){
+        wait(0.25); // Keep logging until button pushed or SDRAM removed
+    }
+    ticker1.detach();
+    ticker2.detach();
+    logOpen=false;
+    pc.printf("Stopping log\n\n");
+    wait(1);
+    readLog();
 }
\ No newline at end of file