Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Files at this revision

API Documentation at this revision

Comitter:
ellingjp
Date:
Mon Jun 09 04:55:16 2014 +0000
Parent:
23:80083138d609
Commit message:
Using RTC filenames

Changed in this revision

RealtimeMath.lib Show annotated file Show diff for this revision Revisions of this file
debug.h Show annotated file Show diff for this revision Revisions of this file
log_data.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
process_data.cpp Show annotated file Show diff for this revision Revisions of this file
receive_data.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/RealtimeMath.lib	Sun Jun 08 04:08:41 2014 +0000
+++ b/RealtimeMath.lib	Mon Jun 09 04:55:16 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/ellingjp/code/RealtimeMath/#922b711e7a4b
\ No newline at end of file
+http://mbed.org/users/ellingjp/code/RealtimeMath/#922b711e7a4b
--- a/debug.h	Sun Jun 08 04:08:41 2014 +0000
+++ b/debug.h	Mon Jun 09 04:55:16 2014 +0000
@@ -7,7 +7,7 @@
 #define DMP_ERROR_RATE 1
 #define SD_ERROR_RATE 2
 
-//#define NDEBUG
+#define NDEBUG
 #ifndef NDEBUG
     /************ On-board LED Access      ****************/
     extern DigitalOut led;
--- a/log_data.cpp	Sun Jun 08 04:08:41 2014 +0000
+++ b/log_data.cpp	Mon Jun 09 04:55:16 2014 +0000
@@ -5,6 +5,7 @@
 #include "debug.h"
 #include "SystemTime.h"
 #include "pins.h"
+#include "main.h"
 
 // SD Card
 SDFileSystem sd(P0_9, P0_8, P0_10, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1
@@ -13,28 +14,28 @@
 
 // Logging vars
 FILE *accelFile;
-FILE *peakFile;
+//FILE *peakFile;
 
 /* Returns true if logging was successfully initialized, false otherwise */
 bool log_init() {
     PC_PRINTLN("Initializing logging...");
 
-    accelFile = fopen(ACCEL_LOG, "a");
+    char filename[32];
+    // year_month_day_hour_min_sec.log
+    int year, month, day, hour, min, sec;
+    rtc.readDate(&day, &month, &year);
+    rtc.readTime(&hour, &min, &sec);
+    sprintf(filename, "/sd/%04d_%02d_%02d_%02d_%02d_%02d.log", year, month, day, hour, min, sec);
+    
+    PC_PRINTLNF("Trying to open %s...", filename);
+
+    accelFile = fopen(filename, "w");
     if (accelFile == NULL) {
-        PC_PRINTLNF("SD card initialization error: Failed to open %s", ACCEL_LOG);
+        PC_PRINTLNF("SD card initialization error: Failed to open %s", filename);
         DIE(SD_ERROR_RATE);
         return false;
     }
     
-    peakFile = fopen(PEAK_LOG, "a");
-    if (peakFile == NULL) {
-        PC_PRINTLNF("SD card initialization error: Failed to open %s", SPLIT_LOG);
-        DIE(SD_ERROR_RATE);
-        return false;
-    }
-
-//    fprintf(accelFile, "---- BEGIN NEW DATASET ----\n");
-//    fprintf(peakFile, "---- BEGIN NEW DATASET ----\n");
     return true;
 }
 
@@ -51,18 +52,18 @@
     return false;
 }
 
-/* Returns true if data was successfully logged, false otherwise 
-   Used for logging split times */
-bool log_data(int time_ms, int split) {
-    if (peakFile != NULL) {
-//        fprintf(peakFile, "%d, %d\n", time_ms, split);
-        fwrite( (void*) &time_ms, sizeof(int), 1, peakFile);
-        fwrite( (void*) &split, sizeof(int), 1, peakFile);
-        return true;
-    }
-    
-    return false;
-}
+///* Returns true if data was successfully logged, false otherwise 
+//   Used for logging split times */
+//bool log_data(int time_ms, int split) {
+//    if (peakFile != NULL) {
+////        fprintf(peakFile, "%d, %d\n", time_ms, split);
+//        fwrite( (void*) &time_ms, sizeof(int), 1, peakFile);
+//        fwrite( (void*) &split, sizeof(int), 1, peakFile);
+//        return true;
+//    }
+//    
+//    return false;
+//}
 
 /* Returns true if logging was successfully closed, false otherwise */
 bool log_close() {
@@ -70,7 +71,7 @@
 //        return ( (fclose(accelFile) == 0) && (fclose(splitFile) == 0) );
     
     fclose(accelFile);
-    fclose(peakFile);
+//    fclose(peakFile);
     
     return true;
 }
\ No newline at end of file
--- a/main.cpp	Sun Jun 08 04:08:41 2014 +0000
+++ b/main.cpp	Mon Jun 09 04:55:16 2014 +0000
@@ -1,6 +1,3 @@
-#define NDEBUG
-#define USE_OLED
-
 #include "main.h"
 #include "mbed.h"
 #include "PinDetect.h"
@@ -65,8 +62,21 @@
         State = IDLE;
 }
 
