初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#############################################################################
#
# Makefile for librf24 examples on Linux
#
# License: GPL (General Public License)
# Author: gnulnulf <arco@appeltaart.mine.nu>
# Date: 2013/02/07 (version 1.0)
#
# Description:
# ------------
# use make all and make install to install the examples
#
ifeq ($(wildcard ../Makefile.inc), )
$(error Configuration not found. Run ./configure first)
endif
include ../Makefile.inc
# define all programs
PROGRAMS = gettingstarted gettingstarted_call_response transfer pingpair_dyn
include Makefile.examples

View File

@@ -0,0 +1,52 @@
#############################################################################
#
# Makefile for librf24 examples on Linux
#
# License: GPL (General Public License)
# Author: gnulnulf <arco@appeltaart.mine.nu>
# Date: 2013/02/07 (version 1.0)
#
# Description:
# ------------
# use make all and make install to install the examples
#
BINARY_PREFIX = rf24
SOURCES = $(PROGRAMS:=.cpp)
LIBS=-l$(LIB)
ifeq ($(DRIVER), LittleWire)
LIBS+= -llittlewire-spi
endif
all: $(PROGRAMS)
$(PROGRAMS): $(SOURCES)
$(CXX) $(CFLAGS) -I$(HEADER_DIR)/.. -I.. -L$(LIB_DIR) $@.cpp $(LIBS) -o $@
clean:
@echo "[Cleaning]"
rm -rf $(PROGRAMS)
install: all
@echo "[Installing examples to $(EXAMPLES_DIR)]"
@mkdir -p $(EXAMPLES_DIR)
@for prog in $(PROGRAMS); do \
install -m 0755 $${prog} $(EXAMPLES_DIR)/$(BINARY_PREFIX)-$${prog}; \
done
upload: all
@echo "[Uploading examples to $(REMOTE):$(REMOTE_EXAMPLES_DIR)]"
ifeq ($(REMOTE),)
@echo "[ERROR] Remote machine not configured. Run configure with respective arguments."
@exit 1
endif
@ssh -q -t -p $(REMOTE_PORT) $(REMOTE) "mkdir -p $(REMOTE_EXAMPLES_DIR)"
@ssh -q -t -p $(REMOTE_PORT) $(REMOTE) "mkdir -p /tmp/RF24_examples"
@scp -q -P $(REMOTE_PORT) $(PROGRAMS) $(REMOTE):/tmp/RF24_examples
@for prog in $(PROGRAMS); do \
ssh -q -t -p $(REMOTE_PORT) $(REMOTE) "sudo install -m 0755 /tmp/RF24_examples/$${prog} $(REMOTE_EXAMPLES_DIR)/$(BINARY_PREFIX)-$${prog}"; \
done
@ssh -q -t -p $(REMOTE_PORT) $(REMOTE) "rm -rf /tmp/RF24_examples"
.PHONY: install upload

View File

@@ -0,0 +1,24 @@
#############################################################################
#
# Makefile for librf24 examples on Raspberry Pi
#
# License: GPL (General Public License)
# Author: gnulnulf <arco@appeltaart.mine.nu>
# Date: 2013/02/07 (version 1.0)
#
# Description:
# ------------
# use make all and make install to install the examples
# You can change the install directory by editing the prefix line
#
ifeq ($(wildcard ../../Makefile.inc), )
$(error Configuration not found. Run ./configure first)
endif
include ../../Makefile.inc
# define all programs
PROGRAMS = rpi-hub scanner
include ../Makefile.examples

View File

