Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL
sync.cpp@14:006d9087d76c, 2014-05-28 (annotated)
- Committer:
- paulbartell
- Date:
- Wed May 28 23:15:05 2014 +0000
- Revision:
- 14:006d9087d76c
- Parent:
- 13:227a6cfd2097
- Child:
- 18:06b718f8e6fd
started working on listSessions piece.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 14:006d9087d76c | 5 | #include "SDFileSystem.h" |
| paulbartell | 13:227a6cfd2097 | 6 | enum state {IDLE, CAPTURE, SYNC}; |
| paulbartell | 13:227a6cfd2097 | 7 | extern enum state State; |
| paulbartell | 13:227a6cfd2097 | 8 | extern DS3231 rtc; |
| paulbartell | 13:227a6cfd2097 | 9 | Timeout t; |
| ellingjp | 9:a711b5b34d73 | 10 | |
| paulbartell | 13:227a6cfd2097 | 11 | char buff[513]; |
| ellingjp | 9:a711b5b34d73 | 12 | /* |
| ellingjp | 9:a711b5b34d73 | 13 | PACKET STRUCTURE: |
| ellingjp | 9:a711b5b34d73 | 14 | |
| ellingjp | 9:a711b5b34d73 | 15 | Command: |
| ellingjp | 9:a711b5b34d73 | 16 | [ 1 CMD | 17 SESSION_ID (optional) ] |
| ellingjp | 9:a711b5b34d73 | 17 | |
| ellingjp | 9:a711b5b34d73 | 18 | Response: |
| ellingjp | 9:a711b5b34d73 | 19 | [ 1 ACK/NACK | X Data (optional) ] |
| ellingjp | 9:a711b5b34d73 | 20 | |
| ellingjp | 9:a711b5b34d73 | 21 | Byte | Description |
| ellingjp | 9:a711b5b34d73 | 22 | ------------------ |
| paulbartell | 13:227a6cfd2097 | 23 | 0x1 | ACK |
| paulbartell | 13:227a6cfd2097 | 24 | 0x0 | NACK |
| ellingjp | 9:a711b5b34d73 | 25 | 0x2 | Delete All |
| ellingjp | 9:a711b5b34d73 | 26 | 0x4 | Receive file |
| ellingjp | 9:a711b5b34d73 | 27 | */ |
| ellingjp | 9:a711b5b34d73 | 28 | |
| paulbartell | 13:227a6cfd2097 | 29 | RawSerial bt(P0_19, P0_18); // tx, rx |
| paulbartell | 13:227a6cfd2097 | 30 | |
| paulbartell | 13:227a6cfd2097 | 31 | uint16_t packetSeq = 0; |
| ellingjp | 9:a711b5b34d73 | 32 | |
| ellingjp | 9:a711b5b34d73 | 33 | bool sync_init() { |
| ellingjp | 9:a711b5b34d73 | 34 | bt.baud(115200); |
| ellingjp | 9:a711b5b34d73 | 35 | return true; |
| ellingjp | 9:a711b5b34d73 | 36 | } |
| ellingjp | 9:a711b5b34d73 | 37 | |
| paulbartell | 13:227a6cfd2097 | 38 | |
| paulbartell | 13:227a6cfd2097 | 39 | struct responsePacket { |
| paulbartell | 13:227a6cfd2097 | 40 | char cmd; |
| paulbartell | 13:227a6cfd2097 | 41 | uint16_t len; |
| paulbartell | 13:227a6cfd2097 | 42 | char data; |
| paulbartell | 13:227a6cfd2097 | 43 | }; |
| paulbartell | 13:227a6cfd2097 | 44 | |
| paulbartell | 13:227a6cfd2097 | 45 | void sendResponse(char cmd, char resp) |
| paulbartell | 13:227a6cfd2097 | 46 | { |
| paulbartell | 13:227a6cfd2097 | 47 | |
| paulbartell | 13:227a6cfd2097 | 48 | struct responsePacket rp = { |
| paulbartell | 13:227a6cfd2097 | 49 | cmd, |
| paulbartell | 13:227a6cfd2097 | 50 | 4, |
| paulbartell | 13:227a6cfd2097 | 51 | resp}; |
| paulbartell | 13:227a6cfd2097 | 52 | |
| paulbartell | 13:227a6cfd2097 | 53 | bt.puts((char*)&rp); |
| paulbartell | 13:227a6cfd2097 | 54 | } |
| paulbartell | 13:227a6cfd2097 | 55 | |
| paulbartell | 13:227a6cfd2097 | 56 | |
| paulbartell | 13:227a6cfd2097 | 57 | uint16_t getLen() |
| paulbartell | 13:227a6cfd2097 | 58 | { |
| paulbartell | 13:227a6cfd2097 | 59 | union Lu |
| paulbartell | 13:227a6cfd2097 | 60 | { |
| paulbartell | 13:227a6cfd2097 | 61 | int16_t len; |
| paulbartell | 13:227a6cfd2097 | 62 | char dat[2]; |
| paulbartell | 13:227a6cfd2097 | 63 | }; |
| paulbartell | 13:227a6cfd2097 | 64 | union Lu lu; |
| paulbartell | 13:227a6cfd2097 | 65 | lu.dat[0] = bt.getc(); |
| paulbartell | 13:227a6cfd2097 | 66 | lu.dat[1] = bt.getc(); |
| paulbartell | 13:227a6cfd2097 | 67 | |
| paulbartell | 13:227a6cfd2097 | 68 | return lu.len; |
| paulbartell | 13:227a6cfd2097 | 69 | } |
| paulbartell | 13:227a6cfd2097 | 70 | |
| paulbartell | 13:227a6cfd2097 | 71 | void setRtc() |
| paulbartell | 13:227a6cfd2097 | 72 | { |
| paulbartell | 13:227a6cfd2097 | 73 | int dayOfWeek=0, date, month, year, hours, minutes, seconds; |
| paulbartell | 13:227a6cfd2097 | 74 | int16_t len; |
| paulbartell | 13:227a6cfd2097 | 75 | len = getLen(); |
| paulbartell | 13:227a6cfd2097 | 76 | int i = 0; |
| paulbartell | 13:227a6cfd2097 | 77 | for(i = 0; i < len; i++) |
| paulbartell | 13:227a6cfd2097 | 78 | { |
| paulbartell | 13:227a6cfd2097 | 79 | buff[i] = bt.getc(); |
| paulbartell | 13:227a6cfd2097 | 80 | } |
| paulbartell | 13:227a6cfd2097 | 81 | buff[i] = 0; // end the string with a zero |
| paulbartell | 13:227a6cfd2097 | 82 | sscanf(buff, "%04d-%02d-%02d %02d:%02d:%02d",&year,&month,&date,&hours,&minutes,&seconds); |
| paulbartell | 13:227a6cfd2097 | 83 | rtc.setDate(dayOfWeek, date, month, year); |
| paulbartell | 13:227a6cfd2097 | 84 | rtc.setTime(hours, minutes, seconds); |
| paulbartell | 13:227a6cfd2097 | 85 | sendResponse(CMD_RTCSET, ACK); |
| paulbartell | 13:227a6cfd2097 | 86 | |
| paulbartell | 13:227a6cfd2097 | 87 | } |
| paulbartell | 13:227a6cfd2097 | 88 | void listSessions() |
| paulbartell | 14:006d9087d76c | 89 | { //todo buffer this with buff |
| paulbartell | 14:006d9087d76c | 90 | DIR *dp; |
| paulbartell | 14:006d9087d76c | 91 | struct dirent *dirp; |
| paulbartell | 14:006d9087d76c | 92 | dp = opendir("/sd/"); |
| paulbartell | 14:006d9087d76c | 93 | while((dirp = readdir(dp)) != NULL) { |
| paulbartell | 14:006d9087d76c | 94 | bt.puts(dirp->d_name); |
| paulbartell | 14:006d9087d76c | 95 | bt.putc(','); |
| paulbartell | 14:006d9087d76c | 96 | //todo: put size here? |
| paulbartell | 14:006d9087d76c | 97 | bt.puts("\r\\n"); |
| paulbartell | 14:006d9087d76c | 98 | } |
| paulbartell | 14:006d9087d76c | 99 | closedir(dp); |
| paulbartell | 13:227a6cfd2097 | 100 | } |
| paulbartell | 13:227a6cfd2097 | 101 | |
| paulbartell | 13:227a6cfd2097 | 102 | void syncSession() |
| paulbartell | 13:227a6cfd2097 | 103 | { |
| paulbartell | 13:227a6cfd2097 | 104 | |
| paulbartell | 13:227a6cfd2097 | 105 | |
| paulbartell | 13:227a6cfd2097 | 106 | } |
| paulbartell | 13:227a6cfd2097 | 107 | |
| paulbartell | 13:227a6cfd2097 | 108 | void deleteSession() |
| paulbartell | 13:227a6cfd2097 | 109 | { |
| paulbartell | 13:227a6cfd2097 | 110 | |
| ellingjp | 9:a711b5b34d73 | 111 | |
| ellingjp | 9:a711b5b34d73 | 112 | } |
| ellingjp | 9:a711b5b34d73 | 113 | |
| ellingjp | 9:a711b5b34d73 | 114 | void sync() { |
| paulbartell | 13:227a6cfd2097 | 115 | while(State == SYNC) |
| paulbartell | 13:227a6cfd2097 | 116 | { |
| paulbartell | 13:227a6cfd2097 | 117 | if(bt.readable()) // get the latest byte available |
| paulbartell | 13:227a6cfd2097 | 118 | { |
| paulbartell | 13:227a6cfd2097 | 119 | buff[0] = bt.getc(); |
| paulbartell | 13:227a6cfd2097 | 120 | |
| paulbartell | 13:227a6cfd2097 | 121 | switch(buff[0]) |
| paulbartell | 13:227a6cfd2097 | 122 | { |
| paulbartell | 13:227a6cfd2097 | 123 | case CMD_RTCSET: |
| paulbartell | 13:227a6cfd2097 | 124 | setRtc(); |
| paulbartell | 13:227a6cfd2097 | 125 | break; |
| paulbartell | 13:227a6cfd2097 | 126 | case CMD_LISTSESSIONS: |
| paulbartell | 13:227a6cfd2097 | 127 | listSessions(); |
| paulbartell | 13:227a6cfd2097 | 128 | break; |
| paulbartell | 13:227a6cfd2097 | 129 | case CMD_SYNCSESSION: |
| paulbartell | 13:227a6cfd2097 | 130 | syncSession(); |
| paulbartell | 13:227a6cfd2097 | 131 | break; |
| paulbartell | 13:227a6cfd2097 | 132 | case CMD_DELETESESSION: |
| paulbartell | 13:227a6cfd2097 | 133 | deleteSession(); |
| paulbartell | 13:227a6cfd2097 | 134 | break; |
| paulbartell | 13:227a6cfd2097 | 135 | case CMD_DONE: |
| paulbartell | 13:227a6cfd2097 | 136 | bt.putc(CMD_DONE); |
| paulbartell | 13:227a6cfd2097 | 137 | sendResponse(CMD_DONE, ACK); |
| paulbartell | 13:227a6cfd2097 | 138 | State = IDLE; |
| paulbartell | 13:227a6cfd2097 | 139 | break; |
| paulbartell | 13:227a6cfd2097 | 140 | } |
| paulbartell | 13:227a6cfd2097 | 141 | } |
| ellingjp | 9:a711b5b34d73 | 142 | } |
| ellingjp | 9:a711b5b34d73 | 143 | } |