Skip to content

Commit

Permalink
Update examples, minimize code size
Browse files Browse the repository at this point in the history
- Updated some example files and tests
- Small changes to minimize code size
  • Loading branch information
TMRh20 committed Apr 15, 2014
1 parent 5fabf26 commit e83a107
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 146 deletions.
112 changes: 35 additions & 77 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,55 +235,39 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)

uint8_t RF24::flush_rx(void)
{
uint8_t status;

#if defined (__arm__)
status = SPI.transfer(csn_pin, FLUSH_RX );
#else
csn(LOW);
status = SPI.transfer( FLUSH_RX );
csn(HIGH);

#endif

return status;
return spiTrans( FLUSH_TX );
}

/****************************************************************************/

uint8_t RF24::flush_tx(void)
{
uint8_t status;
return spiTrans( FLUSH_TX );
}

/****************************************************************************/

uint8_t RF24::spiTrans(uint8_t cmd){

uint8_t status;
#if defined (__arm__)
status = SPI.transfer(csn_pin, FLUSH_TX );
status = SPI.transfer(csn_pin, cmd );
#else

csn(LOW);
status = SPI.transfer( FLUSH_TX );
status = SPI.transfer( cmd );
csn(HIGH);

#endif

return status;

}

/****************************************************************************/

uint8_t RF24::get_status(void)
{
uint8_t status;

#if defined (__arm__)
status = SPI.transfer(csn_pin, NOP );
#else

csn(LOW);
status = SPI.transfer( NOP );
csn(HIGH);
#endif

return status;
return spiTrans(NOP);
}

/****************************************************************************/
Expand Down Expand Up @@ -347,7 +331,7 @@ 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(false), p_variant(false),
payload_size(32), ack_payload_available(false), dynamic_payloads_enabled(false),
payload_size(32), dynamic_payloads_enabled(false),
pipe0_reading_address(0)
{
}
Expand All @@ -367,8 +351,7 @@ void RF24::setChannel(uint8_t channel)

void RF24::setPayloadSize(uint8_t size)
{
const uint8_t max_payload_size = 32;
payload_size = min(size,max_payload_size);
payload_size = min(size,32);
}

/****************************************************************************/
Expand All @@ -380,7 +363,7 @@ uint8_t RF24::getPayloadSize(void)

/****************************************************************************/


#if !defined (MINIMAL)

static const char rf24_datarate_e_str_0[] PROGMEM = "1MBPS";
static const char rf24_datarate_e_str_1[] PROGMEM = "2MBPS";
Expand Down Expand Up @@ -414,7 +397,7 @@ static const char * const rf24_pa_dbm_e_str_P[] PROGMEM = {
rf24_pa_dbm_e_str_2,
rf24_pa_dbm_e_str_3,
};
#if !defined (MINIMAL)

void RF24::printDetails(void)
{
print_status(get_status());
Expand Down Expand Up @@ -479,8 +462,8 @@ void RF24::begin(void)
// sizes must never be used. See documentation for a more complete explanation.
setRetries(5,15);

// Restore our default PA level
setPALevel( RF24_PA_MAX ) ;
// Reset value is MAX
//setPALevel( RF24_PA_MAX ) ;

// Determine if this is a p or non-p RF24 module and then
// reset our data rate back to default value. This works
Expand All @@ -498,8 +481,8 @@ void RF24::begin(void)
// Initialize CRC and request 2-byte (16bit) CRC
setCRCLength( RF24_CRC_16 ) ;

// Disable dynamic payloads, to match dynamic_payloads_enabled setting
write_register(DYNPD,0);
// Disable dynamic payloads, to match dynamic_payloads_enabled setting - Reset value is 0
//write_register(DYNPD,0);

// Reset current status
// Notice reset and flush is the last thing we do
Expand Down Expand Up @@ -532,8 +515,9 @@ void RF24::startListening(void)
write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );

// Restore the pipe0 adddress, if exists
if (pipe0_reading_address)
if (pipe0_reading_address){
write_register(RX_ADDR_P0, reinterpret_cast<const uint8_t*>(&pipe0_reading_address), 5);
}

// Flush buffers
flush_rx();
Expand Down Expand Up @@ -636,16 +620,11 @@ bool RF24::writeBlocking( const void* buf, uint8_t len, uint32_t timeout )
return 1; //Return 1 to indicate successful transmission
}

/****************************************************************************/

void RF24::reUseTX(){
write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag
#if defined (__arm__)
SPI.transfer(csn_pin, REUSE_TX_PL );
#else
csn(LOW);
SPI.transfer( REUSE_TX_PL );
csn(HIGH);
#endif
spiTrans( REUSE_TX_PL );
ce(LOW); //Re-Transfer packet
ce(HIGH);
}
Expand All @@ -659,11 +638,11 @@ bool RF24::writeFast( const void* buf, uint8_t len, const bool multicast )
//Return 0 so the user can control the retrys and set a timer or failure counter if required
//The radio will auto-clear everything in the FIFO as long as CE remains high


