Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to change hardware address size #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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;
Expand Down Expand Up @@ -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<const uint8_t*>(&pipe0_reading_address), 5);
write_register(RX_ADDR_P0, reinterpret_cast<const uint8_t*>(&pipe0_reading_address), address_size);

// Flush buffers
flush_rx();
Expand Down Expand Up @@ -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<uint8_t*>(&value), 5);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), 5);
write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), address_size);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), address_size);

const uint8_t max_payload_size = 32;
write_register(RX_PW_P0,min(payload_size,max_payload_size));
Expand Down Expand Up @@ -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<const uint8_t*>(&address), 5);
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), address_size);
else
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 1);

Expand Down
10 changes: 10 additions & 0 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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
*
Expand Down