Skip to content

Commit

Permalink
Merge pull request #40 from dparson55/discardData
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
dparson55 authored Jun 4, 2020
2 parents 91bcf26 + 642b925 commit c10e6a0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
44 changes: 29 additions & 15 deletions examples/MultiplePacketSizes_RX/MultiplePacketSizes_RX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,38 @@ void loop()
// 'hasData' returns the size of the packet that was received, or 0 if there is no data.
uint8_t packetSize = _radio.hasData();

if (packetSize == sizeof(RadioPacket1))
if (packetSize > 0)
{
_radio.readData(&_radioData1);
if (packetSize == sizeof(RadioPacket1))
{
_radio.readData(&_radioData1);

Serial.print("Received ");
Serial.print(_radioData1.Counter);
Serial.print(" from radio ");
Serial.println(_radioData1.FromRadioId);
}
else if (packetSize == sizeof(RadioPacket2))
{
_radio.readData(&_radioData2);
Serial.print("Received ");
Serial.print(_radioData1.Counter);
Serial.print(" from radio ");
Serial.println(_radioData1.FromRadioId);
}
else if (packetSize == sizeof(RadioPacket2))
{
_radio.readData(&_radioData2);

String msg = String(_radioData2.Message);

Serial.print("Received '");
Serial.print(msg);
Serial.print("' from radio ");
Serial.println(_radioData2.FromRadioId);
}
else
{
// We have a packet with an unexpected size, either an invalid packet or packet
// that was sent from a radio that we cannot handle, so remove it from the radio.

String msg = String(_radioData2.Message);
Serial.print("Received packet with length '");
Serial.print(packetSize);
Serial.println("', it will be discarded");

Serial.print("Received '");
Serial.print(msg);
Serial.print("' from radio ");
Serial.println(_radioData2.FromRadioId);
_radio.discardData(packetSize);
}
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=NRFLite
version=2.2.2
version=2.3.0
author=Dave Parson <dparson55@hotmail.com>
maintainer=Dave Parson <dparson55@hotmail.com>
sentence=nRF24L01+ library requiring very little code along with YouTube videos showing all available features.
Expand Down
14 changes: 14 additions & 0 deletions src/NRFLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ void NRFLite::addAckData(void *data, uint8_t length, uint8_t removeExistingAcks)
spiTransfer(WRITE_OPERATION, (W_ACK_PAYLOAD | 1), data, length);
}

void NRFLite::discardData(uint8_t unexpectedDataLength)
{
// Read data from the RX buffer.
uint8_t data[unexpectedDataLength];
spiTransfer(READ_OPERATION, R_RX_PAYLOAD, &data, unexpectedDataLength);

// Clear data received flag.
uint8_t statusReg = readRegister(STATUS_NRF);
if (statusReg & _BV(RX_DR))
{
writeRegister(STATUS_NRF, statusReg | _BV(RX_DR));
}
}

uint8_t NRFLite::hasAckData()
{
// If we have a pipe 0 packet sitting at the top of the RX buffer, we have auto-acknowledgment data.
Expand Down
14 changes: 8 additions & 6 deletions src/NRFLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ class NRFLite {
uint8_t hasAckData();

// Methods for receivers.
// hasData = Checks to see if a data packet has been received and returns its length. It also puts the radio in RX mode
// if it was previously in TX mode.
// addAckData = Enqueues an acknowledgment data packet for sending back to a transmitter. Whenever the transmitter sends the
// next data packet, it will get this ACK packet back in the response. The radio will store up to 3 ACK packets
// and will not enqueue more if full, so you can clear any stale packets using the 'removeExistingAcks' parameter.
// hasData = Checks to see if a data packet has been received and returns its length. It also puts the radio in RX mode
// if it was previously in TX mode.
// addAckData = Enqueues an acknowledgment data packet for sending back to a transmitter. Whenever the transmitter sends the
// next data packet, it will get this ACK packet back in the response. The radio will store up to 3 ACK packets
// and will not enqueue more if full, so you can clear any stale packets using the 'removeExistingAcks' parameter.
// discardData = Removes the current received data packet. Useful if a packet of an unexpected size is received.
uint8_t hasData(uint8_t usingInterrupts = 0);
void addAckData(void *data, uint8_t length, uint8_t removeExistingAcks = 0);
void addAckData(void *data, uint8_t length, uint8_t removeExistingAcks = 0);
void discardData(uint8_t unexpectedDataLength);

// Methods when using the radio's IRQ pin for interrupts.
// Note that if interrupts are used, do not use the send and hasData functions. Instead the functions below should be used.
Expand Down

0 comments on commit c10e6a0

Please sign in to comment.