Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Revision:
13:227a6cfd2097
Parent:
9:a711b5b34d73
Child:
14:006d9087d76c
--- 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