Update examples, minimize code size

- Updated some example files and tests
- Small changes to minimize code size
This commit is contained in:
TMRh20
2014-04-15 09:40:36 -06:00
parent 5fabf2608a
commit e83a107bdf
12 changed files with 97 additions and 146 deletions
+35 -77
View File
@@ -235,55 +235,39 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)
uint8_t RF24::flush_rx(void) uint8_t RF24::flush_rx(void)
{ {
uint8_t status;
#if defined (__arm__) return spiTrans( FLUSH_TX );
status = SPI.transfer(csn_pin, FLUSH_RX );
#else
csn(LOW);
status = SPI.transfer( FLUSH_RX );
csn(HIGH);
#endif
return status;
} }
/****************************************************************************/ /****************************************************************************/
uint8_t RF24::flush_tx(void) 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__) #if defined (__arm__)
status = SPI.transfer(csn_pin, FLUSH_TX ); status = SPI.transfer(csn_pin, cmd );
#else #else
csn(LOW); csn(LOW);
status = SPI.transfer( FLUSH_TX ); status = SPI.transfer( cmd );
csn(HIGH); csn(HIGH);
#endif #endif
return status; return status;
} }
/****************************************************************************/ /****************************************************************************/
uint8_t RF24::get_status(void) uint8_t RF24::get_status(void)
{ {
uint8_t status; return spiTrans(NOP);
#if defined (__arm__)
status = SPI.transfer(csn_pin, NOP );
#else
csn(LOW);
status = SPI.transfer( NOP );
csn(HIGH);
#endif
return status;
} }
/****************************************************************************/ /****************************************************************************/
@@ -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): RF24::RF24(uint8_t _cepin, uint8_t _cspin):
ce_pin(_cepin), csn_pin(_cspin), wide_band(false), p_variant(false), 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) pipe0_reading_address(0)
{ {
} }
@@ -367,8 +351,7 @@ void RF24::setChannel(uint8_t channel)
void RF24::setPayloadSize(uint8_t size) void RF24::setPayloadSize(uint8_t size)
{ {
const uint8_t max_payload_size = 32; payload_size = min(size,32);
payload_size = min(size,max_payload_size);
} }
/****************************************************************************/ /****************************************************************************/
@@ -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_0[] PROGMEM = "1MBPS";
static const char rf24_datarate_e_str_1[] PROGMEM = "2MBPS"; static const char rf24_datarate_e_str_1[] PROGMEM = "2MBPS";
@@ -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_2,
rf24_pa_dbm_e_str_3, rf24_pa_dbm_e_str_3,
}; };
#if !defined (MINIMAL)
void RF24::printDetails(void) void RF24::printDetails(void)
{ {
print_status(get_status()); print_status(get_status());
@@ -479,8 +462,8 @@ void RF24::begin(void)
// sizes must never be used. See documentation for a more complete explanation. // sizes must never be used. See documentation for a more complete explanation.
setRetries(5,15); setRetries(5,15);
// Restore our default PA level // Reset value is MAX
setPALevel( RF24_PA_MAX ) ; //setPALevel( RF24_PA_MAX ) ;
// Determine if this is a p or non-p RF24 module and then // Determine if this is a p or non-p RF24 module and then
// reset our data rate back to default value. This works // reset our data rate back to default value. This works
@@ -498,8 +481,8 @@ void RF24::begin(void)
// Initialize CRC and request 2-byte (16bit) CRC // Initialize CRC and request 2-byte (16bit) CRC
setCRCLength( RF24_CRC_16 ) ; setCRCLength( RF24_CRC_16 ) ;
// Disable dynamic payloads, to match dynamic_payloads_enabled setting // Disable dynamic payloads, to match dynamic_payloads_enabled setting - Reset value is 0
write_register(DYNPD,0); //write_register(DYNPD,0);
// Reset current status // Reset current status
// Notice reset and flush is the last thing we do // Notice reset and flush is the last thing we do
@@ -532,8 +515,9 @@ void RF24::startListening(void)
write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
// Restore the pipe0 adddress, if exists // 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); write_register(RX_ADDR_P0, reinterpret_cast<const uint8_t*>(&pipe0_reading_address), 5);
}
// Flush buffers // Flush buffers
flush_rx(); flush_rx();
@@ -636,16 +620,11 @@ bool RF24::writeBlocking( const void* buf, uint8_t len, uint32_t timeout )
return 1; //Return 1 to indicate successful transmission return 1; //Return 1 to indicate successful transmission
} }
/****************************************************************************/
void RF24::reUseTX(){ void RF24::reUseTX(){
write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag
#if defined (__arm__) spiTrans( REUSE_TX_PL );
SPI.transfer(csn_pin, REUSE_TX_PL );
#else
csn(LOW);
SPI.transfer( REUSE_TX_PL );
csn(HIGH);
#endif
ce(LOW); //Re-Transfer packet ce(LOW); //Re-Transfer packet
ce(HIGH); ce(HIGH);
} }
@@ -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 //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 //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 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)){ 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 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 //From the user perspective, if you get a 0, just keep trying to send the same payload
} }
@@ -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 void RF24::startFastWrite( const void* buf, uint8_t len, const bool multicast){ //TMRh20
//write_payload( buf,len); //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(HIGH);
} }
@@ -702,7 +681,8 @@ void RF24::startWrite( const void* buf, uint8_t len, const bool multicast ){
// Send the payload // Send the payload
//write_payload( buf, len ); //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(HIGH);
ce(LOW); ce(LOW);
@@ -836,8 +816,9 @@ void RF24::openWritingPipe(uint64_t value)
write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), 5); write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), 5);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), 5); write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), 5);
const uint8_t max_payload_size = 32; //const uint8_t max_payload_size = 32;
write_register(RX_PW_P0,min(payload_size,max_payload_size)); //write_register(RX_PW_P0,min(payload_size,max_payload_size));
write_register(RX_PW_P0,payload_size);
} }
/****************************************************************************/ /****************************************************************************/
@@ -901,15 +882,10 @@ void RF24::toggle_features(void)
void RF24::enableDynamicPayloads(void) void RF24::enableDynamicPayloads(void)
{ {
// Enable dynamic payload throughout the system // 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(); toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) ); write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) );
}
IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE))); IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));
@@ -930,15 +906,8 @@ void RF24::enableAckPayload(void)
// enable ack payload and dynamic payload features // 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(); toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) ); write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) );
}
IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE))); IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));
@@ -955,15 +924,8 @@ void RF24::enableDynamicAck(void){
// //
// enable dynamic ack features // 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(); toggle_features();
write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) ); write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) );
}
IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE))); IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n",read_register(FEATURE)));
@@ -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* current = reinterpret_cast<const uint8_t*>(buf);
const uint8_t max_payload_size = 32; uint8_t data_len = min(len,32);
uint8_t data_len = min(len,max_payload_size);
#if defined (__arm__) #if defined (__arm__)
SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE); SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE);
@@ -1002,10 +963,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
bool RF24::isAckPayloadAvailable(void) bool RF24::isAckPayloadAvailable(void)
{ {
if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){ return ! read_register(FIFO_STATUS) & _BV(RX_EMPTY);
return 1;
}
return 0;
} }
/****************************************************************************/ /****************************************************************************/
+3 -3
View File
@@ -50,9 +50,7 @@ private:
bool wide_band; /* 2Mbs data rate in use? */ bool wide_band; /* 2Mbs data rate in use? */
bool p_variant; /* False for RF24L01 and true for RF24L01P */ bool p_variant; /* False for RF24L01 and true for RF24L01P */
uint8_t payload_size; /**< Fixed size of payloads */ 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. */ 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. */ uint64_t pipe0_reading_address; /**< Last address set on pipe 0 for reading. */
public: public:
@@ -907,6 +905,8 @@ private:
* are enabled. See the datasheet for details. * are enabled. See the datasheet for details.
*/ */
void toggle_features(void); void toggle_features(void);
uint8_t spiTrans(uint8_t cmd);
/**@}*/ /**@}*/
}; };
@@ -1040,7 +1040,7 @@ private:
*/ */
/** /**
* @example scanner.pde * @example scanner.ino
* *
* Example to detect interference on the various channels available. * Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a * This is a good diagnostic tool to check whether you're picking a
+4 -5
View File
@@ -61,12 +61,11 @@ void loop(void){
radio.stopListening(); // First, stop listening so we can talk. radio.stopListening(); // First, stop listening so we can talk.
unsigned long time;
printf("Now sending ",time);
time = micros(); // Take the time, and send it. This will block until complete printf("Now sending \n\r");
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 radio.startListening(); // Now, continue listening
@@ -1,7 +1,7 @@
/* /*
// March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
// Parts derived from examples by J. Coliz <maniacbug@ymail.com> Parts derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/** /**
* Example for efficient call-response using ack-payloads * Example for efficient call-response using ack-payloads
* *
+1 -1
View File
@@ -1,7 +1,7 @@
/* /*
// March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork // March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
// Parts derived from examples by J. Coliz <maniacbug@ymail.com> // Parts derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/** /**
* Example for efficient call-response using ack-payloads * Example for efficient call-response using ack-payloads
* *
+4 -4
View File
@@ -15,7 +15,7 @@
#include <SPI.h> #include <SPI.h>
#include "nRF24L01.h" #include "nRF24L01.h"
#include "RF24.h" #include "RF24.h"
//#include "printf.h" #include "printf.h"
// //
// Hardware configuration // Hardware configuration
@@ -23,7 +23,7 @@
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10); RF24 radio(7,8);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter // Leave open to be the 'ping' transmitter
@@ -74,7 +74,7 @@ void setup(void)
// set up the role pin // set up the role pin
pinMode(role_pin, INPUT); pinMode(role_pin, INPUT);
digitalWrite(role_pin,LOW); digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role // read the address pin, establish our role
@@ -88,7 +88,7 @@ void setup(void)
// //
Serial.begin(57600); Serial.begin(57600);
//printf_begin(); printf_begin();
printf("\n\rRF24/examples/pingpair_dyn/\n\r"); printf("\n\rRF24/examples/pingpair_dyn/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]); printf("ROLE: %s\n\r",role_friendly_name[role]);
+5 -12
View File
@@ -68,12 +68,14 @@ void setup(){
// Open pipes to other node for communication // Open pipes to other node for communication
if ( role == role_sender ) { // This simple sketch opens a pipe on a single address for these two nodes to if ( role == role_sender ) { // This simple sketch opens a pipe on a single address for these two nodes to
radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it. radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it.
radio.openReadingPipe(1,address[1]);
}else{ }else{
radio.openWritingPipe(address[1]);
radio.openReadingPipe(1,address[0]); radio.openReadingPipe(1,address[0]);
radio.startListening(); radio.startListening();
} }
radio.printDetails(); // Dump the configuration of the rf unit for debugging radio.printDetails(); // Dump the configuration of the rf unit for debugging
radio.powerUp(); // Power up the radio delay(50);
attachInterrupt(0, check_radio, FALLING); // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver attachInterrupt(0, check_radio, FALLING); // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver
} }
@@ -87,20 +89,12 @@ void loop() {
if (role == role_sender) { // Sender role. Repeatedly send the current time if (role == role_sender) { // Sender role. Repeatedly send the current time
unsigned long time = millis(); // Take the time, and send it. unsigned long time = millis(); // Take the time, and send it.
printf("Now sending %lu\n\r",time); printf("Now sending %lu\n\r",time);
radio.startWrite( &time, sizeof(unsigned long) ); radio.startWrite( &time, sizeof(unsigned long) ,0);
delay(2000); // Try again soon delay(2000); // Try again soon
} }
if(role == role_receiver){ // Receiver does nothing but sleep between payloads if(role == role_receiver){ // Receiver does nothing except in IRQ
if(!radio.available() && gotMsg == 1 ){ // Display the uptime in millis(). (Counter does not increment during sleep)
gotMsg = 0;
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Put the Arduino to sleep between payloads
sleep_enable();
sleep_mode(); // Arduino goes to sleep here
//ZZZ... sleeping
sleep_disable(); // This happens when the interrupt wakes Arduino up
}
} }
} }
@@ -141,4 +135,3 @@ void check_radio(void) // Receiver role: Does not
} }
} }
} }
+5 -6
View File
@@ -71,7 +71,7 @@ void setup(){
// set up the role pin // set up the role pin
pinMode(role_pin, INPUT); pinMode(role_pin, INPUT);
digitalWrite(role_pin,LOW); digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role // read the address pin, establish our role
@@ -113,7 +113,7 @@ void setup(){
radio.startListening(); radio.startListening();
// Dump the configuration of the rf unit for debugging // Dump the configuration of the rf unit for debugging
radio.printDetails(); //radio.printDetails();
} }
void loop(){ void loop(){
@@ -123,8 +123,9 @@ void loop(){
radio.powerUp(); // Power up the radio after sleeping radio.powerUp(); // Power up the radio after sleeping
radio.stopListening(); // First, stop listening so we can talk. radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending... ");
unsigned long time = millis(); // Take the time, and send it. unsigned long time = millis(); // Take the time, and send it.
printf("Now sending... %lu \n\r",time);
radio.write( &time, sizeof(unsigned long) ); radio.write( &time, sizeof(unsigned long) );
radio.startListening(); // Now, continue listening radio.startListening(); // Now, continue listening
@@ -177,7 +178,7 @@ void loop(){
radio.startListening(); // Now, resume listening so we catch the next packets. radio.startListening(); // Now, resume listening so we catch the next packets.
}else{ }else{
Serial.println("Sleeping"); Serial.println("Sleeping");
delay(200); // Delay so the serial data can print out delay(50); // Delay so the serial data can print out
do_sleep(); do_sleep();
} }
@@ -199,7 +200,6 @@ void setup_watchdog(uint8_t prescalar){
uint8_t wdtcsr = prescalar & 7; uint8_t wdtcsr = prescalar & 7;
if ( prescalar & 8 ) if ( prescalar & 8 )
wdtcsr |= _BV(WDP3); wdtcsr |= _BV(WDP3);
Serial.println(wdtcsr);
MCUSR &= ~_BV(WDRF); // Clear the WD System Reset Flag MCUSR &= ~_BV(WDRF); // Clear the WD System Reset Flag
WDTCSR = _BV(WDCE) | _BV(WDE); // Write the WD Change enable bit to enable changing the prescaler and enable system reset WDTCSR = _BV(WDCE) | _BV(WDE); // Write the WD Change enable bit to enable changing the prescaler and enable system reset
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE); // Write the prescalar bits (how long to sleep, enable the interrupt to wake the MCU WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE); // Write the prescalar bits (how long to sleep, enable the interrupt to wake the MCU
@@ -223,4 +223,3 @@ void do_sleep(void)
detachInterrupt(0); detachInterrupt(0);
WDTCSR &= ~_BV(WDIE); WDTCSR &= ~_BV(WDIE);
} }
@@ -102,13 +102,15 @@ void loop(void)
// Listen for a little // Listen for a little
radio.startListening(); radio.startListening();
delayMicroseconds(128); delayMicroseconds(225);
radio.stopListening();
// Did we get a carrier? // Did we get a carrier?
if ( radio.testCarrier() ) if ( radio.testCarrier() ){
++values[i]; ++values[i];
} }
radio.stopListening();
}
} }
// Print out channel measurements, clamped to a single hex digit // Print out channel measurements, clamped to a single hex digit
+11 -11
View File
@@ -27,11 +27,11 @@ public: RF24Test(int a, int b): RF24(a,b) {}
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24Test radio(8,9); RF24Test radio(48,49);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter // Leave open to be the 'ping' transmitter
const int role_pin = 7; const int role_pin = 5;
// //
// Topology // Topology
@@ -230,28 +230,28 @@ void loop(void)
// Dump the payloads until we've gotten everything // Dump the payloads until we've gotten everything
unsigned long got_time; unsigned long got_time;
bool done = false; bool done = false;
while (!done) while (radio.available())
{ {
// Fetch the payload, and see if this was the last one. // Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) ); radio.read( &got_time, sizeof(unsigned long) );
}
// Spew it
printf("Got payload %lu...",got_time);
// Delay just a little bit to let the other unit // Delay just a little bit to let the other unit
// make the transition to receiver // make the transition to receiver
delay(20); //delay(20);
} //}
// First, stop listening so we can talk // First, stop listening so we can talk
radio.stopListening(); radio.stopListening();
// Spew it
printf("Got payload %lu...",got_time);
// Send the final one back. // Send the final one back.
radio.write( &got_time, sizeof(unsigned long) ); radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets. // Now, resume listening so we catch the next packets.
radio.startListening(); radio.startListening();
printf("Sent response.\n\r");
} }
} }
+12 -12
View File
@@ -29,11 +29,11 @@
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9 // Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9); RF24 radio(7,8);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver // sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter // Leave open to be the 'ping' transmitter
const short role_pin = 7; const short role_pin = 5;
// //
// Topology // Topology
@@ -254,7 +254,7 @@ void setup(void)
// //
attachInterrupt(0, check_radio, FALLING); attachInterrupt(0, check_radio, FALLING);
delay(50);
if ( role == role_receiver ) if ( role == role_receiver )
printf("\n\r+OK "); printf("\n\r+OK ");
} }
@@ -294,7 +294,7 @@ void loop(void)
// Send it. This will block until complete // Send it. This will block until complete
printf("\n\rNow sending length %i...",next_payload_size); printf("\n\rNow sending length %i...",next_payload_size);
radio.startWrite( send_payload, next_payload_size ); radio.startWrite( send_payload, next_payload_size,0 );
// Update size for next time. // Update size for next time.
next_payload_size += payload_size_increments_by; next_payload_size += payload_size_increments_by;
@@ -374,10 +374,9 @@ void check_radio(void)
prbuf_in += sprintf(prbuf_in,"Ack Payload:Failed\n\r"); prbuf_in += sprintf(prbuf_in,"Ack Payload:Failed\n\r");
} }
// Transmitter can power down for now, because // Not powering down since radio is in standby mode
// the transmission is done. //if ( ( tx || fail ) && ( role == role_sender ) )
if ( ( tx || fail ) && ( role == role_sender ) ) //radio.powerDown();
radio.powerDown();
// Did we receive a message? // Did we receive a message?
if ( rx ) if ( rx )
@@ -411,10 +410,11 @@ void check_radio(void)
size_t len = max_payload_size; size_t len = max_payload_size;
memset(receive_payload,0,max_payload_size); memset(receive_payload,0,max_payload_size);
if ( configuration == '3' ) if ( configuration == '3' ){
len = next_payload_size; len = next_payload_size;
else }else{
len = radio.getDynamicPayloadSize(); len = radio.getDynamicPayloadSize();
}
radio.read( receive_payload, len ); radio.read( receive_payload, len );
@@ -427,9 +427,9 @@ void check_radio(void)
// Add an ack packet for the next time around. // Add an ack packet for the next time around.
// Here we will report back how many bytes we got this time. // Here we will report back how many bytes we got this time.
radio.writeAckPayload( pipe_number, &len, sizeof(len) ); radio.writeAckPayload( pipe_number, &len, sizeof(len) );
++message_count; ++message_count;
} }
} }
} }
// vim:ai:cin:sts=2 sw=2 ft=cpp