asdasdasdasd
Dependencies: Audio EthernetInterface mbed-rtos mbed
Fork of TCPEchoClient by
main.cpp@11:9280da8e40bd, 2018-08-10 (annotated)
- Committer:
- irsanjul
- Date:
- Fri Aug 10 09:12:27 2018 +0000
- Revision:
- 11:9280da8e40bd
- Parent:
- 9:a640b18e5931
asdasdas
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:e8f884c94549 | 1 | #include "mbed.h" |
irsanjul | 11:9280da8e40bd | 2 | #include "rtos.h" |
emilmont | 1:a51d8ed156e6 | 3 | #include "EthernetInterface.h" |
irsanjul | 11:9280da8e40bd | 4 | #include "CommChannel.h" |
irsanjul | 11:9280da8e40bd | 5 | #include "LokerFraming.h" |
irsanjul | 11:9280da8e40bd | 6 | #include "const_defines.h" |
irsanjul | 11:9280da8e40bd | 7 | #include <vector> |
irsanjul | 11:9280da8e40bd | 8 | |
irsanjul | 11:9280da8e40bd | 9 | /******************************************************************************* |
irsanjul | 11:9280da8e40bd | 10 | PRIVATE PinOut |
irsanjul | 11:9280da8e40bd | 11 | *******************************************************************************/ |
irsanjul | 11:9280da8e40bd | 12 | Serial dbg(USBTX, USBRX); |
irsanjul | 11:9280da8e40bd | 13 | DigitalOut led(LED_BLUE); |
irsanjul | 11:9280da8e40bd | 14 | |
irsanjul | 11:9280da8e40bd | 15 | |
irsanjul | 11:9280da8e40bd | 16 | /******************************************************************************* |
irsanjul | 11:9280da8e40bd | 17 | PRIVATE VARIABLE |
irsanjul | 11:9280da8e40bd | 18 | *******************************************************************************/ |
irsanjul | 11:9280da8e40bd | 19 | int device_status; |
irsanjul | 11:9280da8e40bd | 20 | int kirim_status; |
irsanjul | 11:9280da8e40bd | 21 | int COL_SCAN, ROW_SCAN; |
irsanjul | 11:9280da8e40bd | 22 | const char* SERVER_ADDRESS = "192.168.0.34"; |
irsanjul | 11:9280da8e40bd | 23 | const char* IP = "192.168.0.111"; |
irsanjul | 11:9280da8e40bd | 24 | const char* SM = "255.255.255.0"; |
irsanjul | 11:9280da8e40bd | 25 | const char* GW = "192.168.0.1"; |
irsanjul | 11:9280da8e40bd | 26 | |
irsanjul | 11:9280da8e40bd | 27 | TCPSocketConnection socket; |
irsanjul | 11:9280da8e40bd | 28 | CommChannel COM; |
irsanjul | 11:9280da8e40bd | 29 | bool comStat; |
irsanjul | 11:9280da8e40bd | 30 | |
irsanjul | 11:9280da8e40bd | 31 | std::vector<int> RCV_BUFF; |
irsanjul | 11:9280da8e40bd | 32 | int timerRxTOutCnt; |
irsanjul | 11:9280da8e40bd | 33 | bool tmr10msTick; |
irsanjul | 11:9280da8e40bd | 34 | Ticker timer10ms; |
irsanjul | 11:9280da8e40bd | 35 | void timer10msTick(); |
emilmont | 1:a51d8ed156e6 | 36 | |
irsanjul | 11:9280da8e40bd | 37 | bool tmr10sTick; |
irsanjul | 11:9280da8e40bd | 38 | Ticker timer10s; |
irsanjul | 11:9280da8e40bd | 39 | void timer10stick(); |
emilmont | 1:a51d8ed156e6 | 40 | |
irsanjul | 11:9280da8e40bd | 41 | void rxRoutine(void const *args) |
irsanjul | 11:9280da8e40bd | 42 | { |
irsanjul | 11:9280da8e40bd | 43 | while(true) |
irsanjul | 11:9280da8e40bd | 44 | { |
irsanjul | 11:9280da8e40bd | 45 | int n=0; |
irsanjul | 11:9280da8e40bd | 46 | char buf[256]; |
irsanjul | 11:9280da8e40bd | 47 | n = socket.receive(buf, 256); |
irsanjul | 11:9280da8e40bd | 48 | if(n > 0) |
irsanjul | 11:9280da8e40bd | 49 | { |
irsanjul | 11:9280da8e40bd | 50 | buf[n] = '\0'; |
irsanjul | 11:9280da8e40bd | 51 | // dbg.printf("Received message from server: '%s'\n\r", buf); |
irsanjul | 11:9280da8e40bd | 52 | for(int i=0; i<n; i++) |
irsanjul | 11:9280da8e40bd | 53 | { |
irsanjul | 11:9280da8e40bd | 54 | RCV_BUFF.push_back((int)buf[i]); |
irsanjul | 11:9280da8e40bd | 55 | } |
irsanjul | 11:9280da8e40bd | 56 | timerRxTOutCnt = 0; |
irsanjul | 11:9280da8e40bd | 57 | } |
irsanjul | 11:9280da8e40bd | 58 | Thread::wait(100); |
irsanjul | 11:9280da8e40bd | 59 | } |
irsanjul | 11:9280da8e40bd | 60 | } |
irsanjul | 11:9280da8e40bd | 61 | |
irsanjul | 11:9280da8e40bd | 62 | //const int ECHO_SERVER_PORT = 5002; |
irsanjul | 11:9280da8e40bd | 63 | |
irsanjul | 11:9280da8e40bd | 64 | Ticker run; |
irsanjul | 11:9280da8e40bd | 65 | void runled(); |
irsanjul | 11:9280da8e40bd | 66 | |
irsanjul | 11:9280da8e40bd | 67 | int main() |
irsanjul | 11:9280da8e40bd | 68 | { |
irsanjul | 11:9280da8e40bd | 69 | tmr10msTick = false; |
irsanjul | 11:9280da8e40bd | 70 | tmr10sTick = false; |
irsanjul | 11:9280da8e40bd | 71 | kirim_status = false; |
irsanjul | 11:9280da8e40bd | 72 | |
emilmont | 1:a51d8ed156e6 | 73 | EthernetInterface eth; |
irsanjul | 11:9280da8e40bd | 74 | eth.init(IP, SM, GW); //Use DHCP |
irsanjul | 11:9280da8e40bd | 75 | int n = eth.connect(); |
irsanjul | 11:9280da8e40bd | 76 | if(n)dbg.printf(GRE"Connected with IP : %s\r\n"DEF, IP); |
irsanjul | 11:9280da8e40bd | 77 | else dbg.printf(RED"Not Connected with IP : %s\r\n"DEF, IP); |
irsanjul | 11:9280da8e40bd | 78 | |
irsanjul | 11:9280da8e40bd | 79 | comStat = false; |
irsanjul | 11:9280da8e40bd | 80 | COM.connect(SERVER_ADDRESS); |
irsanjul | 11:9280da8e40bd | 81 | dbg.printf(GRE"Connected to Server at %s\r\n"DEF, SERVER_ADDRESS); |
irsanjul | 11:9280da8e40bd | 82 | |
irsanjul | 11:9280da8e40bd | 83 | timer10ms.attach_us(&timer10msTick, 10000); |
irsanjul | 11:9280da8e40bd | 84 | |
irsanjul | 11:9280da8e40bd | 85 | set_time(1483228800); |
irsanjul | 11:9280da8e40bd | 86 | run.attach(&runled, 1.0f); |
irsanjul | 11:9280da8e40bd | 87 | Thread thread1(rxRoutine); |
irsanjul | 11:9280da8e40bd | 88 | |
irsanjul | 11:9280da8e40bd | 89 | int Loop = 0; |
emilmont | 1:a51d8ed156e6 | 90 | |
irsanjul | 11:9280da8e40bd | 91 | while(true) |
irsanjul | 11:9280da8e40bd | 92 | { |
irsanjul | 11:9280da8e40bd | 93 | char timeBuf[32]; |
irsanjul | 11:9280da8e40bd | 94 | time_t seconds = time(NULL); |
irsanjul | 11:9280da8e40bd | 95 | strftime(timeBuf, 32, "%Y-%m-%d+%H:%M:%S", localtime(&seconds)); |
irsanjul | 11:9280da8e40bd | 96 | |
irsanjul | 11:9280da8e40bd | 97 | comStat = socket.is_connected(); |
irsanjul | 11:9280da8e40bd | 98 | if(!comStat) |
irsanjul | 11:9280da8e40bd | 99 | { |
irsanjul | 11:9280da8e40bd | 100 | socket.close(); |
irsanjul | 11:9280da8e40bd | 101 | COM.connect(SERVER_ADDRESS); |
irsanjul | 11:9280da8e40bd | 102 | dbg.printf(GRE"Connected to Server at %s\r\n"DEF, SERVER_ADDRESS); |
irsanjul | 11:9280da8e40bd | 103 | } |
irsanjul | 11:9280da8e40bd | 104 | |
irsanjul | 11:9280da8e40bd | 105 | if(tmr10msTick) |
irsanjul | 11:9280da8e40bd | 106 | { |
irsanjul | 11:9280da8e40bd | 107 | tmr10msTick = false; |
irsanjul | 11:9280da8e40bd | 108 | COM.Tick10ms(); |
irsanjul | 11:9280da8e40bd | 109 | } |
irsanjul | 11:9280da8e40bd | 110 | |
irsanjul | 11:9280da8e40bd | 111 | // Receive message from server |
irsanjul | 11:9280da8e40bd | 112 | if(COM.dataAvailable() > 0) |
irsanjul | 11:9280da8e40bd | 113 | { |
irsanjul | 11:9280da8e40bd | 114 | dbg.printf(YEL"%s <<\t", timeBuf); |
irsanjul | 11:9280da8e40bd | 115 | std::vector<int> r = COM.read(); |
irsanjul | 11:9280da8e40bd | 116 | for(int i=0; i<r.size(); i++) |
irsanjul | 11:9280da8e40bd | 117 | dbg.printf("0x%02X ", r[i]); |
irsanjul | 11:9280da8e40bd | 118 | dbg.printf("\r\n"DEF); |
irsanjul | 11:9280da8e40bd | 119 | |
irsanjul | 11:9280da8e40bd | 120 | LokerFraming frm; |
irsanjul | 11:9280da8e40bd | 121 | frm.ProcessFromHost(r); |
irsanjul | 11:9280da8e40bd | 122 | size_t sz = frm.RCV_QUE.size(); |
irsanjul | 11:9280da8e40bd | 123 | if(sz > 0) |
irsanjul | 11:9280da8e40bd | 124 | { |
irsanjul | 11:9280da8e40bd | 125 | LokerFrm lf = frm.RCV_QUE.front(); |
irsanjul | 11:9280da8e40bd | 126 | frm.RCV_QUE.pop(); |
irsanjul | 11:9280da8e40bd | 127 | |
irsanjul | 11:9280da8e40bd | 128 | switch(lf.status) |
irsanjul | 11:9280da8e40bd | 129 | { |
irsanjul | 11:9280da8e40bd | 130 | case '0' : |
irsanjul | 11:9280da8e40bd | 131 | device_status = idle; |
irsanjul | 11:9280da8e40bd | 132 | break; |
irsanjul | 11:9280da8e40bd | 133 | case '1' : |
irsanjul | 11:9280da8e40bd | 134 | case '2' : |
irsanjul | 11:9280da8e40bd | 135 | ROW_SCAN = lf.Data[0]; |
irsanjul | 11:9280da8e40bd | 136 | COL_SCAN = lf.Data[1]; |
irsanjul | 11:9280da8e40bd | 137 | dbg.printf(YEL"++ Scanning : %c %c\r\n\r\n"DEF, ROW_SCAN, COL_SCAN); |
irsanjul | 11:9280da8e40bd | 138 | device_status = lf.status; |
irsanjul | 11:9280da8e40bd | 139 | break; |
irsanjul | 11:9280da8e40bd | 140 | case '3' : // for send status of |
irsanjul | 11:9280da8e40bd | 141 | kirim_status = true; |
irsanjul | 11:9280da8e40bd | 142 | break; |
irsanjul | 11:9280da8e40bd | 143 | case '4' : // for setting the rtc |
irsanjul | 11:9280da8e40bd | 144 | break; |
irsanjul | 11:9280da8e40bd | 145 | default : break; |
irsanjul | 11:9280da8e40bd | 146 | } |
irsanjul | 11:9280da8e40bd | 147 | } |
irsanjul | 11:9280da8e40bd | 148 | } |
irsanjul | 11:9280da8e40bd | 149 | |
irsanjul | 11:9280da8e40bd | 150 | // Send message to server |
irsanjul | 11:9280da8e40bd | 151 | if(kirim_status) |
irsanjul | 11:9280da8e40bd | 152 | { |
irsanjul | 11:9280da8e40bd | 153 | dbg.printf(YEL"%s >>\t", timeBuf); |
irsanjul | 11:9280da8e40bd | 154 | if(comStat) |
irsanjul | 11:9280da8e40bd | 155 | { |
irsanjul | 11:9280da8e40bd | 156 | srand(seconds); |
irsanjul | 11:9280da8e40bd | 157 | std::vector<int>kirim; |
irsanjul | 11:9280da8e40bd | 158 | for(int i=0; i<8; i++)kirim.push_back(rand() % 0xFF); |
irsanjul | 11:9280da8e40bd | 159 | TxFrm tData(kirim, device_status); |
irsanjul | 11:9280da8e40bd | 160 | |
irsanjul | 11:9280da8e40bd | 161 | std::vector<int>tMsg = LokerFraming::CreateFrame(tData); |
irsanjul | 11:9280da8e40bd | 162 | for(int i=0; i<tMsg.size(); i++) |
irsanjul | 11:9280da8e40bd | 163 | dbg.printf("0x%02X ", tMsg[i]); |
irsanjul | 11:9280da8e40bd | 164 | dbg.printf("\r\n\r\n"DEF); |
irsanjul | 11:9280da8e40bd | 165 | |
irsanjul | 11:9280da8e40bd | 166 | COM.write(tMsg); |
irsanjul | 11:9280da8e40bd | 167 | kirim_status = false; |
irsanjul | 11:9280da8e40bd | 168 | Loop++; |
irsanjul | 11:9280da8e40bd | 169 | if(Loop > 4) |
irsanjul | 11:9280da8e40bd | 170 | { |
irsanjul | 11:9280da8e40bd | 171 | Loop = 0; |
irsanjul | 11:9280da8e40bd | 172 | device_status = idle; |
irsanjul | 11:9280da8e40bd | 173 | } |
irsanjul | 11:9280da8e40bd | 174 | } |
irsanjul | 11:9280da8e40bd | 175 | else |
irsanjul | 11:9280da8e40bd | 176 | { |
irsanjul | 11:9280da8e40bd | 177 | socket.close(); |
irsanjul | 11:9280da8e40bd | 178 | COM.connect(SERVER_ADDRESS); |
irsanjul | 11:9280da8e40bd | 179 | } |
irsanjul | 11:9280da8e40bd | 180 | } |
emilmont | 1:a51d8ed156e6 | 181 | } |
irsanjul | 11:9280da8e40bd | 182 | } |
irsanjul | 11:9280da8e40bd | 183 | |
irsanjul | 11:9280da8e40bd | 184 | /******************************************************************************* |
irsanjul | 11:9280da8e40bd | 185 | PRIVATE FUNCTION |
irsanjul | 11:9280da8e40bd | 186 | *******************************************************************************/ |
irsanjul | 11:9280da8e40bd | 187 | void runled() |
irsanjul | 11:9280da8e40bd | 188 | { |
irsanjul | 11:9280da8e40bd | 189 | led = !led; |
emilmont | 1:a51d8ed156e6 | 190 | } |
irsanjul | 11:9280da8e40bd | 191 | |
irsanjul | 11:9280da8e40bd | 192 | void timer10stick() |
irsanjul | 11:9280da8e40bd | 193 | { |
irsanjul | 11:9280da8e40bd | 194 | tmr10sTick = true; |
irsanjul | 11:9280da8e40bd | 195 | } |
irsanjul | 11:9280da8e40bd | 196 | |
irsanjul | 11:9280da8e40bd | 197 | void timer10msTick() |
irsanjul | 11:9280da8e40bd | 198 | { |
irsanjul | 11:9280da8e40bd | 199 | tmr10msTick = true; |
irsanjul | 11:9280da8e40bd | 200 | } |