Skip to content

Instantly share code, notes, and snippets.

@gonAlonso
Last active August 29, 2015 14:25
Show Gist options
  • Save gonAlonso/42fa206ba5e7bc95855e to your computer and use it in GitHub Desktop.
Save gonAlonso/42fa206ba5e7bc95855e to your computer and use it in GitHub Desktop.
// see https://gist.github.com/natevw/5789019 for pins
var NRF24 = require("./index"),
spiDev = "/dev/spidev0.0",
cePin = 24, irqPin = 25, //var ce = require("./gpio").connect(cePin)
pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2],
role = 'pong'; // ping or pong
var stream = require('stream'),
util = require('util');
function CountStream(ms) {
stream.Readable.call(this);
this._n = 0;
}
util.inherits(CountStream, stream.Readable);
CountStream.prototype._read = function () {
console.log("Piping out", this._n);
var b = new Buffer(4);
b.writeUInt32BE(this._n++, 0);
this.push(b);
};
var nrf = NRF24.connect(spiDev, cePin); //, irqPin);
nrf.printDetails();
//nrf._debug = true;
nrf.channel(0x4c).transmitPower('PA_MAX').dataRate('250kbps').crcBytes(2).autoRetransmit({count:15, delay:4000}).begin(function () {
if (role === 'ping') {
console.log("PING out");
var tx = nrf.openPipe('tx', pipes[0], {autoAck:true}),
rx = nrf.openPipe('rx', pipes[1]), {autoack:true};
var count = 0;
rx.on('data', function (d) {
console.log("Got response back:", d.readUInt32BE(0));
});
/*
tx.on('ready', function () { // NOTE: hoping to get rid of need to wait for "ready"
(new CountStream).pipe(tx);
});
*/
tx.on('ready', function(){
console.log("Startig ping\n");
setInterval( function(){
tx.write(( "Hello" ).split("").reverse().join("") );
console.log( "New ping" );
}, 2000);
} else {
console.log("PONG back");
var rx = nrf.openPipe('rx', pipes[0], {autoAck:true}),
tx = nrf.openPipe('tx', pipes[1], {autoAck:true});
rx.on('data', function (d) {
//console.log("Got data, will respond", d.readUInt32BE(0));
console.log("Got data, will respond", d.toString('hex'));
tx.write(d);
});
tx.on('error', function (e) {
console.warn("Error sending reply.", e);
});
}
});
@gonAlonso
Copy link
Author

// Some changes to avoid the use of buffers. I'm not used to it so far and:
Line 31, 32, 51, 52 added autoAck:true
Lines42 to 47 added, changing previous on('ready') function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment