forked from Seeed-Studio/Seeed_Arduino_KCT202
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATSerial.cpp
More file actions
334 lines (274 loc) · 8.47 KB
/
Copy pathATSerial.cpp
File metadata and controls
334 lines (274 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
ATSerial.cpp
Copyright (c) 2018 seeed technology inc.
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ATSerial.h"
/**
Clears the software serial buffer.
*/
template <class T, class T1>
void ATSerial<T, T1>::flush(void) {
while (0 < _uart->read()) {}
}
template <class T, class T1>
bool ATSerial<T, T1>::readResp(String& rsp, uint32_t time_out) {
int i = 0;
int ch = 0;
int cnt = 0;
uint32_t milli = millis();
while (!_uart->available()) {
if ((millis() - milli) > time_out) {
return false;
}
}
delayMicroseconds(5000);
while (1) {
cnt = 0;
rsp += (char)_uart->read();
if (rsp.length() >= UART_MAX_LEN) {
break;
}
while (!_uart->available()) {
delayMicroseconds(200);
cnt++;
if (cnt >= 10) {
break;
}
}
if (!_uart->available()) {
break;
}
}
flush();
return true;
}
/**
Checks for the substring in the response received from the device.
@param s the substring to be checked
@param match_len The substring's length to be check.
@param time_out If the wifi module had not rsp within time_out(ms),return false
@return true if the provided substring is found in the response, else false
*/
template <class T, class T1>
bool ATSerial<T, T1>::checkResponse(String& s, uint16_t match_len, uint32_t time_out) {
bool ret = false;
String rsp;
ret = readResp(rsp, time_out);
if (!ret) {
return false;
}
#if DEBUG_EN
debug.print("Check Read <===: ");
debug.println(rsp);
#endif
for (int i = 0; i < match_len; i++) {
if ((i != match_len) && (rsp[i] != s[i])) {
return false;
}
}
return true;
}
template <class T, class T1>
bool ATSerial<T, T1>::getMsg(String& msg, uint32_t time_out) {
bool ret = false;
//String rsp;
ret = readResp(msg, time_out);
if (!ret) {
return false;
}
#if DEBUG_EN
debug.print("get Read <===: ");
debug.println(msg);
#endif
return true;
}
/** Check the resp of the AT-CMD,and get the msg returned from wifi module.
@param s The substring to be checked.
@param match_len The substring's length to be check.
@param time_out If the wifi module had not rsp within time_out(ms),return false
@param msg The msg reveived from wifi module.
@return true if the provided substring is found in the response, else false
* */
template <class T, class T1>
bool ATSerial<T, T1>::checkResponseAndGetMessage(String& s, uint16_t match_len, uint32_t time_out, String& msg) {
bool ret = false;
String rsp;
ret = readResp(rsp, time_out);
if (!ret) {
return false;
}
#if DEBUG_EN
debug.print("Read <===: ");
debug.println(rsp);
#endif
for (int i = 0; i < match_len; i++) {
if ((i != match_len) && (rsp[i] != s[i])) {
return false;
}
}
msg = rsp;
return true;
}
template <class T, class T1>
int ATSerial<T, T1>::read(void) {
return _uart->read();
}
/**
Writes a string to the device.
@param s the string to be written
@return the number of bytes written
*/
template <class T, class T1>
int ATSerial<T, T1>::ATWrite(const char* s) {
#if DEBUG_EN
debug.print("Write ===>: ");
debug.println(s);
#endif
return _uart->write(s);
}
template <class T, class T1>
int ATSerial<T, T1>::ATWrite(const uint8_t uc) {
return _uart->write(uc);
}
template <class T, class T1>
void ATSerial<T, T1>::binWrite(uint8_t* msg, uint32_t len) {
if (true == checkDebugFlag()) {
debug_uart_->print("Uart===> : ");
printHexArray(msg, len);
debug_uart_->println(" ");
}
for (int i = 0; i < len; i++) {
_uart->write(msg[i]);
}
}
template <class T, class T1>
bool ATSerial<T, T1>::sendCmdAndCheckRsp(String& cmd, String& match, uint16_t match_len, uint32_t time_out,
uint8_t max_resend_cnt) {
bool ret = false;
uint8_t resend_cnt = 0;
ATWrite(cmd.c_str());
ret = checkResponse(match, match_len, time_out);
while (!ret) {
resend_cnt++;
if (resend_cnt > max_resend_cnt) {
return false;
}
ATWrite(cmd.c_str());
ret = checkResponse(match, match_len, time_out);
}
return true;
}
template <class T, class T1>
bool ATSerial<T, T1>::sendCmdAndGetMsg(String& msg, String& cmd, String& match, uint16_t match_len, uint32_t time_out,
uint8_t max_resend_cnt) {
bool ret = false;
uint8_t resend_cnt = 0;
ATWrite(cmd.c_str());
ret = checkResponseAndGetMessage(match, match_len, time_out, msg);
//debug.print("msg = ");
//debug.println(msg);
while (!ret) {
resend_cnt++;
if (resend_cnt > max_resend_cnt) {
return false;
}
ATWrite(cmd.c_str());
ret = checkResponseAndGetMessage(match, match_len, time_out, msg);
}
return true;
}
template <class T, class T1>
bool ATSerial<T, T1>::waitForData(uint8_t* recv_msg, uint32_t& recv_len, uint32_t time_out, uint8_t max_resend_cnt) {
int i = 0;
int ch = 0;
uint32_t milli = millis();
while (!_uart->available()) {
if ((millis() - milli) > time_out) {
return false;
}
}
delayMicroseconds(1000);
while (_uart->available()) {
recv_msg[recv_len] = (uint8_t)_uart->read();
recv_len++;
if (recv_len >= UART_MAX_LEN) {
break;
}
delayMicroseconds(1000);
}
flush();
return true;
}
template <class T, class T1>
bool ATSerial<T, T1>::waitForDataTillRespon(uint8_t* recv_msg, uint32_t& recv_len, uint8_t max_resend_cnt) {
int i = 0;
int ch = 0;
uint32_t time = 0;
recv_len = 0;
if (NULL == recv_msg) {
return false;
}
uint32_t milli = millis();
while (!_uart->available()) {
if ((millis() - milli) > 5000) {
milli = millis();
time += 5;
debug_uart_ -> print("Wait for response of finger-print sensor ");
debug_uart_ -> print(time);
debug_uart_ -> println(" seconds");
}
}
delayMicroseconds(1000);
while (_uart->available()) {
recv_msg[recv_len] = (uint8_t)_uart->read();
recv_len++;
if (recv_len >= UART_MAX_LEN) {
break;
}
delayMicroseconds(1000);
}
if (true == checkDebugFlag()) {
debug_uart_->print("Uart<=== : ");
printHexArray(recv_msg, recv_len);
debug_uart_->println(" ");
}
flush();
return true;
}
template <class T, class T1>
void ATSerial<T, T1>::printHexArray(uint8_t* data, uint32_t data_len) {
for (int i = 0; i < data_len; i++) {
debug_uart_->print(data[i], HEX);
debug_uart_->print(".");
}
debug_uart_->println(" ");
}
#if defined(ARDUINO_ARCH_AVR)
template class ATSerial<SoftwareSerial, HardwareSerial>;
#elif defined(ARDUINO_ARCH_SAM)
//template class ATSerial<HardwareSerial,HardwareSerial>;
//template class ATSerial<HardwareSerial,Serial_>;
template class ATSerial<Uart, Serial_>;
#elif defined(ARDUINO_ARCH_SAMD)
//template class ATSerial<HardwareSerial,HardwareSerial>;
//template class ATSerial<HardwareSerial,Serial_>;
template class ATSerial<Uart, Serial_>;
#else
template class ATSerial<SoftwareSerial, HardwareSerial>;
#endif