Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "rpi-gpio", | ||
"author": "James Barwell <jb@jamesbarwell.co.uk>", | ||
"version": "0.2.0", | ||
"description": "Control Raspberry Pi GPIO pins with node.js", | ||
"version": "0.1.0", | ||
"main": "rpi-gpio.js", | ||
@@ -12,8 +11,6 @@ "keywords:": [ | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/JamesBarwell/rpi-gpio.js.git" | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"devDependencies": { | ||
"jasmine-node": "~1.14.3", | ||
"mocha": "~1.18.2", | ||
@@ -25,4 +22,12 @@ "sinon": "~1.9.0", | ||
"test": "mocha", | ||
"spec": "mocha --reporter spec", | ||
"coverage": "istanbul cover _mocha" | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/JamesBarwell/rpi-gpio.js.git" | ||
}, | ||
"author": "James Barwell <jb@jamesbarwell.co.uk>", | ||
"license": "MIT", | ||
"readmeFilename": "README.md" | ||
} |
184
rpi-gpio.js
@@ -1,6 +0,4 @@ | ||
var fs = require('fs'), | ||
util = require('util'), | ||
EventEmitter = require('events').EventEmitter, | ||
// path.exists for 0.6.x support | ||
path = require('path'); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -17,38 +15,62 @@ // Constants | ||
var pins = { | ||
'1': null, | ||
'2': null, | ||
'3': 0, | ||
'4': null, | ||
'5': 1, | ||
'6': null, | ||
'7': 4, | ||
'8': 14, | ||
'9': null, | ||
'10': 15, | ||
'11': 17, | ||
'12': 18, | ||
'13': 21, | ||
'14': null, | ||
'15': 22, | ||
'16': 23, | ||
'17': null, | ||
'18': 24, | ||
'19': 10, | ||
'20': null, | ||
'21': 9, | ||
'22': 25, | ||
'23': 11, | ||
'24': 8, | ||
'25': null, | ||
'26': 7 | ||
current: undefined, | ||
v1: { | ||
'1': null, | ||
'2': null, | ||
'3': 0, | ||
'4': null, | ||
'5': 1, | ||
'6': null, | ||
'7': 4, | ||
'8': 14, | ||
'9': null, | ||
'10': 15, | ||
'11': 17, | ||
'12': 18, | ||
'13': 21, | ||
'14': null, | ||
'15': 22, | ||
'16': 23, | ||
'17': null, | ||
'18': 24, | ||
'19': 10, | ||
'20': null, | ||
'21': 9, | ||
'22': 25, | ||
'23': 11, | ||
'24': 8, | ||
'25': null, | ||
'26': 7 | ||
}, | ||
v2: { | ||
'1': null, | ||
'2': null, | ||
'3': 2, | ||
'4': null, | ||
'5': 3, | ||
'6': null, | ||
'7': 4, | ||
'8': 14, | ||
'9': null, | ||
'10': 15, | ||
'11': 17, | ||
'12': 18, | ||
'13': 27, | ||
'14': null, | ||
'15': 22, | ||
'16': 23, | ||
'17': null, | ||
'18': 24, | ||
'19': 10, | ||
'20': null, | ||
'21': 9, | ||
'22': 25, | ||
'23': 11, | ||
'24': 8, | ||
'25': null, | ||
'26': 7 | ||
} | ||
}; | ||
var changedPinsV2 = { | ||
'3' : 2, | ||
'5' : 3, | ||
'13' : 27 | ||
}; | ||
// Constants | ||
@@ -59,4 +81,3 @@ Gpio.prototype.DIR_IN = 'in'; | ||
Gpio.prototype.MODE_RPI = function(channel) { | ||
// RPi to BCM | ||
return pins[channel] + ''; | ||
return pins.current[channel] + ''; | ||
}; | ||
@@ -68,39 +89,2 @@ Gpio.prototype.MODE_BCM = function(channel) { | ||
/** | ||
* Changes the necessary pins for the Raspberry V2 | ||
*/ | ||
Gpio.prototype.changePins = function(newScheme) { | ||
Object.keys(newScheme).forEach(function(index) { | ||
pins[index] = newScheme[index]; | ||
}); | ||
}; | ||
/** | ||
* Sets the version of the model | ||
*/ | ||
Gpio.prototype.setRaspberryVersion = function(cb) { | ||
var self = this; | ||
fs.readFile('/proc/cpuinfo', 'utf8', function(err, data) { | ||
data = self.parseCpuinfo(data); | ||
data = data.trim().slice(-1); | ||
if (data == '2' || data == '3') { | ||
self.version = 1; | ||
} else { | ||
self.version = 2; | ||
} | ||
cb(); | ||
}); | ||
}; | ||
/** | ||
* Detects if the Raspberry Pi is version 2 | ||
*/ | ||
Gpio.prototype.parseCpuinfo = function(data) { | ||
var res = data.split('Revision')[1].trim(); | ||
return res[2] + res[3] + res[4] + res[5]; | ||
}; | ||
/** | ||
* Set pin reference mode. Defaults to 'rpi'. | ||
@@ -142,7 +126,3 @@ * | ||
var self = this; | ||
this.setRaspberryVersion(function() { | ||
if (self.version === 2) { | ||
self.changePins(changedPinsV2); | ||
} | ||
setRaspberryVersion.call(this, function() { | ||
var pin = self.getPin(channel); | ||
@@ -231,4 +211,37 @@ | ||
this.removeAllListeners(); | ||
pins.current = undefined; | ||
}; | ||
/** | ||
* Sets the version of the model | ||
*/ | ||
function setRaspberryVersion(cb) { | ||
if (pins.current) { | ||
return cb(); | ||
} | ||
var self = this; | ||
fs.readFile('/proc/cpuinfo', 'utf8', function(err, data) { | ||
data = parseCpuinfo(data); | ||
data = data.trim().slice(-1); | ||
if (data == '2' || data == '3') { | ||
pins.current = pins.v1; | ||
} else { | ||
pins.current = pins.v2; | ||
} | ||
cb(); | ||
}); | ||
}; | ||
/** | ||
* Detects if the Raspberry Pi is version 2 | ||
*/ | ||
function parseCpuinfo(data) { | ||
var res = data.split('Revision')[1].trim(); | ||
return res[2] + res[3] + res[4] + res[5]; | ||
}; | ||
function setDirection(pin, direction, cb) { | ||
@@ -247,7 +260,3 @@ fs.writeFile(PATH + '/gpio' + pin + '/direction', direction, function(err) { | ||
function unexportPin(pin, cb) { | ||
unexportPin(pin, cb); | ||
fs.unwatchFile(PATH + '/gpio' + pin + '/value'); | ||
} | ||
function unexportPin(pin, cb) { | ||
fs.writeFile(PATH + '/unexport', pin, function(err) { | ||
@@ -259,4 +268,3 @@ if (cb) return cb(err); | ||
function isExported(pin, cb) { | ||
// path.exists deprecated in 0.8.0 | ||
(fs.exists || path.exists)(PATH + '/gpio' + pin, function(exists) { | ||
fs.exists(PATH + '/gpio' + pin, function(exists) { | ||
if (cb) return cb(exists); | ||
@@ -263,0 +271,0 @@ }); |
@@ -7,2 +7,4 @@ var assert = require('assert'); | ||
var PATH = '/sys/class/gpio'; | ||
var cpuinfo = { | ||
@@ -21,2 +23,3 @@ v1: '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 : 0002\nSerial : 000000009a5d9c22', | ||
.withArgs('/proc/cpuinfo').yieldsAsync(null, cpuinfo.v1); | ||
sinon.spy(fs, 'unwatchFile'); | ||
}); | ||
@@ -82,29 +85,3 @@ | ||
describe('parseCpuInfo()', function() { | ||
context('using Raspberry Pi revision 1 hardware', function() { | ||
var result; | ||
beforeEach(function() { | ||
result = gpio.parseCpuinfo(cpuinfo.v1); | ||
}); | ||
it('should return the revision 0002', function() { | ||
assert.equal(result, '0002'); | ||
}); | ||
}); | ||
context('using Raspberry Pi revision 2 hardware', function() { | ||
var result; | ||
beforeEach(function() { | ||
result = gpio.parseCpuinfo(cpuinfo.v2); | ||
}); | ||
it('should return the revision 0004', function() { | ||
assert.equal(result, '0004'); | ||
}); | ||
}); | ||
}); | ||
describe('setup()', function() { | ||
@@ -139,3 +116,3 @@ context('when given an invalid channel', function() { | ||
var args0 = fs.writeFile.getCall(0).args; | ||
assert.equal(args0[0], '/sys/class/gpio/unexport'); | ||
assert.equal(args0[0], PATH + '/unexport'); | ||
assert.equal(args0[1], '1'); | ||
@@ -147,3 +124,3 @@ }); | ||
var args1 = fs.writeFile.getCall(1).args; | ||
assert.equal(args1[0], '/sys/class/gpio/export'); | ||
assert.equal(args1[0], PATH + '/export'); | ||
assert.equal(args1[1], '1'); | ||
@@ -172,3 +149,3 @@ }); | ||
var args0 = fs.writeFile.getCall(0).args; | ||
assert.equal(args0[0], '/sys/class/gpio/export'); | ||
assert.equal(args0[0], PATH + '/export'); | ||
assert.equal(args0[1], '1'); | ||
@@ -184,3 +161,3 @@ }); | ||
var args1 = fs.writeFile.getCall(1).args; | ||
assert.equal(args1[0], '/sys/class/gpio/gpio1/direction'); | ||
assert.equal(args1[0], PATH + '/gpio1/direction'); | ||
assert.equal(args1[1], 'out'); | ||
@@ -191,3 +168,3 @@ }); | ||
var args = fs.watchFile.lastCall.args; | ||
assert.equal(args[0], '/sys/class/gpio/gpio1/value'); | ||
assert.equal(args[0], PATH + '/gpio1/value'); | ||
}); | ||
@@ -203,3 +180,3 @@ }); | ||
var args = fs.writeFile.lastCall.args; | ||
assert.equal(args[0], '/sys/class/gpio/gpio1/direction'); | ||
assert.equal(args[0], PATH + '/gpio1/direction'); | ||
assert.equal(args[1], 'in'); | ||
@@ -216,3 +193,3 @@ }); | ||
var args = fs.writeFile.lastCall.args; | ||
assert.equal(args[0], '/sys/class/gpio/gpio1/direction'); | ||
assert.equal(args[0], PATH + '/gpio1/direction'); | ||
assert.equal(args[1], 'out'); | ||
@@ -254,3 +231,3 @@ }); | ||
var args = fs.writeFile.lastCall.args; | ||
assert.equal(args[0], '/sys/class/gpio/gpio1/value'); | ||
assert.equal(args[0], PATH + '/gpio1/value'); | ||
assert.equal(args[1], '1'); | ||
@@ -351,3 +328,3 @@ | ||
var args = fs.readFile.lastCall.args; | ||
assert.equal(args[0], '/sys/class/gpio/gpio1/value'); | ||
assert.equal(args[0], PATH + '/gpio1/value'); | ||
sinon.assert.calledWith(callback, null, true); | ||
@@ -360,3 +337,3 @@ }); | ||
context('when pins 1, 2, 3 have been exported', function() { | ||
var unexportPath = '/sys/class/gpio/unexport'; | ||
var unexportPath = PATH + '/unexport'; | ||
@@ -391,2 +368,14 @@ beforeEach(function(done) { | ||
it('should unwatch pin 1', function() { | ||
sinon.assert.calledWith(fs.unwatchFile, PATH + '/gpio1/value'); | ||
}); | ||
it('should unwatch pin 2', function() { | ||
sinon.assert.calledWith(fs.unwatchFile, PATH + '/gpio2/value'); | ||
}); | ||
it('should unwatch pin 3', function() { | ||
sinon.assert.calledWith(fs.unwatchFile, PATH + '/gpio3/value'); | ||
}); | ||
}); | ||
@@ -393,0 +382,0 @@ }); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31839
3
651
0