Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyFlem committed Jan 4, 2019
1 parent b7bb259 commit 4646f6d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@ module.exports = {

"env": {
"es6": true
},
"rules": {
"node/exports-style": ["error", "module.exports"],
"node/prefer-global/buffer": ["error", "always"],
"node/prefer-global/console": ["error", "always"],
"node/prefer-global/process": ["error", "always"],
"node/prefer-global/url-search-params": ["error", "always"],
"node/prefer-global/url": ["error", "always"]
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rfm69_node",
"name": "rfm69radio",
"version": "1.0.0",
"description": "",
"description": "A Node module for sending and receiving through RFM69 radios on the Raspberry Pi.",
"main": "index.js",
"repository": {"type": "git","url": "https://github.com/AndyFlem/rfm69.git"},
"scripts": {
Expand Down
57 changes: 36 additions & 21 deletions rfm69.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict';
const spi = require('spi-device');
const Gpio = require('onoff').Gpio;
Expand Down Expand Up @@ -167,26 +168,40 @@ RFM69.prototype.send = function({ toAddress = 0, payload = '', attempts = 3, att

let attempt = 0;

setTimeout(function tick() {
attempt += 1;
debug(`Send attempt: ${attempt} of ${attempts}`);
scope._sendFrame(toAddress, payload, requireAck, function() {
setTimeout(function() {
if (attempt < attempts && packet.hasAck == false) {
debug(`No Ack received for our packet, retry.`);
setTimeout(tick, attemptWait);
} else if (packet.hasAck == true) {
debug(`Ack received for our packet to address ${toAddress} on send attempt ${attempt}.`);
peer.sending=false;
ackCallback(null, attempt);
} else if (attempt == attempts) {
debug(`No Ack received. Giving up.`);
peer.sending=false;
ackCallback(new Error(`No Ack received for our packet to address ${toAddress} after ${attempt} attempts.`), attempt);
}
}, 1000);
});
}, 20);
if (requireAck){
setTimeout(function tick() {
attempt += 1;
debug(`Send attempt: ${attempt} of ${attempts}`);
scope._sendFrame(toAddress, payload, requireAck, function() {
setTimeout(function() {
if (attempt < attempts && packet.hasAck == false) {
debug(`No Ack received for our packet, retry.`);
setTimeout(tick, attemptWait);
} else if (packet.hasAck == true) {
debug(`Ack received for our packet to address ${toAddress} on send attempt ${attempt}.`);
peer.sending=false;
if (typeof ackCallback==='function') {ackCallback(null, attempt);}
} else if (attempt == attempts) {
debug(`No Ack received. Giving up.`);
peer.sending=false;
if (typeof ackCallback==='function') {ackCallback(new Error(`No Ack received for our packet to address ${toAddress} after ${attempt} attempts.`), attempt);}
}
}, 1000);
});
}, 20);
}else {
scope._sendFrame(toAddress, payload, false);
peer.sending=false;
if (typeof ackCallback==='function') {ackCallback(null, 1);}
}
};

//Broadcast a message to network i.e. sends to node 255 with no ACK request.
RFM69.prototype.broadcast = function(payload = '', callback){
debug(`********* Broadcast: ${payload}`);
this.send({toAddress: 255,payload: payload, attempts: 1, requireAck: false,ackCallback: function(){
if (typeof callback==='function') {callback();}
}});
};

//Calibrate the internal RC oscillator for use in wide temperature variations.
Expand Down Expand Up @@ -232,7 +247,7 @@ RFM69.prototype._sendFrame = function(toAddress, payload, requestAck, callback)
scope._setMode(reg.RF69_MODE_TX);
debug(`Sent: ${payload}`);

callback();
if (typeof callback==='function') {callback();}
});
};

Expand Down
15 changes: 11 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const rfm69 = new RFM69();
rfm69.initialize({
address: 5,
encryptionKey: '0123456789abcdef',
verbose:true,
verbose:false,
initializedCallback: initializedCallback,
packetReceivedCallback: packetReceivedCallback,
});
});

function initializedCallback() {
console.log('Initialized');
Expand All @@ -23,7 +23,7 @@ function initializedCallback() {
const toAddress=2;
console.log(`Sending packet to address ${toAddress}`);
rfm69.send({
toAddress: toAddress, payload: 'hello', ackCallback: function(err, res) {
toAddress: toAddress, payload: 'hello', attempts: 1, requireAck: false, ackCallback: function(err, res) {
if (err){
console.log(err)
}else
Expand All @@ -35,6 +35,13 @@ function initializedCallback() {
}, 1000);


setTimeout(
function() {rfm69.broadcast('Broadcast!!',function(){
console.log("Sent broadcast")
});}
,2000
);
/*
setInterval(function() {
const toAddress=2;
console.log(`Sending packet to address ${toAddress}`);
Expand All @@ -49,7 +56,7 @@ function initializedCallback() {
},
});
}, 4000);

*/
}

function packetReceivedCallback(packet) {
Expand Down

0 comments on commit 4646f6d

Please sign in to comment.