@@ -0,0 +1,125 @@
/*
*
* Filename : rpi-hub.cpp
*
* This program makes the RPi as a hub listening to all six pipes from the remote sensor nodes ( usually Arduino )
* and will return the packet back to the sensor on pipe0 so that the sender can calculate the round trip delays
* when the payload matches.
*
* I encounter that at times, it also receive from pipe7 ( or pipe0 ) with content of FFFFFFFFF that I will not sent
* back to the sender
*
* Refer to RF24/examples/rpi_hub_arduino/ for the corresponding Arduino sketches to work with this code.
*
*
* CE is not used and CSN is GPIO25 (not pinout)
*
* Refer to RPi docs for GPIO numbers
*
* Author : Stanley Seow
* e-mail : stanleyseow@gmail.com
* date : 6th Mar 2013
*
* 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
* Modified to use with Arduipi board http://hallard.me/arduipi
* Changed to use modified bcm2835 and RF24 library
*
*
*/
#include <cstdlib>
#include <iostream>
#include <RF24/RF24.h>
using namespace std;
// Radio pipe addresses for the 2 nodes to communicate.
// First pipe is for writing, 2nd, 3rd, 4th, 5th & 6th is for reading...
const uint64_t pipes[6] = {0xF0F0F0F0D2LL, 0xF0F0F0F0E1LL, 0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL, 0xF0F0F0F0F1, 0xF0F0F0F0F2};
// CE Pin, CSN Pin, SPI Speed
// Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ);
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
int main(int argc, char** argv)
{
uint8_t len;
// Refer to RF24.h or nRF24L01 DS for settings
radio.begin();
radio.enableDynamicPayloads();
radio.setAutoAck(1);
radio.setRetries(15, 15);
radio.setDataRate(RF24_1MBPS);
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(76);
radio.setCRCLength(RF24_CRC_16);
// Open 6 pipes for readings ( 5 plus pipe0, also can be used for reading )
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.openReadingPipe(2, pipes[2]);
radio.openReadingPipe(3, pipes[3]);
radio.openReadingPipe(4, pipes[4]);
radio.openReadingPipe(5, pipes[5]);
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
printf("Output below : \n");
delay(1);
while (1) {
char receivePayload[32];
uint8_t pipe = 1;
// Start listening
radio.startListening();
while (radio.available(&pipe)) {
len = radio.getDynamicPayloadSize();
radio.read(receivePayload, len);
// Display it on screen
printf("Recv: size=%i payload=%s pipe=%i", len, receivePayload, pipe);
// Send back payload to sender
radio.stopListening();
// if pipe is 7, do not send it back
if (pipe != 7) {
radio.write(receivePayload, len);
receivePayload[len] = 0;
printf("\t Send: size=%i payload=%s pipe:%i\n", len, receivePayload, pipe);
} else {
printf("\n");
}
pipe++;
// reset pipe to 0
if (pipe > 6) {
pipe = 0;
}
}
delayMicroseconds(20);
}
return 0;
}

View File

