Commit 99571d65 authored by TMRh20's avatar TMRh20

Updated some examples to show the new lib usage

Updated GettingStarted, pingpair, added medium speed version of transfer
rate test
parent 63ee0ce2
......@@ -301,8 +301,8 @@ public:
*
* This will not block until the 3 FIFO buffers are filled with data.
* Once the FIFOs are full, writeFast will simply return 0. From a user
* perspective, just keep trying to send. The library will keep auto
* retrying the previous payload using the built in functionality.
* perspective, just keep trying to send the same data. The library will
* keep auto retrying the previous payload using the built in functionality.
*
* The maximum size of data written is the fixed payload size, see
* getPayloadSize(). However, you can write less, and the remainder
......@@ -329,7 +329,7 @@ public:
* used to control the timeout period.
*
* This will never return a 0. It will not return until a packet is
* sent successfully
* loaded successfully into the FIFO and TX is complete.
*
* The maximum size of data written is the fixed payload size, see
* getPayloadSize(). However, you can write less, and the remainder
......
/*
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.
*/
/**
* Example for Getting Started with nRF24L01+ radios.
*
* This is an example of how to use the RF24 class. Write this sketch to two
* different nodes. Put one of the nodes into 'transmit' mode by connecting
* with the serial monitor and sending a 'T'. The ping node sends the current
* time to the pong node, which responds by sending the value back. The ping
* node can then see how long the whole cycle took.
*/
//March 2014 - TMRh20 - Updated to utilize High Speed RF24 Library fork
//This
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
// Hardware configuration
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(48,49);
// Topology
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
//
// Role management
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing.
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role = role_pong_back;
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/GettingStarted/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
//
// Setup and configure rf radio
radio.begin();
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Optionally, increase the delay between retries & # of retries
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening(); // Start listening
radio.printDetails(); // Dump the configuration of the rf unit for debugging
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
if (role == role_ping_out)
{
radio.stopListening(); // First, stop listening so we can talk.
unsigned long time = millis(); // Take the time, and send it. This will block until complete
printf("Now sending %lu...",time);
bool ok = radio.writeFast( &time, sizeof(unsigned long) );//New function for proper use of FIFO buffers
while( ! radio.txStandBy() ){} //Called when STANDBY-I mode is engaged (User is finished sending)
if (!ok)
printf("failed.\n\r");
radio.startListening(); // Now, continue listening
unsigned long started_waiting_at = millis(); // Wait here until we get a response, or timeout (250ms)
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 200 )
timeout = true;
if ( timeout ) // Describe the results
{
printf("Failed, response timed out.\n\r");
}
else
{
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );
unsigned long tim = millis();
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,tim-got_time);
}
// Try again 1s later
delay(1000);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if ( role == role_pong_back )
{
if( radio.available()){
unsigned long got_time; // Dump the payloads until we've gotten everything
bool done = false;
while (radio.available()) { // While there is data ready
// Fetch the payload, and see if this was the last one.
radio.read( &got_time, sizeof(unsigned long) );
}
// Spew it
//printf("Got payload %lu...",got_time);
// Delay just a little bit to let the other unit
// make the transition to receiver
//delay(2);
radio.stopListening(); // First, stop listening so we can talk
radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
//printf("Sent response.\n\r");
}
}
//
// Change roles
//
if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == role_pong_back )
{
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
role = role_ping_out; // Become the primary transmitter (ping out)
//radio.openWritingPipe(pipes[0]);
//radio.openReadingPipe(1,pipes[1]);
}
else if ( c == 'R' && role == role_ping_out )
{
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
role = role_pong_back; // Become the primary receiver (pong back)
//radio.openWritingPipe(pipes[1]);
//radio.openReadingPipe(1,pipes[0]);
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp
\ No newline at end of file
/*
TMRh20 2014
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.
*/
/** General Data Transfer Rate Test
* This example demonstrates basic data transfer functionality with the
updated library. This example will display the transfer rates acheived using
the slower form of high-speed transfer using blocking-writes.
*/
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
// Hardware configuration
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(48,49);
// Topology
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
// Role management
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing.
// The various roles supported by this sketch
typedef enum { role_TX = 1, role_RX } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
//Set this to determine the default role of the current running sketch
role_e role = role_RX;
//Data buffer for testing data transfer speeds
byte data[32];
//Counter and timer for keeping track transfer info
unsigned long counter, rxTimer;
float startTime, stopTime;
void setup(void)
{
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/GettingStarted/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
radio.begin(); // Setup and configure rf radio
radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX);
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(2,3); // Optionally, increase the delay between retries & # of retries
//radio.disableCRC();
radio.setCRCLength(RF24_CRC_8);
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening(); // Start listening
radio.printDetails(); // Dump the configuration of the rf unit for debugging
randomSeed(analogRead(0)); //Seed for random number generation
for(int i=0; i<32; i++){
data[i] = random(255); //Load the buffer with random data
}
radio.powerUp();
}
void loop(void){
//TX with pre-blocking writes
if(role == role_TX){
delay(1500);
printf("Initiating Basic Data Transfer\n\r");
float cycles = 10000;
startTime = millis();
for(int i=0; i<cycles; i++){
if(!radio.writeFast(&data,32)){
counter++;
}
}
stopTime = millis();
while(!radio.txStandBy()){ }
float numBytes = cycles * 32;
float rate = numBytes / (stopTime - startTime) / cycles;
printf("Transfer complete at "); Serial.print(rate); printf(" KB/s \n\r");
printf("%d of ",counter); Serial.print(cycles); printf(" Packets Failed to Send\n\r");
counter = 0;
}
if(role == role_RX){
while(radio.available()){
radio.read(&data,32);
counter++;
}
if(millis() - rxTimer > 1000){
rxTimer = millis();
// printf("Rx Rate: ");
// Serial.print(counter*32/1000);
// printf(" KB/s\n\r");
printf("Payload Count: %d \n\r", counter);
counter = 0;
}
}
//
// Change roles
//
if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == role_RX )
{
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
radio.stopListening();
role = role_TX; // Become the primary transmitter (ping out)
//radio.openWritingPipe(pipes[0]);
//radio.openReadingPipe(1,pipes[1]);
}
else if ( c == 'R' && role == role_TX )
{
radio.startListening();
printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
role = role_RX; // Become the primary receiver (pong back)
//radio.openWritingPipe(pipes[1]);
//radio.openReadingPipe(1,pipes[0]);
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp
/*
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.
*/
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#ifdef ARDUINO
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__
......@@ -15,6 +15,8 @@
* took.
*/
//March 2014 TMRh20 - Updated to utilize High Speed RF24 Library fork
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
......@@ -22,20 +24,14 @@
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
const int role_pin = 5;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
......@@ -63,14 +59,11 @@ void setup(void)
//
// Role
//
pinMode(role_pin, INPUT); // set up the role pin
digitalWrite(role_pin,HIGH); //Change this to HIGH/LOW to pre-choose a role
delay(20); // Just to get a solid reading on the role pin
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
if ( ! digitalRead(role_pin) )
if ( ! digitalRead(role_pin) ) // read the address pin, establish our role
role = role_ping_out;
else
role = role_pong_back;
......@@ -89,13 +82,10 @@ void setup(void)
//
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(8);
radio.setRetries(15,15); // optionally, increase the delay between retries & # of retries
radio.setPayloadSize(8); // optionally, reduce the payload size. seems to improve reliability
//
// Open pipes to other nodes for communication
......@@ -117,17 +107,9 @@ void setup(void)
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.startListening(); // Start listening
radio.printDetails();
radio.printDetails(); // Dump the configuration of the rf unit for debugging
}
void loop(void)
......@@ -137,47 +119,39 @@ void loop(void)
//
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
{
radio.stopListening(); // First, stop listening so we can talk.
// Take the time, and send it. This will block until complete
unsigned long time = millis();
unsigned long time = millis(); // Take the time, and send it. This will block until complete
printf("Now sending %lu...",time);
bool ok = radio.write( &time, sizeof(unsigned long) );
bool ok = radio.writeFast( &time, sizeof(unsigned long) ); // NEW command to buffer data more efficiently
if (ok)
printf("ok...");
else
if (!ok)
printf("failed.\n\r");
// Now, continue listening
radio.startListening();
while(!radio.txStandBy()){} //NEW command needs to be called after done sending. Allows FIFO buffers to be filled
radio.startListening(); // Now, continue listening
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
unsigned long started_waiting_at = millis(); // Wait here until we get a response, or timeout (250ms)
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 200 )
timeout = true;
// Describe the results
if ( timeout )
if ( timeout ) // Describe the results
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time); // Spew it
}
// Try again 1s later
delay(1000);
delay(1000); // Try again 1s later
}
//
......@@ -186,35 +160,27 @@ void loop(void)
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
if ( radio.available() ) // if there is data ready
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
unsigned long got_time; // Dump the payloads until we've gotten everything
bool done = false;
while (!done)
while (radio.available())
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
// Spew it
printf("Got payload %lu...",got_time);
radio.read( &got_time, sizeof(unsigned long) ); // Fetch the payload, and see if this was the last one.
}
printf("Got payload %lu...",got_time); // Spew it
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
}
//delay(20);
// First, stop listening so we can talk
radio.stopListening();
radio.stopListening(); // First, stop listening so we can talk
// Send the final one back.
radio.write( &got_time, sizeof(unsigned long) );
radio.write( &got_time, sizeof(unsigned long) );// Send the final one back.
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
radio.startListening(); // Now, resume listening so we catch the next packets.
}
}
}
// vim:cin:ai:sts=2 sw=2 ft=cpp
// vim:cin:ai:sts=2 sw=2 ft=cpp
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment