-
Notifications
You must be signed in to change notification settings - Fork 11
/
ATSerial.cpp
214 lines (189 loc) · 4.92 KB
/
ATSerial.cpp
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
/*
* 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"
/**
* Uses default parameters.
*/
ATSerial::ATSerial(uint32_t timeout,uint8_t max_resend_cnt) {
_timeout = timeout;
_max_resend_cnt = max_resend_cnt;
}
#if defined(ARDUINO_ARCH_SAMD) || defined(HAVE_HWSERIAL1)
void ATSerial::begin(HardwareSerial &uart,uint32_t baud)
{
_uart = &uart;
_uart->begin(baud);
}
#else
void ATSerial::begin(SoftwareSerial &uart,uint32_t baud)
{
_uart = &uart;
_uart->begin(baud);
}
#endif
/**
* Clears the software serial buffer.
*/
void ATSerial::flush(void) {
while (0 < _uart->read()){}
}
bool ATSerial::readResp(char* rsp)
{
int cnt = 0;
int len = 0;
uint32_t milli = millis();
while(!_uart->available()){
if((millis() - milli) > _timeout){
return false;
}
}
//delayMicroseconds(5000);
while(1)
{
cnt = 0;
*rsp = (char)_uart->read();
rsp ++;
len ++;
if(len >= UART_MAX_LEN)
break;
// wait upto 2000us for another character to become available
while(!_uart->available())
{
delayMicroseconds(200);
cnt++;
if(cnt >= 10)
break;
}
if(!_uart->available())
break;
} // this loop exits when UART_MAX_LEN chars have been read or _uart is unavailable for more than 2000us.
flush();
*rsp=0; // null terminate string
return true;
}
/**
* @param prefix
* @param resp_buffer char array of length UART_MAX_LEN+1 (513) to which response from device is written
* @return whether the response begins with prefix
*/
bool ATSerial::checkResponse(const char* prefix,char* resp_buffer)
{
if(!readResp(resp_buffer)){
return false;
}
#if DEBUG_EN
debug.print(F("Check Read <===: "));
debug.println(resp_buffer);
#endif
return (strncmp(prefix,resp_buffer,strlen(prefix)) == 0);
}
/**
* Send an AT command. Read the response into resp_buffer and return if the responce begins with the specified prefix
* @param prefix, the desired reponse prefix, eg AT_OK
* @
*/
bool ATSerial::sendCmdAndCheckRsp(const char* cmd, const char* prefix, char* resp_buffer)
{
uint8_t resend_cnt = 0;
ATWrite(cmd);
while(!checkResponse(prefix, resp_buffer)){
resend_cnt++;
if(resend_cnt > _max_resend_cnt){
return false;
}
ATWrite(cmd);
}
#if DEBUG_EN
debug.print(F("AT cmd ===>: "));
debug.println(cmd);
debug.print(F("RESP <===: "));
debug.println(resp_buffer);
#endif
return true;
}
bool ATSerial::waitForData(uint8_t *recv_msg,uint32_t *chars_read)
{
int recv_len = 0;
uint32_t milli = millis();
while(!_uart->available()){
if((millis() - milli) > _timeout){
*chars_read = 0;
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();
*chars_read = recv_len;
return true;
}
int ATSerial::read(void) {
return _uart->read();
}
/**
* Writes a string to the device.
*
* @param s the string to be written
* @return the number of bytes written
*/
int ATSerial::ATWrite(const char* s) {
#if DEBUG_EN
debug.print(F("Write ===>: "));
debug.println(s);
#endif
return _uart->write(s);
}
/***
* fmt : A format string stored in Flash.
* ... : arguaments to pass to sprintf_P(fmt).
***/
// int ATSerial::ATWritef_P(const __FlashStringHelper* fmt, ...) {
// va_list va;
// va_start(va, fmt);
// size_t len = vsnprintf_P(nullptr, 0, reinterpret_cast<const char*>(fmt), va);
// va_end(va);
// char buf[len];
// va_start(va, fmt);
// vsprintf_P(buf, reinterpret_cast<const char*>(fmt), va);
// va_end(va);
// ATWrite(buf);
// }
int ATSerial::ATWrite(const uint8_t uc) {
return _uart->write(uc);
}
void ATSerial::binWrite(uint8_t *msg, uint32_t len)
{
for(uint32_t i=0; i < len; i++)
{
_uart->write(msg[i]);
}
}