From 997da83a95fa0210572639ec6ca275f84bac7ea8 Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Wed, 24 Feb 2016 02:14:48 -0500 Subject: [PATCH 01/12] ceHigh --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c71d052..6159b24 100644 --- a/index.js +++ b/index.js @@ -558,7 +558,7 @@ exports.connect = function (spi,ce,irq) { nrf.setStates(s, function (e) { // (± fine to call with no keys) if (e) return cb(e); var sendOpts = _extend({},this._sendOpts); - //if (rxPipes.length) sendOpts.ceHigh = true; // PRX will already have CE high + if (rxPipes.length) sendOpts.ceHigh = true; // PRX will already have CE high nrf.sendPayload(data, sendOpts, function (e) { if (e) return cb(e); var s = {}; // NOTE: if another TX is waiting, switching to RX is a waste… @@ -680,4 +680,4 @@ exports.connect = function (spi,ce,irq) { nrf.on('interrupt', function (d) { if (nrf._debug) console.log("IRQ.", d); }); return nrf; -} \ No newline at end of file +} From 424a91eb61844056e2f9c5d4f7c103883fe6c749 Mon Sep 17 00:00:00 2001 From: wuyuanyi Date: Wed, 24 Feb 2016 08:00:48 +0000 Subject: [PATCH 02/12] do not register listener for ack payload write --- index.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 6159b24..b9647d4 100644 --- a/index.js +++ b/index.js @@ -335,21 +335,24 @@ exports.connect = function (spi,ce,irq) { } nrf.execCommand(cmd, data, function (e) { if (e) return cb(e); - if (!opts.ceHigh) nrf.pulseCE('pece2csn'); - // TODO: if _sendOpts.asAckTo we won't get MAX_RT interrupt — how to prevent a blocked TX FIFO? (see p.33) - nrf.once('interrupt', function (d) { - if (d.MAX_RT) nrf.execCommand('FLUSH_TX', function (e) { // see p.56 - finish(new Error("Packet timeout, transmit queue flushed.")); - }); - else if (!d.TX_DS) console.warn("Unexpected IRQ during transmit phase!"); - else finish(); - - function finish(e) { // clear our interrupts, leaving RX_DR - nrf.setStates({TX_DS:true,MAX_RT:true,RX_DR:false}, function () { - cb(e||null); + if (!opts.ceHigh) { + nrf.pulseCE('pece2csn'); + // TODO: if _sendOpts.asAckTo we won't get MAX_RT interrupt — how to prevent a blocked TX FIFO? (see p.33) + // Do not register the listener when writing to ack payload. No TX_DS in next frame. + nrf.once('interrupt', function (d) { + if (d.MAX_RT) nrf.execCommand('FLUSH_TX', function (e) { // see p.56 + finish(new Error("Packet timeout, transmit queue flushed.")); }); - } - }); + else if (!d.TX_DS) console.warn("Unexpected IRQ during transmit phase!"); + else finish(); + + function finish(e) { // clear our interrupts, leaving RX_DR + nrf.setStates({TX_DS:true,MAX_RT:true,RX_DR:false}, function () { + cb(e||null); + }); + } + }); + } }); }; From bf1662e8b1f5c304f1072641f4f7f0232d2f0583 Mon Sep 17 00:00:00 2001 From: wuyuanyi Date: Wed, 24 Feb 2016 08:49:14 +0000 Subject: [PATCH 03/12] fix ack payload handler --- index.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index b9647d4..401be55 100644 --- a/index.js +++ b/index.js @@ -339,21 +339,35 @@ exports.connect = function (spi,ce,irq) { nrf.pulseCE('pece2csn'); // TODO: if _sendOpts.asAckTo we won't get MAX_RT interrupt — how to prevent a blocked TX FIFO? (see p.33) // Do not register the listener when writing to ack payload. No TX_DS in next frame. + nrf.once('interrupt', irqHandler); + + + } else { + // Incoming RX_DS then (TX_DS or MAX_RT) nrf.once('interrupt', function (d) { - if (d.MAX_RT) nrf.execCommand('FLUSH_TX', function (e) { // see p.56 - finish(new Error("Packet timeout, transmit queue flushed.")); - }); - else if (!d.TX_DS) console.warn("Unexpected IRQ during transmit phase!"); - else finish(); - - function finish(e) { // clear our interrupts, leaving RX_DR - nrf.setStates({TX_DS:true,MAX_RT:true,RX_DR:false}, function () { - cb(e||null); - }); + if(d.RX_DR) { + //desired + nrf.once('interrupt', irqHandler); + } else { + console.warn("Unexpected IRQ during transmit phase!"); } - }); + }); } }); + + function irqHandler(d) { + if (d.MAX_RT) nrf.execCommand('FLUSH_TX', function (e) { // see p.56 + finish(new Error("Packet timeout, transmit queue flushed.")); + }); + else if (!d.TX_DS) console.warn("Unexpected IRQ during transmit phase!"); + else finish(); + + function finish(e) { // clear our interrupts, leaving RX_DR + nrf.setStates({TX_DS:true,MAX_RT:true,RX_DR:false}, function () { + cb(e||null); + }); + } + } }; nrf.reset = function (states, cb) { From 0de837b6f62cd30639086572d34ff234f89d0f94 Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Wed, 24 Feb 2016 21:15:39 -0500 Subject: [PATCH 04/12] Add transmitted signal --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 401be55..5cf0a35 100644 --- a/index.js +++ b/index.js @@ -360,6 +360,7 @@ exports.connect = function (spi,ce,irq) { finish(new Error("Packet timeout, transmit queue flushed.")); }); else if (!d.TX_DS) console.warn("Unexpected IRQ during transmit phase!"); + else if (d.TX_DS) this.emit('transmitted'); else finish(); function finish(e) { // clear our interrupts, leaving RX_DR From 7d52124e07362c2136751abc7e1932bb002e0ec8 Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Thu, 25 Feb 2016 05:34:04 -0500 Subject: [PATCH 05/12] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5cf0a35..bbd96ad 100644 --- a/index.js +++ b/index.js @@ -360,11 +360,11 @@ exports.connect = function (spi,ce,irq) { finish(new Error("Packet timeout, transmit queue flushed.")); }); else if (!d.TX_DS) console.warn("Unexpected IRQ during transmit phase!"); - else if (d.TX_DS) this.emit('transmitted'); else finish(); function finish(e) { // clear our interrupts, leaving RX_DR nrf.setStates({TX_DS:true,MAX_RT:true,RX_DR:false}, function () { + if (d.TX_DS) nrf._prevSender.emit('transmitted'); cb(e||null); }); } From 22f8a1b08a9d70b518af8ce2decefb0aeb962e1f Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Thu, 25 Feb 2016 18:40:06 -0500 Subject: [PATCH 06/12] promise --- index.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/index.js b/index.js index bbd96ad..43634c0 100644 --- a/index.js +++ b/index.js @@ -613,6 +613,45 @@ exports.connect = function (spi,ce,irq) { this.push(null); this.emit('close'); }; + PxX.prototype.willWriteAck = function (buffer) { + var self = this; + return new Promise (function (resolve, reject) { + self.write(buffer); + var timeout = false; + + self.on("data", function(d) { + timeout = true; + resolve (d); + }); + + setTimeout (function(){ + if(!timeout) { + this.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code + }); + } + PxX.prototype.willWrite = function (buffer) { + // Introducing Promise will require node > 0.11 + var self = this; + return new Promise (function (resolve, reject) { + self.write(buffer); + var timeout = false; + + self.on("transmitted", function() { + timeout = true; + resolve (); + }); + + setTimeout (function(){ + if(!timeout) { + this.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code + }); + } function PTX(addr,opts) { opts = _extend({size:'auto',autoAck:true,ackPayloads:false}, opts); From 4f82808d77c8d99547ee14d0254b9fa5027bc251 Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Thu, 25 Feb 2016 18:51:57 -0500 Subject: [PATCH 07/12] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 43634c0..89723ca 100644 --- a/index.js +++ b/index.js @@ -626,7 +626,7 @@ exports.connect = function (spi,ce,irq) { setTimeout (function(){ if(!timeout) { - this.emit('timeout'); + self.emit('timeout'); throw new Error("timeout"); } },1000); // TODO: remove hard code @@ -646,7 +646,7 @@ exports.connect = function (spi,ce,irq) { setTimeout (function(){ if(!timeout) { - this.emit('timeout'); + self.emit('timeout'); throw new Error("timeout"); } },1000); // TODO: remove hard code From 1320373a1c94c77ee9b30be53848637e53550558 Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Thu, 25 Feb 2016 19:10:17 -0500 Subject: [PATCH 08/12] Update index.js --- index.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 89723ca..755be6e 100644 --- a/index.js +++ b/index.js @@ -617,19 +617,19 @@ exports.connect = function (spi,ce,irq) { var self = this; return new Promise (function (resolve, reject) { self.write(buffer); - var timeout = false; + var timeout = setTimeout (function(){ + if(!timeout) { + self.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code; self.on("data", function(d) { - timeout = true; + clearTimeout(timeout); resolve (d); }); - setTimeout (function(){ - if(!timeout) { - self.emit('timeout'); - throw new Error("timeout"); - } - },1000); // TODO: remove hard code + }); } PxX.prototype.willWrite = function (buffer) { @@ -637,19 +637,17 @@ exports.connect = function (spi,ce,irq) { var self = this; return new Promise (function (resolve, reject) { self.write(buffer); - var timeout = false; - - self.on("transmitted", function() { - timeout = true; - resolve (); - }); - - setTimeout (function(){ + var timeout = setTimeout (function(){ if(!timeout) { self.emit('timeout'); throw new Error("timeout"); } - },1000); // TODO: remove hard code + },1000); // TODO: remove hard code; + + self.on("transmitted", function() { + clearTimeout(timeout); + resolve (); + }); }); } From f9a70f1a6ed77e6ff27804da0337110be483947f Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Thu, 25 Feb 2016 19:59:34 -0500 Subject: [PATCH 09/12] promise should be wrapped in a funciton This enables ``` Promise.resolve() .then(willWrite("3")) .then(willWriteAck("4")) .then(function(d){console.log(d)}) ``` --- index.js | 56 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 755be6e..a7919e1 100644 --- a/index.js +++ b/index.js @@ -615,40 +615,42 @@ exports.connect = function (spi,ce,irq) { }; PxX.prototype.willWriteAck = function (buffer) { var self = this; - return new Promise (function (resolve, reject) { - self.write(buffer); - var timeout = setTimeout (function(){ - if(!timeout) { - self.emit('timeout'); - throw new Error("timeout"); - } - },1000); // TODO: remove hard code; + return function (d) { + return new Promise (function (resolve, reject) { + self.write(buffer); + var timeout = setTimeout (function(){ + if(!timeout) { + self.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code; - self.on("data", function(d) { - clearTimeout(timeout); - resolve (d); + self.on("data", function(d) { + clearTimeout(timeout); + resolve (d); + }); }); - - - }); + } } PxX.prototype.willWrite = function (buffer) { // Introducing Promise will require node > 0.11 var self = this; - return new Promise (function (resolve, reject) { - self.write(buffer); - var timeout = setTimeout (function(){ - if(!timeout) { - self.emit('timeout'); - throw new Error("timeout"); - } - },1000); // TODO: remove hard code; - - self.on("transmitted", function() { - clearTimeout(timeout); - resolve (); + return function (d) { + return new Promise (function (resolve, reject) { + self.write(buffer); + var timeout = setTimeout (function(){ + if(!timeout) { + self.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code; + + self.on("transmitted", function() { + clearTimeout(timeout); + resolve (); + }); }); - }); + } } function PTX(addr,opts) { From 077c92e5ae2774cce7a94f5b4723e9dd3050e25d Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Thu, 25 Feb 2016 20:08:51 -0500 Subject: [PATCH 10/12] on -> once : memory leak --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a7919e1..e63f88a 100644 --- a/index.js +++ b/index.js @@ -625,7 +625,7 @@ exports.connect = function (spi,ce,irq) { } },1000); // TODO: remove hard code; - self.on("data", function(d) { + self.once("data", function(d) { clearTimeout(timeout); resolve (d); }); @@ -645,7 +645,7 @@ exports.connect = function (spi,ce,irq) { } },1000); // TODO: remove hard code; - self.on("transmitted", function() { + self.once("transmitted", function() { clearTimeout(timeout); resolve (); }); From bb467a9b8645c71ae5797090f01a5aff79b1061f Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Sun, 28 Feb 2016 06:32:00 -0500 Subject: [PATCH 11/12] fix promise format --- index.js | 56 ++++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index e63f88a..c82581b 100644 --- a/index.js +++ b/index.js @@ -615,42 +615,38 @@ exports.connect = function (spi,ce,irq) { }; PxX.prototype.willWriteAck = function (buffer) { var self = this; - return function (d) { - return new Promise (function (resolve, reject) { - self.write(buffer); - var timeout = setTimeout (function(){ - if(!timeout) { - self.emit('timeout'); - throw new Error("timeout"); - } - },1000); // TODO: remove hard code; - - self.once("data", function(d) { - clearTimeout(timeout); - resolve (d); - }); + return new Promise (function (resolve, reject) { + self.write(buffer); + var timeout = setTimeout (function(){ + if(!timeout) { + self.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code; + + self.once("data", function(d) { + clearTimeout(timeout); + resolve (d); }); - } + }); } PxX.prototype.willWrite = function (buffer) { // Introducing Promise will require node > 0.11 var self = this; - return function (d) { - return new Promise (function (resolve, reject) { - self.write(buffer); - var timeout = setTimeout (function(){ - if(!timeout) { - self.emit('timeout'); - throw new Error("timeout"); - } - },1000); // TODO: remove hard code; - - self.once("transmitted", function() { - clearTimeout(timeout); - resolve (); - }); + return new Promise (function (resolve, reject) { + self.write(buffer); + var timeout = setTimeout (function(){ + if(!timeout) { + self.emit('timeout'); + throw new Error("timeout"); + } + },1000); // TODO: remove hard code; + + self.once("transmitted", function() { + clearTimeout(timeout); + resolve (); }); - } + }); } function PTX(addr,opts) { From 49c3e8facd699aa48d98b83572a3cb02be2e205f Mon Sep 17 00:00:00 2001 From: wuyuanyi135 Date: Fri, 4 Mar 2016 03:58:57 -0500 Subject: [PATCH 12/12] Update index.js --- index.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index c82581b..26d2623 100644 --- a/index.js +++ b/index.js @@ -630,22 +630,40 @@ exports.connect = function (spi,ce,irq) { }); }); } - PxX.prototype.willWrite = function (buffer) { + PxX.prototype.willWrite = function (buffer, options) { // Introducing Promise will require node > 0.11 var self = this; + var defaultOptions = { + maxRetry: -1, + retryDelay: 0 + }; + var retryCount = 0; + options = Object.assign(defaultOptions, options); + return new Promise (function (resolve, reject) { - self.write(buffer); - var timeout = setTimeout (function(){ - if(!timeout) { - self.emit('timeout'); - throw new Error("timeout"); + function _sender() { + self.write(buffer); + self.once("transmitted", onSent); + function onSent() { + self.removeListener('transmitted', onSent); + self.removeListener('error', onErr); + resolve (); } - },1000); // TODO: remove hard code; - - self.once("transmitted", function() { - clearTimeout(timeout); - resolve (); - }); + + self.once("error", onError); + function onError() { + retryCount ++; + if (retryCount > options.maxRetry && options.maxRetry > 0) { + self.emit('fail', {}); + reject(); + } else { + self.emit('failOnce', {count: retryCount}); + self.removeListener('transmitted', onSent); + self.removeListener('error', onErr); + setTimeout(_sender,options.retryDelay); + } + } + } }); }