@@ -0,0 +1,137 @@
/*
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.
03/17/2013 : Charles-Henri Hallard (http://hallard.me)
Modified to use with Arduipi board http://hallard.me/arduipi
Changed to use modified bcm2835 and RF24 library
*/
/**
* Channel scanner
*
* Example to detect interference on the various channels available.
* This is a good diagnostic tool to check whether you're picking a
* good channel for your application.
*
* Inspired by cpixip.
* See http://arduino.cc/forum/index.php/topic,54795.0.html
*/
#include <cstdlib>
#include <iostream>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
//
// CE Pin, CSN Pin, SPI Speed
// Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ);
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
//RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
// Generic setup
RF24 radio(22, 0);
//
// Channel info
//
const uint8_t num_channels = 126;
uint8_t values[num_channels];
const int num_reps = 100;
int reset_array = 0;
int main(int argc, char** argv)
{
//
// Print preamble
//
//Serial.begin(115200);
//printf_begin();
printf("RF24/examples/scanner/\n");
//
// Setup and configure rf radio
//
radio.begin();
radio.setAutoAck(false);
// Get into standby mode
radio.startListening();
radio.stopListening();
radio.printDetails();
// Print out header, high then low digit
int i = 0;
while (i < num_channels) {
printf("%x", i >> 4);
++i;
}
printf("\n");
i = 0;
while (i < num_channels) {
printf("%x", i & 0xf);
++i;
}
printf("\n");
// forever loop
while (1) {
// Clear measurement values
memset(values, 0, sizeof(values));
// Scan all channels num_reps times
int rep_counter = num_reps;
while (rep_counter--) {
int i = num_channels;
while (i--) {
// Select this channel
radio.setChannel(i);
// Listen for a little
radio.startListening();
delayMicroseconds(128);
radio.stopListening();
// Did we get a carrier?
if (radio.testCarrier()) {
++values[i];
}
}
}
// Print out channel measurements, clamped to a single hex digit
i = 0;
while (i < num_channels) {
printf("%x", min(0xf, (values[i] & 0xf)));
++i;
}
printf("\n");
}
return 0;
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

View File

@@ -0,0 +1,184 @@
/*
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.
03/17/2013 : Charles-Henri Hallard (http://hallard.me)
Modified to use with Arduipi board http://hallard.me/arduipi
Changed to use modified bcm2835 and RF24 library
TMRh20 2014 - Updated to work with optimized RF24 Arduino library
*/
/**
* Example RF Radio Ping Pair
*
* This is an example of how to use the RF24 class on RPi, communicating to an Arduino running
* the GettingStarted sketch.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
// Configure the appropriate pins for your connections
/****************** Linux ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
// CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
// ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
// Generic:
RF24 radio(22,0);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://tmrh20.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
/********** User Config *********/
// Assign a unique identifier for this node, 0 or 1
bool radioNumber = 1;
/********************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t pipes[][6] = {"1Node", "2Node"};
int main(int argc, char** argv)
{
bool role_ping_out = true, role_pong_back = false;
bool role = role_pong_back;
cout << "RF24/examples/GettingStarted/\n";
// Setup and configure rf radio
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15, 15);
// Dump the configuration of the rf unit for debugging
radio.printDetails();
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit) \n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
if (!radioNumber) {
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
} else {
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1, pipes[0]);
}
radio.startListening();
// forever loop
while (1) {
if (role == role_ping_out) {
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
printf("Now sending...\n");
unsigned long time = millis();
bool ok = radio.write(&time, sizeof(unsigned long));
if (!ok) {
printf("failed.\n");
}
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while (!radio.available() && !timeout) {
if (millis() - started_waiting_at > 200) {
timeout = true;
}
}
// Describe the results
if (timeout) {
printf("Failed, response timed out.\n");
} else {
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read(&got_time, sizeof(unsigned long));
// Spew it
printf("Got response %lu, round-trip delay: %lu\n", got_time, millis() - got_time);
}
sleep(1);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if (role == role_pong_back) {
// if there is data ready
if (radio.available()) {
// Dump the payloads until we've gotten everything
unsigned long got_time;
// Fetch the payload, and see if this was the last one.
while (radio.available()) {
radio.read(&got_time, sizeof(unsigned long));
}
radio.stopListening();
radio.write(&got_time, sizeof(unsigned long));
// Now, resume listening so we catch the next packets.
radio.startListening();
// Spew it
printf("Got payload(%d) %lu...\n", sizeof(unsigned long), got_time);
delay(925); //Delay after payload responded to, minimize RPi CPU time
}
}
} // forever loop
return 0;
}

View File

@@ -0,0 +1,147 @@
/*
TMRh20 2014 - Updated to work with optimized RF24 Arduino library
*/
/**
* Example for efficient call-response using ack-payloads
*
* This example continues to make use of all the normal functionality of the radios including
* the auto-ack and auto-retry features, but allows ack-payloads to be written optionlly as well.
* This allows very fast call-response communication, with the responding radio never having to
* switch out of Primary Receiver mode to send back a payload, but having the option to switch to
* primary transmitter if wanting to initiate communication instead of respond to a commmunication.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
// Configure the appropriate pins for your connections
/****************** Linux ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
// CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
// ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
// Generic:
RF24 radio(22,0);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://tmrh20.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
/********** User Config *********/
// Assign a unique identifier for this node, 0 or 1. Arduino example uses radioNumber 0 by default.
bool radioNumber = 1;
/********************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t addresses[][6] = {"1Node", "2Node"};
bool role_ping_out = 1, role_pong_back = 0, role = 0;
uint8_t counter = 1; // A single byte to keep track of the data being sent back and forth
int main(int argc, char** argv)
{
cout << "RPi/RF24/examples/gettingstarted_call_response\n";
radio.begin();
radio.enableAckPayload(); // Allow optional ack payloads
radio.enableDynamicPayloads();
radio.printDetails(); // Dump the configuration of the rf unit for debugging
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit)\n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
// This opens two pipes for these two nodes to communicate
// back and forth.
if (!radioNumber) {
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
} else {
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
}
radio.startListening();
radio.writeAckPayload(1, &counter, 1);
// forever loop
while (1) {
/****************** Ping Out Role ***************************/
if (role == role_ping_out) { // Radio is in ping mode
uint8_t gotByte; // Initialize a variable for the incoming response
radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending %d as payload. ", counter); // Use a simple byte counter as payload
unsigned long time = millis(); // Record the current microsecond count
if (radio.write(&counter, 1)) { // Send the counter variable to the other radio
if (!radio.available()) { // If nothing in the buffer, we got an ack but it is blank
printf("Got blank response. round-trip delay: %lu ms\n\r", millis() - time);
} else {
while (radio.available()) { // If an ack with payload was received
radio.read(&gotByte, 1); // Read it, and display the response time
printf("Got response %d, round-trip delay: %lu ms\n\r", gotByte, millis() - time);
counter++; // Increment the counter variable
}
}
} else {
printf("Sending failed.\n\r");
} // If no ack response, sending failed
sleep(1); // Try again later
}
/****************** Pong Back Role ***************************/
if (role == role_pong_back) {
uint8_t pipeNo, gotByte; // Declare variables for the pipe and the byte received
if (radio.available(&pipeNo)) { // Read all available payloads
radio.read(&gotByte, 1);
// Since this is a call-response. Respond directly with an ack payload.
gotByte += 1; // Ack payloads are much more efficient than switching to transmit mode to respond to a call
radio.writeAckPayload(pipeNo, &gotByte, 1); // This can be commented out to send empty payloads.
printf("Loaded next response %d \n\r", gotByte);
delay(900); //Delay after a response to minimize CPU usage on RPi
//Expects a payload every second
}
}
} //while 1
} //main

