#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>
using namespace std;
//
// Hardware configuration
//
// CE Pin, CSN Pin, SPI Speed
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 4Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS1,
BCM2835_SPI_SPEED_4MHZ);
// 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);
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
//RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0,
BCM2835_SPI_SPEED_8MHZ);
// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t pipes[][6] = {"1Node", "2Node"};
#define RETRY 10
#define DEBUG 0
int RadioAvailable() {
int i;
for (i=0;i<RETRY;i++) {
if (radio.available()) return 1;
if(DEBUG)printf("Waiting..\n");
sleep(1);
}
return 0;
}
int main(int argc, char** argv){
// Setup and configure rf radio
radio.begin();
radio.openWritingPipe(pipes[1]);
// 応答を待つときはPipeの1番目から使う
radio.openReadingPipe(1,pipes[0]);
radio.printDetails();
//radio.startListening();
unsigned long sdata = 0;
unsigned long rdata;
// forever loop
while(1) {
// First, stop listening so we can
talk.
radio.stopListening();
int ret=radio.write( &sdata,
sizeof(sdata) );
printf("radio.write ret=%d
sdata=%lu\n",ret,sdata);
if (ret) {
// Now, continue listening
radio.startListening();
while(1) {
if (
RadioAvailable() ) break;
}
radio.read( &rdata,
sizeof(rdata) );
printf("rdata=%lu\n",rdata);
sdata++;
}
sleep(1);
} // end for
}
|