Skip to content

Commit

Permalink
Simplified example to use less calls.
Browse files Browse the repository at this point in the history
Defaults now to 32-byte payloads, but you can call in with any size under that.  Added example to docs.
  • Loading branch information
james committed Apr 1, 2011
1 parent 0dc43ab commit e97e023
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.o
.*.swp
docs/
6 changes: 3 additions & 3 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ EXCLUDE_SYMBOLS =
# directories that contain example code fragments that are included (see
# the \include command).

EXAMPLE_PATH =
EXAMPLE_PATH = examples

# If the value of the EXAMPLE_PATH tag contains directories, you can use the
# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
Expand All @@ -645,7 +645,7 @@ EXAMPLE_PATTERNS =
# commands irrespective of the value of the RECURSIVE tag.
# Possible values are YES and NO. If left blank NO is used.

EXAMPLE_RECURSIVE = NO
EXAMPLE_RECURSIVE = YES

# The IMAGE_PATH tag can be used to specify one or more files or
# directories that contain image that are included in the documentation (see
Expand Down Expand Up @@ -1548,4 +1548,4 @@ GENERATE_LEGEND = YES
# remove the intermediate dot files that are used to generate
# the various graphs.

DOT_CLEANUP = YES
DOT_CLEANUP = YES
30 changes: 18 additions & 12 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,20 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value)

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

