Skip to content

Commit

Permalink
add reversePayloads option for better Arduino/RF24 compatibility (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
natevw committed Mar 1, 2015
1 parent 81f902f commit 3b24f10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ The nRF24 radios use "logical channels" for communications within a physical cha

* Finally, via the `opts` parameter you can set a fixed payload `size` (in bytes, defaults to `'auto'`) or disable auto-acknowlegement with `autoAck:false` (defaults to `true`). Note that if you want to disable auto-acknowlegment, you *must* also set a payload size — for some reason these are linked in the nRF24 feature set.

* One other useful `opts` key is `reversePayloads`: the [RF24 library](https://github.com/maniacbug/RF24) commonly used on Arduino devices does not follow the datasheet's byte order for payload values. So normally, when talking between node-nrf and RF24 devices, both ends will see the reversed byte order. You can have node-nrf automatically correct this, but note it does add another layer of additional overhead. (If the data you write is subsequently unused, you can pass `{reversePayloads:'leave'}` to avoid some part of this overhead when sending — it will leave your buffer reversed.)

* For `'tx'` pipes, the `opts` parameter also lets you provide individual `retryCount`, `retryDelay`, `txPower` options instead of using the `radio.autoRetransmit` and `radio.transmitPower` methods; if you do this you should provide values for *every* `'tx`' pipe you open, to make sure the hardware configuration gets updated between each different transmission. [**TBD**: what is `ackPayloads` option supposed to do for `'tx`` pipes?]

Note that, while you can `.pipe()` to these streams as any other, `node-nrf` will not split data into packets for you, and will get upset if passed more than 32 bytes of data! Make sure all your `write`s to the stream fit the necessary MTU; **TBD** I imagine the common "transfer an arbitrarily large stream of data" case could be handled by a simple [object mode?] transform stream, find or provide a recommended module.
Expand Down
11 changes: 10 additions & 1 deletion xcvr_pipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ PxX.prototype._tx = function (data, cb, _n) { cb = this._SERIAL_(cb, function ()
}
}
var self = this;
if (self._opts.reversePayloads) {
Array.prototype.reverse.call(data);
}
self._xcvr.setStates(s, function (e) { // (± fine to call with no keys)
if (e) return cb(e);
var sendOpts = _extend({}, self._sendOpts);
Expand All @@ -102,6 +105,9 @@ PxX.prototype._tx = function (data, cb, _n) { cb = this._SERIAL_(cb, function ()
}
self._xcvr.setStates(s, cb);
});
if (self._opts.reversePayloads && self._opts.reversePayloads !== 'leave') {
Array.prototype.reverse.call(data); // put back data the way caller had it
}
if (!rxPipes.length) self._xcvr._prevSender = self; // we might avoid setting state next time
});
}, (_n === this._NESTED_)); };
Expand All @@ -113,7 +119,10 @@ PxX.prototype._rx = function (d) {
var self = this;
self._xcvr.readPayload({width:self._size}, function (e,d) {
if (e) self.emit('error', e);
else self._wantsRead = self.push(d);
else {
if (self._opts.reversePayloads) Array.prototype.reverse.call(d);
self._wantsRead = self.push(d);
}
self._xcvr._checkStatus(false); // see footnote c, p.63
});
};
Expand Down

0 comments on commit 3b24f10

Please sign in to comment.