Skip to content

Commit

Permalink
Cleanup, change non-debug printf, minor changes
Browse files Browse the repository at this point in the history
Moving call to printf into the DEBUG_MINIMAL statement can reduce code
size quite a bit on Arduino .
Cleanup debugging a bit
  • Loading branch information
TMRh20 committed Sep 29, 2014
1 parent eff7ee0 commit 324e413
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
53 changes: 21 additions & 32 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ uint8_t RF24Network::update(void)
// Is this for us?
if ( header.to_node == node_address ){
if(res == NETWORK_ACK){ // If received a routing payload, (Network ACK) discard it, and indicate what it was.
#ifdef SERIAL_DEBUG_ROUTING
printf_P(PSTR("MAC: Network ACK Rcvd \n"));
#endif
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("MAC: Network ACK Rcvd \n")); );
returnVal = NETWORK_ACK;
continue;
} // Add it to the buffer of frames for us
Expand Down Expand Up @@ -173,9 +171,7 @@ uint8_t RF24Network::update(void)
if( header.to_node == 0100){
if(header.id != lastMultiMessageID){
if(multicastRelay){
#ifdef SERIAL_DEBUG_ROUTING
printf_P(PSTR("MAC: FWD multicast frame from 0%o to level %d\n"),header.from_node,multicast_level+1);
#endif
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("MAC: FWD multicast frame from 0%o to level %d\n"),header.from_node,multicast_level+1); );
write(levelToAddress(multicast_level)<<3,4);
}

Expand All @@ -193,11 +189,9 @@ uint8_t RF24Network::update(void)
enqueue();
lastMultiMessageID = header.id;
}
#ifdef SERIAL_DEBUG_ROUTING
else{
printf_P(PSTR("MAC: Drop duplicate multicast frame %d from 0%o\n"),header.id,header.from_node);
}
#endif
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("MAC: Drop duplicate multicast frame %d from 0%o\n"),header.id,header.from_node); );
}
}else{
write(header.to_node,1); //Send it on, indicate it is a routed payload
}
Expand Down Expand Up @@ -311,6 +305,7 @@ size_t RF24Network::read(RF24NetworkHeader& header,void* message, size_t maxlen)
return bufsize;
}


#if defined RF24NetworkMulticast
/******************************************************************/
bool RF24Network::multicast(RF24NetworkHeader& header,const void* message, size_t len, uint8_t level){
Expand Down Expand Up @@ -344,7 +339,7 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
#if defined (DISABLE_FRAGMENTATION)
return _write(header,message,len,writeDirect);
#else
bool txSuccess = true;
//bool txSuccess = true;

//Check payload size
if (len > MAX_PAYLOAD_SIZE) {
Expand All @@ -355,15 +350,12 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
//If the message payload is too big, whe cannot generate enough fragments
//and enumerate them
if (len > 255*max_frame_payload_size) {
txSuccess = false;
return txSuccess;
return false;
}

//Normal Write (Un-Fragmented)
if (len <= max_frame_payload_size) {
txSuccess = _write(header,message,len,writeDirect);
//radio.startListening();
return txSuccess;
return _write(header,message,len,writeDirect);
}

//Divide the message payload into chuncks of max_frame_payload_size
Expand All @@ -388,7 +380,7 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
fragmentHeader.type = NETWORK_MORE_FRAGMENTS; //Set the more fragments flag to indicate a fragmented frame
}
}

size_t offset = msgCount*max_frame_payload_size;
size_t fragmentLen = min(len-offset,max_frame_payload_size);

Expand All @@ -407,7 +399,7 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le

if (!ok) {
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%u: FRG message transmission with fragmentID '%i' failed. Abort.\n\r",millis(),fragment_id););
txSuccess = false;
return false;
break;
}

Expand All @@ -430,7 +422,7 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
//Return true if all the chunks where sent successfully
//else return false
IF_SERIAL_DEBUG(printf("%u: NET total message fragments sent %i. txSuccess ",millis(),msgCount); printf("%s\n\r", txSuccess ? "YES" : "NO"););
return txSuccess;
return true;

#endif //Fragmentation enabled
}
Expand Down Expand Up @@ -496,15 +488,15 @@ bool RF24Network::write(uint16_t to_node, uint8_t directTo) // Direct To: 0 = F
/**Write it*/
ok=write_to_pipe(conversion.send_node, conversion.send_pipe, conversion.multicast);

