Skip to content

Commit

Permalink
Fully working now
Browse files Browse the repository at this point in the history
  • Loading branch information
maniacbug committed Jun 10, 2011
1 parent d7be898 commit 26bf3ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
32 changes: 21 additions & 11 deletions RF24Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ void RF24Network::update(void)
// Read the beginning of the frame as the header
const RF24NetworkHeader& header = * reinterpret_cast<RF24NetworkHeader*>(frame_buffer);

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Received on %u %s\n\r"),millis(),pipe_num,header.toString()));
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Received on %u %s\n\r"),millis(),pipe_num,header.toString()));

#ifdef 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);
#endif

// Is this for us?
if ( header.to_node == node_address )
Expand All @@ -82,7 +87,7 @@ bool RF24Network::enqueue(void)
{
bool result = false;

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: Enqueue "),millis()));
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Enqueue "),millis()));

// Copy the current frame into the frame queue
if ( next_frame <= frame_buffer + frame_size )
Expand All @@ -108,21 +113,21 @@ bool RF24Network::available(void)
size_t RF24Network::read(RF24NetworkHeader& header,void* message, size_t maxlen)
{
size_t bufsize = 0;
uint8_t* frame = next_frame;

if ( available() )
{
// Move the pointer back one in the queue
next_frame -= frame_size;
uint8_t* frame = next_frame;

// How much buffer size should we actually copy?
bufsize = min(maxlen,frame_size-sizeof(RF24NetworkHeader));

// Copy the next available frame from the queue into the provided buffer
memcpy(&header,frame,sizeof(RF24NetworkHeader));
memcpy(message,frame,bufsize);
memcpy(message,frame+sizeof(RF24NetworkHeader),bufsize);

// And move on to the next one
next_frame -= frame_size;

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MCU Received %s\n\r"),millis(),header.toString()));
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Received %s\n\r"),millis(),header.toString()));
}

return bufsize;
Expand All @@ -137,8 +142,13 @@ bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t le
memcpy(frame_buffer,&header,sizeof(RF24NetworkHeader));
memcpy(frame_buffer + sizeof(RF24NetworkHeader),message,min(frame_size-sizeof(RF24NetworkHeader),len));

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MCU Sending %s\n\r"),millis(),header.toString()));

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Sending %s\n\r"),millis(),header.toString()));

#ifdef SERIAL_DEBUG
const uint16_t* i = reinterpret_cast<const uint16_t*>(message);
printf_P(PSTR("%lu: NET message %04x\n\r"),millis(),*i);
#endif

// If the user is trying to send it to himself
if ( header.to_node == node_address )
// Just queue it in the received queue
Expand Down Expand Up @@ -186,7 +196,7 @@ bool RF24Network::write(uint16_t to_node)
// Now, continue listening
radio.startListening();

IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Sent on %u %s\n\r"),millis(),out_pipe,ok?"ok":"failed"));
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Sent on %x %s\n\r"),millis(),(uint16_t)out_pipe,ok?"ok":"failed"));

return ok;
}
Expand Down
12 changes: 6 additions & 6 deletions examples/meshping/meshping.pde
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ void loop(void)
{
// If so, grab it and print it out
RF24NetworkHeader header;
network.read(header,&message,sizeof(message));
printf_P(PSTR("%lu: RECEIVED %lu from %u\n\r"),millis(),message,header.from_node);
network.read(header,&message,sizeof(unsigned long));
printf_P(PSTR("%lu: APP Received %lu from %u\n\r"),millis(),message,header.from_node);
}

// Send a ping to the other guy every 'interval' ms
Expand All @@ -102,18 +102,18 @@ void loop(void)
{
last_time_sent = now;

printf_P(PSTR("%lu: SENDING %lu..."),millis(),now);
printf_P(PSTR("%lu: APP Sending %lu...\n\r"),millis(),now);

message = now;
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,&message,sizeof(message));
bool ok = network.write(header,&message,sizeof(unsigned long));
if (ok)
{
printf_P(PSTR("ok\n\r"));
printf_P(PSTR("%lu: APP Send ok\n\r"),millis());
}
else
{
printf_P(PSTR("failed\n\r"));
printf_P(PSTR("%lu: APP Send failed\n\r"),millis());

// Try sending at a different time next time
last_time_sent += 100;
Expand Down

0 comments on commit 26bf3ad

Please sign in to comment.