View File

@@ -0,0 +1,24 @@
#############################################################################
#
# Makefile for librf24 examples on Raspberry Pi
#
# License: GPL (General Public License)
# Author: gnulnulf <arco@appeltaart.mine.nu>
# Date: 2013/02/07 (version 1.0)
#
# Description:
# ------------
# use make all and make install to install the examples
# You can change the install directory by editing the prefix line
#
ifeq ($(wildcard ../../Makefile.inc), )
$(error Configuration not found. Run ./configure first)
endif
include ../../Makefile.inc
# define all programs
PROGRAMS = gettingstarted_call_response_int gettingstarted_call_response_int2 transfer_interrupt pingpair_dyn_int
include ../Makefile.examples

View File

@@ -0,0 +1,147 @@
/*
TMRh20 2014 - Updated to work with optimized RF24 Arduino library
*/
/**
* Example for efficient call-response using ack-payloads and interrupts
*
* This example continues to make use of all the normal functionality of the radios including
* the auto-ack and auto-retry features, but allows ack-payloads to be written optionlly as well.
* This allows very fast call-response communication, with the responding radio never having to
* switch out of Primary Receiver mode to send back a payload, but having the option to switch to
* primary transmitter if wanting to initiate communication instead of respond to a commmunication.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
// Configure the appropriate pins for your connections
/****************** Raspberry Pi ***********************/
RF24 radio(22, 0); //GPIO, SPI-BUS
/********** User Config *********/
// Assign a unique identifier for this node, 0 or 1. Arduino example uses radioNumber 0 by default.
bool radioNumber = 1;
int interruptPin = 23;
/********************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t addresses[][6] = {"1Node", "2Node"};
volatile bool role_ping_out = 1, role_pong_back = 0, role = 0;
uint8_t counter = 1; // A single byte to keep track of the data being sent back and forth
volatile bool gotResponse = false;
void intHandler()
{
if (role == role_pong_back) {
uint8_t pipeNo, gotByte; // Declare variables for the pipe and the byte received
if (radio.available(&pipeNo)) { // Read all available payloads
radio.read(&gotByte, 1);
// Since this is a call-response. Respond directly with an ack payload.
gotByte += 1; // Ack payloads are much more efficient than switching to transmit mode to respond to a call
radio.writeAckPayload(pipeNo, &gotByte, 1); // This can be commented out to send empty payloads.
printf("Loaded next response %d \n\r", gotByte);
}
}
}
int main(int argc, char** argv)
{
cout << "RPi/RF24/examples/gettingstarted_call_response_int\n";
radio.begin();
radio.enableAckPayload(); // Allow optional ack payloads
radio.enableDynamicPayloads();
radio.printDetails(); // Dump the configuration of the rf unit for debugging
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit)\n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
// This opens two pipes for these two nodes to communicate
// back and forth.
if (!radioNumber) {
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
} else {
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
}
radio.startListening();
radio.writeAckPayload(1, &counter, 1);
radio.maskIRQ(1, 1, 0); //Mask tx_ok & tx_fail interrupts
attachInterrupt(interruptPin, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23
// forever loop
while (1) {
/****************** Ping Out Role ***************************/
if (role == role_ping_out) { // Radio is in ping mode
uint8_t gotByte; // Initialize a variable for the incoming response
radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending %d as payload. ", counter); // Use a simple byte counter as payload
unsigned long time = millis(); // Record the current microsecond count
if (radio.write(&counter, 1)) { // Send the counter variable to the other radio
if (!radio.available()) { // If nothing in the buffer, we got an ack but it is blank
printf("Got blank response. round-trip delay: %lu ms\n\r", millis() - time);
} else {
while (radio.available()) { // If an ack with payload was received
radio.read(&gotByte, 1); // Read it, and display the response time
printf("Got response %d, round-trip delay: %lu ms\n\r", gotByte, millis() - time);
counter++; // Increment the counter variable
}
}
} else {
printf("Sending failed.\n\r");
} // If no ack response, sending failed
sleep(1); // Try again later
}
/****************** Pong Back Role ***************************/
} //while 1
} //main

View File

