Asynchronous Serial Library with flow control and tag detection.
Diff: source/AsyncSerial.cpp
- Revision:
- 4:e0a0eef4ca18
- Parent:
- 3:af3caa18e928
- Child:
- 5:aecd37846dcc
--- a/source/AsyncSerial.cpp Wed Apr 01 13:41:38 2015 +0000 +++ b/source/AsyncSerial.cpp Wed Apr 01 15:26:57 2015 +0100 @@ -14,17 +14,21 @@ sendMember(), sendDoneObject(NULL), - receiveBuffer(NULL), - receiveMaxLength(0), - receiveIndex(0), - receiveDoneHandler(NULL), receiveObject(NULL), receiveMember(), receiveDoneObject(NULL), - conditionBuffer(NULL), - conditionLength(0), + isReceiving(false), + receiveBuffer(NULL), + receiveMaxLength(0), + receiveIndex(0), + + insideCondition(false), + conditionStartBuffer(NULL), + conditionStartLength(0), + conditionEndBuffer(NULL), + conditionEndLength(0), conditionIndex(0), timeout() { @@ -62,33 +66,59 @@ void AsyncSerial::getReady() { - uint8_t input = SerialBase::_base_getc(); - - if (receiveMaxLength > 0) + if (isReceiving) { - receiveBuffer[receiveIndex] = input; - receiveIndex++; + uint8_t input = SerialBase::_base_getc(); - if (receiveIndex == receiveMaxLength) + if (insideCondition) { - timeout.detach(); - getDone(); - } - else if (conditionLength > 0) - { - if (receiveBuffer[receiveIndex - 1] == conditionBuffer[conditionIndex]) + if (receiveBuffer != NULL) { - conditionIndex++; + receiveBuffer[receiveIndex] = input; + receiveIndex++; - if (conditionIndex == conditionLength) + if (receiveIndex == receiveMaxLength) { timeout.detach(); getDone(); } } - else + + if (conditionEndBuffer != NULL) { - conditionIndex = 0; + if (input == conditionEndBuffer[conditionIndex]) + { + conditionIndex++; + + if (conditionIndex == conditionEndLength) + { + timeout.detach(); + getDone(); + } + } + else + { + conditionIndex = 0; + } + } + } + else + { + if (conditionStartBuffer != NULL) + { + if (input == conditionStartBuffer[conditionIndex]) + { + conditionIndex++; + + if (conditionIndex == conditionStartLength) + { + insideCondition = true; + } + } + else + { + conditionIndex = 0; + } } } } @@ -96,7 +126,7 @@ void AsyncSerial::getDone() { - receiveMaxLength = 0; + isReceiving = false; if (receiveDoneHandler) { @@ -133,7 +163,8 @@ void AsyncSerial::receive(receive_done_t handler, uint8_t* buffer, uint16_t maxLength, - const uint8_t* conditionBuffer, uint16_t conditionLength, + const uint8_t* conditionStartBuffer, uint16_t conditionStartLength, + const uint8_t* conditionEndBuffer, uint16_t conditionEndLength, uint32_t timeoutMilli) { if (handler) @@ -142,23 +173,39 @@ receiveObject = NULL; - receive(buffer, maxLength, conditionBuffer, conditionLength, timeoutMilli); + receive(buffer, maxLength, + conditionStartBuffer, conditionStartLength, + conditionEndBuffer, conditionEndLength, + timeoutMilli); } } void AsyncSerial::receive(uint8_t* buffer, uint16_t maxLength, - const uint8_t* _conditionBuffer, uint16_t _conditionLength, + const uint8_t* _conditionStartBuffer, uint16_t _conditionStartLength, + const uint8_t* _conditionEndBuffer, uint16_t _conditionEndLength, uint32_t timeoutMilli) { receiveBuffer = buffer; receiveMaxLength = maxLength; receiveIndex = 0; - conditionBuffer = _conditionBuffer; - conditionLength = _conditionLength; + conditionEndBuffer = _conditionEndBuffer; + conditionEndLength = _conditionEndLength; conditionIndex = 0; + if ((_conditionStartBuffer != NULL) && (_conditionStartLength != 0)) + { + insideCondition = false; + conditionStartBuffer = _conditionStartBuffer; + conditionStartLength = _conditionStartLength; + } + else + { + insideCondition = true; + } + timeout.attach_us<AsyncSerial>(this, &AsyncSerial::getDone, timeoutMilli * 1000); + isReceiving = true; } int AsyncSerial::getc()