Skip to content

Modified update() to use in combination with interrupts and modified RF24::whatHappened() #4

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

Open
wants to merge 1 commit 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
83 changes: 45 additions & 38 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,53 +50,60 @@ void RF24Network::begin(uint8_t _channel, uint16_t _node_address )

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

void RF24Network::update(void)
void RF24Network::update(uint8_t pipe_num)
{
// if there is data ready
uint8_t pipe_num;
while ( radio.available(&pipe_num) )
// Dump the payloads until we've gotten everything
boolean done = false;
while (!done)
{
// Dump the payloads until we've gotten everything
boolean done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( frame_buffer, sizeof(frame_buffer) );
// Fetch the payload, and see if this was the last one.
done = radio.read( frame_buffer, sizeof(frame_buffer) );

// Read the beginning of the frame as the header
const RF24NetworkHeader& header = * reinterpret_cast<RF24NetworkHeader*>(frame_buffer);
// Read the beginning of the frame as the header
const RF24NetworkHeader& header = * reinterpret_cast<RF24NetworkHeader*>(frame_buffer);

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Received on %u %s\n\r"),millis(),pipe_num,header.toString()));
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(frame_buffer + sizeof(RF24NetworkHeader));printf_P(PSTR("%lu: NET message %04x\n\r"),millis(),*i));
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Received on %u %s\n\r"),millis(),pipe_num,header.toString()));
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(frame_buffer + sizeof(RF24NetworkHeader));printf_P(PSTR("%lu: NET message %04x\n\r"),millis(),*i));

// Throw it away if it's not a valid address
if ( !is_valid_address(header.to_node) )
continue;
// Throw it away if it's not a valid address
if ( !is_valid_address(header.to_node) )
continue;

// Is this for us?
if ( header.to_node == node_address )
// Add it to the buffer of frames for us
enqueue();
else
// Relay it
write(header.to_node);
// Is this for us?
if ( header.to_node == node_address )
// Add it to the buffer of frames for us
enqueue();
else
// Relay it
write(header.to_node);

// NOT NEEDED anymore. Now all reading pipes are open to start.
// NOT NEEDED anymore. Now all reading pipes are open to start.
#if 0
// If this was for us, from one of our children, but on our listening
// pipe, it could mean that we are not listening to them. If so, open up
// and listen to their talking pipe

if ( header.to_node == node_address && pipe_num == 0 && is_descendant(header.from_node) )
{
uint8_t pipe = pipe_to_descendant(header.from_node);
radio.openReadingPipe(pipe,pipe_address(node_address,pipe));

// Also need to open pipe 1 so the system can get the full 5-byte address of the pipe.
radio.openReadingPipe(1,pipe_address(node_address,1));
}
#endif
// If this was for us, from one of our children, but on our listening
// pipe, it could mean that we are not listening to them. If so, open up
// and listen to their talking pipe

if ( header.to_node == node_address && pipe_num == 0 && is_descendant(header.from_node) )
{
uint8_t pipe = pipe_to_descendant(header.from_node);
radio.openReadingPipe(pipe,pipe_address(node_address,pipe));

// Also need to open pipe 1 so the system can get the full 5-byte address of the pipe.
radio.openReadingPipe(1,pipe_address(node_address,1));
}
#endif
}
}

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

void RF24Network::update(void)
{
// if there is data ready
uint8_t pipe_num;
while ( radio.available(&pipe_num) )
{
update(pipe_num);
}
}

Expand Down
16 changes: 16 additions & 0 deletions RF24Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ class RF24Network
* the action happens!
*/
void update(void);

/**
* Alternative main layer loop when used in combination with interrupts.
*
* This function must be called when an interrupt is triggered and data
* is received. It behaves exactly as update(), but it will not loop through
* all pipes, but only the specified one. This way, you can keep your interrupt
* routine short.
*
* (To be honest, pipe_num is only used to print debug information. It is not
* used for any other purposes.)
*
* @warning Only use it in combination with interrupts!
* @see RF24::whatHappened()
*/
void update(uint8_t pipe_num);

/**
* Test whether there is a message available for this node
Expand Down