From 976a48628a68573920af56c3d5d30d54bce429ad Mon Sep 17 00:00:00 2001 From: Mike Trebilcock Date: Thu, 12 Jan 2017 21:14:27 +0000 Subject: [PATCH] feat(get_mode): Add get_mode --- docs/definitions.js.html | 4 +- docs/global.html | 4 +- docs/index.html | 4 +- docs/index.js.html | 18 +++- docs/pigpio.html | 174 +++++++++++++++++++++++++++++++++++++-- js-pigpio/index.js | 14 +++- js-pigpio/utils.js | 7 +- test/pigpio.spec.js | 48 ++++++----- 8 files changed, 227 insertions(+), 46 deletions(-) diff --git a/docs/definitions.js.html b/docs/definitions.js.html index fc8b829..71a2b17 100644 --- a/docs/definitions.js.html +++ b/docs/definitions.js.html @@ -22,7 +22,7 @@
@@ -348,7 +348,7 @@

definitions.js


- Documentation generated by JSDoc 3.4.3 on Wed Jan 11 2017 21:31:24 GMT+0000 (GMT) using the docdash theme. + Documentation generated by JSDoc 3.4.3 on Thu Jan 12 2017 21:14:03 GMT+0000 (GMT) using the docdash theme.
diff --git a/docs/global.html b/docs/global.html index e35a76d..dd8c49e 100644 --- a/docs/global.html +++ b/docs/global.html @@ -22,7 +22,7 @@
@@ -191,7 +191,7 @@

(constant) defi
- Documentation generated by JSDoc 3.4.3 on Wed Jan 11 2017 21:31:24 GMT+0000 (GMT) using the docdash theme. + Documentation generated by JSDoc 3.4.3 on Thu Jan 12 2017 21:14:03 GMT+0000 (GMT) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index b27355a..f835786 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,7 +22,7 @@
@@ -71,7 +71,7 @@

Thanks

Massive thanks for the inspiration and idea from

diff --git a/docs/index.js.html b/docs/index.js.html index 5196a2c..3213485 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -22,7 +22,7 @@
@@ -124,7 +124,6 @@

index.js