uint8_t RF24::write_payload(const void* buf)
uint8_t RF24::write_payload(const void* buf, uint8_t len)
{
uint8_t status;

const uint8_t* current = (const uint8_t*)buf;

csn(LOW);
status = SPI.transfer( W_TX_PAYLOAD );
uint8_t len = payload_size;
while ( len-- )
uint8_t data_len = min(len,payload_size);
uint8_t blank_len = payload_size - data_len;
while ( data_len-- )
SPI.transfer(*current++);
while ( blank_len-- )
SPI.transfer(0);

csn(HIGH);

Expand All @@ -99,16 +102,19 @@ uint8_t RF24::write_payload(const void* buf)

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

uint8_t RF24::read_payload(void* buf)
uint8_t RF24::read_payload(void* buf, uint8_t len)
{
uint8_t status;
uint8_t* current = (uint8_t*)buf;

csn(LOW);
status = SPI.transfer( R_RX_PAYLOAD );
uint8_t len = payload_size;
while ( len-- )
uint8_t data_len = min(len,payload_size);
uint8_t blank_len = payload_size - data_len;
while ( data_len-- )
*current++ = SPI.transfer(0xff);
while ( blank_len-- )
SPI.transfer(0xff);
csn(HIGH);

return status;
Expand Down Expand Up @@ -181,7 +187,7 @@ void RF24::print_observe_tx(uint8_t value)
/******************************************************************/

RF24::RF24(int _cepin, int _cspin):
ce_pin(_cepin), csn_pin(_cspin)
ce_pin(_cepin), csn_pin(_cspin), payload_size(32)
{
}

Expand All @@ -197,7 +203,6 @@ void RF24::setChannel(int channel)
void RF24::setPayloadSize(uint8_t size)
{
payload_size = min(size,32);
write_register(RX_PW_P0,min(size,32));
}

/******************************************************************/
Expand Down Expand Up @@ -300,15 +305,15 @@ void RF24::stopListening(void)

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

boolean RF24::write( const void* buf )
boolean RF24::write( const void* buf, uint8_t len )
{
boolean result = false;

// Transmitter power-up
write_register(CONFIG, _BV(EN_CRC) | _BV(PWR_UP));

// Send the payload
write_payload( buf );
write_payload( buf, len );

// Allons!
ce(HIGH);
Expand Down Expand Up @@ -366,13 +371,13 @@ boolean RF24::available(void)

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

boolean RF24::read( void* buf )
boolean RF24::read( void* buf, uint8_t len )
{
// was this the last of the data available?
boolean result = false;

// Fetch the payload
read_payload( buf );
read_payload( buf, len );

uint8_t fifo_status;
read_register(FIFO_STATUS,&fifo_status,1);
Expand All @@ -391,6 +396,7 @@ 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);
write_register(RX_PW_P0,min(payload_size,32));
}

/******************************************************************/
Expand Down
47 changes: 36 additions & 11 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
/**
* Driver for nRF24L01 2.4GHz Wireless Transceiver
*
* See <a href="http://www.nordicsemi.com/files/Product/data_sheet/nRF24L01_Product_Specification_v2_0.pdf">Datasheet</a>
* Please refer to:
*
* @li <a href="http://maniacbug.github.com/RF24/classRF24.html">Detailed Documentation</a>
* @li <a href="https://github.com/maniacbug/RF24/">Source Code</a>
* @li <a href="http://www.nordicsemi.com/files/Product/data_sheet/nRF24L01_Product_Specification_v2_0.pdf">Chip Datasheet</a>
*
* This chip uses the SPI bus, plus two chip control pins. Remember that pin 10 must still remain an output, or
* the SPI hardware will go into 'slave' mode.
*
* Design Goals: This library is designed to be...
* * Maximally compliant with the intended operation of the chip
* * Easy for beginners to use
* * Consumed with a public interface that's similiar to other Arduino standard libraries
* * Built against the standard SPI library.
* @li Maximally compliant with the intended operation of the chip
* @li Easy for beginners to use
* @li Consumed with a public interface that's similiar to other Arduino standard libraries
* @li Built against the standard SPI library.
*/

class RF24
Expand Down Expand Up @@ -91,19 +95,21 @@ class RF24
* The size of data written is the fixed payload size, see getPayloadSize()
*
* @param buf Where to get the data
* @param len Number of bytes to be sent
* @return Current value of status register
*/
uint8_t write_payload(const void* buf);
uint8_t write_payload(const void* buf, uint8_t len);

/**
* Read the receive payload
*
* The size of data read is the fixed payload size, see getPayloadSize()
*
* @param buf Where to put the data
* @param len Maximum number of bytes to read
* @return Current value of status register
*/
uint8_t read_payload(void* buf) ;
uint8_t read_payload(void* buf, uint8_t len) ;

/**
* Empty the receive buffer
Expand Down Expand Up @@ -222,12 +228,15 @@ class RF24
* the receiver or the timeout/retransmit maxima are reached. In
* the current configuration, the max delay here is 60ms.
*
* The size of data written is the fixed payload size, see getPayloadSize()
* The maximum size of data written is the fixed payload size, see
* getPayloadSize(). However, you can write less, and the remainder
* will just be filled with zeroes.
*
* @param buf Pointer to the data to be sent
* @param len Number of bytes to be sent
* @return True if the payload was delivered successfully false if not
*/
boolean write( const void* buf );
boolean write( const void* buf, uint8_t len );

/**
* Test whether there are bytes available to be read
Expand All @@ -249,9 +258,10 @@ class RF24
* for beginners to use. No casting needed.
*
* @param buf Pointer to a buffer where the data should be written
* @param len Maximum number of bytes to read into the buffer
* @return True if the payload was delivered successfully false if not
*/
boolean read( void* buf ) ;
boolean read( void* buf, uint8_t len ) ;

/**
* Open a pipe for writing
Expand Down Expand Up @@ -296,6 +306,21 @@ class RF24
*/
void openReadingPipe(uint8_t number, uint64_t address);

};
};

/**
* @example pingpair.pde
*
* This is an example of how to use the RF24 class. Write this sketch to two different nodes,
* connect the role_pin to ground on one. The ping node sends the current time to the pong node,
* which responds by sending the value back.
*/

/**
* @mainpage Driver Library for nRF24L01
*
* See the RF24 class for details on how to drive this chip.
*/

#endif // __RF24_H__

1 change: 1 addition & 0 deletions examples/pingpair/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output/
Loading

0 comments on commit e97e023

Please sign in to comment.