while ( (read_register(FIFO_STATUS) & _BV(FIFO_FULL))){ //Blocking only if FIFO is full. This will loop and block until TX is successful

if( get_status() & _BV(MAX_RT)){
reUseTX(); //Set re-transmit
//reUseTX(); //Set re-transmit
write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag
return 0; //Return 0. The previous payload has been retransmitted
//From the user perspective, if you get a 0, just keep trying to send the same payload
}
Expand All @@ -689,7 +668,7 @@ bool RF24::writeFast( const void* buf, uint8_t len ){
void RF24::startFastWrite( const void* buf, uint8_t len, const bool multicast){ //TMRh20

//write_payload( buf,len);
write_payload( buf, len,multicast?static_cast<uint8_t>(W_TX_PAYLOAD_NO_ACK):static_cast<uint8_t>(W_TX_PAYLOAD) ) ;
write_payload( buf, len,multicast ? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ;
ce(HIGH);

}
Expand All @@ -702,7 +681,8 @@ void RF24::startWrite( const void* buf, uint8_t len, const bool multicast ){
// Send the payload

//write_payload( buf, len );
write_payload( buf, len,multicast?static_cast<uint8_t>(W_TX_PAYLOAD_NO_ACK):static_cast<uint8_t>(W_TX_PAYLOAD) ) ;
write_payload( buf, len,multicast? W_TX_PAYLOAD_NO_ACK : W_TX_PAYLOAD ) ;

ce(HIGH);
ce(LOW);

Expand Down Expand Up @@ -836,8 +816,9 @@ void RF24::openWritingPipe(uint64_t value)
write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), 5);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), 5);

const uint8_t max_payload_size = 32;
write_register(RX_PW_P0,min(payload_size,max_payload_size));
//const uint8_t max_payload_size = 32;
//write_register(RX_PW_P0,min(payload_size,max_payload_size));
write_register(RX_PW_P0,payload_size);
}

/****************************************************************************/
Expand Down Expand Up @@ -901,15 +882,10 @@ void RF24::toggle_features(void)
void RF24::enableDynamicPayloads(void)
{
// Enable dynamic payload throughout the system
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) );

// If it didn't work, the features are not enabled
if ( ! read_register(FEATURE) )
{
// So enable them and try again
toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) );
}


IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));

Expand All @@ -930,15 +906,8 @@ void RF24::enableAckPayload(void)
// enable ack payload and dynamic payload features
//

write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) );

// If it didn't work, the features are not enabled
if ( ! read_register(FEATURE) )
{
// So enable them and try again
toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) );
}

IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));

Expand All @@ -955,15 +924,8 @@ void RF24::enableDynamicAck(void){
//
// enable dynamic ack features
//

write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) );

// If it didn't work, the features are not enabled
if ( ! read_register(FEATURE) & _BV(EN_DYN_ACK) ){
// So enable them and try again
toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) );
}

IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));

Expand All @@ -976,8 +938,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
{
const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);

const uint8_t max_payload_size = 32;
uint8_t data_len = min(len,max_payload_size);
uint8_t data_len = min(len,32);

#if defined (__arm__)
SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE);
Expand All @@ -1002,10 +963,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)

bool RF24::isAckPayloadAvailable(void)
{
if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){
return 1;
}
return 0;
return ! read_register(FIFO_STATUS) & _BV(RX_EMPTY);
}

/****************************************************************************/
Expand Down
6 changes: 3 additions & 3 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +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 */
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. */
uint64_t pipe0_reading_address; /**< Last address set on pipe 0 for reading. */

public:
Expand Down Expand Up @@ -907,6 +905,8 @@ class RF24
* are enabled. See the datasheet for details.
*/
void toggle_features(void);

uint8_t spiTrans(uint8_t cmd);
/**@}*/

};
Expand Down Expand Up @@ -1040,7 +1040,7 @@ class RF24
*/

/**
* @example scanner.pde
* @example scanner.ino
*
* Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a
Expand Down
9 changes: 4 additions & 5 deletions examples/GettingStarted/GettingStarted.ino
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ void loop(void){

radio.stopListening(); // First, stop listening so we can talk.

unsigned long time;
printf("Now sending ",time);

printf("Now sending \n\r");

time = micros(); // Take the time, and send it. This will block until complete
boolean ok = radio.write( &time, sizeof(unsigned long) ); // Send the current time
if (!ok){ printf("failed.\n\r"); }
unsigned long time = micros(); // Take the time, and send it. This will block until complete
if (!radio.write( &time, sizeof(unsigned long) )){ printf("failed.\n\r"); }

radio.startListening(); // Now, continue listening

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
// March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
// Parts derived from examples by J. Coliz <maniacbug@ymail.com>
March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
Parts derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/**
* Example for efficient call-response using ack-payloads
*
Expand Down
8 changes: 4 additions & 4 deletions examples/Transfer/printf.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

/**
* @file printf.h
*
Expand All @@ -18,12 +18,12 @@

#ifdef ARDUINO

int serial_putc( char c, FILE * )
int serial_putc( char c, FILE * )
{
Serial.write( c );

return c;
}
}

void printf_begin(void)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/pingpair_ack/pingpair_ack.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
// March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
// Parts derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/**
* Example for efficient call-response using ack-payloads
*
Expand Down
Loading

0 comments on commit e83a107

Please sign in to comment.