Comparing version 0.3.3 to 0.3.4
{ | ||
"name": "rpi-gpio", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "Control Raspberry Pi GPIO pins with node.js", | ||
@@ -5,0 +5,0 @@ "main": "rpi-gpio.js", |
100
rpi-gpio.js
@@ -8,4 +8,3 @@ var fs = require('fs'); | ||
var PATH = '/sys/class/gpio'; | ||
var pins = { | ||
var PINS = { | ||
v1: { | ||
@@ -124,8 +123,10 @@ '1': null, | ||
var pin; | ||
var pinForSetup; | ||
async.waterfall([ | ||
function(next) { | ||
setRaspberryVersion(currentPins, function(err, pinSchema) { | ||
setRaspberryVersion(function(err, pinSchema) { | ||
if (err) next(err); | ||
currentPins = pinSchema; | ||
if (pinSchema) { | ||
currentPins = pinSchema; | ||
} | ||
next(); | ||
@@ -135,9 +136,9 @@ }); | ||
function(next) { | ||
pin = getPinForCurrentMode(currentPins, channel); | ||
debug('set up pin %d', pin); | ||
isExported(pin, next); | ||
pinForSetup = getPinForCurrentMode(channel); | ||
debug('set up pin %d', pinForSetup); | ||
isExported(pinForSetup, next); | ||
}, | ||
function(isExported, next) { | ||
if (isExported) { | ||
return unexportPin(pin, next); | ||
return unexportPin(pinForSetup, next); | ||
} | ||
@@ -147,9 +148,9 @@ return next(null); | ||
function(next) { | ||
exportPin(pin, next); | ||
exportPin(pinForSetup, next); | ||
}, | ||
function(next) { | ||
exportedPins[pin] = true; | ||
exportedPins[pinForSetup] = true; | ||
this.emit('export', channel); | ||
createListener.call(this, channel, pin); | ||
setDirection(pin, direction, next); | ||
createListener.call(this, channel, pinForSetup); | ||
setDirection(pinForSetup, direction, next); | ||
}.bind(this) | ||
@@ -167,3 +168,3 @@ ], cb); | ||
this.write = this.output = function(channel, value, cb /*err*/ ) { | ||
var pin = getPinForCurrentMode(currentPins, channel); | ||
var pin = getPinForCurrentMode(channel); | ||
@@ -187,3 +188,3 @@ if (!exportedPins[pin]) { | ||
this.read = this.input = function(channel, cb /*err,value*/) { | ||
var pin = getPinForCurrentMode(currentPins, channel); | ||
var pin = getPinForCurrentMode(channel); | ||
@@ -231,47 +232,48 @@ if (!exportedPins[pin]) { | ||
this.reset(); | ||
} | ||
util.inherits(Gpio, EventEmitter); | ||
function setRaspberryVersion(currentPins, cb) { | ||
if (currentPins) { | ||
return cb(null); | ||
} | ||
// Private functions requring access to state | ||
function setRaspberryVersion(cb) { | ||
if (currentPins) { | ||
return cb(null); | ||
} | ||
fs.readFile('/proc/cpuinfo', 'utf8', function(err, data) { | ||
if (err) return cb(err); | ||
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*[0-9a-f]*([0-9a-f]{4})/); | ||
var revisionNumber = parseInt(match[1], 16); | ||
var pinVersion = (revisionNumber < 4) ? 'v1' : 'v2'; | ||
// Match the last 4 digits of the number following "Revision:" | ||
var match = data.match(/Revision\s*:\s*[0-9a-f]*([0-9a-f]{4})/); | ||
var revisionNumber = parseInt(match[1], 16); | ||
var pinVersion = (revisionNumber < 4) ? 'v1' : 'v2'; | ||
debug( | ||
'seen hardware revision %d; using pin mode %s', | ||
revisionNumber, | ||
pinVersion | ||
); | ||
debug( | ||
'seen hardware revision %d; using pin mode %s', | ||
revisionNumber, | ||
pinVersion | ||
); | ||
return cb(null, pins[pinVersion]); | ||
}); | ||
}; | ||
return cb(null, PINS[pinVersion]); | ||
}); | ||
}; | ||
function getPinRpi(currentPins, channel) { | ||
return currentPins[channel] + ''; | ||
}; | ||
function getPinRpi(channel) { | ||
return currentPins[channel] + ''; | ||
}; | ||
function getPinBcm(currentPins, channel) { | ||
return channel + ''; | ||
}; | ||
function getPinBcm(channel) { | ||
return channel + ''; | ||
}; | ||
function createListener(channel, pin) { | ||
debug('listen for pin %d', 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); | ||
function createListener(channel, pin) { | ||
debug('listen for pin %d', pin); | ||
var Gpio = this; | ||
fs.watchFile(PATH + '/gpio' + pin + '/value', function() { | ||
Gpio.read(channel, function(err, value) { | ||
if (err) return cb(err); | ||
Gpio.emit('change', channel, value); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
util.inherits(Gpio, EventEmitter); | ||
@@ -278,0 +280,0 @@ function setDirection(pin, direction, cb) { |
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
42332
855