123 lines
2.6 KiB
C++
123 lines
2.6 KiB
C++
#include "DTE.h"
|
|
|
|
DTE::DTE(Stream & stream, unsigned int size)
|
|
: stream(stream), bufferSize(size), result(EXPECT_RESULT)
|
|
{
|
|
buffer.reserve(size);
|
|
}
|
|
|
|
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;
|
|
flush();
|
|
stream.print(command);
|
|
buffer = "";
|
|
tick = millis();
|
|
process();
|
|
}
|
|
|
|
void DTE::SendCommand(const __FlashStringHelper * 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
|
|
flush();
|
|
// send command
|
|
stream.print((const __FlashStringHelper *) command);
|
|
buffer = "";
|
|
tick = millis();
|
|
process();
|
|
}
|
|
|
|
void DTE::Delay(unsigned long delay)
|
|
{
|
|
timeout = delay;
|
|
result = EXPECT_DELAY;
|
|
tick = millis();
|
|
process();
|
|
}
|
|
|
|
bool DTE::getIsBusy()
|
|
{
|
|
process();
|
|
return (result == EXPECT_BUSY) || (result == EXPECT_DELAY);
|
|
}
|
|
|
|
DTE::CommandResult DTE::getResult()
|
|
{
|
|
return result;
|
|
}
|
|
|
|
unsigned int DTE::getMatch() const
|
|
{
|
|
return match;
|
|
}
|
|
|
|
String & DTE::getBuffer()
|
|
{
|
|
return buffer;
|
|
}
|
|
|
|
void DTE::flush()
|
|
{
|
|
// clear rx buffer
|
|
while (stream.available()) {
|
|
stream.read();
|
|
}
|
|
}
|
|
|
|
void DTE::process()
|
|
{
|
|
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.concat(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;
|
|
}
|