@@ -19,105 +19,92 @@ TMRh20 2014
19
19
#include " RF24.h"
20
20
#include " printf.h"
21
21
22
- // Hardware configuration
23
- // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
22
+ /* ************ USER Configuration *****************************/
23
+ // Hardware configuration
24
+ RF24 radio (48 ,49 ); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
25
+ boolean RADIO_NO = 0 ; // SET THIS TO 0 or 1 for 1st or 2nd radio
24
26
25
- RF24 radio ( 48 , 49 );
27
+ /* ************************************************************* */
26
28
27
- // Topology
28
- // Radio pipe addresses for the 2 nodes to communicate.
29
- const uint64_t pipes[2 ] = { 0xABCDABCD71LL , 0x544d52687CLL };
29
+ const uint64_t pipes[2 ] = { 0xABCDABCD71LL , 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate.
30
30
31
- // Role management
32
- // Set up role. This sketch uses the same software for all the nodes
33
- // in this system. Doing so greatly simplifies testing.
31
+ byte data[32 ]; // Data buffer for testing data transfer speeds
34
32
35
- // The various roles supported by this sketch
36
- typedef enum { role_TX = 1 , role_RX } role_e;
37
-
38
- // The debug-friendly names of those roles
39
- const char * role_friendly_name[] = { " invalid" , " Ping out" , " Pong back" };
40
-
41
- // Set this to determine the default role of the current running sketch
42
- role_e role = role_RX;
43
-
44
- // Data buffer for testing data transfer speeds
45
- byte data[32 ];
46
-
47
- // Counter and timer for keeping track transfer info
48
- unsigned long counter, rxTimer;
33
+ unsigned long counter, rxTimer; // Counter and timer for keeping track transfer info
49
34
float startTime, stopTime;
35
+ bool TX=1 ,RX=0 ,role=0 ;
50
36
51
- void setup (void )
52
- {
37
+ void setup (void ) {
53
38
54
39
Serial.begin (57600 );
55
40
printf_begin ();
56
- printf (" \n\r RF24/examples/GettingStarted/\n\r " );
57
- printf (" ROLE: %s\n\r " ,role_friendly_name[role]);
58
- printf (" *** PRESS 'T' to begin transmitting to the other node\n\r " );
59
41
60
- radio.begin (); // Setup and configure rf radio
42
+ radio.begin (); // Setup and configure rf radio
61
43
radio.setChannel (1 );
62
44
radio.setPALevel (RF24_PA_MAX);
63
- radio.setAutoAck (1 ); // Ensure autoACK is enabled
64
- radio.setRetries (2 ,3 ); // Optionally, increase the delay between retries & # of retries
65
- // radio.disableCRC();
45
+ radio.setAutoAck (1 ); // Ensure autoACK is enabled
46
+ radio.setRetries (1 ,1 ); // Optionally, increase the delay between retries & # of retries
66
47
radio.setCRCLength (RF24_CRC_8);
67
- radio.openWritingPipe (pipes[1 ]);
68
- radio.openReadingPipe (1 ,pipes[0 ]);
69
-
48
+
49
+ if (RADIO_NO){ // Lets the two radios choose the correct pipes depending on user selection
50
+ radio.openWritingPipe (pipes[0 ]);
51
+ radio.openReadingPipe (1 ,pipes[1 ]);
52
+ }else {
53
+ radio.openWritingPipe (pipes[1 ]);
54
+ radio.openReadingPipe (1 ,pipes[0 ]);
55
+ }
56
+
70
57
radio.startListening (); // Start listening
71
58
radio.printDetails (); // Dump the configuration of the rf unit for debugging
72
59
60
+ printf (" \n\r RF24/examples/Transfer Rates/\n\r " );
61
+ printf (" *** PRESS 'T' to begin transmitting to the other node\n\r " );
62
+
73
63
randomSeed (analogRead (0 )); // Seed for random number generation
74
64
75
65
for (int i=0 ; i<32 ; i++){
76
66
data[i] = random (255 ); // Load the buffer with random data
77
67
}
78
- radio.powerUp ();
68
+ radio.powerUp (); // Power up the radio
79
69
}
80
70
81
71
void loop (void ){
82
72
83
-
84
- // TX with pre-blocking writes
85
- if (role == role_TX){
73
+
74
+ if (role == TX){
86
75
87
76
delay (1500 );
88
77
89
78
printf (" Initiating Basic Data Transfer\n\r " );
90
79
91
80
92
- float cycles = 10000 ;
81
+ float cycles = 1000 ; // Change this to a higher or lower number.
93
82
94
83
startTime = millis ();
95
84
96
- for (int i=0 ; i<cycles; i++){
97
- if (!radio.writeFast (&data,32 )){
98
- counter++;
85
+ for (int i=0 ; i<cycles; i++){ // Loop through a number of cycles
86
+ data[0 ] = i; // Change the first byte of the payload for identification
87
+ if (!radio.writeFast (&data,32 )){ // Write to the FIFO buffers
88
+ counter++; // Keep count of failed payloads
99
89
}
100
90
}
91
+
101
92
stopTime = millis ();
102
-
103
- while (!radio.txStandBy ()){ }
93
+
94
+ while (!radio.txStandBy ()){ } // This should be called to wait for completion and put the radio in standby mode after transmission
104
95
105
96
float numBytes = cycles * 32 ;
106
97
float rate = numBytes / (stopTime - startTime) / cycles;
107
-
108
98
109
- printf (" Transfer complete at " ); Serial.print (rate); printf (" KB/s \n\r " );
110
- printf (" %d of " ,counter); Serial.print (cycles); printf (" Packets Failed to Send\n\r " );
111
- counter = 0 ;
99
+ printf (" Transfer complete at " ); Serial.print (rate); printf (" KB/s \n\r " );
100
+ printf (" %d of " ,counter); Serial.print (cycles); printf (" Packets Failed to Send\n\r " );
101
+ counter = 0 ;
112
102
113
-
114
-
115
-
116
103
}
117
104
118
105
119
106
120
- if (role == role_RX ){
107
+ if (role == RX ){
121
108
while (radio.available ()){
122
109
radio.read (&data,32 );
123
110
counter++;
@@ -130,34 +117,26 @@ void loop(void){
130
117
printf (" Payload Count: %d \n\r " , counter);
131
118
counter = 0 ;
132
119
}
133
-
134
120
}
135
-
136
121
//
137
122
// Change roles
138
123
//
139
124
140
125
if ( Serial.available () )
141
126
{
142
127
char c = toupper (Serial.read ());
143
- if ( c == ' T' && role == role_RX )
128
+ if ( c == ' T' && role == RX )
144
129
{
145
130
printf (" *** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r " );
146
131
radio.stopListening ();
147
- role = role_TX; // Become the primary transmitter (ping out)
148
- // radio.openWritingPipe(pipes[0]);
149
- // radio.openReadingPipe(1,pipes[1]);
132
+ role = TX; // Become the primary transmitter (ping out)
150
133
}
151
- else if ( c == ' R' && role == role_TX )
134
+ else if ( c == ' R' && role == TX )
152
135
{
153
136
radio.startListening ();
154
- printf (" *** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r " );
155
-
156
- role = role_RX; // Become the primary receiver (pong back)
157
-
158
- // radio.openWritingPipe(pipes[1]);
159
- // radio.openReadingPipe(1,pipes[0]);
137
+ printf (" *** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r " );
138
+ role = RX; // Become the primary receiver (pong back)
160
139
}
161
140
}
162
141
}
163
- // vim:cin:ai:sts=2 sw=2 ft=cpp
142
+ // vim:cin:ai:sts=2 sw=2 ft=cpp
0 commit comments