[nRF24] Implemented getPacketLength
This commit is contained in:
parent
3ad5dfa444
commit
94301c9043
2 changed files with 24 additions and 13 deletions
|
@ -1,6 +1,6 @@
|
||||||
#include "nRF24.h"
|
#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;
|
_mod = mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ int16_t nRF24::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
|
||||||
(void)addr;
|
(void)addr;
|
||||||
|
|
||||||
// check packet length
|
// check packet length
|
||||||
if(len > 32) {
|
if(len > NRF24_MAX_PACKET_LENGTH) {
|
||||||
return(ERR_PACKET_TOO_LONG);
|
return(ERR_PACKET_TOO_LONG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,19 +239,13 @@ int16_t nRF24::readData(uint8_t* data, size_t len) {
|
||||||
return(state);
|
return(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
// read payload length
|
// get packet length
|
||||||
uint8_t buff[1];
|
size_t length = len;
|
||||||
SPItransfer(NRF24_CMD_READ_RX_PAYLOAD_WIDTH, false, NULL, buff, 1);
|
if(len == NRF24_MAX_PACKET_LENGTH) {
|
||||||
|
length = getPacketLength();
|
||||||
size_t length = buff[0];
|
}
|
||||||
|
|
||||||
// read packet data
|
// 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);
|
SPIreadRxPayload(data, length);
|
||||||
|
|
||||||
// add terminating null
|
// add terminating null
|
||||||
|
@ -479,6 +473,13 @@ int16_t nRF24::setFrequencyDeviation(float freqDev) {
|
||||||
return(ERR_NONE);
|
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() {
|
void nRF24::clearIRQ() {
|
||||||
// clear status bits
|
// clear status bits
|
||||||
_mod->SPIsetRegValue(NRF24_REG_STATUS, NRF24_RX_DR | NRF24_TX_DS | NRF24_MAX_RT, 6, 4);
|
_mod->SPIsetRegValue(NRF24_REG_STATUS, NRF24_RX_DR | NRF24_TX_DS | NRF24_MAX_RT, 6, 4);
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// nRF24 physical layer properties (dummy only)
|
// nRF24 physical layer properties (dummy only)
|
||||||
#define NRF24_CRYSTAL_FREQ 1.0
|
#define NRF24_CRYSTAL_FREQ 1.0
|
||||||
#define NRF24_DIV_EXPONENT 0
|
#define NRF24_DIV_EXPONENT 0
|
||||||
|
#define NRF24_MAX_PACKET_LENGTH 32
|
||||||
|
|
||||||
// nRF24 SPI commands
|
// nRF24 SPI commands
|
||||||
#define NRF24_CMD_READ 0b00000000
|
#define NRF24_CMD_READ 0b00000000
|
||||||
|
@ -398,6 +399,15 @@ class nRF24: public PhysicalLayer {
|
||||||
*/
|
*/
|
||||||
int16_t setFrequencyDeviation(float freqDev);
|
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:
|
private:
|
||||||
Module* _mod;
|
Module* _mod;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue