Skip to content

Commit eb2b8f4

Browse files
committedJun 8, 2011
Implemented begin()
1 parent aa79084 commit eb2b8f4

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed
 

‎RF24Network.cpp

+56-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@
1818

1919
RF24Network::RF24Network( RF24& _radio, const RF24NodeLine* _topology ): radio(_radio), topology(_topology)
2020
{
21+
num_nodes = 0;
22+
const RF24NodeLine* i = topology;
23+
while ( (i++)->parent_node != 0xFFFF )
24+
++num_nodes;
2125
}
2226

2327
void RF24Network::begin(uint8_t _channel, uint16_t _node_address, rf24_direction_e /*_direction*/ )
2428
{
25-
node_address = _node_address;
29+
if ( node_address < num_nodes )
30+
node_address = _node_address;
31+
2632
radio.setChannel(_channel);
33+
radio.setDataRate(RF24_1MBPS);
34+
radio.setCRCLength(RF24_CRC_16);
35+
radio.startListening();
36+
radio.printDetails();
2737
}
2838

2939
void RF24Network::update(void)
@@ -45,4 +55,49 @@ bool RF24Network::write(RF24NetworkHeader& /*header*/,const void* /*buf*/, size_
4555
return false;
4656
}
4757

58+
void RF24Network::open_pipes(void)
59+
{
60+
// In order to open the right pipes, we need to know whether the node has parents
61+
// and/or children
62+
bool has_parent = ( topology[node_address].parent_node != 0 );
63+
bool has_children = false;
64+
65+
// If there are any nodes in the topology table which consider this
66+
// a parent, then we do have children
67+
int i = num_nodes;
68+
while (i-- && !has_children)
69+
if ( topology[i].parent_node == node_address )
70+
has_children = true;
71+
72+
// Open pipes for parent
73+
if ( has_parent )
74+
{
75+
// Writing pipe to speak to our parent
76+
radio.openWritingPipe(topology[node_address].talking_pipe);
77+
78+
// Listen to our parent. If we have children, we need to do so
79+
// on pipe 0 to make room for more children
80+
if ( has_children )
81+
radio.openReadingPipe(0,topology[node_address].listening_pipe);
82+
else
83+
radio.openReadingPipe(1,topology[node_address].listening_pipe);
84+
}
85+
86+
// Listen on children's talking pipes
87+
if ( has_children )
88+
{
89+
// First child listening pipe is #1
90+
uint8_t current_pipe = 1;
91+
92+
// The topology table tells us who our children are
93+
int i = num_nodes;
94+
while (i--)
95+
{
96+
if ( topology[i].parent_node == node_address )
97+
radio.openReadingPipe(current_pipe++,topology[i].talking_pipe);
98+
}
99+
}
100+
101+
}
102+
48103
// vim:ai:cin:sts=2 sw=2 ft=cpp

‎RF24Network.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ typedef enum { RF24_NET_UNIDIRECTIONAL = 0, RF24_NET_BIDIRECTIONAL } rf24_direct
6565

6666
class RF24Network
6767
{
68-
private:
69-
RF24& radio; /**< Underlying radio driver, provides link/physical layers */
70-
uint16_t node_address; /**< Logical node address of this unit, 1 .. UINT_MAX */
71-
const RF24NodeLine* topology; /**< Mapping table of logical node addresses to physical RF pipes */
72-
7368
public:
7469
RF24Network( RF24& _radio, const RF24NodeLine* _topology);
7570

@@ -124,6 +119,16 @@ class RF24Network
124119
* @return Whether the message was successfully received
125120
*/
126121
bool write(RF24NetworkHeader& header,const void* buf, size_t len);
122+
123+
protected:
124+
void open_pipes(void);
125+
126+
private:
127+
RF24& radio; /**< Underlying radio driver, provides link/physical layers */
128+
uint16_t node_address; /**< Logical node address of this unit, 1 .. UINT_MAX */
129+
const RF24NodeLine* topology; /**< Mapping table of logical node addresses to physical RF pipes */
130+
uint16_t num_nodes; /**< Number of nodes in the topology table */
131+
127132
};
128133
#endif // __RF24_H__
129134
// vim:ai:cin:sts=2 sw=2 ft=cpp

‎examples/meshping/meshping.pde

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <RF24Network.h>
33
#include <RF24.h>
44
#include <SPI.h>
5+
#include "printf.h"
56

67
// Avoid spurious warnings
78
#undef PROGMEM
@@ -17,6 +18,7 @@ RF24NodeLine topology[] =
1718
{ 0x0000000000LL, 0x0000000000LL, 0 }, // Node 0: Invalid
1819
{ 0xE7E7E7E701LL, 0xE7E7E7E701LL, 0 }, // Node 1: Base, has no parent
1920
{ 0xE7E7E7E70ELL, 0xE7E7E7E70ELL, 1 }, // Node 2: Leaf, child of #1
21+
{ 0xFFFFFFFFFFLL, 0xFFFFFFFFFFLL, -1 }, // End of data marker
2022
};
2123

2224
RF24 radio(8,9);
@@ -36,10 +38,18 @@ unsigned long last_time_sent;
3638

3739
void setup(void)
3840
{
39-
SPI.begin();
40-
radio.begin();
41+
//
42+
// Print preamble
43+
//
44+
45+
Serial.begin(57600);
46+
printf_begin();
47+
printf("\n\rRF24Network/examples/meshping/\n\r");
48+
49+
//
50+
// Node configuration
51+
//
4152

42-
// Figure out which node we are
4353
pinMode(role_pin,INPUT);
4454
digitalWrite(role_pin,HIGH);
4555
if ( digitalRead(role_pin) )
@@ -52,9 +62,14 @@ void setup(void)
5262
this_node = 2;
5363
other_node = 1;
5464
}
65+
printf("ADDRESS: %i\n\r",this_node);
5566

56-
67+
//
5768
// Bring up the RF network
69+
//
70+
71+
SPI.begin();
72+
radio.begin();
5873
network.begin(/*channel*/ 100, /*node address*/ this_node, /*directionality*/ RF24_NET_BIDIRECTIONAL);
5974
}
6075

0 commit comments

Comments
 (0)