From e97e0239d7861c27b08bb7541a1966b9c89a2120 Mon Sep 17 00:00:00 2001 From: james Date: Thu, 31 Mar 2011 21:32:45 -0700 Subject: [PATCH] Simplified example to use less calls. Defaults now to 32-byte payloads, but you can call in with any size under that. Added example to docs. --- .gitignore | 1 + Doxyfile | 6 +- RF24.cpp | 30 ++-- RF24.h | 47 ++++-- examples/pingpair/.gitignore | 1 + examples/pingpair/makefile | 277 +++++++++++++++++++++++++++++++++ examples/pingpair/pingpair.pde | 91 +++++------ 7 files changed, 376 insertions(+), 77 deletions(-) create mode 100644 examples/pingpair/.gitignore create mode 100644 examples/pingpair/makefile diff --git a/.gitignore b/.gitignore index 35123f9..f49947e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +*.o .*.swp docs/ diff --git a/Doxyfile b/Doxyfile index 1bb2af3..ee5ba85 100644 --- a/Doxyfile +++ b/Doxyfile @@ -631,7 +631,7 @@ EXCLUDE_SYMBOLS = # directories that contain example code fragments that are included (see # the \include command). -EXAMPLE_PATH = +EXAMPLE_PATH = examples # If the value of the EXAMPLE_PATH tag contains directories, you can use the # EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp @@ -645,7 +645,7 @@ EXAMPLE_PATTERNS = # commands irrespective of the value of the RECURSIVE tag. # Possible values are YES and NO. If left blank NO is used. -EXAMPLE_RECURSIVE = NO +EXAMPLE_RECURSIVE = YES # The IMAGE_PATH tag can be used to specify one or more files or # directories that contain image that are included in the documentation (see @@ -1548,4 +1548,4 @@ GENERATE_LEGEND = YES # remove the intermediate dot files that are used to generate # the various graphs. -DOT_CLEANUP = YES \ No newline at end of file +DOT_CLEANUP = YES diff --git a/RF24.cpp b/RF24.cpp index 5d8cb60..2533df1 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -80,7 +80,7 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value) /******************************************************************/ -uint8_t RF24::write_payload(const void* buf) +uint8_t RF24::write_payload(const void* buf, uint8_t len) { uint8_t status; @@ -88,9 +88,12 @@ uint8_t RF24::write_payload(const void* buf) csn(LOW); status = SPI.transfer( W_TX_PAYLOAD ); - uint8_t len = payload_size; - while ( len-- ) + uint8_t data_len = min(len,payload_size); + uint8_t blank_len = payload_size - data_len; + while ( data_len-- ) SPI.transfer(*current++); + while ( blank_len-- ) + SPI.transfer(0); csn(HIGH); @@ -99,16 +102,19 @@ uint8_t RF24::write_payload(const void* buf) /******************************************************************/ -uint8_t RF24::read_payload(void* buf) +uint8_t RF24::read_payload(void* buf, uint8_t len) { uint8_t status; uint8_t* current = (uint8_t*)buf; csn(LOW); status = SPI.transfer( R_RX_PAYLOAD ); - uint8_t len = payload_size; - while ( len-- ) + uint8_t data_len = min(len,payload_size); + uint8_t blank_len = payload_size - data_len; + while ( data_len-- ) *current++ = SPI.transfer(0xff); + while ( blank_len-- ) + SPI.transfer(0xff); csn(HIGH); return status; @@ -181,7 +187,7 @@ void RF24::print_observe_tx(uint8_t value) /******************************************************************/ RF24::RF24(int _cepin, int _cspin): - ce_pin(_cepin), csn_pin(_cspin) + ce_pin(_cepin), csn_pin(_cspin), payload_size(32) { } @@ -197,7 +203,6 @@ void RF24::setChannel(int channel) void RF24::setPayloadSize(uint8_t size) { payload_size = min(size,32); - write_register(RX_PW_P0,min(size,32)); } /******************************************************************/ @@ -300,7 +305,7 @@ void RF24::stopListening(void) /******************************************************************/ -boolean RF24::write( const void* buf ) +boolean RF24::write( const void* buf, uint8_t len ) { boolean result = false; @@ -308,7 +313,7 @@ boolean RF24::write( const void* buf ) write_register(CONFIG, _BV(EN_CRC) | _BV(PWR_UP)); // Send the payload - write_payload( buf ); + write_payload( buf, len ); // Allons! ce(HIGH); @@ -366,13 +371,13 @@ boolean RF24::available(void) /******************************************************************/ -boolean RF24::read( void* buf ) +boolean RF24::read( void* buf, uint8_t len ) { // was this the last of the data available? boolean result = false; // Fetch the payload - read_payload( buf ); + read_payload( buf, len ); uint8_t fifo_status; read_register(FIFO_STATUS,&fifo_status,1); @@ -391,6 +396,7 @@ void RF24::openWritingPipe(uint64_t value) write_register(RX_ADDR_P0, reinterpret_cast(&value), 5); write_register(TX_ADDR, reinterpret_cast(&value), 5); + write_register(RX_PW_P0,min(payload_size,32)); } /******************************************************************/ diff --git a/RF24.h b/RF24.h index 8ae96f5..8916f0b 100644 --- a/RF24.h +++ b/RF24.h @@ -14,16 +14,20 @@ /** * Driver for nRF24L01 2.4GHz Wireless Transceiver * - * See Datasheet + * Please refer to: + * + * @li Detailed Documentation + * @li Source Code + * @li Chip Datasheet * * This chip uses the SPI bus, plus two chip control pins. Remember that pin 10 must still remain an output, or * the SPI hardware will go into 'slave' mode. * * Design Goals: This library is designed to be... - * * Maximally compliant with the intended operation of the chip - * * Easy for beginners to use - * * Consumed with a public interface that's similiar to other Arduino standard libraries - * * Built against the standard SPI library. + * @li Maximally compliant with the intended operation of the chip + * @li Easy for beginners to use + * @li Consumed with a public interface that's similiar to other Arduino standard libraries + * @li Built against the standard SPI library. */ class RF24 @@ -91,9 +95,10 @@ protected: * The size of data written is the fixed payload size, see getPayloadSize() * * @param buf Where to get the data + * @param len Number of bytes to be sent * @return Current value of status register */ - uint8_t write_payload(const void* buf); + uint8_t write_payload(const void* buf, uint8_t len); /** * Read the receive payload @@ -101,9 +106,10 @@ protected: * The size of data read is the fixed payload size, see getPayloadSize() * * @param buf Where to put the data + * @param len Maximum number of bytes to read * @return Current value of status register */ - uint8_t read_payload(void* buf) ; + uint8_t read_payload(void* buf, uint8_t len) ; /** * Empty the receive buffer @@ -222,12 +228,15 @@ public: * the receiver or the timeout/retransmit maxima are reached. In * the current configuration, the max delay here is 60ms. * - * The size of data written is the fixed payload size, see getPayloadSize() + * The maximum size of data written is the fixed payload size, see + * getPayloadSize(). However, you can write less, and the remainder + * will just be filled with zeroes. * * @param buf Pointer to the data to be sent + * @param len Number of bytes to be sent * @return True if the payload was delivered successfully false if not */ - boolean write( const void* buf ); + boolean write( const void* buf, uint8_t len ); /** * Test whether there are bytes available to be read @@ -249,9 +258,10 @@ public: * for beginners to use. No casting needed. * * @param buf Pointer to a buffer where the data should be written + * @param len Maximum number of bytes to read into the buffer * @return True if the payload was delivered successfully false if not */ - boolean read( void* buf ) ; + boolean read( void* buf, uint8_t len ) ; /** * Open a pipe for writing @@ -296,6 +306,21 @@ public: */ void openReadingPipe(uint8_t number, uint64_t address); -}; +}; + +/** + * @example pingpair.pde + * + * This is an example of how to use the RF24 class. Write this sketch to two different nodes, + * connect the role_pin to ground on one. The ping node sends the current time to the pong node, + * which responds by sending the value back. + */ + +/** + * @mainpage Driver Library for nRF24L01 + * + * See the RF24 class for details on how to drive this chip. + */ + #endif // __RF24_H__ diff --git a/examples/pingpair/.gitignore b/examples/pingpair/.gitignore new file mode 100644 index 0000000..ea1472e --- /dev/null +++ b/examples/pingpair/.gitignore @@ -0,0 +1 @@ +output/ diff --git a/examples/pingpair/makefile b/examples/pingpair/makefile new file mode 100644 index 0000000..52db900 --- /dev/null +++ b/examples/pingpair/makefile @@ -0,0 +1,277 @@ +# Arduino Makefile +# Arduino adaptation by mellis, eighthave, oli.keller +# Modified by Kerry Wong to support NetBeans +# Modified by Rob Gray (Graynomad) for use with Windows and no IDE +# This works in my environment and I use it to program two different +# 328-based boards and a Mega2560. It's not necessarily robust and +# I may have broken something in the original file that I don't use. +# +# This makefile allows you to build sketches from the command line +# without the Arduino environment. +# +# Instructions for using the makefile: +# +# 1. Copy this file into the folder with your sketch. The project code file +# should have a .c extension however the file gets copied to a .cpp before +# compilation so you still write in C++. +# +# 2. Modify the lines between the double ### rows to set the paths +# comm ports etc for your system. EG. c:/progra~1/arduino/arduino-00 +# for the Arduino IDE, Note the use of short folder name, don't use +# "Program files" because spaces will break the build. +# +# Set the line containing "MCU" to match your board's processor. +# Typically ATmega328 or ATmega2560. If you're using a LilyPad Arduino, +# change F_CPU to 8000000. +# +# 3. At the command line, change to the directory containing your +# program's file and the makefile. +# +# 4. Type "make" and press enter to compile/verify your program. +# The default make target will also perform the uplode using avrdude. +# +# The first time this is done all required libraries will be built +# and a core.a file will be created in the output folder. +# +# NOTES: +# All output goes into a folder called "output" underneath the working folder. +# The default all: target creates symbol (.sym) and expanded assembly +# (.lss) files and uploads the program. +# +# +########################################################## +########################################################## +# Select processor here +MCU = atmega328p +#MCU = atmega2560 + +ifeq ($(MCU),atmega2560) +UPLOAD_RATE = 115200 +AVRDUDE_PROTOCOL = stk500v2 +COM = 39 +endif + +ifeq ($(MCU),atmega328p) +UPLOAD_RATE = 57600 +AVRDUDE_PROTOCOL = stk500v1 +COM = 33 +endif + +ARDUINO_VERSION = 22 +PROJECT_NAME = $(notdir $(PWD)) +PROJECT_DIR = . +ARDUINO_DIR = /opt/arduino-00$(ARDUINO_VERSION) +ARDUINO_CORE = $(ARDUINO_DIR)/hardware/arduino/cores/arduino +ARDUINO_AVR = $(ARDUINO_DIR)/hardware/tools/avr/avr/include/avr +ARDUINO_LIB = $(ARDUINO_DIR)/libraries +AVR_TOOLS_PATH = /usr/bin +AVRDUDECONFIG_PATH = $(ARDUINO_DIR)/hardware/tools +PORT = /dev/ttyUSB0 +PORT2 = /dev/ttyUSB1 +F_CPU = 16000000 + +########################################################## +########################################################## + +# Note that if your program has dependencies other than those +# already listed below, you will need to add them accordingly. +C_MODULES = \ +$(ARDUINO_CORE)/wiring_pulse.c \ +$(ARDUINO_CORE)/wiring_analog.c \ +$(ARDUINO_CORE)/pins_arduino.c \ +$(ARDUINO_CORE)/wiring.c \ +$(ARDUINO_CORE)/wiring_digital.c \ +$(ARDUINO_CORE)/WInterrupts.c \ +$(ARDUINO_CORE)/wiring_shift.c \ + +CXX_MODULES = \ +$(ARDUINO_CORE)/Tone.cpp \ +$(ARDUINO_CORE)/main.cpp \ +$(ARDUINO_CORE)/WMath.cpp \ +$(ARDUINO_CORE)/Print.cpp \ +$(ARDUINO_CORE)/HardwareSerial.cpp \ +$(ARDUINO_LIB)/SPI/SPI.cpp \ +../../RF24.cpp + +CXX_APP = output/$(PROJECT_NAME).cpp +MODULES = $(C_MODULES) $(CXX_MODULES) +SRC = $(C_MODULES) +CXXSRC = $(CXX_MODULES) $(CXX_APP) +FORMAT = ihex + +# Name of this Makefile (used for "make depend"). +MAKEFILE = Makefile + +# Debugging format. +# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. +# AVR (extended) COFF requires stabs, plus an avr-objcopy run. +#DEBUG = stabs +DEBUG = + +OPT = s + +# Place -D or -U options here +CDEFS = -DF_CPU=$(F_CPU)L -DARDUINO=$(ARDUINO_VERSION) +CXXDEFS = -DF_CPU=$(F_CPU)L -DARDUINO=$(ARDUINO_VERSION) + +# Place -I options here +CINCS = -I$(ARDUINO_CORE) -I$(ARDUINO_LIB) -I$(PROJECT_DIR) -I$(ARDUINO_AVR) -I$(ARDUINO_LIB)/SPI -I../.. + +CXXINCS = -I$(ARDUINO_CORE) -I$(ARDUINO_LIB) + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +#CSTANDARD = -std=gnu99 +CDEBUG = -g$(DEBUG) +#CWARN = -Wall -Wstrict-prototypes +#CWARN = -Wall # show all warnings +CWARN = -w # suppress all warnings +CMAP = -Wl,-Map,output.map +####CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +CTUNING = -ffunction-sections -fdata-sections +CXXTUNING = -fno-exceptions -ffunction-sections -fdata-sections +#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) +MMCU = -mmcu=$(MCU) + +CFLAGS = $(CDEBUG) -O$(OPT) $(CMAP) $(CWARN) $(CTUNING) $(MMCU) $(CDEFS) $(CINCS) $(CSTANDARD) $(CEXTRA) +CXXFLAGS = $(CDEBUG) -O$(OPT) $(CWARN) $(CXXTUNING) $(CDEFS) $(CINCS) +#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs +LDFLAGS = -O$(OPT) -lm -Wl,--gc-sections +#LDFLAGS = -O$(OPT) -lm -Wl,-Map,output/$(PROJECT_NAME).map + +# Programming support using avrdude. Settings and variables. +AVRDUDE_PORT = $(PORT) +AVRDUDE_WRITE_FLASH = -U flash:w:output/$(PROJECT_NAME).hex:i + +AVRDUDE_FLAGS = -V -F -D -C $(AVRDUDECONFIG_PATH)/avrdude.conf \ +-p $(MCU) -c $(AVRDUDE_PROTOCOL) -b $(UPLOAD_RATE) + +# Program settings +CC = $(AVR_TOOLS_PATH)/avr-gcc +CXX = $(AVR_TOOLS_PATH)/avr-g++ +LD = $(AVR_TOOLS_PATH)/avr-gcc +OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy +OBJDUMP = $(AVR_TOOLS_PATH)/avr-objdump +AR = $(AVR_TOOLS_PATH)/avr-ar +SIZE = $(AVR_TOOLS_PATH)/avr-size +NM = $(AVR_TOOLS_PATH)/avr-nm +AVRDUDE = $(AVR_TOOLS_PATH)/avrdude +REMOVE = rm -f +MV = mv -f + +# Define all object files. +OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o) +OBJ_MODULES = $(C_MODULES:.c=.o) $(CXX_MODULES:.cpp=.o) + +# Define all listing files. +LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst) + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = $(CFLAGS) -mmcu=$(MCU) +ALL_CXXFLAGS = $(CXXFLAGS) -mmcu=$(MCU) +ALL_ASFLAGS = -x assembler-with-cpp $(ASFLAGS) -mmcu=$(MCU) +ALL_LDFLAGS = $(LDFLAGS) -mmcu=$(MCU) + +# Default target. +# This is th etarget that gets executed with a make command +# that has no parameters, ie "make". +all: applet_files build sym lss size upload + +build: elf hex + +output/$(PROJECT_NAME).cpp: $(PROJECT_NAME).pde + test -d output || mkdir output + echo "#include " > $@ + cat $< >> $@ + +elf: output/$(PROJECT_NAME).elf +hex: output/$(PROJECT_NAME).hex +eep: output/$(PROJECT_NAME).eep +lss: output/$(PROJECT_NAME).lss +#sym: output/$(PROJECT_NAME).sym + +# Upload HEX file to Arduino +upload: output/$(PROJECT_NAME).hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -P $(PORT) $(AVRDUDE_WRITE_FLASH) + $(AVRDUDE) $(AVRDUDE_FLAGS) -P $(PORT2) $(AVRDUDE_WRITE_FLASH) + +sym: + $(NM) -n -C --format=posix output/$(PROJECT_NAME).elf > output/$(PROJECT_NAME).sym + +# Display size of file. +size: + $(SIZE) output/$(PROJECT_NAME).elf + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT=$(OBJCOPY) --debugging \ +--change-section-address .data-0x800000 \ +--change-section-address .bss-0x800000 \ +--change-section-address .noinit-0x800000 \ +--change-section-address .eeprom-0x810000 + +coff: output/$(PROJECT_NAME).elf + $(COFFCONVERT) -O coff-avr output/$(PROJECT_NAME).elf $(PROJECT_NAME).cof + + extcoff: $(PROJECT_NAME).elf + $(COFFCONVERT) -O coff-ext-avr output/$(PROJECT_NAME).elf $(PROJECT_NAME).cof + +.SUFFIXES: .elf .hex .eep .lss .sym + +.elf.hex: + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +.elf.eep: + $(OBJCOPY) -O $(FORMAT) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --no-change-warnings \ + --change-section-lma .eeprom=0 $< $@ + +# Create extended listing file from ELF output file. +.elf.lss: + $(OBJDUMP) -h -S $< > $@ + +# Link: create ELF output file from library. +#output/$(PROJECT_NAME).elf: $(PROJECT_NAME).c output/core.a +output/$(PROJECT_NAME).elf: output/$(PROJECT_NAME).o output/core.a + $(LD) $(ALL_LDFLAGS) -o $@ output/$(PROJECT_NAME).o output/core.a + +output/core.a: $(OBJ_MODULES) + @for i in $(OBJ_MODULES); do echo $(AR) rcs output/core.a $$i; $(AR) rcs output/core.a $$i; done + +# Compile: create object files from C++ source files. +.cpp.o: + $(CXX) -c $(ALL_CXXFLAGS) $< -o $@ + +# Compile: create object files from C source files. +.c.o: + $(CC) -c $(ALL_CFLAGS) $< -o $@ + +# Compile: create assembler files from C source files. +.c.s: + $(CC) -S $(ALL_CFLAGS) $< -o $@ + +# Assemble: create object files from assembler source files. +.S.o: + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + +# Automatic dependencies +%.d: %.c + $(CC) -M $(ALL_CFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@ + +%.d: %.cpp + $(CXX) -M $(ALL_CXXFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@ + +# Target: clean project. +clean: + $(REMOVE) output/$(PROJECT_NAME).hex output/$(PROJECT_NAME).eep output/$(PROJECT_NAME).cof output/$(PROJECT_NAME).elf \ + output/$(PROJECT_NAME).map output/$(PROJECT_NAME).sym output/$(PROJECT_NAME).lss output/core.a \ + $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) + +#.PHONY: all build elf hex eep lss sym program coff extcoff clean applet_files sizebefore sizeafter +.PHONY: all build elf hex eep lss sym program coff extcoff applet_files sizebefore sizeafter + +#include $(SRC:.c=.d) +#include $(CXXSRC:.cpp=.d) diff --git a/examples/pingpair/pingpair.pde b/examples/pingpair/pingpair.pde index 46ff5b2..dc70270 100644 --- a/examples/pingpair/pingpair.pde +++ b/examples/pingpair/pingpair.pde @@ -29,13 +29,9 @@ RF24 radio(8,9); -// sets the address (and therefore the role of operation) of this unit. -// lo = node0, hi = node1 -const int addr_pin = 7; - -// The actual value of the node's address will be filled in by the sketch -// when it reads the addr_pin -int node_address; +// sets the role of this unit in hardware. Connect to GND to be the 'ping' sender. +// Connect to +5V to be the 'pong' receiver. +const int role_pin = 7; // // Topology @@ -47,21 +43,18 @@ const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL }; // // Role management // -// Set up address & role. This sketch uses the same software for all the nodes +// Set up role. This sketch uses the same software for all the nodes // in this system. Doing so greatly simplifies testing. The hardware itself specifies // which node it is. // -// This is done through the addr_pin. Set it low for address #0, high for #1. +// This is done through the role_pin // // The various roles supported by this sketch -typedef enum { role_rx = 1, role_tx1, role_end } role_e; +typedef enum { role_ping_out = 1, role_pong_back } role_e; // The debug-friendly names of those roles -const char* role_friendly_name[] = { "invalid", "Receive", "Transmit"}; - -// Which role is assumed by each of the possible hardware addresses -const role_e role_map[2] = { role_rx, role_tx1 }; +const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"}; // The role of the current running sketch role_e role; @@ -69,17 +62,19 @@ role_e role; void setup(void) { // - // Address & Role + // Role // - // set up the address pin - pinMode(addr_pin, INPUT); - digitalWrite(addr_pin,HIGH); - delay(20); // Just to get a solid reading on the addr 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 address and role - node_address = digitalRead(addr_pin) ? 0 : 1; - role = role_map[node_address]; + // read the address pin, establish our role + if ( digitalRead(role_pin) ) + role = role_pong_back; + else + role = role_ping_out; // // Print preamble @@ -87,8 +82,7 @@ void setup(void) Serial.begin(9600); printf_begin(); - printf("\n\rRF24 pingpair example\n\r"); - printf("ADDRESS: %x\n\r",node_address); + printf("\n\rRF24/examples/pingpair/\n\r"); printf("ROLE: %s\n\r",role_friendly_name[role]); // @@ -97,32 +91,26 @@ void setup(void) radio.begin(); - // Set channel (optional) - radio.setChannel(1); - - // Set size of payload (optional, but recommended) - // The library uses a fixed-size payload, so if you don't set one, it will pick - // one for you! - radio.setPayloadSize(sizeof(unsigned long)); - // - // Open pipes to other nodes for communication (required) + // Open pipes to other nodes for communication // // This simple sketch opens two pipes for these two nodes to communicate // back and forth. + // Open 'our' pipe for writing + // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading) - // We will open 'our' pipe for writing - radio.openWritingPipe(pipes[node_address]); - - // We open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading) - int other_node_address; - if (node_address == 0) - other_node_address = 1; + if ( role == role_ping_out ) + { + radio.openWritingPipe(pipes[0]); + radio.openReadingPipe(1,pipes[1]); + } else - other_node_address = 0; - radio.openReadingPipe(1,pipes[other_node_address]); - + { + radio.openWritingPipe(pipes[1]); + radio.openReadingPipe(1,pipes[0]); + } + // // Start listening // @@ -139,10 +127,10 @@ void setup(void) void loop(void) { // - // Transmitter role. Repeatedly send the current time + // Ping out role. Repeatedly send the current time // - if (role == role_tx1) + if (role == role_ping_out) { // First, stop listening so we can talk. radio.stopListening(); @@ -150,7 +138,7 @@ void loop(void) // Take the time, and send it. This will block until complete unsigned long time = millis(); printf("Now sending %lu...",time); - bool ok = radio.write( &time ); + bool ok = radio.write( &time, sizeof(unsigned long) ); // Now, continue listening radio.startListening(); @@ -171,7 +159,7 @@ void loop(void) { // Grab the response, compare, and send to debugging spew unsigned long got_time; - radio.read( &got_time ); + radio.read( &got_time, sizeof(unsigned long) ); // Spew it printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time); @@ -182,10 +170,10 @@ void loop(void) } // - // Receiver role. Receive each packet, dump it out, and send it back to the transmitter + // Pong back role. Receive each packet, dump it out, and send it back // - if ( role == role_rx ) + if ( role == role_pong_back ) { // if there is data ready if ( radio.available() ) @@ -196,7 +184,7 @@ void loop(void) while (!done) { // Fetch the payload, and see if this was the last one. - done = radio.read( &got_time ); + done = radio.read( &got_time, sizeof(unsigned long) ); // Spew it printf("Got payload %lu...",got_time); @@ -206,7 +194,7 @@ void loop(void) radio.stopListening(); // Send the final one back. - radio.write( &got_time ); + radio.write( &got_time, sizeof(unsigned long) ); printf("Sent response.\n\r"); // Now, resume listening so we catch the next packets. @@ -214,3 +202,4 @@ void loop(void) } } } +// vim:ci sts=2 sw=2 ft=cpp