Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

sync.cpp

Committer:
paulbartell
Date:
2014-05-28
Revision:
13:227a6cfd2097
Parent:
9:a711b5b34d73
Child:
14:006d9087d76c

File content as of revision 13:227a6cfd2097:

#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:
    
    Command:
        [ 1 CMD | 17 SESSION_ID (optional) ]
    
    Response:
        [ 1 ACK/NACK | X Data (optional) ]
        
    Byte | Description
    ------------------
    0x1  | ACK        
    0x0  | NACK         
    0x2  | Delete All  
    0x4  | Receive file 
*/

RawSerial bt(P0_19, P0_18); // tx, rx

uint16_t packetSeq = 0;

bool sync_init() {
    bt.baud(115200);
    return true;
}


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() {
    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;
            }
        }
    }
}