@@ -0,0 +1,156 @@
/*
TMRh20 2014 - Updated to work with optimized RF24 Arduino library
*/
/**
* Example for efficient call-response using ack-payloads
*
* This example continues to make use of all the normal functionality of the radios including
* the auto-ack and auto-retry features, but allows ack-payloads to be written optionlly as well.
* This allows very fast call-response communication, with the responding radio never having to
* switch out of Primary Receiver mode to send back a payload, but having the option to switch to
* primary transmitter if wanting to initiate communication instead of respond to a commmunication.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
// Configure the appropriate pins for your connections
/****************** Raspberry Pi ***********************/
RF24 radio(22, 0);
/********** User Config *********/
// Assign a unique identifier for this node, 0 or 1. Arduino example uses radioNumber 0 by default.
bool radioNumber = 1;
int interruptPin = 23;
/********************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t addresses[][6] = {"1Node", "2Node"};
bool role_ping_out = 1, role_pong_back = 0, role = 0;
uint8_t counter = 1; // A single byte to keep track of the data being sent back and forth
uint32_t timer = 0;
void intHandler()
{
bool tx_ok, tx_fail, rx;
radio.whatHappened(tx_ok, tx_fail, rx);
if (tx_fail) {
printf("Sending failed.\n\r");
}
if (role == role_ping_out && tx_ok) {
if (!radio.available()) {
printf("Got blank response. round-trip delay: %u ms\n\r", millis() - timer);
}
}
if (role == role_ping_out) {
while (radio.available()) {
uint8_t gotByte;
radio.read(&gotByte, 1);
printf("Got response %d, round-trip delay: %u ms\n\r", gotByte, millis() - timer);
counter++;
}
}
if (role == role_pong_back) {
if (tx_ok) {
printf("Ack Payload Sent\n");
}
uint8_t pipeNo, gotByte;
if (radio.available(&pipeNo)) {
radio.read(&gotByte, 1);
gotByte += 1;
radio.writeAckPayload(pipeNo, &gotByte, 1);
printf("Loaded next response %d \n\r", gotByte);
}
}
}
int main(int argc, char** argv)
{
cout << "RPi/RF24/examples/gettingstarted_call_response\n";
radio.begin();
radio.enableAckPayload(); // Allow optional ack payloads
radio.enableDynamicPayloads();
radio.printDetails(); // Dump the configuration of the rf unit for debugging
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit)\n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
// This opens two pipes for these two nodes to communicate
// back and forth.
if (!radioNumber) {
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
} else {
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
}
radio.startListening();
radio.writeAckPayload(1, &counter, 1);
attachInterrupt(interruptPin, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23
// forever loop
while (1) {
/****************** Ping Out Role ***************************/
if (role == role_ping_out) { // Radio is in ping mode
//uint8_t gotByte; // Initialize a variable for the incoming response
radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending %d as payload. ", counter); // Use a simple byte counter as payload
timer = millis(); // Record the current microsecond count
radio.startWrite(&counter, 1, false); // Send the counter variable to the other radio
sleep(1); // Try again later
}
/****************** Pong Back Role ***************************/
} //while 1
} //main

View File

