Refactor and clean up Cylon.IO.DigitalPin class

This commit is contained in:
Andrew Stewart 2014-03-14 16:06:32 -07:00
parent e7f96b8a6c
commit 70dde8148c
1 changed files with 66 additions and 80 deletions

View File

@ -6,11 +6,11 @@
* Licensed under the Apache 2.0 license. * Licensed under the Apache 2.0 license.
*/ */
'use strict'; "use strict";
var FS = require('fs'); var FS = require('fs'),
var EventEmitter = require('events').EventEmitter; EventEmitter = require('events').EventEmitter,
var namespace = require('node-namespace'); namespace = require('node-namespace');
// DigitalPin class offers an interface with the Linux GPIO system present in // DigitalPin class offers an interface with the Linux GPIO system present in
// single-board computers such as a Raspberry Pi, or a BeagleBone // single-board computers such as a Raspberry Pi, or a BeagleBone
@ -19,8 +19,10 @@ namespace("Cylon.IO", function() {
subclass(DigitalPin, klass); subclass(DigitalPin, klass);
var GPIO_PATH = "/sys/class/gpio"; var GPIO_PATH = "/sys/class/gpio";
var GPIO_READ = "in"; var GPIO_READ = "in";
var GPIO_WRITE = "out"; var GPIO_WRITE = "out";
var HIGH = 1; var HIGH = 1;
var LOW = 0; var LOW = 0;
@ -32,47 +34,45 @@ namespace("Cylon.IO", function() {
} }
DigitalPin.prototype.connect = function(mode) { DigitalPin.prototype.connect = function(mode) {
var _this = this; var self = this;
if (mode == null) {
mode = null; if (this.mode == null) { this.mode = mode; }
}
if (this.mode == null) { FS.exists(this._pinPath(), function(exists) {
this.mode = mode; exists ? self._openPin() : self._createGPIOPin();
}
return FS.exists(this._pinPath(), function(exists) {
if (exists) {
return _this._openPin();
} else {
return _this._createGPIOPin();
}
}); });
}; };
DigitalPin.prototype.close = function() { DigitalPin.prototype.close = function() {
var _this = this; var self = this,
return FS.writeFile(this._unexportPath(), "" + this.pinNum, function(err) { num = this.pinNum.toString();
return _this._closeCallback(err);
FS.writeFile(this._unexportPath(), num, function(err) {
self._closeCallback(err);
}); });
}; };
DigitalPin.prototype.closeSync = function() { DigitalPin.prototype.closeSync = function() {
FS.writeFileSync(this._unexportPath(), "" + this.pinNum); var num = this.pinNum.toString();
return this._closeCallback(false);
FS.writeFileSync(this._unexportPath(), num);
this._closeCallback(false);
}; };
DigitalPin.prototype.digitalWrite = function(value) { DigitalPin.prototype.digitalWrite = function(value) {
var _this = this; if (this.mode !== 'w') { this._setMode('w'); }
if (this.mode !== 'w') {
this._setMode('w'); var self = this;
}
this.status = value === 1 ? 'high' : 'low'; this.status = value === 1 ? 'high' : 'low';
FS.writeFile(this._valuePath(), value, function(err) { FS.writeFile(this._valuePath(), value, function(err) {
if (err) { if (err) {
return _this.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum); self.emit('error', "Error occurred while writing value " + value + " to pin " + self.pinNum);
} else { } else {
return _this.emit('digitalWrite', value); self.emit('digitalWrite', value);
} }
}); });
return value; return value;
}; };
@ -83,112 +83,98 @@ namespace("Cylon.IO", function() {
// //
// Returns the defined interval // Returns the defined interval
DigitalPin.prototype.digitalRead = function(interval) { DigitalPin.prototype.digitalRead = function(interval) {
var readData, var self = this;
_this = this;
if (this.mode !== 'r') { if (this.mode !== 'r') { this._setMode('r'); }
this._setMode('r');
} every(interval, function() {
readData = null; FS.readFile(self._valuePath(), function(err, data) {
return every(interval, function() {
return FS.readFile(_this._valuePath(), function(err, data) {
if (err) { if (err) {
return _this.emit('error', "Error occurred while reading from pin " + _this.pinNum); var error = "Error occurred while reading from pin " + self.pinNum;
self.emit('error', error);
} else { } else {
readData = parseInt(data.toString()); var readData = parseInt(data.toString());
return _this.emit('digitalRead', readData); self.emit('digitalRead', readData);
} }
}); });
}); });
}; };
DigitalPin.prototype.setHigh = function() { DigitalPin.prototype.setHigh = function() { return this.digitalWrite(1); };
return this.digitalWrite(1); DigitalPin.prototype.setLow = function() { return this.digitalWrite(0); };
};
DigitalPin.prototype.setLow = function() {
return this.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() { DigitalPin.prototype.toggle = function() {
if (this.status === 'low') { return (this.status === 'low') ? this.setHigh() : this.setLow();
return this.setHigh();
} else {
return this.setLow();
}
}; };
// Creates the GPIO file to read/write from // Creates the GPIO file to read/write from
DigitalPin.prototype._createGPIOPin = function() { DigitalPin.prototype._createGPIOPin = function() {
var _this = this; var self = this,
return FS.writeFile(this._exportPath(), "" + this.pinNum, function(err) { num = this.pinNum.toString();
FS.writeFile(this._exportPath(), num, function(err) {
if (err) { if (err) {
return _this.emit('error', 'Error while creating pin files'); self.emit('error', 'Error while creating pin files');
} else { } else {
return _this._openPin(); self._openPin();
} }
}); });
}; };
DigitalPin.prototype._openPin = function() { DigitalPin.prototype._openPin = function() {
this._setMode(this.mode, true); this._setMode(this.mode, true);
return this.emit('open'); this.emit('open');
}; };
DigitalPin.prototype._closeCallback = function(err) { DigitalPin.prototype._closeCallback = function(err) {
if (err) { if (err) {
return this.emit('error', 'Error while closing pin files'); this.emit('error', 'Error while closing pin files');
} else { } else {
return this.emit('close', this.pinNum); this.emit('close', this.pinNum);
} }
}; };
// Sets the mode for the GPIO pin by writing the correct values to the pin reference files // Sets the mode for the GPIO pin by writing the correct values to the pin reference files
DigitalPin.prototype._setMode = function(mode, emitConnect) { DigitalPin.prototype._setMode = function(mode, emitConnect) {
var _this = this; if (emitConnect == null) { emitConnect = false; }
if (emitConnect == null) {
emitConnect = false;
}
this.mode = mode; this.mode = mode;
if (mode === 'w') {
return FS.writeFile(this._directionPath(), GPIO_WRITE, function(err) { var self = this,
return _this._setModeCallback(err, emitConnect); data = (mode === 'w') ? GPIO_WRITE : GPIO_READ;
});
} else if (mode === 'r') { FS.writeFile(this._directionPath(), data, function(err) {
return FS.writeFile(this._directionPath(), GPIO_READ, function(err) { self._setModeCallback(err, emitConnect);
return _this._setModeCallback(err, emitConnect); });
});
}
}; };
DigitalPin.prototype._setModeCallback = function(err, emitConnect) { DigitalPin.prototype._setModeCallback = function(err, emitConnect) {
if (err) { if (err) {
return this.emit('error', "Setting up pin direction failed"); this.emit('error', "Setting up pin direction failed");
} else { } else {
this.ready = true; this.ready = true;
if (emitConnect) { if (emitConnect) { this.emit('connect', this.mode); }
return this.emit('connect', this.mode);
}
} }
}; };
DigitalPin.prototype._directionPath = function() { DigitalPin.prototype._directionPath = function() {
return "" + (this._pinPath()) + "/direction"; return this._pinPath() + "/direction";
}; };
DigitalPin.prototype._valuePath = function() { DigitalPin.prototype._valuePath = function() {
return "" + (this._pinPath()) + "/value"; return this._pinPath() + "/value";
}; };
DigitalPin.prototype._pinPath = function() { DigitalPin.prototype._pinPath = function() {
return "" + GPIO_PATH + "/gpio" + this.pinNum; return GPIO_PATH + "/gpio" + this.pinNum;
}; };
DigitalPin.prototype._exportPath = function() { DigitalPin.prototype._exportPath = function() {
return "" + GPIO_PATH + "/export"; return GPIO_PATH + "/export";
}; };
DigitalPin.prototype._unexportPath = function() { DigitalPin.prototype._unexportPath = function() {
return "" + GPIO_PATH + "/unexport"; return GPIO_PATH + "/unexport";
}; };
return DigitalPin; return DigitalPin;