Skip to content

Commit 944007b

Browse files
committed
feat: remove deprecated fs.exists function; enable setting the gpio path by setting up configuration for the whole module
1 parent 295860a commit 944007b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/gpio.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var fs = require('fs');
22
var util = require('util');
33
var path = require('path');
44
var EventEmitter = require('events').EventEmitter;
5-
var exists = fs.exists || path.exists;
65

76
var gpiopath = '/sys/class/gpio/';
87

@@ -40,12 +39,15 @@ var _unexport = function(number, fn) {
4039
if(typeof fn === 'function') fn();
4140
}, 1);
4241
};
42+
4343
var _export = function(n, fn) {
44-
if(exists(gpiopath + 'gpio'+n)) {
44+
try {
45+
fs.accessSync(gpiopath + 'gpio'+n)
4546
// already exported, unexport and export again
4647
logMessage('Header already exported');
4748
_unexport(n, function() { _export(n, fn); });
48-
} else {
49+
50+
} catch (e) {
4951
logMessage('Exporting gpio' + n);
5052
_write(n, gpiopath + 'export', function(err) {
5153
// if there's an error when exporting, unexport and repeat
@@ -61,7 +63,7 @@ var _testwrite = function(file, fn) {
6163
return;
6264
}
6365
fs.close(fd, function(err){
64-
fn(true, null);
66+
fn(true, null);
6567
});
6668
});
6769
};
@@ -202,7 +204,7 @@ GPIO.prototype.setDirection = function(dir, fn) {
202204
self.emit('directionChange', dir);
203205
}
204206
}, 1);
205-
207+
206208
});
207209
};
208210

@@ -252,4 +254,6 @@ GPIO.prototype.reset = function(fn) { this.set(0, fn); };
252254
exports.logging = false;
253255
exports.export = function(headerNum, direction) { return new GPIO(headerNum, direction); };
254256
exports.unexport = _unexport;
255-
257+
exports.setConfig = function (config) {
258+
gpiopath = config.gpiopath || gpiopath
259+
}

0 commit comments

Comments
 (0)