@@ -0,0 +1,177 @@
/*
TMRh20 2014 - Optimized RF24 Library Fork
*/
/**
* Example using Dynamic Payloads
*
* This is an example of how to use payloads of a varying (dynamic) size.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
// Configure the appropriate pins for your connections
/****************** Raspberry Pi ***********************/
RF24 radio(22, 0); // CE GPIO, CSN SPI-BUS
int interruptPin = 23; // GPIO pin for interrupts
/**************************************************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};
const int min_payload_size = 4;
const int max_payload_size = 32;
const int payload_size_increments_by = 1;
int next_payload_size = min_payload_size;
char receive_payload[max_payload_size + 1]; // +1 to allow room for a terminating NULL char
bool role_ping_out = 1, role_pong_back = 0;
bool role = 0;
void intHandler()
{
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if (role == role_pong_back) {
// if there is data ready
if (radio.available()) {
// Dump the payloads until we've gotten everything
uint8_t len = 0;
while (radio.available()) {
// Fetch the payload, and see if this was the last one.
len = radio.getDynamicPayloadSize();
radio.read(receive_payload, len);
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got payload size=%i value=%s\n\r", len, receive_payload);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write(receive_payload, len);
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
int main(int argc, char** argv)
{
// Print preamble:
cout << "RF24/examples/pingpair_dyn/\n";
// Setup and configure rf radio
radio.begin();
radio.enableDynamicPayloads();
radio.setRetries(5, 15);
radio.printDetails();
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) \n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
if (role == role_ping_out) {
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
} else {
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1, pipes[0]);
radio.startListening();
}
attachInterrupt(interruptPin, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23
// forever loop
while (1) {
if (role == role_ping_out) {
// The payload will always be the same, what will change is how much of it we send.
static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
printf("Now sending length %i...", next_payload_size);
radio.write(send_payload, next_payload_size);
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout
unsigned long started_waiting_at = millis();
bool timeout = false;
while (!radio.available() && !timeout) {
if (millis() - started_waiting_at > 500) {
timeout = true;
}
}
// Describe the results
if (timeout) {
printf("Failed, response timed out.\n\r");
} else {
// Grab the response, compare, and send to debugging spew
uint8_t len = radio.getDynamicPayloadSize();
radio.read(receive_payload, len);
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got response size=%i value=%s\n\r", len, receive_payload);
}
// Update size for next time.
next_payload_size += payload_size_increments_by;
if (next_payload_size > max_payload_size) {
next_payload_size = min_payload_size;
}
// Try again 1s later
delay(100);
}
}
}

View File

@@ -0,0 +1,190 @@
/*
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 <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
#include <unistd.h>
using namespace std;
//
// Hardware configuration
//
/****************** Raspberry Pi ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// See http://www.airspayce.com/mikem/bcm2835/group__constants.html#ga63c029bd6500167152db4e57736d0939 and the related enumerations for pin information.
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
// NEW: Setup for RPi B+
//RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
// Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
/*** RPi Alternate ***/
//Note: Specify SPI BUS 0 or 1 instead of CS pin number.
// See http://tmrh20.github.io/RF24/RPi.html for more information on usage
//RPi Alternate, with MRAA
//RF24 radio(15,0);
//RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
//RF24 radio(22,0);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://tmrh20.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
// Setup for ARM(Linux) devices like BBB using spidev (default is "/dev/spidev1.0" )
//RF24 radio(115,0);
//BBB Alternate, with mraa
// CE pin = (Header P9, Pin 13) = 59 = 13 + 46
//Note: Specify SPI BUS 0 or 1 instead of CS pin number.
//RF24 radio(59,0);
/**************************************************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t addresses[2] = {0xABCDABCD71LL, 0x544d52687CLL};
uint8_t data[32];
unsigned long startTime, stopTime, counter, rxTimer = 0;
void intHandler()
{
//Read as long data is available
//Single interrupts may be lost if a lot of data comes in.
while (radio.available()) {
radio.read(&data, 32);
counter++;
}
}
int main(int argc, char** argv)
{
bool role_ping_out = 1, role_pong_back = 0;
bool role = 0;
// Print preamble:
cout << "RF24/examples/Transfer/\n";
radio.begin(); // Setup and configure rf radio
radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(2, 15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
radio.printDetails();
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit)\n>";
getline(cin, input);
attachInterrupt(23, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
if (role == role_ping_out) {
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.stopListening();
} else {
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.startListening();
}
for (int i = 0; i < 32; i++) {
data[i] = rand() % 255; //Load the buffer with random data
}
// forever loop
while (1) {
if (role == role_ping_out) {
sleep(2);
printf("Initiating Basic Data Transfer\n\r");
long int cycles = 10000; //Change this to a higher or lower number.
// unsigned long pauseTime = millis(); //Uncomment if autoAck == 1 ( NOACK )
startTime = millis();
for (int i = 0; i < cycles; i++) { //Loop through a number of cycles
data[0] = i; //Change the first byte of the payload for identification
if (!radio.writeFast(&data, 32)) { //Write to the FIFO buffers
counter++; //Keep count of failed payloads
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
/* if(millis() - pauseTime > 3){ // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
pauseTime = millis();
radio.txStandBy(); // This gives the PLL time to sync back up
}
*/
}
stopTime = millis();
if (!radio.txStandBy()) {
counter += 3;
}
float numBytes = cycles * 32;
float rate = numBytes / (stopTime - startTime);
printf("Transfer complete at %.2f KB/s \n\r", rate);
printf("%lu of %lu Packets Failed to Send\n\r", counter, cycles);
counter = 0;
}
if (role == role_pong_back) {
if (millis() - rxTimer > 1000) {
rxTimer = millis();
printf("Rate: ");
float numBytes = counter * 32;
printf("%.2f KB/s \n\r", numBytes / 1000);
printf("Payload Count: %lu \n\r", counter);
counter = 0;
}
delay(2);
}
} // loop
} // main

View File

