Skip to content

Commit

Permalink
direction: Use constants vs Strings
Browse files Browse the repository at this point in the history
Aligned to IoT.js's GPIO API

Change-Id: I430dd3c0ea141e2b677826f61d7069f7a4f66750
Signed-off-by: Philippe Coval <p.coval@samsung.com>
  • Loading branch information
rzr committed Sep 27, 2018
1 parent 2a811af commit 4e0f5a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var gpio = require("gpio");
var gpio4 = gpio.export(4, {
// When you export a pin, the default direction is out. This allows you to set
// the pin value to either LOW or HIGH (3.3V) from your program.
direction: 'out',
direction: gpio.DIRECTION.OUT,

// set the time interval (ms) between each read when watching for value changes
// note: this is default to 100, setting value too low will cause high CPU usage
Expand All @@ -61,13 +61,13 @@ var gpio4 = gpio.export(4, {
});
```

### Header direction "in"
### Header direction IN

If you plan to set the header voltage externally, use direction `in` and read value from your program.
```js
var gpio = require("gpio");
var gpio4 = gpio.export(4, {
direction: "in",
direction: gpio.DIRECTION.IN,
ready: function() {
}
});
Expand Down Expand Up @@ -120,8 +120,8 @@ gpio4.removeListener("change", processPin4);
gpio4.removeAllListeners("change");

// you can also manually change the direction anytime after instantiation
gpio4.setDirection("out");
gpio4.setDirection("in");
gpio4.setDirection(gpio.DIRECTION.OUT);
gpio4.setDirection(gpio.DIRECTION.IN);
```

## Example
Expand Down
21 changes: 12 additions & 9 deletions lib/gpio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ var EventEmitter = require('events').EventEmitter;
var exists = fs.existsSync || path.exists;

var gpiopath = '/sys/class/gpio/';
var DIRECTION = { 'IN': 'in', 'OUT': 'out' };

var logError = function(e) { if(e) console.log(e.code, e.action, e.path); };
var logMessage = function() { if (exports.logging) console.log.apply(console, arguments); };


var _write = function(str, file, fn, override) {
if(typeof fn !== "function") fn = logError;
fs.writeFile(file, str, function(err) {
Expand Down Expand Up @@ -111,7 +113,7 @@ var GPIO = function(headerNum, opts) {
this.export(function() {
var onSuccess = function() {
self.setDirection(dir, function () {
if ( dir === "out") {
if ( dir === DIRECTION.OUT) {
if(typeof opts.ready === 'function') opts.ready.call(self);
} else {
self.value = undefined;
Expand Down Expand Up @@ -157,20 +159,20 @@ GPIO.prototype.unexport = function(fn) {


/**
* Sets direction, default is "out"
* Sets direction, default is OUT
*/
GPIO.prototype.setDirection = function(dir, fn) {
var self = this, path = this.PATH.DIRECTION;
if(typeof dir !== "string" || dir !== "in") dir = "out";
if(typeof dir !== "string" || dir !== DIRECTION.IN) dir = DIRECTION.OUT;
this.direction = dir;

logMessage('Setting direction "' + dir + '" on gpio' + this.headerNum);

function watch () {
if(dir === 'in') {
if(dir === DIRECTION.IN) {
if (!self.valueWatcher) {
// watch for value changes only for direction "in"
// since we manually trigger event for "out" direction when setting value
// watch for value changes only for direction IN
// since we manually trigger event for OUT direction when setting value
self.valueWatcher = new FileWatcher(self.PATH.VALUE, self.interval, function(val) {
val = parseInt(val, 10);
self.value = val;
Expand All @@ -179,7 +181,7 @@ GPIO.prototype.setDirection = function(dir, fn) {
});
}
} else {
// if direction is "out", try to clear the valueWatcher
// if direction is OUT, try to clear the valueWatcher
if(self.valueWatcher) {
self.valueWatcher.stop();
self.valueWatcher = null;
Expand Down Expand Up @@ -212,7 +214,7 @@ GPIO.prototype.setDirection = function(dir, fn) {
GPIO.prototype._get = function(fn) {
var self = this, currVal = this.value;

if(this.direction === 'out') return currVal;
if(this.direction === DIRECTION.OUT) return currVal;

_read(this.PATH.VALUE, function(val) {
val = parseInt(val, 10);
Expand All @@ -234,7 +236,7 @@ GPIO.prototype.set = function(v, fn) {

// if direction is out, just emit change event since we can reliably predict
// if the value has changed; we don't have to rely on watching a file
if(this.direction === 'out') {
if(this.direction === DIRECTION.OUT) {
if(this.value !== v) {
_write(v, this.PATH.VALUE, function() {
self.value = v;
Expand All @@ -251,5 +253,6 @@ GPIO.prototype.reset = function(fn) { this.set(0, fn); };

exports.logging = false;
exports.export = function(headerNum, direction) { return new GPIO(headerNum, direction); };
exports.DIRECTION = DIRECTION;
exports.unexport = _unexport;

8 changes: 4 additions & 4 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('GPIO', function() {

before(function(done) {
gpio4 = gpio.export(4, {
direction: 'out',
direction: gpio.DIRECTION.OUT,
ready: done
});
});
Expand All @@ -34,7 +34,7 @@ describe('GPIO', function() {
describe('initializing', function() {
it('should open specified header', function(done) {
read('/sys/class/gpio/gpio4/direction', function(val) {
assert.equal(rmws(val), 'out');
assert.equal(rmws(val), gpio.DIRECTION.OUT);
done();
});
});
Expand Down Expand Up @@ -81,15 +81,15 @@ describe('GPIO', function() {
});

// For these tests, make sure header 4 is connected to header 25
// header 25 is exported with direction "out" and header 4 is used
// header 25 is exported with direction OUT and header 4 is used
// to simulate a hardware interrupt
describe('Header Direction In', function() {

var gpio25;

before(function(done) {
gpio25 = gpio.export(25, {
direction: 'in',
direction: gpio.DIRECTION.IN,
ready: done
});
});
Expand Down

0 comments on commit 4e0f5a8

Please sign in to comment.