Skip to content

Commit

Permalink
Added 'echo' ping test
Browse files Browse the repository at this point in the history
  • Loading branch information
maniacbug committed Dec 20, 2011
1 parent bf24995 commit 99f61e4
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void Sync::update(void)
// TODO handle the case where this has to be broken into
// multiple messages
RF24NetworkHeader header(/*to node*/ to_node, /*type*/ 'S' /*Sync*/);
network.write(header,message,sizeof(unsigned long));
network.write(header,message,sizeof(message));
}

// Look for messages from the network
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/Globals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

// STL headers
// C headers
// Framework headers
// Library headers
#include <RF24.h>
#include <RF24Network.h>
// Project headers
// This component's header
#include <Globals.h>

#include "WProgram.h"

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

HardwareSPI SPI(2);

// Radio on Maple Native w/ Getting Started board
RF24 radio(7,6);
RF24Network network(radio);

// vim:cin:ai:sts=2 sw=2 ft=cpp
36 changes: 36 additions & 0 deletions tests/unit/Globals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

#ifndef __GLOBALS_H__
#define __GLOBALS_H__

// STL headers
// C headers
// Framework headers
// Library headers
// Project headers

class RF24;
class RF24Network;

extern RF24 radio;
extern RF24Network network;

/**
* Example for how classes should be declared
*/

class Template
{
private:
protected:
public:
};

#endif // __GLOBALS_H__
// vim:cin:ai:sts=2 sw=2 ft=cpp
89 changes: 89 additions & 0 deletions tests/unit/PingTest.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

// STL headers
#include <vector>
#include <string>
#include <iostream>
// C headers
#include <stdlib.h>
// Framework headers
// Library headers
#include <cxxtest/TestSuite.h>
// Project headers
#include <RF24Network.h>
#include <RF24.h>
// This component's header
#include <Sync.h>

#include "WProgram.h"
#include "Globals.h"

using namespace std;

class PingTestSuite: public CxxTest::TestSuite
{

public:
void setUp()
{
SPI.begin();
radio.begin();
network.begin(/* channel */100,/* this node */0);

// Reset remote to initial state
RF24NetworkHeader header(/*to node*/ 1, /*type*/ 'R' /*Reset*/);
network.write(header,0,0);

// Wait a bit for the message to take
delay(50);
}

void tearDown()
{
}

void testPing()
{
// Send an echo ping to the other unit
RF24NetworkHeader header(/*to node*/ 1, /*type*/ 'E' /*Echo*/);
uint32_t testval = 0x12345678, gotval = 0;
network.write(header,&testval,sizeof(testval));

const unsigned long timeout = 1000;
unsigned long sent_at = millis();
while ( millis() - sent_at < timeout && ! gotval )
{
network.update();

// Is there anything ready for us?
if ( network.available() )
{
// If so, take a look at it
RF24NetworkHeader header;
network.peek(header);

switch (header.type)
{
case 'E':
network.read(header,&gotval,sizeof(gotval));
break;
default:
// Anything else is unexpected, and ergo a test failure
network.read(header,0,0);
gotval = -1;
break;
};
}
}

TS_ASSERT_EQUALS( gotval, testval );
}

};
// vim:cin:ai:sts=2 sw=2 ft=cpp
6 changes: 1 addition & 5 deletions tests/unit/SyncTest.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@

#include "WProgram.h"

HardwareSPI SPI(2);

// Radio on Maple Native w/ Getting Started board
RF24 radio(7,6);
RF24Network network(radio);
#include "Globals.h"

using namespace std;

Expand Down
19 changes: 17 additions & 2 deletions tests/unit_rx/unit_rx.pde
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ sync_data_t sync_data;
// Old value of 'first'
uint16_t old_first;

// Message buffer space
uint8_t message[32];

void setup(void)
{
Serial.begin(57600);
Expand All @@ -68,7 +71,7 @@ void loop(void)
if ( old_first != sync_data.first )
{
printf_P(PSTR("%lu: APP Updated first to %u\n\r"),millis(),sync_data.first);

// Move the first value over to the second
sync_data.second = sync_data.first;

Expand All @@ -79,10 +82,12 @@ void loop(void)
// Are there any messages for us?
while ( network.available() )
{
uint16_t from;

// If so, take a look at it
RF24NetworkHeader header;
network.peek(header);

// Dispatch the message to the correct handler.
switch (header.type)
{
Expand All @@ -94,6 +99,16 @@ void loop(void)
sync_data = sync_data_t();
old_first = sync_data.first;
break;

// Echo back to the sender.
case 'E':
network.read(header,message,sizeof(message));
from = header.from_node;
printf_P(PSTR("%lu: APP Received ECHO request from %o\n\r"),millis(),from);
network.write(header = RF24NetworkHeader(from,'E'),message,sizeof(message));
break;

// Unrecognized message type
default:
printf_P(PSTR("*** WARNING *** Unknown message type %c\n\r"),header.type);
network.read(header,0,0);
Expand Down

0 comments on commit 99f61e4

Please sign in to comment.