@@ -0,0 +1,179 @@
/*
TMRh20 2014 - Optimized RF24 Library Fork
*/
/**
* Example using Dynamic Payloads
*
* This is an example of how to use payloads of a varying (dynamic) size.
*/
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include "./RF24.h"
using namespace std;
//
// Hardware configuration
// Configure the appropriate pins for your connections
/****************** Linux ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
// CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
// ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
// Generic:
RF24 radio(22,0);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://tmrh20.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
/**************************************************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};
const int min_payload_size = 4;
const int max_payload_size = 32;
const int payload_size_increments_by = 1;
int next_payload_size = min_payload_size;
char receive_payload[max_payload_size + 1]; // +1 to allow room for a terminating NULL char
int main(int argc, char** argv)
{
bool role_ping_out = 1, role_pong_back = 0;
bool role = 0;
// Print preamble:
cout << "RF24/examples/pingpair_dyn/\n";
// Setup and configure rf radio
radio.begin();
radio.enableDynamicPayloads();
radio.setRetries(5, 15);
radio.printDetails();
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) \n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
if (role == role_ping_out) {
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
} else {
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1, pipes[0]);
radio.startListening();
}
// forever loop
while (1) {
if (role == role_ping_out) {
// The payload will always be the same, what will change is how much of it we send.
static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
// First, stop listening so we can talk.
radio.stopListening();
// Take the time, and send it. This will block until complete
printf("Now sending length %i...", next_payload_size);
radio.write(send_payload, next_payload_size);
// Now, continue listening
radio.startListening();
// Wait here until we get a response, or timeout
unsigned long started_waiting_at = millis();
bool timeout = false;
while (!radio.available() && !timeout) {
if (millis() - started_waiting_at > 500) {
timeout = true;
}
}
// Describe the results
if (timeout) {
printf("Failed, response timed out.\n\r");
} else {
// Grab the response, compare, and send to debugging spew
uint8_t len = radio.getDynamicPayloadSize();
radio.read(receive_payload, len);
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got response size=%i value=%s\n\r", len, receive_payload);
}
// Update size for next time.
next_payload_size += payload_size_increments_by;
if (next_payload_size > max_payload_size) {
next_payload_size = min_payload_size;
}
// Try again 1s later
delay(100);
}
//
// Pong back role. Receive each packet, dump it out, and send it back
//
if (role == role_pong_back) {
// if there is data ready
if (radio.available()) {
// Dump the payloads until we've gotten everything
uint8_t len = 0;
while (radio.available()) {
// Fetch the payload, and see if this was the last one.
len = radio.getDynamicPayloadSize();
radio.read(receive_payload, len);
// Put a zero at the end for easy printing
receive_payload[len] = 0;
// Spew it
printf("Got payload size=%i value=%s\n\r", len, receive_payload);
}
// First, stop listening so we can talk
radio.stopListening();
// Send the final one back.
radio.write(receive_payload, len);
printf("Sent response.\n\r");
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
}

View File

@@ -0,0 +1,134 @@
#!/usr/bin/env python
#
# Example using Dynamic Payloads
#
# This is an example of how to use payloads of a varying (dynamic) size.
#
from __future__ import print_function
import time
from RF24 import *
import RPi.GPIO as GPIO
irq_gpio_pin = None
########### USER CONFIGURATION ###########
# See https://github.com/TMRh20/RF24/blob/master/pyRF24/readme.md
# Radio CE Pin, CSN Pin, SPI Speed
# CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
# CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
# ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
# Generic:
radio = RF24(22,0);
# RPi Alternate, with SPIDEV - Note: Edit RF24/arch/BBB/spi.cpp and set 'this->device = "/dev/spidev0.0";;' or as listed in /dev
#radio = RF24(22, 0);
# Setup for connected IRQ pin, GPIO 24 on RPi B+; uncomment to activate
# irq_gpio_pin = 24
##########################################
def try_read_data(channel=0):
if radio.available():
while radio.available():
len = radio.getDynamicPayloadSize()
receive_payload = radio.read(len)
print('Got payload size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
# First, stop listening so we can talk
radio.stopListening()
# Send the final one back.
radio.write(receive_payload)
print('Sent response.')
# Now, resume listening so we catch the next packets.
radio.startListening()
pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2]
min_payload_size = 4
max_payload_size = 32
payload_size_increments_by = 1
next_payload_size = min_payload_size
inp_role = 'none'
send_payload = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012'
millis = lambda: int(round(time.time() * 1000))
print('pyRF24/examples/pingpair_dyn/')
radio.begin()
radio.enableDynamicPayloads()
radio.setRetries(5, 15)
radio.printDetails()
print(' ************ Role Setup *********** ')
while (inp_role != '0') and (inp_role != '1'):
inp_role = str(input('Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) '))
if inp_role == '0':
print('Role: Pong Back, awaiting transmission')
if irq_gpio_pin is not None:
# set up callback for irq pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(irq_gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(irq_gpio_pin, GPIO.FALLING, callback=try_read_data)
radio.openWritingPipe(pipes[1])
radio.openReadingPipe(1, pipes[0])
radio.startListening()
else:
print('Role: Ping Out, starting transmission')
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])
# forever loop
while 1:
if inp_role == '1': # ping out
# The payload will always be the same, what will change is how much of it we send.
# First, stop listening so we can talk.
radio.stopListening()
# Take the time, and send it. This will block until complete
print('Now sending length {} ... '.format(next_payload_size), end="")
radio.write(send_payload[:next_payload_size])
# Now, continue listening
radio.startListening()
# Wait here until we get a response, or timeout
started_waiting_at = millis()
timeout = False
while (not radio.available()) and (not timeout):
if (millis() - started_waiting_at) > 500:
timeout = True
# Describe the results
if timeout:
print('failed, response timed out.')
else:
# Grab the response, compare, and send to debugging spew
len = radio.getDynamicPayloadSize()
receive_payload = radio.read(len)
# Spew it
print('got response size={} value="{}"'.format(len, receive_payload.decode('utf-8')))
# Update size for next time.
next_payload_size += payload_size_increments_by
if next_payload_size > max_payload_size:
next_payload_size = min_payload_size
time.sleep(0.1)
else:
# Pong back role. Receive each packet, dump it out, and send it back
# if there is data ready
if irq_gpio_pin is None:
# no irq pin is set up -> poll it
try_read_data()
else:
# callback routine set for irq pin takes care for reading -
# do nothing, just sleeps in order not to burn cpu by looping
time.sleep(1000)

View File

@@ -0,0 +1,3 @@
Note: These examples were originally designed for RPi, but should work on any supported Linux platform, with the proper pin configuration.
See http://tmrh20.github.io/RF24 for more information

View File

@@ -0,0 +1,163 @@
/*
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 <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
#include <unistd.h>
using namespace std;
//
// Hardware configuration
//
/****************** Linux ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
// CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
// ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
// Generic:
RF24 radio(22,0);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://tmrh20.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
/**************************************************************/
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t addresses[2] = {0xABCDABCD71LL, 0x544d52687CLL};
uint8_t data[32];
unsigned long startTime, stopTime, counter, rxTimer = 0;
int main(int argc, char** argv)
{
bool role_ping_out = 1, role_pong_back = 0;
bool role = 0;
// Print preamble:
cout << "RF24/examples/Transfer/\n";
radio.begin(); // Setup and configure rf radio
radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(2, 15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); // Use 8-bit CRC for performance
radio.printDetails();
/********* Role chooser ***********/
printf("\n ************ Role Setup ***********\n");
string input = "";
char myChar = {0};
cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit)\n>";
getline(cin, input);
if (input.length() == 1) {
myChar = input[0];
if (myChar == '0') {
cout << "Role: Pong Back, awaiting transmission " << endl << endl;
} else {
cout << "Role: Ping Out, starting transmission " << endl << endl;
role = role_ping_out;
}
}
/***********************************/
if (role == role_ping_out) {
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.stopListening();
} else {
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.startListening();
}
for (int i = 0; i < 32; i++) {
data[i] = rand() % 255; //Load the buffer with random data
}
// forever loop
while (1) {
if (role == role_ping_out) {
sleep(2);
printf("Initiating Basic Data Transfer\n\r");
long int cycles = 10000; //Change this to a higher or lower number.
// unsigned long pauseTime = millis(); //Uncomment if autoAck == 1 ( NOACK )
startTime = millis();
for (int i = 0; i < cycles; i++) { //Loop through a number of cycles
data[0] = i; //Change the first byte of the payload for identification
if (!radio.writeFast(&data, 32)) { //Write to the FIFO buffers
counter++; //Keep count of failed payloads
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
/* if(millis() - pauseTime > 3){ // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
pauseTime = millis();
radio.txStandBy(); // This gives the PLL time to sync back up
}
*/
}
stopTime = millis();
if (!radio.txStandBy()) {
counter += 3;
}
float numBytes = cycles * 32;
float rate = numBytes / (stopTime - startTime);
printf("Transfer complete at %.2f KB/s \n\r", rate);
printf("%lu of %lu Packets Failed to Send\n\r", counter, cycles);
counter = 0;
}
if (role == role_pong_back) {
while (radio.available()) {
radio.read(&data, 32);
counter++;
}
if (millis() - rxTimer > 1000) {
rxTimer = millis();
printf("Rate: ");
float numBytes = counter * 32;
printf("%.2f KB/s \n\r", numBytes / 1000);
printf("Payload Count: %lu \n\r", counter);
counter = 0;
}
}
} // loop
} // main