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:
paulbartell
Date:
Wed May 28 22:56:25 2014 +0000
Parent:
12:bf282a100fbc
Child:
14:006d9087d76c
Commit message:
Started bluetooth functionality.

Changed in this revision

DS3231.lib 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
log_data.h 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
sync.cpp Show annotated file Show diff for this revision Revisions of this file
sync.h Show annotated file Show diff for this revision Revisions of this file
--- a/DS3231.lib	Wed May 28 20:28:14 2014 +0000
+++ b/DS3231.lib	Wed May 28 22:56:25 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/CSE477/code/DS3231/#b5736355af3a
+http://mbed.org/teams/CSE477/code/DS3231/#71aad49938bc
--- a/log_data.cpp	Wed May 28 20:28:14 2014 +0000
+++ b/log_data.cpp	Wed May 28 22:56:25 2014 +0000
@@ -3,19 +3,25 @@
 #include "SDFileSystem.h"
 #include "helper_3dmath.h"
 #include "debug.h"
+#include "pins.h"
 
 // SD Card
 SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1
 
-DS3231 rtc(I2C_SDA, I2C_SCL);
+
 
 // Logging vars
 FILE *accelFile;
 FILE *splitFile;
 
 /* Returns true if logging was successfully initialized, false otherwise */
-bool log_init() {
-    accelFile = fopen(ACCEL_LOG, "a");
+bool log_init(char* timestamp) {
+    char dataLog[33] = "/sd/00-00-0000 00:00:00 data.log";
+    if(true) // TODO: checkIf RTC is initialized properly
+    {
+        strncpy(&dataLog[4],timestamp,19);
+    }
+    accelFile = fopen(dataLog, "a");
     if (accelFile == NULL) {
         PC_PRINTLNF("SD card initialization error: Failed to open %s", ACCEL_LOG);
         DIE(SD_ERROR_RATE);
--- a/log_data.h	Wed May 28 20:28:14 2014 +0000
+++ b/log_data.h	Wed May 28 22:56:25 2014 +0000
@@ -5,7 +5,7 @@
 #define ACCEL_LOG "/sd/data.log"
 #define SPLIT_LOG "/sd/splits.log"
 
-bool log_init();
+bool log_init(char* timestamp);
 bool log_data(VectorInt16 *data);
 bool log_data(uint16_t split);
 bool log_close();
--- a/main.cpp	Wed May 28 20:28:14 2014 +0000
+++ b/main.cpp	Wed May 28 22:56:25 2014 +0000
@@ -10,6 +10,9 @@
 #include "debug.h"
 #include "SystemTime.h"
 #include "sync.h"
+#include "pins.h"
+#include "DS3231.h"
+
 
 // Capture button stuff
 #define PIN_DETECT_SAMPLE_PERIOD_uS 20000   // 20 ms sample period
@@ -21,6 +24,8 @@
 Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS
 #endif
 
+DS3231 rtc(I2C_SDA, I2C_SCL);
+
 // Mode button
 PinDetect captureButton(P0_16, PullUp);
 
@@ -77,7 +82,7 @@
         } else if (State == CAPTURE) {
             OLED_PRINTP("Starting capture...", 0, 0);
             OLED_PRINTP("Init SD card...", 0, 10);
-            log_init();
+            log_init(rtc.getTimestamp());
             OLED_PRINTP("Init peak detect...", 0, 10);
             process_init();
             OLED_PRINTP("Init data receipt...", 0, 10);
--- a/sync.cpp	Wed May 28 20:28:14 2014 +0000
+++ b/sync.cpp	Wed May 28 22:56:25 2014 +0000
@@ -1,6 +1,13 @@
 #include "mbed.h"
 #include "sync.h"
+#include "DS3231.h"
+#include "Timeout.h"
+enum state {IDLE, CAPTURE, SYNC};
+extern enum state State;
+extern DS3231 rtc;
+Timeout t;
 
+char buff[513];
 /*
     PACKET STRUCTURE:
     
@@ -12,28 +19,115 @@
         
     Byte | Description
     ------------------
-    0x0  | ACK        
-    0x1  | NACK         
+    0x1  | ACK        
+    0x0  | NACK         
     0x2  | Delete All  
     0x4  | Receive file 
 */
 
-Serial bt(P0_19, P0_18); // tx, rx
+RawSerial bt(P0_19, P0_18); // tx, rx
+
+uint16_t packetSeq = 0;
 
 bool sync_init() {
     bt.baud(115200);
     return true;
 }
 
-unsigned char get_command() {
+
+struct responsePacket {
+    char cmd;
+    uint16_t len;
+    char data;
+};
+
+void sendResponse(char cmd, char resp)
+{
+    
+    struct responsePacket rp = {
+        cmd,
+        4,
+        resp};
+        
+    bt.puts((char*)&rp);
+}
+
+
+uint16_t getLen()
+{
+    union Lu
+    {
+        int16_t len;
+        char dat[2];
+    };
+    union Lu lu;
+    lu.dat[0] = bt.getc();
+    lu.dat[1] = bt.getc();
+    
+    return lu.len;
+}
+
+void setRtc()
+{
+    int dayOfWeek=0, date, month, year, hours, minutes, seconds;
+    int16_t len;
+    len = getLen();
+    int i = 0;
+    for(i = 0; i < len; i++)
+    {
+        buff[i] = bt.getc();
+    }
+    buff[i] = 0; // end the string with a zero
+    sscanf(buff, "%04d-%02d-%02d %02d:%02d:%02d",&year,&month,&date,&hours,&minutes,&seconds);
+    rtc.setDate(dayOfWeek, date, month, year);
+    rtc.setTime(hours, minutes, seconds);
+    sendResponse(CMD_RTCSET, ACK);
+    
+}
+void listSessions()
+{
+    
+}
+
+void syncSession()
+{
+    
+    
+}
+
+void deleteSession()
+{
+    
     
 }
 
 void sync() {
-    if(bt.readable()) {
-        bt.putc(bt.getc());
-        bt.putc('\n');
+    while(State == SYNC)
+    {
+        if(bt.readable()) // get the latest byte available
+        {
+            buff[0] = bt.getc();
+            
+            switch(buff[0])
+            {
+                case CMD_RTCSET:
+                    setRtc();
+                    break;
+                case CMD_LISTSESSIONS:
+                    listSessions();
+                    break;
+                case CMD_SYNCSESSION:
+                    syncSession();
+                    break;
+                case CMD_DELETESESSION:
+                    deleteSession();
+                    break;
+                case CMD_DONE:
+                    bt.putc(CMD_DONE);
+                    sendResponse(CMD_DONE, ACK);
+                    State = IDLE;
+                    break;
+            }
+        }
     }
-    
-    unsigned char cmd = get_command();
 }
\ No newline at end of file
--- a/sync.h	Wed May 28 20:28:14 2014 +0000
+++ b/sync.h	Wed May 28 22:56:25 2014 +0000
@@ -4,4 +4,13 @@
 bool sync_init();
 void sync();
 
+#define CMD_RTCSET 0x01
+#define CMD_LISTSESSIONS 0x02
+#define CMD_SYNCSESSION 0x03
+#define CMD_DELETESESSION 0x04
+#define CMD_DONE 0x05
+#define ACK 0x06
+#define NACK 0x07
+
+
 #endif // _SYNC_H
\ No newline at end of file