Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tessel & RPi compat #7

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ In contrast, *this* module is implemented in pure JavaScript on top of native [S

[Streams](https://github.com/substack/stream-handbook#readme)!

```
var radio = require('nrf').connect(spiDev, cePin, irqPin);
radio.channel(0x4c).dataRate('1Mbps').crcBytes(2).autoRetransmit({count:15, delay:4000});
radio.begin(function () {
```js
var radio = require('nrf')
.channel(0x4c).dataRate('1Mbps')
.crcBytes(2).autoRetransmit({count:15, delay:4000})
.use(spiDev, cePin, irqPin);

radio.on('ready', function () {
var rx = radio.openPipe('rx', 0xF0F0F0F0E1),
tx = radio.openPipe('tx', 0xF0F0F0F0D2);
rx.pipe(tx); // echo back everything
Expand Down
67 changes: 67 additions & 0 deletions examples/RF24-pingpair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* These are settings for Tessel to work out of the box with
* maniacbug's RF24 pingpair example (https://github.com/maniacbug/RF24/blob/07a4bcf425d91c99105dbdbad0226296c7cd3a93/examples/pingpair/pingpair.pde)
* Useful for bridging an Arduino + nRF24 to Tessel + nRF24
*/

var tessel = require('tessel'),
NRF24 = require("../"),
pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2],
role = 'ping'; // swap this to pong if you want to wait for receive

var nrf = NRF24.channel(0x4c) // set the RF channel to 76. Frequency = 2400 + RF_CH [MHz] = 2476MHz
.transmitPower('PA_MAX') // set the transmit power to max
.dataRate('1Mbps')
.crcBytes(2) // 2 byte CRC
.autoRetransmit({count:15, delay:4000})
.use(tessel.port['A']);

nrf._debug = false;

nrf.on('ready', function () {
if (role === 'ping') {
console.log("PING out");
/*
* The Arduino pong code needs to have its timeout changed. On line #205
* https://github.com/maniacbug/RF24/blob/07a4bcf425d91c99105dbdbad0226296c7cd3a93/examples/pingpair/pingpair.pde#L205
* the delay(20) needs to be swapped out with delay(2000)
*/

var tx = nrf.openPipe('tx', pipes[1]), // transmit address F0F0F0F0D2
rx = nrf.openPipe('rx', pipes[1], {size: 8}); // receive address F0F0F0F0D2
tx.on('ready', function () { // NOTE: hoping to get rid of need to wait for "ready"
var n = 0;
setInterval(function () {
var b = new Buffer(8); // set buff len of 8 for compat with maniac bug's RF24 lib
b.fill(0);
b.writeUInt32BE(n++, 4); // offset by 4 because our buffer length is 8 bytes
console.log("Sending", n);
tx.write(b);
}, 5e3); // transmit every 5 seconds
});
rx.on('data', function (d) {
console.log("Got response back:", d.readUInt32BE(4)); //offset by 4 again
});
} else {
console.log("PONG back");
/*
* The Arduino ping code needs to have its timeout changed. On line #161
* https://github.com/maniacbug/RF24/blob/07a4bcf425d91c99105dbdbad0226296c7cd3a93/examples/pingpair/pingpair.pde#L161
* instead of "if (millis() - started_waiting_at > 200 )"
* change to "if (millis() - started_waiting_at > 2000 )"
*/

var rx = nrf.openPipe('rx', pipes[0], {size: 8});
tx = nrf.openPipe('tx', pipes[0], {autoAck: false});
rx.on('data', function (d) {
console.log("Got data, will respond", d);
tx.write(d);
});
tx.on('error', function (e) {
console.warn("Error sending reply.", e);
});
}
});

// hold this process open
process.ref();
68 changes: 68 additions & 0 deletions examples/nrf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

/*********************************************
This nRF24 example requires two nRF24 modules
(and ideally two Tessels). Put one Tessel +
nRF24 module on "ping" mode and the other
pair on "pong" mode to make them send
information back and forth.
*********************************************/

var tessel = require('tessel');
var NRF24 = require('../'); // Replace '../' with 'rf-nrf24' in your own code
var pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2];

var role = 'ping'; // 'ping' to send; 'pong' to receive

// Set up NRF
var nrf = NRF24.channel(0x4c) // set the RF channel to 76. Frequency = 2400 + RF_CH [MHz] = 2476MHz
.transmitPower('PA_MAX') // set the transmit power to max
.dataRate('1Mbps')
.crcBytes(2) // 2 byte CRC
.autoRetransmit({count:15, delay:4000})
.use(tessel.port['A']);

nrf._debug = false;

// Wait for the module to connect
nrf.on('ready', function () {
setTimeout(function(){
nrf.printDetails();
}, 5000);

if (role === 'ping') {
console.log('PING out');
// If set to 'ping' mode, send data
var tx = nrf.openPipe('tx', pipes[0], {autoAck: false}), // transmit address F0F0F0F0D2
rx = nrf.openPipe('rx', pipes[1], {size: 4}); // receive address F0F0F0F0D2
tx.on('ready', function () {
var n = 0;
setInterval(function () {
var buff = new Buffer(4); // set buff len of 8 for compat with maniac bug's RF24 lib
buff.fill(0);
buff.writeUInt32BE(n++);
console.log("Sending", n);
tx.write(buff);
}, 5e3); // transmit every 5 seconds
});
rx.on('data', function (data) {
console.log("Got response back:", data);
});
} else {
console.log("PONG back");
// If set to 'pong' mode, receive data
var rx = nrf.openPipe('rx', pipes[0], {size: 4});
tx = nrf.openPipe('tx', pipes[1], {autoAck: false});
rx.on('data', function (data) {
console.log("Got data, will respond", data);
tx.write(data);
});
tx.on('error', function (err) {
console.warn("Error sending reply.", err);
});
}
});

// hold this process open
process.ref();
58 changes: 58 additions & 0 deletions examples/nrf24.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* tessel to tessel
* requires 2 nrf24 modules (and ideally two tessels)
* put one tessel+nrf on "ping" mode and another one on "pong" mode
*/

var tessel = require('tessel'),
NRF24 = require("../"),
pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2],
role = 'ping'; // swap this to pong if you want to wait for receive

var nrf = NRF24.channel(0x4c) // set the RF channel to 76. Frequency = 2400 + RF_CH [MHz] = 2476MHz
.transmitPower('PA_MAX') // set the transmit power to max
.dataRate('1Mbps')
.crcBytes(2) // 2 byte CRC
.autoRetransmit({count:15, delay:4000})
.use(tessel.port['A']);

nrf._debug = false;

nrf.on('ready', function () {
setTimeout(function(){
nrf.printDetails();
}, 5000);

if (role === 'ping') {
console.log("PING out");

var tx = nrf.openPipe('tx', pipes[0], {autoAck: false}), // transmit address F0F0F0F0D2
rx = nrf.openPipe('rx', pipes[1], {size: 4}); // receive address F0F0F0F0D2
tx.on('ready', function () {
var n = 0;
setInterval(function () {
var b = new Buffer(4); // set buff len of 8 for compat with maniac bug's RF24 lib
b.fill(0);
b.writeUInt32BE(n++);
console.log("Sending", n);
tx.write(b);
}, 5e3); // transmit every 5 seconds
});
rx.on('data', function (d) {
console.log("Got response back:", d);
});
} else {
console.log("PONG back");
var rx = nrf.openPipe('rx', pipes[0], {size: 4});
tx = nrf.openPipe('tx', pipes[1], {autoAck: false});
rx.on('data', function (d) {
console.log("Got data, will respond", d);
tx.write(d);
});
tx.on('error', function (e) {
console.warn("Error sending reply.", e);
});
}
});

// hold this process open
process.ref();
Loading