diff --git a/RF24.cpp b/RF24.cpp index 9471583d..04dff00a 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -240,8 +240,8 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty) RF24::RF24(uint8_t _cepin, uint8_t _cspin): ce_pin(_cepin), csn_pin(_cspin), wide_band(true), p_variant(false), - payload_size(32), ack_payload_available(false), dynamic_payloads_enabled(false), - pipe0_reading_address(0) + payload_size(32), address_size(5), ack_payload_available(false), + dynamic_payloads_enabled(false), pipe0_reading_address(0) { } @@ -266,6 +266,14 @@ void RF24::setPayloadSize(uint8_t size) /****************************************************************************/ +void RF24::setAddressSize(uint8_t size) +{ + address_size = max(3,min(5,size)); + write_register(SETUP_AW, (address_size-2) & 0x03 ); +} + +/****************************************************************************/ + uint8_t RF24::getPayloadSize(void) { return payload_size; @@ -400,7 +408,7 @@ void RF24::startListening(void) // Restore the pipe0 adddress, if exists if (pipe0_reading_address) - write_register(RX_ADDR_P0, reinterpret_cast(&pipe0_reading_address), 5); + write_register(RX_ADDR_P0, reinterpret_cast(&pipe0_reading_address), address_size); // Flush buffers flush_rx(); @@ -606,8 +614,8 @@ void RF24::openWritingPipe(uint64_t value) // Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+) // expects it LSB first too, so we're good. - write_register(RX_ADDR_P0, reinterpret_cast(&value), 5); - write_register(TX_ADDR, reinterpret_cast(&value), 5); + write_register(RX_ADDR_P0, reinterpret_cast(&value), address_size); + write_register(TX_ADDR, reinterpret_cast(&value), address_size); const uint8_t max_payload_size = 32; write_register(RX_PW_P0,min(payload_size,max_payload_size)); @@ -640,7 +648,7 @@ void RF24::openReadingPipe(uint8_t child, uint64_t address) { // For pipes 2-5, only write the LSB if ( child < 2 ) - write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast(&address), 5); + write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast(&address), address_size); else write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast(&address), 1); diff --git a/RF24.h b/RF24.h index 18914f09..4baea079 100644 --- a/RF24.h +++ b/RF24.h @@ -50,6 +50,7 @@ class RF24 bool wide_band; /* 2Mbs data rate in use? */ bool p_variant; /* False for RF24L01 and true for RF24L01P */ uint8_t payload_size; /**< Fixed size of payloads */ + uint8_t address_size; /**< Size of hardware addresses */ bool ack_payload_available; /**< Whether there is an ack payload waiting */ bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */ uint8_t ack_payload_length; /**< Dynamic size of pending ack payload. */ @@ -388,6 +389,15 @@ class RF24 */ void setPayloadSize(uint8_t size); + /** + * Set Hardware Address Size + * + * Set to either 3, 4 or 5 bytes. Default is 5. + * + * @param size The number of bytes in the payload + */ + void setAddressSize(uint8_t size); + /** * Get Static Payload Size *