Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
paulbartell
Date:
Wed May 28 22:56:25 2014 +0000
Revision:
13:227a6cfd2097
Parent:
9:a711b5b34d73
Child:
14:006d9087d76c
Started bluetooth functionality.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellingjp 9:a711b5b34d73 1 #include "mbed.h"
ellingjp 9:a711b5b34d73 2 #include "sync.h"
paulbartell 13:227a6cfd2097 3 #include "DS3231.h"
paulbartell 13:227a6cfd2097 4 #include "Timeout.h"
paulbartell 13:227a6cfd2097 5 enum state {IDLE, CAPTURE, SYNC};
paulbartell 13:227a6cfd2097 6 extern enum state State;
paulbartell 13:227a6cfd2097 7 extern DS3231 rtc;
paulbartell 13:227a6cfd2097 8 Timeout t;
ellingjp 9:a711b5b34d73 9
paulbartell 13:227a6cfd2097 10 char buff[513];
ellingjp 9:a711b5b34d73 11 /*
ellingjp 9:a711b5b34d73 12 PACKET STRUCTURE:
ellingjp 9:a711b5b34d73 13
ellingjp 9:a711b5b34d73 14 Command:
ellingjp 9:a711b5b34d73 15 [ 1 CMD | 17 SESSION_ID (optional) ]
ellingjp 9:a711b5b34d73 16
ellingjp 9:a711b5b34d73 17 Response:
ellingjp 9:a711b5b34d73 18 [ 1 ACK/NACK | X Data (optional) ]
ellingjp 9:a711b5b34d73 19
ellingjp 9:a711b5b34d73 20 Byte | Description
ellingjp 9:a711b5b34d73 21 ------------------
paulbartell 13:227a6cfd2097 22 0x1 | ACK
paulbartell 13:227a6cfd2097 23 0x0 | NACK
ellingjp 9:a711b5b34d73 24 0x2 | Delete All
ellingjp 9:a711b5b34d73 25 0x4 | Receive file
ellingjp 9:a711b5b34d73 26 */
ellingjp 9:a711b5b34d73 27
paulbartell 13:227a6cfd2097 28 RawSerial bt(P0_19, P0_18); // tx, rx
paulbartell 13:227a6cfd2097 29
paulbartell 13:227a6cfd2097 30 uint16_t packetSeq = 0;
ellingjp 9:a711b5b34d73 31
ellingjp 9:a711b5b34d73 32 bool sync_init() {
ellingjp 9:a711b5b34d73 33 bt.baud(115200);
ellingjp 9:a711b5b34d73 34 return true;
ellingjp 9:a711b5b34d73 35 }
ellingjp 9:a711b5b34d73 36
paulbartell 13:227a6cfd2097 37
paulbartell 13:227a6cfd2097 38 struct responsePacket {
paulbartell 13:227a6cfd2097 39 char cmd;
paulbartell 13:227a6cfd2097 40 uint16_t len;
paulbartell 13:227a6cfd2097 41 char data;
paulbartell 13:227a6cfd2097 42 };
paulbartell 13:227a6cfd2097 43
paulbartell 13:227a6cfd2097 44 void sendResponse(char cmd, char resp)
paulbartell 13:227a6cfd2097 45 {
paulbartell 13:227a6cfd2097 46
paulbartell 13:227a6cfd2097 47 struct responsePacket rp = {
paulbartell 13:227a6cfd2097 48 cmd,
paulbartell 13:227a6cfd2097 49 4,
paulbartell 13:227a6cfd2097 50 resp};
paulbartell 13:227a6cfd2097 51
paulbartell 13:227a6cfd2097 52 bt.puts((char*)&rp);
paulbartell 13:227a6cfd2097 53 }
paulbartell 13:227a6cfd2097 54
paulbartell 13:227a6cfd2097 55
paulbartell 13:227a6cfd2097 56 uint16_t getLen()
paulbartell 13:227a6cfd2097 57 {
paulbartell 13:227a6cfd2097 58 union Lu
paulbartell 13:227a6cfd2097 59 {
paulbartell 13:227a6cfd2097 60 int16_t len;
paulbartell 13:227a6cfd2097 61 char dat[2];
paulbartell 13:227a6cfd2097 62 };
paulbartell 13:227a6cfd2097 63 union Lu lu;
paulbartell 13:227a6cfd2097 64 lu.dat[0] = bt.getc();
paulbartell 13:227a6cfd2097 65 lu.dat[1] = bt.getc();
paulbartell 13:227a6cfd2097 66
paulbartell 13:227a6cfd2097 67 return lu.len;
paulbartell 13:227a6cfd2097 68 }
paulbartell 13:227a6cfd2097 69
paulbartell 13:227a6cfd2097 70 void setRtc()
paulbartell 13:227a6cfd2097 71 {
paulbartell 13:227a6cfd2097 72 int dayOfWeek=0, date, month, year, hours, minutes, seconds;
paulbartell 13:227a6cfd2097 73 int16_t len;
paulbartell 13:227a6cfd2097 74 len = getLen();
paulbartell 13:227a6cfd2097 75 int i = 0;
paulbartell 13:227a6cfd2097 76 for(i = 0; i < len; i++)
paulbartell 13:227a6cfd2097 77 {
paulbartell 13:227a6cfd2097 78 buff[i] = bt.getc();
paulbartell 13:227a6cfd2097 79 }
paulbartell 13:227a6cfd2097 80 buff[i] = 0; // end the string with a zero
paulbartell 13:227a6cfd2097 81 sscanf(buff, "%04d-%02d-%02d %02d:%02d:%02d",&year,&month,&date,&hours,&minutes,&seconds);
paulbartell 13:227a6cfd2097 82 rtc.setDate(dayOfWeek, date, month, year);
paulbartell 13:227a6cfd2097 83 rtc.setTime(hours, minutes, seconds);
paulbartell 13:227a6cfd2097 84 sendResponse(CMD_RTCSET, ACK);
paulbartell 13:227a6cfd2097 85
paulbartell 13:227a6cfd2097 86 }
paulbartell 13:227a6cfd2097 87 void listSessions()
paulbartell 13:227a6cfd2097 88 {
paulbartell 13:227a6cfd2097 89
paulbartell 13:227a6cfd2097 90 }
paulbartell 13:227a6cfd2097 91
paulbartell 13:227a6cfd2097 92 void syncSession()
paulbartell 13:227a6cfd2097 93 {
paulbartell 13:227a6cfd2097 94
paulbartell 13:227a6cfd2097 95
paulbartell 13:227a6cfd2097 96 }
paulbartell 13:227a6cfd2097 97
paulbartell 13:227a6cfd2097 98 void deleteSession()
paulbartell 13:227a6cfd2097 99 {
paulbartell 13:227a6cfd2097 100
ellingjp 9:a711b5b34d73 101
ellingjp 9:a711b5b34d73 102 }
ellingjp 9:a711b5b34d73 103
ellingjp 9:a711b5b34d73 104 void sync() {
paulbartell 13:227a6cfd2097 105 while(State == SYNC)
paulbartell 13:227a6cfd2097 106 {
paulbartell 13:227a6cfd2097 107 if(bt.readable()) // get the latest byte available
paulbartell 13:227a6cfd2097 108 {
paulbartell 13:227a6cfd2097 109 buff[0] = bt.getc();
paulbartell 13:227a6cfd2097 110
paulbartell 13:227a6cfd2097 111 switch(buff[0])
paulbartell 13:227a6cfd2097 112 {
paulbartell 13:227a6cfd2097 113 case CMD_RTCSET:
paulbartell 13:227a6cfd2097 114 setRtc();
paulbartell 13:227a6cfd2097 115 break;
paulbartell 13:227a6cfd2097 116 case CMD_LISTSESSIONS:
paulbartell 13:227a6cfd2097 117 listSessions();
paulbartell 13:227a6cfd2097 118 break;
paulbartell 13:227a6cfd2097 119 case CMD_SYNCSESSION:
paulbartell 13:227a6cfd2097 120 syncSession();
paulbartell 13:227a6cfd2097 121 break;
paulbartell 13:227a6cfd2097 122 case CMD_DELETESESSION:
paulbartell 13:227a6cfd2097 123 deleteSession();
paulbartell 13:227a6cfd2097 124 break;
paulbartell 13:227a6cfd2097 125 case CMD_DONE:
paulbartell 13:227a6cfd2097 126 bt.putc(CMD_DONE);
paulbartell 13:227a6cfd2097 127 sendResponse(CMD_DONE, ACK);
paulbartell 13:227a6cfd2097 128 State = IDLE;
paulbartell 13:227a6cfd2097 129 break;
paulbartell 13:227a6cfd2097 130 }
paulbartell 13:227a6cfd2097 131 }
ellingjp 9:a711b5b34d73 132 }
ellingjp 9:a711b5b34d73 133 }