Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Revision:
4:b962f5a783a1
Parent:
0:cd1fe4f0ed39
Child:
5:7c2e7d657716
Child:
7:33a74adff0ff
--- a/main.cpp	Thu May 08 20:01:10 2014 +0000
+++ b/main.cpp	Mon May 12 18:52:45 2014 +0000
@@ -13,7 +13,7 @@
 // Display
 #ifdef OLED_DEBUG
 SPI spi0(P0_9, NC, P0_10); // mosi, miso, sclk
-Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // MOSI, MISO, SCLK SPI0
+Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS
 #endif
 
 // MPU
@@ -61,7 +61,7 @@
 // Forward declarations
 void die(int flash_rate_s);
 bool log_open();
-void log_acceleration(VectorInt16 data);
+void log_data(VectorInt16 data, Quaternion q);
 void log_close();
 void mpu_init();
 void get_data();
@@ -71,15 +71,29 @@
     mpuInterrupt = true;
 }
 
+//void captureSwitchISR() {
+//    // used for debouncing
+//    static int prev_time = 0;
+//
+//    if (totalTime.read_ms() - prev_time < 200)
+//        return;
+//        
+//    State = (State == IDLE) ? CAPTURE : IDLE;
+//    prev_time = totalTime.read_ms();
+//}
+
 void captureSwitchISR() {
     // used for debouncing
     static int prev_time = 0;
-
-    if (totalTime.read_ms() - prev_time < 200)
-        return;
+    int curr_time = totalTime.read_ms();
+    
+    // Only change state after an amount of time
+    // Note: abs value is necessary in case of 
+    //   overflows
+    if (abs(curr_time - prev_time) > 200)
+        State = (State == IDLE) ? CAPTURE : IDLE;
         
-    State = (State == IDLE) ? CAPTURE : IDLE;
-    prev_time = totalTime.read_ms();
+    prev_time = curr_time;
 }
 
 int main(void)
@@ -118,6 +132,7 @@
             
             log_close();
             captureTime.stop();
+            captureTime.reset();
         }
     
         PC_PRINTLN("Idling...");
@@ -170,6 +185,7 @@
 
 /* Requires the log to be open and mpu to be initialized*/
 void get_data() {
+    static uint32_t n_overflows = 0;
     //while (true) {
         //if (!dmpReady) break;   // do nothing if dmp not ready
         if (!dmpReady) return;
@@ -186,6 +202,7 @@
         // check for overflow (this should never happen unless our code is too inefficient)
         if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
             PC_PRINTLNF("**** FIFO OVERFLOW @ %d ms ****", captureTime.read_ms());
+            n_overflows++;
             // reset so we can continue cleanly
             mpu.resetFIFO();
             // otherwise, check for DMP data ready interrupt (this should happen frequently)
@@ -200,19 +217,19 @@
             // Get acceleration data
             mpu.dmpGetAccel(&aa, fifoBuffer);
             mpu.dmpGetQuaternion(&q, fifoBuffer);
-            mpu.dmpGetGravity(&gravity, &q);
-            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
-            mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
+//            mpu.dmpGetGravity(&gravity, &q);
+//            mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
+//            mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
             
             PC_PRINTF("%d, ", aaWorld.x); PC_PRINTF("%d, ", aaWorld.y); PC_PRINTLNF("%d", aaWorld.z);
             
 //            OLED_SETCURS(0, 10); OLED_PRINTF("%d, ", aaWorld.x); OLED_PRINTF("%d, ", aaWorld.y); OLED_PRINTLNF("%d", aaWorld.z);
             
-            log_acceleration(aaWorld);
+            fprintf(logFile, "%u,%d,%d,%d,%f,%f,%f,%f,%u\n", captureTime.read_ms(), aa.x, aa.y, aa.z, q.x, q.y, q.z, q.w, n_overflows);
         //}
     }
 }
 
-void log_acceleration(VectorInt16 data) {
-    fprintf(logFile, "%d, %d,%d,%d\n", captureTime.read_ms(), data.x, data.y, data.z);
+void log_data(VectorInt16 data, Quaternion q) {
+    fprintf(logFile, "%d,%d,%d,%d,%f,%f,%f,%f\n", captureTime.read_ms(), data.x, data.y, data.z, q.x, q.y, q.z, q.w);
 }
\ No newline at end of file