diff --git a/examples_linux/BBB/Makefile b/examples_linux/BBB/Makefile new file mode 100644 index 000000000..cf4d8ff6e --- /dev/null +++ b/examples_linux/BBB/Makefile @@ -0,0 +1,23 @@ +############################################################################# +# +# Makefile for librf24 examples on Linux +# +# License: GPL (General Public License) +# Author: gnulnulf +# 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 = rf24_bbb + +include ../Makefile.examples diff --git a/examples_linux/BBB/README.md b/examples_linux/BBB/README.md new file mode 100644 index 000000000..e60cd6919 --- /dev/null +++ b/examples_linux/BBB/README.md @@ -0,0 +1,69 @@ +rf24_bbb is a program for BBB Node.JS developer. + +### Usage: + + Usage: rf24_bbb [-w target] [[-c channel] -r read_addr] ... + -D show module detail + -h show this help + -w target set target address + -r read addr set read address + -c channel set read channel + -R[timeout] read string + -S string send string + +## Node.JS Example + +### Send + + var exec = require('child_process').exec; + // rf24_bbb binary file + var _nrflib = __dirname + "/rf24_bbb"; + // set RF24 writing pipe address to 2NODE and reading pipe 1NODE. + var nrf = exec(_nrflib + " " + "-w 2Node -r 1Node -D"); + + // Send string "Hello" per second + setInterval(function(){ + var x = exec(_nrflib + " " + "-S Hello"); + x.stdout.on('data', function (data) + { + console.log(data); + }); + }, 1000); + + nrf.stdout.on('data', function (data) + { + console.log(data); + }); + + nrf.stderr.on('data', function (data) + { + console.log(data); + }); + +### Receive + + var exec = require('child_process').exec; + // rf24_bbb binary file + var _nrflib = __dirname + "/rf24_bbb"; + // set RF24 writing pipe address to 1NODE and reading pipe 2NODE. + var nrf = exec(_nrflib + " " + "-w 1Node -r 2Node -D"); + + // Start listening and set timeout 1000 ms (default: 200 ms) + var x = exec(_nrflib + " " + "-R1000"); + x.stdout.on('data', function (data) + { + var d = JSON.parse(data); + console.log(d); + }); + + nrf.stdout.on('data', function (data) + { + console.log(data); + }); + + nrf.stderr.on('data', function (data) + { + console.log(data); + }); + +See http://tmrh20.github.io/RF24 for more information \ No newline at end of file diff --git a/examples_linux/BBB/rf24_bbb.cpp b/examples_linux/BBB/rf24_bbb.cpp new file mode 100644 index 000000000..433380815 --- /dev/null +++ b/examples_linux/BBB/rf24_bbb.cpp @@ -0,0 +1,194 @@ +/** + * + * RF24 for BBB + * + * This program provide a way to help BBB developer easy to use RF24 library. + * + */ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +/****************** 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); +// CE pin = P9_16 +RF24 radio(51,0); + +/********** User Config *********/ +// Assign a unique identifier for this node, 0 or 1 +bool radioNumber = 1; +const int min_payload_size = 4; +const int max_payload_size = 32; +char receive_payload[max_payload_size+1]; +extern char *optarg; +/********************************/ +int channel = 1; + +void setWritingPipe(char *Addr) +{ + radio.openWritingPipe((uint8_t*) Addr); + cout << "WRITE: " << Addr << endl; + cout.flush(); +} + +void setReadingPipe(char *Addr, int pipe) +{ + radio.openReadingPipe(pipe, (uint8_t*) Addr); + cout << "READ: " << Addr << endl; + cout.flush(); +} + +void sendData(char *data) +{ + int length = strlen(data); + bool timeout = false; + radio.stopListening(); + + bool isSent = radio.write( data, length ); + + if(!isSent) + { + printf("{\"status\":\"failed\"}"); + return; + } + + radio.startListening(); + unsigned long started_time = millis(); + + while (!radio.available() && !timeout) + { + if (millis() - started_time > 200 ) + { + timeout = true; + } + } + + if ( timeout ) + { + printf("{\"status\":\"timeout\"}"); + } else { + printf("{\"status\":\"success\"}"); + } + fflush(stdout); +} + +void readData(char *setTimeout) +{ + // Dump the payloads until we've gotten everything + unsigned int len, waiting_time; + bool timeout = false; + + if ( setTimeout > 0) + waiting_time = atoi(setTimeout); + else + waiting_time = 200; + + radio.startListening(); + unsigned long started_time = millis(); + + + while (!radio.available() && !timeout) + { + if (millis() - started_time > waiting_time ) + { + timeout = true; + } + } + + if( timeout ) + { + printf("{\"status\":\"timeout\"}"); + } + else + { + // 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; + + printf("{\"status\":\"received\", \"length\":\"%i\", \"value\":\"%s\"}",len,receive_payload); + fflush(stdout); + + radio.stopListening(); + + radio.write( "R", 1 ); + + // Now, resume listening so we catch the next packets. + radio.startListening(); + } +} + +void showHelp() +{ + cout << "Usage: r24lib [-w target] [[-c channel] -r read_addr] [-options...]" << endl; + cout << "-D\tshow module detail" << endl; + cout << "-h\tshow this help" << endl; + cout << "-w target\tset target address" << endl; + cout << "-r read addr\tset read address" << endl; + cout << "-c channel\tset read channel" << endl; + cout << "-R[timeout]\tread string" << endl; + cout << "-S string\tsend string" << endl; + cout.flush(); +} + + +int main(int argc, char** argv) +{ + + // Setup and configure rf radio + radio.begin(); + radio.enableDynamicPayloads(); + // optionally, increase the delay between retries & # of retries + radio.setRetries(15,15); + + int c; + while((c = getopt( argc, argv, "Dhw:r:S:c:R::"))!=-1) + { + switch(c) + { + case 'w': setWritingPipe(optarg); + break; + case 'r': setReadingPipe(optarg, channel); + break; + case 'S': sendData(optarg); + break; + case 'D': radio.printDetails(); + break; + case 'R': readData(optarg); + break; + case 'h': showHelp(); + break; + case 'c': channel = 1; + break; + case '?': fprintf(stderr, "Illegal option:-%c\n", isprint(optopt)?optopt:'#'); + showHelp(); + break; + default: showHelp(); + break; + } + } + + fflush(stdout); + radio.startListening(); + + return 0; +} \ No newline at end of file