diff --git a/RF24Network.cpp b/RF24Network.cpp index 900c230..d5160d1 100644 --- a/RF24Network.cpp +++ b/RF24Network.cpp @@ -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(frame_buffer); + // Read the beginning of the frame as the header + const RF24NetworkHeader& header = * reinterpret_cast(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(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(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); } } diff --git a/RF24Network.h b/RF24Network.h index 4e8652a..2e008d5 100644 --- a/RF24Network.h +++ b/RF24Network.h @@ -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