Library for HopeRF RFM22 / RFM22B transceiver module ported to mbed. Original Software from Mike McCauley (mikem@open.com.au) . See http://www.open.com.au/mikem/arduino/RF22/
Dependents: RF22_MAX_test_Send Geofence_receiver Geofence_sender Geofence_sender ... more
More Info about RFM22-modules like connecting and a demo-program see RF22-Notebook
Diff: RF22ReliableDatagram.cpp
- Revision:
- 5:0386600f3408
- Parent:
- 1:813d4f57d630
--- a/RF22ReliableDatagram.cpp Sun Feb 19 21:12:10 2012 +0000 +++ b/RF22ReliableDatagram.cpp Sat Mar 02 20:49:07 2013 +0000 @@ -28,24 +28,28 @@ //////////////////////////////////////////////////////////////////// // Public methods -void RF22ReliableDatagram::setTimeout(uint16_t timeout) { +void RF22ReliableDatagram::setTimeout(uint16_t timeout) +{ _timeout = timeout; } //////////////////////////////////////////////////////////////////// -void RF22ReliableDatagram::setRetries(uint8_t retries) { +void RF22ReliableDatagram::setRetries(uint8_t retries) +{ _retries = retries; } //////////////////////////////////////////////////////////////////// -boolean RF22ReliableDatagram::sendtoWait(uint8_t* buf, uint8_t len, uint8_t address) { +boolean RF22ReliableDatagram::sendtoWait(uint8_t* buf, uint8_t len, uint8_t address) +{ // Assemble the message uint8_t thisSequenceNumber = ++_lastSequenceNumber; Timer t; uint8_t retries = 0; - while (retries++ <= _retries) { + while (retries++ <= _retries) + { setHeaderId(thisSequenceNumber); setHeaderFlags(0); sendto(buf, len, address); @@ -62,10 +66,11 @@ // Compute a new timeout, random between _timeout and _timeout*2 - // This is to prevent collissions on every retransmit + // This is to prevent collisions on every retransmit // if 2 nodes try to transmit at the same time uint16_t timeout = _timeout + (_timeout * (rand() % 100) / 100); - while (t.read_ms() < (thisSendTime + timeout)) { + while (t.read_ms() < (thisSendTime + timeout)) + { if (available()) { clearRxBuf(); // Not using recv, so clear it ourselves uint8_t from = headerFrom(); @@ -76,11 +81,14 @@ if ( from == address && to == _thisAddress && (flags & RF22_FLAGS_ACK) - && (id == thisSequenceNumber)) { + && (id == thisSequenceNumber)) + { // Its the ACK we are waiting for return true; - } else if ( !(flags & RF22_FLAGS_ACK) - && (id == _seenIds[from])) { + } + else if ( !(flags & RF22_FLAGS_ACK) + && (id == _seenIds[from])) + { // This is a request we have already received. ACK it again acknowledge(id, from); } @@ -94,23 +102,28 @@ } //////////////////////////////////////////////////////////////////// -boolean RF22ReliableDatagram::recvfromAck(uint8_t* buf, uint8_t* len, uint8_t* from, uint8_t* to, uint8_t* id, uint8_t* flags) { +boolean RF22ReliableDatagram::recvfromAck(uint8_t* buf, uint8_t* len, uint8_t* from, uint8_t* to, uint8_t* id, uint8_t* flags) +{ uint8_t _from; uint8_t _to; uint8_t _id; uint8_t _flags; // Get the message before its clobbered by the ACK (shared rx anfd tx buffer in RF22 - if (available() && recvfrom(buf, len, &_from, &_to, &_id, &_flags)) { + if (available() && recvfrom(buf, len, &_from, &_to, &_id, &_flags)) + { // Never ACK an ACK - if (!(_flags & RF22_FLAGS_ACK)) { + if (!(_flags & RF22_FLAGS_ACK)) + { // Its a normal message for this node, not an ACK - if (_to != RF22_BROADCAST_ADDRESS) { + if (_to != RF22_BROADCAST_ADDRESS) + { // Its not a broadcast, so ACK it // Acknowledge message with ACK set in flags and ID set to received ID acknowledge(_id, _from); } // If we have not seen this message before, then we are interested in it - if (_id != _seenIds[_from]) { + if (_id != _seenIds[_from]) + { if (from) *from = _from; if (to) *to = _to; if (id) *id = _id; @@ -134,11 +147,13 @@ return false; } -uint16_t RF22ReliableDatagram::retransmissions() { +uint16_t RF22ReliableDatagram::retransmissions() +{ return _retransmissions; } -void RF22ReliableDatagram::acknowledge(uint8_t id, uint8_t from) { +void RF22ReliableDatagram::acknowledge(uint8_t id, uint8_t from) +{ setHeaderId(id); setHeaderFlags(RF22_FLAGS_ACK); // We would prefer to send a zero length ACK, @@ -150,4 +165,3 @@ sendto(&ack, sizeof(ack), from); waitPacketSent(); } -