145 lines
3.0 KiB
C++
145 lines
3.0 KiB
C++
#include "DTE.h"
|
|
|
|
DTE::DTE(Stream & stream, unsigned int size)
|
|
: mStream(stream), mBufferSize(size), mResult(EXPECT_RESULT)
|
|
{
|
|
mBuffer.reserve(size);
|
|
}
|
|
|
|
void DTE::SendCommand(const char * command, unsigned long timeout, const char * response1, const char * response2, const char * response3)
|
|
{
|
|
mMatch = 0;
|
|
mResult = EXPECT_BUSY;
|
|
mResponse[0] = response1;
|
|
mResponse[1] = response2;
|
|
mResponse[2] = response3;
|
|
mTimeout = timeout;
|
|
flush();
|
|
mStream.print(command);
|
|
clearBuffer();
|
|
mTick = millis();
|
|
process();
|
|
}
|
|
|
|
void DTE::SendCommand(const __FlashStringHelper * command, unsigned long timeout, const char * response1, const char * response2, const char * response3)
|
|
{
|
|
|
|
mMatch = 0;
|
|
mResult = EXPECT_BUSY;
|
|
mResponse[0] = response1;
|
|
mResponse[1] = response2;
|
|
mResponse[2] = response3;
|
|
mTimeout = timeout;
|
|
// clear rx mBuffer
|
|
flush();
|
|
// send command
|
|
mStream.print((const __FlashStringHelper *) command);
|
|
clearBuffer();
|
|
mTick = millis();
|
|
process();
|
|
}
|
|
|
|
void DTE::Delay(unsigned long delay)
|
|
{
|
|
mTimeout = delay;
|
|
mResult = EXPECT_DELAY;
|
|
mTick = millis();
|
|
process();
|
|
}
|
|
|
|
bool DTE::getIsBusy()
|
|
{
|
|
process();
|
|
return (mResult == EXPECT_BUSY) || (mResult == EXPECT_DELAY);
|
|
}
|
|
|
|
DTE::CommandResult DTE::getResult()
|
|
{
|
|
return mResult;
|
|
}
|
|
|
|
unsigned int DTE::getMatch() const
|
|
{
|
|
return mMatch;
|
|
}
|
|
|
|
String & DTE::getBuffer()
|
|
{
|
|
return mBuffer;
|
|
}
|
|
|
|
void DTE::flush()
|
|
{
|
|
// clear rx mBuffer
|
|
while (mStream.available()) {
|
|
mStream.read();
|
|
}
|
|
}
|
|
|
|
void DTE::process()
|
|
{
|
|
if (mResult == EXPECT_DELAY) {
|
|
if (millis() - mTick >= mTimeout)
|
|
mResult = EXPECT_RESULT;
|
|
|
|
return;
|
|
}
|
|
|
|
if (mResult != EXPECT_BUSY)
|
|
return;
|
|
|
|
char c;
|
|
unsigned long now = millis();
|
|
|
|
while (millis() - mTick < mTimeout) {
|
|
while (mStream.available() && (mBuffer.length() < mBufferSize)) {
|
|
c = mStream.read();
|
|
mBuffer.concat(c);
|
|
if (mBuffer.endsWith(mResponse[0])) {
|
|
mMatch = 0;
|
|
mResult = EXPECT_RESULT;
|
|
return;
|
|
} else if (mResponse[1].length() != 0) {
|
|
if (mBuffer.endsWith(mResponse[1])) {
|
|
mMatch = 1;
|
|
mResult = EXPECT_RESULT;
|
|
return;
|
|
}
|
|
} else if (mResponse[2].length() != 0) {
|
|
if (mBuffer.endsWith(mResponse[2])) {
|
|
mMatch = 2;
|
|
mResult = EXPECT_RESULT;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (millis() - now > 5)
|
|
return;
|
|
}
|
|
|
|
// time out
|
|
mResult = EXPECT_TIMEOUT;
|
|
}
|
|
|
|
bool DTE::buffer()
|
|
{
|
|
|
|
while (mStream.available() > 0 && mBuffer.length() < mBufferSize) {
|
|
char c = mStream.read();
|
|
|
|
if (c >= 0x20) {
|
|
mBuffer.concat(c);
|
|
} else if (c == '\r')
|
|
return true;
|
|
}
|
|
if (mBuffer.length() == mBufferSize)
|
|
clearBuffer();
|
|
return false;
|
|
}
|
|
|
|
void DTE::clearBuffer()
|
|
{
|
|
|
|
mBuffer = "";
|
|
}
|