if (that._next !== undefined) { that._next(undefined, reverse_string(data.toString('hex'))); } - }); this.client.on('error', (e) => { @@ -212,7 +211,6 @@

index.js

pigpio.prototype.getHardwareRevision = function(cb) { "use strict"; this._pi_gpio_command(def.PI_CMD_HWVER, 0, 0, cb, true); - }; /** @@ -229,6 +227,18 @@

index.js

}; +/** + * Returns the GPIO mode. + * + * @param {number} gpio - Port 0-53. + * @param {callback} callback - Function to be run when the data has been received. + */ +pigpio.prototype.get_mode = function (gpio, callback) { + "use strict"; + assert_gpio_pin_in_range(gpio,0,53); + this._pi_gpio_command(def.PI_CMD_MODEG, gpio, 0, callback, true); +} + /** * Sets or clears the internal GPIO pull-up/down resistor. * @@ -316,7 +326,7 @@

index.js


- Documentation generated by JSDoc 3.4.3 on Wed Jan 11 2017 21:31:24 GMT+0000 (GMT) using the docdash theme. + Documentation generated by JSDoc 3.4.3 on Thu Jan 12 2017 21:14:03 GMT+0000 (GMT) using the docdash theme.
diff --git a/docs/pigpio.html b/docs/pigpio.html index 42410bc..75383b1 100644 --- a/docs/pigpio.html +++ b/docs/pigpio.html @@ -22,7 +22,7 @@
@@ -163,7 +163,7 @@

closeSource:
@@ -225,6 +225,162 @@

closeget_mode(gpio, callback)

+ + + + + + +
+ + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ Returns the GPIO mode. +
+ + + + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
gpio + + +number + + + + Port 0-53.
callback + + +callback + + + + Function to be run when the data has been received.
+ + + + + + + + + + + @@ -247,7 +403,7 @@

ge
Source:
@@ -605,7 +761,7 @@

set_
Source:
@@ -766,7 +922,7 @@

set_modeSource:
@@ -922,7 +1078,7 @@

set_p
Source:
@@ -1078,7 +1234,7 @@

setPwm
Source:
@@ -1245,7 +1401,7 @@

set
Source:
@@ -1422,7 +1578,7 @@

Parameters:

- Documentation generated by JSDoc 3.4.3 on Wed Jan 11 2017 21:31:24 GMT+0000 (GMT) using the docdash theme. + Documentation generated by JSDoc 3.4.3 on Thu Jan 12 2017 21:14:03 GMT+0000 (GMT) using the docdash theme.
diff --git a/js-pigpio/index.js b/js-pigpio/index.js index 6e2ffeb..a622770 100644 --- a/js-pigpio/index.js +++ b/js-pigpio/index.js @@ -85,7 +85,6 @@ pigpio.prototype.pi = function(host, port, cb) { if (that._next !== undefined) { that._next(undefined, reverse_string(data.toString('hex'))); } - }); this.client.on('error', (e) => { @@ -173,7 +172,6 @@ pigpio.prototype.setPwmDutycycle = function(userGpio, dutycycle) { pigpio.prototype.getHardwareRevision = function(cb) { "use strict"; this._pi_gpio_command(def.PI_CMD_HWVER, 0, 0, cb, true); - }; /** @@ -190,6 +188,18 @@ pigpio.prototype.set_mode = function (gpio, mode) { }; +/** + * Returns the GPIO mode. + * + * @param {number} gpio - Port 0-53. + * @param {callback} callback - Function to be run when the data has been received. + */ +pigpio.prototype.get_mode = function (gpio, callback) { + "use strict"; + assert_gpio_pin_in_range(gpio,0,53); + this._pi_gpio_command(def.PI_CMD_MODEG, gpio, 0, callback, true); +} + /** * Sets or clears the internal GPIO pull-up/down resistor. * diff --git a/js-pigpio/utils.js b/js-pigpio/utils.js index 529443d..df75c2c 100644 --- a/js-pigpio/utils.js +++ b/js-pigpio/utils.js @@ -3,11 +3,12 @@ exports.reverse_string = function (str) { var result=''; var i = str.length-1; - while (i>0) { - if (str.substr(i-1, 2)!=='00' && str.substr(i-1, 2)!=='11') { + //First two bytes indicate buffer size + while (i>2) { + if (str.substr(i-1, 2)!=='00') { result +=str [i-1] + str[i] ; } i -=2; } - return result; + return parseInt(result,'16'); }; \ No newline at end of file diff --git a/test/pigpio.spec.js b/test/pigpio.spec.js index 7e39be2..6982e3e 100644 --- a/test/pigpio.spec.js +++ b/test/pigpio.spec.js @@ -4,7 +4,7 @@ const assert = require('chai').assert; const expect = require('chai').expect; const PiGPIO = require('../js-pigpio/index.js'); const net = require('net'); -const reverse_string = require('../js-pigpio/utils.js').reverse_string; +const Put = require('put'); let last_command = ""; let server_response = ""; @@ -16,10 +16,7 @@ describe('js-pigpio', () => { socket.on("data", (data) => { last_command = data.toString('hex'); if (server_response!=="") { - server_response = reverse_string(server_response); - var replyData = new Buffer(server_response, - 'hex'); - + var replyData = server_response; socket.write(replyData); server_response=""; } @@ -68,7 +65,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -84,7 +80,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -100,7 +95,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -116,7 +110,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -132,7 +125,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -148,7 +140,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -164,7 +155,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -180,7 +170,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -221,6 +210,23 @@ describe('js-pigpio', () => { }); }); + it('get_mode_returns_a_value', (done) => { + const pigpio = new PiGPIO(); + pigpio.pi('127.0.0.1', 5000, () => { + "use strict"; + const cmd = Put() + .word32le(0x0104); + server_response = cmd.buffer(); + pigpio.get_mode(2,(err, data)=>{ + assert (err === undefined, "Error occured"); + assert(pigpio.OUTPUT===data,"Invalid Server response"); + assert(last_command.substr(0,2)==='01', "Wrong Command Send"); + pigpio.close(); + done(); + }); + }); + }); + it('set_pull_up_down as off', (done) => { const pigpio = new PiGPIO(); pigpio.pi('127.0.0.1', 5000, () => { @@ -233,7 +239,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -249,7 +254,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -286,7 +290,6 @@ describe('js-pigpio', () => { pigpio.set_pull_up_down(); }).to.throw(Error); pigpio.close(); - }); }); @@ -302,7 +305,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -338,7 +340,6 @@ describe('js-pigpio', () => { pigpio.setServoPulsewidth(1); }).to.throw(Error); pigpio.close(); - }); }); @@ -354,7 +355,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -408,7 +408,6 @@ describe('js-pigpio', () => { done(); }, 100, done); pigpio.close(); - }); }); @@ -445,9 +444,14 @@ describe('js-pigpio', () => { const pigpio = new PiGPIO(); pigpio.pi('127.0.0.1', 5000, () => { "use strict"; - server_response = 'a02082'; + const cmd = Put() + .word32le(0x0011) + .word32le(0) + .word32le(0) + .word32le(0x00a02082); + server_response = cmd.buffer(); pigpio.getHardwareRevision((err, data)=>{ - assert('a02082'===data,"Invalid Server response"); + assert(parseInt('a02082',16)===data,"Invalid Server response"); assert(last_command.substr(0,2)==='11', "Wrong Command Send"); pigpio.close(); done();