Skip to content

Commit

Permalink
feat: remove deprecated fs.exists function; enable setting the gpio p…
Browse files Browse the repository at this point in the history
…ath by setting up configuration for the whole module
  • Loading branch information
makeros committed Oct 8, 2019
1 parent 295860a commit 944007b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/gpio.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var fs = require('fs');
var util = require('util');
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var exists = fs.exists || path.exists;

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

Expand Down Expand Up @@ -40,12 +39,15 @@ var _unexport = function(number, fn) {
if(typeof fn === 'function') fn();
}, 1);
};

var _export = function(n, fn) {
if(exists(gpiopath + 'gpio'+n)) {
try {
fs.accessSync(gpiopath + 'gpio'+n)
// already exported, unexport and export again
logMessage('Header already exported');
_unexport(n, function() { _export(n, fn); });
} else {

} catch (e) {
logMessage('Exporting gpio' + n);
_write(n, gpiopath + 'export', function(err) {
// if there's an error when exporting, unexport and repeat
Expand All @@ -61,7 +63,7 @@ var _testwrite = function(file, fn) {
return;
}
fs.close(fd, function(err){
fn(true, null);
fn(true, null);
});
});
};
Expand Down Expand Up @@ -202,7 +204,7 @@ GPIO.prototype.setDirection = function(dir, fn) {
self.emit('directionChange', dir);
}
}, 1);

});
};

Expand Down Expand Up @@ -252,4 +254,6 @@ GPIO.prototype.reset = function(fn) { this.set(0, fn); };
exports.logging = false;
exports.export = function(headerNum, direction) { return new GPIO(headerNum, direction); };
exports.unexport = _unexport;

exports.setConfig = function (config) {
gpiopath = config.gpiopath || gpiopath
}

0 comments on commit 944007b

Please sign in to comment.