#ifdef SERIAL_DEBUG_ROUTING
if(!ok){ printf_P(PSTR("%lu: MAC Send fail to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe); }
#endif

if(!ok){ IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu: MAC Send fail to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe);); }

if( directTo == TX_ROUTED && ok && conversion.send_node == to_node && frame_buffer[6] != NETWORK_ACK ){

RF24NetworkHeader& header = * reinterpret_cast<RF24NetworkHeader*>(frame_buffer);
header.type = NETWORK_ACK; // Set the payload type to NETWORK_ACK
header.to_node = header.from_node; // Change the 'to' address to the 'from' address

dynLen=8;

conversion.send_node=header.from_node;
Expand All @@ -515,9 +507,7 @@ bool RF24Network::write(uint16_t to_node, uint8_t directTo) // Direct To: 0 = F
write_to_pipe(conversion.send_node, conversion.send_pipe, multicast);

dynLen=0;
#if defined (SERIAL_DEBUG_ROUTING)
printf_P(PSTR("%lu MAC: Route OK to 0%o ACK sent to 0%o\n"),millis(),to_node,header.to_node);
#endif
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu MAC: Route OK to 0%o ACK sent to 0%o\n"),millis(),to_node,header.to_node); );
}

// if(!ok){ printf_P(PSTR("%lu: MAC No Ack from 0%o via 0%o on pipe %x\n\r"),millis(),to_node,send_node,send_pipe); }
Expand All @@ -531,9 +521,7 @@ bool RF24Network::write(uint16_t to_node, uint8_t directTo) // Direct To: 0 = F
uint32_t reply_time = millis();
while( update() != NETWORK_ACK){
if(millis() - reply_time > routeTimeout){
#ifdef SERIAL_DEBUG_ROUTING
printf_P(PSTR("%lu: MAC Network ACK fail from 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe);
#endif
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu: MAC Network ACK fail from 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe); );
ok=0;
break;
}
Expand Down Expand Up @@ -707,9 +695,9 @@ void RF24Network::setup_address(void)
parent_pipe = i;
//parent_pipe = i-1;

#ifdef SERIAL_DEBUG
printf_P(PSTR("setup_address node=0%o mask=0%o parent=0%o pipe=0%o\n\r"),node_address,node_mask,parent_node,parent_pipe);
#endif

IF_SERIAL_DEBUG( printf_P(PSTR("setup_address node=0%o mask=0%o parent=0%o pipe=0%o\n\r"),node_address,node_mask,parent_node,parent_pipe););

}

/******************************************************************/
Expand Down Expand Up @@ -771,7 +759,8 @@ bool RF24Network::is_valid_address( uint16_t node )
#endif
{
result = false;
printf_P(PSTR("*** WARNING *** Invalid address 0%o\n\r"),node);
IF_SERIAL_DEBUG_MINIMAL(printf_P(PSTR("*** WARNING *** Invalid address 0%o\n\r"),node););
printf_P(PSTR("*** WARNING *** Invalid address 0%o\n\r"),node);
break;
}
node >>= 3;
Expand Down
5 changes: 4 additions & 1 deletion RF24Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#define NETWORK_ACK_REQUEST 128
#define NETWORK_ACK 129
/**System-Sub Types (0-255)*/
#define NETWORK_REQ_STREAM 11;
//#define NETWORK_REQ_STREAM 11;
#define NETWORK_POLL 130

/** System retained types */
Expand All @@ -54,9 +54,12 @@
#define USER_TX_TO_LOGICAL_ADDRESS 3
#define USER_TX_MULTICAST 4

/** System defines */
#define MAX_FRAME_SIZE 32
#define MAX_PAYLOAD_SIZE 1500



class RF24;

/**
Expand Down
13 changes: 13 additions & 0 deletions RF24Network_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define DISABLE_FRAGMENTATION // Saves a bit of space by disabling fragmentation

//#define SERIAL_DEBUG
//#define SERIAL_DEBUG_MINIMAL
//#define SERIAL_DEBUG_ROUTING
//#define SERIAL_DEBUG_FRAGMENTATION
/*************************************/
Expand Down Expand Up @@ -71,12 +72,24 @@
#define printf_P(...)
#endif
#endif

#if defined (SERIAL_DEBUG_MINIMAL)
#define IF_SERIAL_DEBUG_MINIMAL(x) ({x;})
#else
#define IF_SERIAL_DEBUG_MINIMAL(x)
#endif

#if defined (SERIAL_DEBUG_FRAGMENTATION)
#define IF_SERIAL_DEBUG_FRAGMENTATION(x) ({x;})
#else
#define IF_SERIAL_DEBUG_FRAGMENTATION(x)
#endif

#if defined (SERIAL_DEBUG_ROUTING)
#define IF_SERIAL_DEBUG_ROUTING(x) ({x;})
#else
#define IF_SERIAL_DEBUG_ROUTING(x)
#endif

// Avoid spurious warnings
// Arduino DUE is arm and uses traditional PROGMEM constructs
Expand Down

0 comments on commit 324e413

Please sign in to comment.