diff options
author | Cristian Maglie <c.maglie@bug.st> | 2013-06-05 11:50:33 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-06-05 11:50:33 +0200 |
commit | 9f9bca0f197ee7920dc4daa81cbb4c6698e660a7 (patch) | |
tree | df3f08342f37d5e4ad02a1d69fa0a6a80a80b808 /libraries/Bridge | |
parent | 5b8fe8acbaa29d5e2673be4a1f1dbf8a4a1d784c (diff) |
Bridge protocol frame lenght is now a 16-bit field. Added more efficient Bridge.trasnfer() methods.
Diffstat (limited to 'libraries/Bridge')
-rw-r--r-- | libraries/Bridge/Bridge.cpp | 46 | ||||
-rw-r--r-- | libraries/Bridge/Bridge.h | 16 |
2 files changed, 47 insertions, 15 deletions
diff --git a/libraries/Bridge/Bridge.cpp b/libraries/Bridge/Bridge.cpp index f13f909..2aafc52 100644 --- a/libraries/Bridge/Bridge.cpp +++ b/libraries/Bridge/Bridge.cpp @@ -168,21 +168,34 @@ bool BridgeClass::crcCheck(uint16_t _CRC) { return CRC == _CRC; } -uint8_t BridgeClass::transfer(const uint8_t *buff, uint8_t len, - uint8_t *rxbuff, uint8_t rxlen) +uint8_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1, + const uint8_t *buff2, uint16_t len2, + const uint8_t *buff3, uint16_t len3, + uint8_t *rxbuff, uint16_t rxlen) { + uint16_t len = len1 + len2 + len3; for ( ; ; delay(100), dropAll() /* Delay for retransmission */) { // Send packet crcReset(); - write((char)0xFF); // Start of packet (0xFF) + write((char)0xFF); // Start of packet (0xFF) crcUpdate(0xFF); - write((char)index); // Message index + write((char)index); // Message index crcUpdate(index); - write((char)len); // Message length - crcUpdate(len); - for (uint8_t i=0; i<len; i++) { // Payload - write((char)buff[i]); - crcUpdate(buff[i]); + write((char)((len >> 8) & 0xFF)); // Message length (hi) + crcUpdate((len >> 8) & 0xFF); + write((char)(len & 0xFF)); // Message length (lo) + crcUpdate(len & 0xFF); + for (uint8_t i=0; i<len1; i++) { // Payload + write((char)buff1[i]); + crcUpdate(buff1[i]); + } + for (uint8_t i=0; i<len2; i++) { // Payload + write((char)buff2[i]); + crcUpdate(buff2[i]); + } + for (uint8_t i=0; i<len3; i++) { // Payload + write((char)buff3[i]); + crcUpdate(buff3[i]); } crcWrite(); // CRC @@ -198,13 +211,20 @@ uint8_t BridgeClass::transfer(const uint8_t *buff, uint8_t len, crcUpdate(index); // Recv len - uint8_t l = timedRead(5); - if (l < 0) + int lh = timedRead(5); + if (lh < 0) + continue; + crcUpdate(lh); + int ll = timedRead(5); + if (ll < 0) continue; - crcUpdate(l); + crcUpdate(ll); + uint16_t l = lh; + l <<= 8; + l += ll; // Recv data - for (uint8_t i=0; i<l; i++) { + for (uint16_t i=0; i<l; i++) { int c = timedRead(5); if (c < 0) continue; diff --git a/libraries/Bridge/Bridge.h b/libraries/Bridge/Bridge.h index ff5cd5f..c7b5361 100644 --- a/libraries/Bridge/Bridge.h +++ b/libraries/Bridge/Bridge.h @@ -65,8 +65,20 @@ public: void flush() { stream.flush(); } // Trasnfer a frame (with error correction and response) - uint8_t transfer(const uint8_t *buff, uint8_t len, - uint8_t *rxbuff=NULL, uint8_t rxlen=0); + uint8_t transfer(const uint8_t *buff1, uint16_t len1, + const uint8_t *buff2, uint16_t len2, + const uint8_t *buff3, uint16_t len3, + uint8_t *rxbuff, uint16_t rxlen); + // multiple inline versions of the same function to allow efficient frame concatenation + uint8_t transfer(const uint8_t *buff1, uint16_t len1) + { transfer(buff1, len1, NULL, 0); } + uint8_t transfer(const uint8_t *buff1, uint16_t len1, + uint8_t *rxbuff, uint16_t rxlen) + { transfer(buff1, len1, NULL, 0, rxbuff, rxlen); } + uint8_t transfer(const uint8_t *buff1, uint16_t len1, + const uint8_t *buff2, uint16_t len2, + uint8_t *rxbuff, uint16_t rxlen) + { transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); } private: uint8_t index; int timedRead(unsigned int timeout); |