diff --git a/src/modules/nRF24.cpp b/src/modules/nRF24.cpp index 59e13a04..f4df1cbe 100644 --- a/src/modules/nRF24.cpp +++ b/src/modules/nRF24.cpp @@ -1,6 +1,6 @@ #include "nRF24.h" -nRF24::nRF24(Module* mod) : PhysicalLayer(NRF24_CRYSTAL_FREQ, NRF24_DIV_EXPONENT) { +nRF24::nRF24(Module* mod) : PhysicalLayer(NRF24_CRYSTAL_FREQ, NRF24_DIV_EXPONENT, NRF24_MAX_PACKET_LENGTH) { _mod = mod; } @@ -161,7 +161,7 @@ int16_t nRF24::startTransmit(uint8_t* data, size_t len, uint8_t addr) { (void)addr; // check packet length - if(len > 32) { + if(len > NRF24_MAX_PACKET_LENGTH) { return(ERR_PACKET_TOO_LONG); } @@ -239,19 +239,13 @@ int16_t nRF24::readData(uint8_t* data, size_t len) { return(state); } - // read payload length - uint8_t buff[1]; - SPItransfer(NRF24_CMD_READ_RX_PAYLOAD_WIDTH, false, NULL, buff, 1); - - size_t length = buff[0]; + // get packet length + size_t length = len; + if(len == NRF24_MAX_PACKET_LENGTH) { + length = getPacketLength(); + } // read packet data - if(len == 0) { - // argument 'len' equal to zero indicates String call, which means dynamically allocated data array - // dispose of the original and create a new one - delete[] data; - data = new uint8_t[length + 1]; - } SPIreadRxPayload(data, length); // add terminating null @@ -479,6 +473,13 @@ int16_t nRF24::setFrequencyDeviation(float freqDev) { return(ERR_NONE); } +size_t nRF24::getPacketLength(bool update) { + (void)update; + uint8_t length = 0; + SPItransfer(NRF24_CMD_READ_RX_PAYLOAD_WIDTH, false, NULL, &length, 1); + return((size_t)length); +} + void nRF24::clearIRQ() { // clear status bits _mod->SPIsetRegValue(NRF24_REG_STATUS, NRF24_RX_DR | NRF24_TX_DS | NRF24_MAX_RT, 6, 4); diff --git a/src/modules/nRF24.h b/src/modules/nRF24.h index dfbded20..6b577576 100644 --- a/src/modules/nRF24.h +++ b/src/modules/nRF24.h @@ -9,6 +9,7 @@ // nRF24 physical layer properties (dummy only) #define NRF24_CRYSTAL_FREQ 1.0 #define NRF24_DIV_EXPONENT 0 +#define NRF24_MAX_PACKET_LENGTH 32 // nRF24 SPI commands #define NRF24_CMD_READ 0b00000000 @@ -398,6 +399,15 @@ class nRF24: public PhysicalLayer { */ int16_t setFrequencyDeviation(float freqDev); + /*! + \brief Query modem for the packet length of received payload. + + \param update Update received packet length. Will return cached value when set to false. + + \returns Length of last received packet in bytes. + */ + size_t getPacketLength(bool update = true); + private: Module* _mod;