-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDTE.cpp
More file actions
102 lines (87 loc) · 2.79 KB
/
DTE.cpp
File metadata and controls
102 lines (87 loc) · 2.79 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DTE.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Neta Yahav <neta540@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/31 17:53:23 by Neta Yahav #+# #+# */
/* Updated: 2020/08/02 18:43:04 by Neta Yahav ### ########.fr */
/* */
/* ************************************************************************** */
#include "DTE.h"
DTE::DTE(Stream &stream, unsigned int size)
: stream(stream), bufferSize(size), result(EXPECT_RESULT) {
buffer.reserve(size);
}
DTE::~DTE(){};
void DTE::SendCommand(const char *command, unsigned long timeout,
const char *response1, const char *response2,
const char *response3) {
match = 0;
result = EXPECT_BUSY;
response[0] = response1;
response[1] = response2;
response[2] = response3;
this->timeout = timeout;
// clear rx buffer
while (stream.available()) {
stream.read();
}
// send command
stream.print(command);
buffer = "";
tick = millis();
proccess();
}
void DTE::Delay(unsigned long delay) {
timeout = delay;
result = EXPECT_DELAY;
tick = millis();
proccess();
}
bool DTE::getIsBusy() {
proccess();
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
}
DTE::CommandResult DTE::getResult() { return result; }
unsigned int DTE::getMatch() { return match; }
String &DTE::getBuffer() { return buffer; }
void DTE::proccess() {
if (result == EXPECT_DELAY) {
if (millis() - tick >= timeout)
result = EXPECT_RESULT;
return;
}
if (result != EXPECT_BUSY)
return;
char c;
unsigned long now = millis();
while (millis() - tick < timeout) {
while (stream.available() && (buffer.length() < (bufferSize))) {
c = stream.read();
buffer += c;
if (buffer.endsWith(response[0])) {
match = 0;
result = EXPECT_RESULT;
return;
} else if (response[1].length() != 0) {
if (buffer.endsWith(response[1])) {
match = 1;
result = EXPECT_RESULT;
return;
}
} else if (response[2].length() != 0) {
if (buffer.endsWith(response[2])) {
match = 2;
result = EXPECT_RESULT;
return;
}
}
}
if (millis() - now > 5)
return;
}
// time out
result = EXPECT_TIMEOUT;
}