forked from neta540/ArduinoThreadedGSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDTE.h
129 lines (119 loc) · 2.3 KB
/
DTE.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
/*
* DTE.h
*
* Created: 20/09/2016 15:40:51
* Author: Neta Yahav
*/
#ifndef __DTE_H__
#define __DTE_H__
#include <Arduino.h>
class DTE
{
//variables
public:
enum CommandResult
{
EXPECT_BUSY,
EXPECT_TIMEOUT,
EXPECT_DELAY,
EXPECT_RESULT
};
protected:
private:
String buffer;
Stream& stream;
unsigned int bufferIndex;
unsigned int bufferSize;
String response[3];
unsigned long timeout;
unsigned long tick;
unsigned int match;
CommandResult result;
//functions
public:
DTE(Stream& stream, unsigned int size) : stream(stream), bufferSize(size), result(EXPECT_RESULT) { buffer.reserve(size); }
~DTE() {};
void SendCommand(const char* command, unsigned long timeout, const char* response1, const char* response2 = 0, const char* response3 = 0)
{
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 Delay(unsigned long delay)
{
timeout = delay;
result = EXPECT_DELAY;
tick = millis();
proccess();
}
bool getIsBusy()
{
proccess();
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
}
CommandResult getResult() { return result ; }
unsigned int getMatch() { return match; }
String& getBuffer() { return buffer; }
protected:
private:
void 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;
}
}; //DTE
#endif //__DTE_H__