Comparing version 0.0.1 to 0.0.2
@@ -5,3 +5,3 @@ { | ||
"description": "Control Raspberry Pi GPIO pins with node.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "rpi-gpio.js", | ||
@@ -8,0 +8,0 @@ "keywords:": ["raspberry", "pi", "gpio"], |
@@ -10,3 +10,10 @@ rpi-gpio.js | ||
This module can be installed with npm: | ||
```js | ||
npm install rpi-gpio | ||
``` | ||
## Usage | ||
First, make sure you are running as root, else the Raspberry Pi will not let you output to the GPIO. | ||
After loading the module, initialise a pin by calling `setup`. Each GPIO pin can be set as either an input or output, which lets you read and write to it respectively. The 'channel' must be specified, to indicate which pin to use. There are two different ways to reference a channel; either using the Raspberry Pi or the BCM naming schema (sadly, neither of which match the physical pins!). This module supports both schemas, with Raspberry Pi being the default. Please see [this page](http://elinux.org/RPi_Low-level_peripherals) for more details. | ||
@@ -13,0 +20,0 @@ |
var fs = require('fs'), | ||
path = require('path'), | ||
util = require('util'), | ||
EventEmitter = require('events').EventEmitter; | ||
EventEmitter = require('events').EventEmitter, | ||
// path.exists for 0.6.x support | ||
path = require('path'); | ||
@@ -80,4 +81,10 @@ // Constants | ||
} | ||
direction = direction || this.DIR_OUT; | ||
if (typeof direction === 'function') { | ||
cb = direction; | ||
direction = this.DIR_OUT; | ||
} | ||
var self = this; | ||
@@ -139,8 +146,16 @@ function doExport() { | ||
* Unexport any open pins | ||
* | ||
* @param {function} cb Optional callback | ||
*/ | ||
Gpio.prototype.destroy = function(cb) { | ||
for (var pin in exportedPins) { | ||
unexportPin(pin); | ||
}; | ||
cb(); | ||
var pins = Object.keys(exportedPins); | ||
var pinCount = pins.length; | ||
while (pinCount--) { | ||
var pin = pins[pinCount]; | ||
if (pinCount === 0 && cb) { | ||
unexportPin(pin, cb); | ||
} else { | ||
unexportPin(pin); | ||
} | ||
} | ||
} | ||
@@ -190,3 +205,4 @@ | ||
var pin = getPin(channel); | ||
path.exists(PATH + '/gpio' + pin, function(exists) { | ||
// path.exists deprecated in 0.8.0 | ||
(fs.exists || path.exists)(PATH + '/gpio' + pin, function(exists) { | ||
if (cb) return cb(exists); | ||
@@ -201,3 +217,2 @@ }); | ||
} | ||
//@todo validate this properly | ||
@@ -204,0 +219,0 @@ return pin + ''; |
@@ -42,3 +42,3 @@ describe('rpi-gpio', function() { | ||
beforeEach(function() { | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(true); | ||
@@ -64,3 +64,3 @@ }); | ||
beforeEach(function() { | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -70,4 +70,3 @@ }); | ||
spyOn(gpio, 'emit'); | ||
var callback = jasmine.createSpy(); | ||
gpio.setup(1, null, callback); | ||
gpio.setup(1); | ||
}); | ||
@@ -96,3 +95,3 @@ it('should export the channel', function() { | ||
beforeEach(function() { | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -118,2 +117,15 @@ }); | ||
}); | ||
describe('and callback is specified', function() { | ||
beforeEach(function() { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
}); | ||
spyOn(fs, 'watchFile').andCallFake(function(path, cb) { }); | ||
}); | ||
it('should execute the callback when direction is missing', function() { | ||
var callback = jasmine.createSpy(); | ||
gpio.setup(1, callback); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
@@ -125,3 +137,3 @@ }); | ||
spyOn(fs, 'writeFile').andCallFake(function(path, value, cb) { cb(); }); | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -150,3 +162,3 @@ }); | ||
spyOn(fs, 'writeFile').andCallFake(function(path, value, cb) { cb(); }); | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -171,3 +183,3 @@ }); | ||
spyOn(fs, 'writeFile').andCallFake(function(path, value, cb) { cb(); }); | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -186,10 +198,18 @@ }); | ||
var exportPath = '/sys/class/gpio/unexport'; | ||
expect(fs.writeFile.calls[0].args[0]).toEqual(exportPath); | ||
expect(fs.writeFile.calls[1].args[0]).toEqual(exportPath); | ||
expect(fs.writeFile.calls[2].args[0]).toEqual(exportPath); | ||
var unexportPath = '/sys/class/gpio/unexport'; | ||
var pathsExported = []; | ||
expect(fs.writeFile.calls[0].args[0]).toEqual(unexportPath); | ||
expect(fs.writeFile.calls[1].args[0]).toEqual(unexportPath); | ||
expect(fs.writeFile.calls[2].args[0]).toEqual(unexportPath); | ||
expect(fs.writeFile.calls[0].args[1]).toEqual('1'); | ||
expect(fs.writeFile.calls[1].args[1]).toEqual('2'); | ||
expect(fs.writeFile.calls[2].args[1]).toEqual('3'); | ||
// Paths are unexported in reverse order, so just get them | ||
// into an array and sort before asserting | ||
[0,1,2].forEach(function(callNumber) { | ||
pathsExported.push(fs.writeFile.calls[callNumber].args[1]); | ||
}); | ||
pathsExported.sort(); | ||
expect(pathsExported[0]).toEqual('1'); | ||
expect(pathsExported[1]).toEqual('2'); | ||
expect(pathsExported[2]).toEqual('3'); | ||
expect(callback).toHaveBeenCalled(); | ||
@@ -203,3 +223,3 @@ }); | ||
spyOn(fs, 'writeFile').andCallFake(function(path, value, cb) { cb(); }); | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -227,3 +247,3 @@ }); | ||
spyOn(fs, 'writeFile').andCallFake(function(path, value, cb) { cb(); }); | ||
spyOn(path, 'exists').andCallFake(function(path, cb) { | ||
spyOn((fs.exists ? fs : path), 'exists').andCallFake(function(path, cb) { | ||
cb(false); | ||
@@ -230,0 +250,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22146
7
471
86