This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspUART.h
275 lines (217 loc) · 8 KB
/
WaspUART.h
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
/*! \file WaspUART.h
\brief Library for managing UART bus
Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
http://www.libelium.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Version: 3.0
Design: David Gascón
Implementation: Yuri Carmona
*/
#ifndef WaspUART_h
#define WaspUART_h
/******************************************************************************
* Definitions & Declarations
******************************************************************************/
//! DEBUG MODE
/*! 0: No debug mode enabled
* 1: debug mode enabled for error output messages
* 2: debug mode enabled for both error and ok messages
* \Remarks do not enable mode 2 unless SOCKET1 is used
*/
#define DEBUG_UART 0
// define print MACRO
#define PRINT_UART(str) USB.print(F("[UART] ")); USB.print(str);
/*! \def UART0
\brief UART0 of the MCU
*/
/*! \def UART1
\brief UART1 of the MCU
*/
#define UART0 0
#define UART1 1
/*! \def DEF_COMMAND_TIMEOUT
\brief default timeout for command operations
*/
#define DEF_COMMAND_TIMEOUT 5000
/*! \def DEF_COMMAND_DELAY
\brief default time to wait after sending command
*/
#define DEF_COMMAND_DELAY 100
/*! \def DEF_BAUD_RATE
\brief default baudrate
*/
#define DEF_BAUD_RATE 115200
/******************************************************************************
* Class
******************************************************************************/
class WaspUART
{
protected:
uint8_t _uart;
uint32_t _baudrate;
uint32_t _def_timeout;
uint32_t _def_delay;
bool _flush_mode;
public:
WaspUART()
{
_baudrate = DEF_BAUD_RATE;
_def_timeout= DEF_COMMAND_TIMEOUT;
_def_delay = DEF_COMMAND_DELAY;
_flush_mode = true;
};
//! buffer for rx data
uint8_t _buffer[512];
//! length of the contents in '_buffer'
uint16_t _length;
//! It open the corresponding uart
void beginUART();
//! It closes the corresponding uart
void closeUART();
//! It sets the time to wait for command responses
void setTimeout(uint32_t newTimeout);
//! It gets the time to wait for command responses
uint32_t getTimeout();
//! It sets the time to wait after sending the command
void setDelay( uint32_t newDelay );
//! It gets the time to wait after sending the command
uint32_t getDelay();
//! It sends a command through the selected uart expecting a specific answer
/*!
\param char* command : string to send to the module
\param char* ans1 : string expected to be answered by the module
\return '0' if timeout error,
'1' if ans1
*/
uint8_t sendCommand(char* command,
char* ans1);
uint8_t sendCommand(char* command,
char* ans1,
uint32_t timeout);
//! It sends a command through the selected uart expecting specific answers
/*!
\param char* command : string to send to the module
\param char* ans1 : string expected to be answered by the module
\param char* ans2 : string expected to be answered by the module
\param uint32_t timeout : time to wait for responses before exit with error
\return '0' if timeout error,
'1' if ans1
'2' if ans2
*/
uint8_t sendCommand(char* command,
char* ans1,
char* ans2);
uint8_t sendCommand(char* command,
char* ans1,
char* ans2,
uint32_t timeout);
//! It sends a command through the selected uart expecting specific answers
/*!
\param char* command : string to send to the module
\param char* ans1 : string expected to be answered by the module
\param char* ans2 : string expected to be answered by the module
\param char* ans3 : string expected to be answered by the module
\param uint32_t timeout : time to wait for responses before exit with error
\return '0' if timeout error,
'1' if ans1
'2' if ans2
'3' if ans3
*/
uint8_t sendCommand(char* command,
char* ans1,
char* ans2,
char* ans3 );
uint8_t sendCommand(char* command,
char* ans1,
char* ans2,
char* ans3,
uint32_t timeout);
//! It sends a command through the selected uart expecting specific answers
/*!
\param char* command : string to send to the module
\param char* ans1 : string expected to be answered by the module
\param char* ans2 : string expected to be answered by the module
\param char* ans3 : string expected to be answered by the module
\param char* ans4 : string expected to be answered by the module
\param uint32_t timeout : time to wait for responses before exit with error
\return '0' if timeout error,
'1' if ans1
'2' if ans2
'3' if ans3
'4' if ans4
*/
uint8_t sendCommand(char* command,
char* ans1,
char* ans2,
char* ans3,
char* ans4);
uint8_t sendCommand(char* command,
char* ans1,
char* ans2,
char* ans3,
char* ans4,
uint32_t timeout);
//! It seeks 'pattern' inside the 'buffer' array
bool find(uint8_t* buffer, uint16_t length, char* pattern);
//! It sends a command without waiting answer (only send)
void sendCommand(uint8_t* command, uint16_t length);
/*!
\brief This function waits for one of the answers during a certain period
of time. The result is stored in '_buffer'.
\param char* command: command to be sent
\param char* ans1: expected answer
\param char* ans2: expected answer
\param char* ans3: expected answer
\param char* ans4: expected answer
\param uint32_t timeout : time to wait for responses before exit with error
\return '0' if timeout error,
'1' if ans1
'2' if ans2
'3' if ans3
'4' if ans4
*/
uint8_t waitFor( char* ans1);
uint8_t waitFor( char* ans1, uint32_t timeout);
uint8_t waitFor( char* ans1, char* ans2);
uint8_t waitFor( char* ans1, char* ans2, uint32_t timeout);
uint8_t waitFor( char* ans1, char* ans2, char* ans3 );
uint8_t waitFor( char* ans1, char* ans2, char* ans3, uint32_t timeout);
uint8_t waitFor( char* ans1, char* ans2, char* ans3, char* ans4);
uint8_t waitFor( char* ans1, char* ans2, char* ans3, char* ans4, uint32_t timeout);
//! Read the contents of the rx buffer
uint16_t readBuffer(uint16_t requestBytes);
uint16_t readBuffer(uint16_t requestBytes, bool clearBuffer);
//! It waits depending on the baudrate used
void latencyDelay();
void setUART(uint8_t uart);
void setBaudrate(uint32_t baudrate);
protected:
//! It parses the contents of _buffer and copies the string to the pointer
uint8_t parseString(char* str, uint16_t size, char* delimiters);
uint8_t parseString(char* str, uint16_t size, char* delimiters, uint8_t n);
//! It parses the contents of _buffer and converts it to a float type
uint8_t parseFloat(float* value, char* delimiters);
//! It parses the contents of _buffer and converts it to a uint8_t type
uint8_t parseUint8(uint8_t* value, char* delimiters);
uint8_t parseUint8(uint8_t* value, char* delimiters, uint8_t n);
//! It parses the contents of _buffer and converts it to a uint32_t type
uint8_t parseUint32(uint32_t* value, char* delimiters);
//! It parses the contents of _buffer and converts it to a int32_t type
uint8_t parseInt32(int32_t* value, char* delimiters);
uint8_t parseInt32(int32_t* value, char* delimiters, uint8_t n);
//! It parses the contents of _buffer and converts it to a int type
uint8_t parseInt(int* value, char* delimiters);
//! It parses the contents of _buffer and converts it to a uint16_t type
uint8_t parseHex(uint8_t* value, char* delimiters);
void secureBegin();
};
#endif