+void printSplit(int split)
+{
+    int min = split / 60000;
+    int sec = (split / 1000) % 60;
+    int hund = (split / 10) % 100;
+
+    OLED_PRINTPF("%1d", min, X_POS(0), Y_POS(20));
+    OLED_PRINTPF("%02d", sec, X_POS(5), Y_POS(20));
+    OLED_DRAWPIXEL(X_POS(14), Y_POS(24), 0x1);
+    OLED_PRINTPF("%02d", hund, X_POS(15), Y_POS(20));
+}
+
 int main(void)
 {
+
     SystemTime::start();
 
     State = IDLE;
@@ -82,41 +92,33 @@
     VectorInt16 *data;
     while (true) {
         if (State == IDLE){
-            OLED_CLEAR();
+            PC_PRINTLN("Idling...");
             OLED_PRINTP("Idling...", X_POS(0), Y_POS(20));
-            PC_PRINTLN("Idling...");
-                
+            OLED_CLEAR();
         } else if (State == CAPTURE) {
-            OLED_PRINTP("Starting capture...", X_POS(0), Y_POS(20));
-//            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);
+            PC_PRINTLN("Starting capture...");
+            PC_PRINTLN("Init SD card...");
+            log_init();
+            PC_PRINTLN("Init peak detect...");
+            process_init();
+            PC_PRINTLN("Init data receipt...");
+            receive_init();           
+            PC_PRINTLN("Capturing data...");
             
             captureTimer.start();
+            int split = 0;
+            printSplit(split);
             while (State == CAPTURE) {
                 data = receive_data();
-                PC_PRINTF("x: %d ", data->x);
-                PC_PRINTF("y: %d ", data->y);
-                PC_PRINTF("z: %d ", data->z);
-//                log_data(captureTimer.read_ms(), data);
+//                PC_PRINTLNF("x: %d ", data->x);
+//                PC_PRINTLNF("y: %d ", data->y);
+//                PC_PRINTLNF("z: %d ", data->z);
+                log_data(captureTimer.read_ms(), data);
 
-                int split;
                 if (process_data((int) (data->x), (int) (data->y), &split)) {
-                    PC_PRINTLNF("Peak time: %d", split);
-                    
-                    int min = 47; //split / 60000;
-                    int sec = 32; //(split / 1000) % 60;
-                    int hund = 9; //(split / 10) % 100;
-                    
-                    OLED_PRINTPF("%1d", min, 0, 40);
-                    OLED_PRINTPF("%02d", sec, 5, 40);
-                    OLED_DRAWPIXEL(14, 44, 0x1);
-                    OLED_PRINTPF("%02d", hund, 15, 40);
+                    PC_PRINTLNF("Split time: %d", split);
+                    printSplit(split);
+
 //                    log_data(captureTimer.read_ms(), split);
                 }
             }
@@ -124,7 +126,7 @@
             captureTimer.reset();
             receive_close();
             process_close();
-//            log_close();
+            log_close();
         } else if (State == SYNC) {
             OLED_PRINTP("Ready to sync...", 0, 0);
             sync_init();
--- a/main.h	Sun Jun 08 04:08:41 2014 +0000
+++ b/main.h	Mon Jun 09 04:55:16 2014 +0000
@@ -5,6 +5,7 @@
 #define DEBOUNCE_TIME_MS 200
 
 #include "Adafruit_SSD1306.h"
+#include "DS3231.h"
 
 #define USE_OLED
 #ifdef USE_OLED
@@ -15,7 +16,7 @@
     #define OLED_SETCURS(xpos,ypos) oled.setCursor(xpos,ypos);
     #define OLED_CLEAR() oled.clearDisplay(); oled.display();
     
-    #define OLED_DRAWPIXEL(x,y,color) oled.drawPixel(x, y, color);
+    #define OLED_DRAWPIXEL(x,y,color) oled.drawPixel(x, y, color); oled.display();
     
     #define OLED_PRINT(x) oled.printf("%s", x); oled.display();
     #define OLED_PRINTF(x,y) oled.printf(x, y); oled.display();
@@ -53,4 +54,7 @@
     #define OLED_PRINTPFR(x,y,xpos,ypos)
 #endif
 
+extern DS3231 rtc;
+
+
 #endif // _MAIN_H
\ No newline at end of file
--- a/process_data.cpp	Sun Jun 08 04:08:41 2014 +0000
+++ b/process_data.cpp	Mon Jun 09 04:55:16 2014 +0000
@@ -7,6 +7,7 @@
 #include "FloatingThresholdPeakDetector.h"
 #include "SimplePeakDetector.h"
 #include "main.h"
+#include "debug.h"
 
 PeakDetector::PeakDetector *peakDetector;
 PeakDetector::PeakDetector *startDetector;
@@ -36,13 +37,19 @@
             swimState = TIMING;
             split_timer.start();
             start_time = SystemTime::read_ms();
-            OLED_PRINTPF("xpeak - %d     ", start_time, 0, 30);
+            PC_PRINTLNF("xpeak - %d", start_time);
         }
     } else if (swimState == TIMING) {
+        // Potential source of overflow if read_ms rolls over
         if (peakDetector->onPeak(ydata) && SystemTime::read_ms() - start_time > 5000 ) {
-            *split = split_timer.read_ms();
-            OLED_PRINTPF("ypeak - %d     ", *split, 0, 40);
-            return true;
+            PC_PRINTLNF("ypeak - %d", split_timer.read_ms());
+            static int i = 0;
+
+            if (++i % 2 == 0) {
+                *split = split_timer.read_ms();
+                split_timer.reset();
+                return true;
+            }
         }
     }
     
--- a/receive_data.cpp	Sun Jun 08 04:08:41 2014 +0000
+++ b/receive_data.cpp	Mon Jun 09 04:55:16 2014 +0000
@@ -80,6 +80,7 @@
 bool receive_init() {
     PC_PRINTLN("Initializing MPU");
     mpu.initialize();
+    PC_PRINTLN("MPU initialized, initializing DMP");
     devStatus = mpu.dmpInitialize();
     
     if (devStatus == 0) {