Comparing version 0.3.1 to 0.3.2
{ | ||
"name": "rpi-gpio", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Control Raspberry Pi GPIO pins with node.js", | ||
@@ -30,3 +30,6 @@ "main": "rpi-gpio.js", | ||
"license": "MIT", | ||
"readmeFilename": "README.md" | ||
"readmeFilename": "README.md", | ||
"dependencies": { | ||
"async": "~0.8.0" | ||
} | ||
} |
@@ -7,2 +7,5 @@ rpi-gpio.js | ||
[![Build Status](https://travis-ci.org/JamesBarwell/rpi-gpio.js.svg?branch=master)](https://travis-ci.org/JamesBarwell/rpi-gpio.js) | ||
[![NPM version](https://badge.fury.io/js/rpi-gpio.svg)](http://badge.fury.io/js/rpi-gpio) | ||
## Setup | ||
@@ -9,0 +12,0 @@ See this guide on how to get [node.js running on Raspberry Pi](http://elsmorian.com/post/23474168753/node-js-on-raspberry-pi). |
106
rpi-gpio.js
var fs = require('fs'); | ||
var util = require('util'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var async = require('async'); | ||
// Constants | ||
var PATH = '/sys/class/gpio'; | ||
@@ -82,5 +82,5 @@ | ||
/** | ||
* Set pin reference mode. Defaults to 'rpi'. | ||
* Set pin reference mode. Defaults to 'mode_rpi'. | ||
* | ||
* @param {string} mode Pin reference mode, 'rpi' or 'bcm' | ||
* @param {string} mode Pin reference mode, 'mode_rpi' or 'mode_bcm' | ||
*/ | ||
@@ -122,29 +122,27 @@ Gpio.prototype.setMode = function(mode) { | ||
var self = this; | ||
setRaspberryVersion.call(this, function() { | ||
var pin = getPinForCurrentMode(channel); | ||
function doExport() { | ||
exportPin(pin, function() { | ||
self.exportedPins[pin] = true; | ||
self.emit('export', channel); | ||
setListener(pin, function() { | ||
self.read(channel, function(err, value) { | ||
if (err) return cb(err); | ||
self.emit('change', channel, value); | ||
}); | ||
}); | ||
setDirection(pin, direction, cb); | ||
}); | ||
} | ||
// Unexport pin if already open | ||
isExported(pin, function(isOpen) { | ||
if (isOpen) { | ||
unexportPin(pin, doExport); | ||
} else { | ||
doExport(); | ||
var pin; | ||
async.waterfall([ | ||
function(next) { | ||
setRaspberryVersion(next); | ||
}, | ||
function(next) { | ||
pin = getPinForCurrentMode(channel); | ||
isExported(pin, next); | ||
}, | ||
function(isExported, next) { | ||
if (isExported) { | ||
return unexportPin(pin, next); | ||
} | ||
}.bind(self)); | ||
}); | ||
return next(null); | ||
}, | ||
function(next) { | ||
exportPin(pin, next); | ||
}, | ||
function(next) { | ||
this.exportedPins[pin] = true; | ||
this.emit('export', channel); | ||
createListener.call(this, channel, pin); | ||
setDirection(pin, direction, next); | ||
}.bind(this) | ||
], cb); | ||
}; | ||
@@ -167,5 +165,3 @@ | ||
value = (!!value && value !== '0') ? '1' : '0'; | ||
fs.writeFile(PATH + '/gpio' + pin + '/value', value, function(err) { | ||
if (cb) return cb(err); | ||
}.bind(this)); | ||
fs.writeFile(PATH + '/gpio' + pin + '/value', value, cb || function () {}); | ||
}; | ||
@@ -189,3 +185,3 @@ Gpio.prototype.output = Gpio.prototype.write; | ||
data = (data + '').trim() || '0'; | ||
return cb(err, (data === '1' ? true : false)); | ||
return cb(err, data === '1'); | ||
}); | ||
@@ -196,3 +192,3 @@ }; | ||
/** | ||
* Unexport any open pins | ||
* Unexport any pins setup by this module | ||
* | ||
@@ -202,12 +198,8 @@ * @param {function} cb Optional callback | ||
Gpio.prototype.destroy = function(cb) { | ||
var pins = Object.keys(this.exportedPins); | ||
var pinCount = pins.length; | ||
while (pinCount--) { | ||
var pin = pins[pinCount]; | ||
if (pinCount === 0 && cb) { | ||
unexportPin(pin, cb); | ||
} else { | ||
unexportPin(pin); | ||
var tasks = Object.keys(this.exportedPins).map(function(pin) { | ||
return function(done) { | ||
unexportPin(pin, done); | ||
} | ||
} | ||
}); | ||
async.parallel(tasks, cb); | ||
}; | ||
@@ -231,13 +223,13 @@ | ||
if (pins.current) { | ||
return cb(); | ||
return cb(null); | ||
} | ||
var self = this; | ||
fs.readFile('/proc/cpuinfo', 'utf8', function(err, data) { | ||
if (err) return cb(err); | ||
// Match the last 4 digits of the number following "Revision:" | ||
var match = data.match(/Revision\s*:\s*\d*(\d{4})/); | ||
var revisionNumber = match[1]; | ||
var match = data.match(/Revision\s*:\s*[0-9a-f]*([0-9a-f]{4})/); | ||
var revisionNumber = parseInt(match[1], 16); | ||
if (revisionNumber === '0002' || revisionNumber === '0003') { | ||
if (revisionNumber < 3) { | ||
pins.current = pins.v1; | ||
@@ -247,3 +239,4 @@ } else { | ||
} | ||
cb(); | ||
return cb(null); | ||
}); | ||
@@ -261,2 +254,11 @@ }; | ||
function createListener(channel, pin) { | ||
var self = this; | ||
fs.watchFile(PATH + '/gpio' + pin + '/value', function() { | ||
self.read(channel, function(err, value) { | ||
if (err) return cb(err); | ||
self.emit('change', channel, value); | ||
}); | ||
}); | ||
} | ||
@@ -284,10 +286,6 @@ function setDirection(pin, direction, cb) { | ||
fs.exists(PATH + '/gpio' + pin, function(exists) { | ||
if (cb) return cb(exists); | ||
return cb(null, exists); | ||
}); | ||
} | ||
function setListener(pin, cb) { | ||
fs.watchFile(PATH + '/gpio' + pin + '/value', cb); | ||
} | ||
module.exports = new Gpio; |
@@ -12,4 +12,4 @@ var assert = require('assert'); | ||
v1Overvolted: 'Processor : ARMv6-compatible processor rev 7 (v6l)\nBogoMIPS : 697.95\nFeatures : swp half thumb fastmult vfp edsp java tls\nCPU implementer : 0x41\nCPU architecture: 7\nCPU variant : 0x0\nCPU part : 0xb76\nCPU revision : 7\n\n\nHardware : BCM2708\nRevision : 100000002\nSerial : 000000009a5d9c22', | ||
v2: 'Processor : ARMv6-compatible processor rev 7 (v6l)\nBogoMIPS : 697.95\nFeatures : swp half thumb fastmult vfp edsp java tls\nCPU implementer : 0x41\nCPU architecture: 7\nCPU variant : 0x0\nCPU part : 0xb76\nCPU revision : 7\n\n\nHardware : BCM2708\nRevision : 0004\nSerial : 000000009a5d9c22', | ||
v2Overvolted: 'Processor : ARMv6-compatible processor rev 7 (v6l)\nBogoMIPS : 697.95\nFeatures : swp half thumb fastmult vfp edsp java tls\nCPU implementer : 0x41\nCPU architecture: 7\nCPU variant : 0x0\nCPU part : 0xb76\nCPU revision : 7\n\n\nHardware : BCM2708\nRevision : 10000004\nSerial : 000000009a5d9c22' | ||
v2: 'Processor : ARMv6-compatible processor rev 7 (v6l)\nBogoMIPS : 697.95\nFeatures : swp half thumb fastmult vfp edsp java tls\nCPU implementer : 0x41\nCPU architecture: 7\nCPU variant : 0x0\nCPU part : 0xb76\nCPU revision : 7\n\n\nHardware : BCM2708\nRevision : 000e\nSerial : 000000009a5d9c22', | ||
v2Overvolted: 'Processor : ARMv6-compatible processor rev 7 (v6l)\nBogoMIPS : 697.95\nFeatures : swp half thumb fastmult vfp edsp java tls\nCPU implementer : 0x41\nCPU architecture: 7\nCPU variant : 0x0\nCPU part : 0xb76\nCPU revision : 7\n\n\nHardware : BCM2708\nRevision : 10000003\nSerial : 000000009a5d9c22' | ||
} | ||
@@ -153,2 +153,3 @@ | ||
context('and minimum arguments are specified', function() { | ||
var onSetup; | ||
var listener; | ||
@@ -160,3 +161,4 @@ | ||
gpio.setup(1, null, done); | ||
onSetup = sinon.spy(done); | ||
gpio.setup(1, null, onSetup); | ||
}); | ||
@@ -172,2 +174,6 @@ | ||
it('should run the setup callback', function() { | ||
sinon.assert.calledOnce(onSetup); | ||
}); | ||
it('should emit an export event', function() { | ||
@@ -174,0 +180,0 @@ // The emitted channel is the same format as given |
Sorry, the diff of this file is not supported yet
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
38929
8
792
187
1
+ Addedasync@~0.8.0
+ Addedasync@0.8.0(transitive)