Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Revision:
8:8430a5c0914c
Parent:
7:33a74adff0ff
Child:
9:a711b5b34d73
--- a/main.cpp	Sat May 17 01:20:31 2014 +0000
+++ b/main.cpp	Tue May 20 00:43:55 2014 +0000
@@ -1,87 +1,105 @@
 #include "main.h"
 #include "mbed.h"
+#include "PinDetect.h"
 #include "USBSerial.h"
 #include "Adafruit_SSD1306.h"
 #include "SDFileSystem.h"
 #include "receive_data.h"
+#include "log_data.h"
+#include "process_data.h"
 #include "debug.h"
+#include "SystemTime.h"
 
+// Capture button stuff
+#define PIN_DETECT_SAMPLE_PERIOD_uS 20000   // 20 ms sample period
+#define SYNC_HELD_SAMPLES   (SYNC_HOLD_TIME_MS * 1000 / PIN_DETECT_SAMPLE_PERIOD_uS)
+
+#ifdef USE_OLED
 // Display
-#ifdef OLED_DEBUG
 SPI spi0(P0_9, NC, P0_10); // mosi, miso, sclk
 Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS
 #endif
 
-// SD Card
-SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1
-
-// Logging vars
-FILE *logFile;
-
-// Timer
-Timer totalTime;
-Timer captureTime;
-
-// Switch
-InterruptIn captureSwitch(P0_16);
-
+// Mode button
+PinDetect captureButton(P0_16, PullUp);
 
 // State
-enum state {IDLE, CAPTURE};
+enum state {IDLE, CAPTURE, SYNC};
 enum state State;
 
-void captureSwitchISR() {
-    // used for debouncing
-    static int prev_time = 0;
-    int curr_time = totalTime.read_ms();
+// This is used to ensure the rising edge after a hold is ignored
+bool ignore_edge = false;
+void buttonHeld() {
+    if (State == IDLE)
+        State = SYNC;
+    else
+        State = IDLE;
     
-    // 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;
-        
-    prev_time = curr_time;
+    // Button was held down, so don't count the next assert
+    ignore_edge = true;
+}
+
+void buttonPressed() {
+    // Don't ignore subsequent edges
+    if (ignore_edge) {
+        ignore_edge = false;
+        return;
+    }
+    
+    if (State == IDLE)
+        State = CAPTURE;
+    else if (State == CAPTURE)
+        State = IDLE;
 }
 
 int main(void)
 {
-    totalTime.start();
+    SystemTime::start();
     
     State = IDLE;
-    captureSwitch.mode(PullUp);
-    captureSwitch.rise(captureSwitchISR);   
     
+    // After button is held, the next rising edge will call buttonPressed.
+    //   Because of this, inside the callbacks need to ensure that edge
+    //   does not do anything unintended.
+    captureButton.attach_deasserted_held(buttonHeld);
+    captureButton.attach_asserted(buttonPressed);
+    captureButton.setSampleFrequency();
+    captureButton.setSamplesTillHeld(SYNC_HELD_SAMPLES);
+
+    
+    VectorInt16 *data;
     while (true) {
         if (State == IDLE){
-            PC_PRINT("Idling\r");
+            OLED_CLEAR();
+            OLED_PRINTP("Idling...", 0, 0);
+            PC_PRINTLN("Idling...");
         } else if (State == CAPTURE) {
-            receive_init();
+            OLED_PRINTP("Starting capture...", 0, 0);
+            OLED_PRINTP("Init SD card...", 0, 10);
+            log_init();
+            OLED_PRINTP("Init peak detect...", 0, 10);
+            process_init();
+            OLED_PRINTP("Init data receipt...", 0, 10);
+            receive_init();           
+            OLED_CLEAR();
+            OLED_PRINTP("Capturing data...", 0, 0);
+            
+            
             while (State == CAPTURE) {
-                __disable_irq();
-                receive_data();
-                __enable_irq();
+                data = receive_data();
+                log_data(data);
+                int split = process_data(data->x);
+                if (split != UINT16_MAX) {
+                    PC_PRINTLNF("Split time: %d", split);
+                    OLED_PRINTPF("Split: %d", split, 0, 40);
+                }
             }
-            // data = receive_data();
-            // log_data(data);
-            // split = get_split(data);
-            // display_split;
-        } 
-        // else if (State == SYNC) {
-        // begin_sync();
-        // }
+            
+            receive_close();
+            process_close();
+            log_close();
+        } else if (State == SYNC) {
+            OLED_PRINTP("Ready to sync...", 0, 0);
+        }
     }
 }
-
-
-
-/* Returns false on failure, true otherwise */
-//bool log_open() {
-//    logFile = fopen(LOG_FILE, "a");
-//    if (logFile == NULL) {
-//        PC_PRINTLNF("SD card initialization error: Failed to open %s", LOG_FILE);
-//        return false;
-//    }
-//    fprintf(logFile, "---- BEGIN NEW DATASET ----\n");
-//    return true;
-//}
\ No newline at end of file