Clean up DigitalPin class

This commit is contained in:
Andrew Stewart 2014-08-29 12:59:44 -07:00
parent c063d254e0
commit 897668f3ab
1 changed files with 39 additions and 37 deletions

View File

@ -33,21 +33,19 @@ var DigitalPin = module.exports = function DigitalPin(opts) {
Utils.subclass(DigitalPin, EventEmitter);
DigitalPin.prototype.connect = function(mode) {
var _this = this;
if (this.mode == null) { this.mode = mode; }
if (this.mode == null) {
this.mode = mode;
}
FS.exists(this._pinPath(), function(exists) {
exists ? _this._openPin() : _this._createGPIOPin();
});
exists ? this._openPin() : this._createGPIOPin();
}.bind(this));
};
DigitalPin.prototype.close = function() {
var _this = this;
FS.writeFile(this._unexportPath(), this.pinNum, function(err) {
_this._closeCallback(err);
});
this._closeCallback(err);
}.bind(this));
};
DigitalPin.prototype.closeSync = function() {
@ -56,18 +54,19 @@ DigitalPin.prototype.closeSync = function() {
};
DigitalPin.prototype.digitalWrite = function(value) {
if (this.mode !== 'w') { this._setMode('w'); }
if (this.mode !== 'w') {
this._setMode('w');
}
var _this = this;
this.status = value === 1 ? 'high' : 'low';
FS.writeFile(this._valuePath(), value, function(err) {
if (err) {
_this.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
this.emit('error', "Error occurred while writing value " + value + " to pin " + this.pinNum);
} else {
_this.emit('digitalWrite', value);
this.emit('digitalWrite', value);
}
});
}.bind(this));
return value;
};
@ -79,25 +78,28 @@ DigitalPin.prototype.digitalWrite = function(value) {
//
// Returns the defined interval
DigitalPin.prototype.digitalRead = function(interval) {
var _this = this;
if (this.mode !== 'r') { this._setMode('r'); }
Utils.every(interval, function() {
FS.readFile(_this._valuePath(), function(err, data) {
FS.readFile(this._valuePath(), function(err, data) {
if (err) {
var error = "Error occurred while reading from pin " + _this.pinNum;
_this.emit('error', error);
var error = "Error occurred while reading from pin " + this.pinNum;
this.emit('error', error);
} else {
var readData = parseInt(data.toString());
_this.emit('digitalRead', readData);
this.emit('digitalRead', readData);
}
});
});
}.bind(this));
}.bind(this));
};
DigitalPin.prototype.setHigh = function() { return this.digitalWrite(1); };
DigitalPin.prototype.setLow = function() { return this.digitalWrite(0); };
DigitalPin.prototype.setHigh = function() {
return this.digitalWrite(1);
};
DigitalPin.prototype.setLow = function() {
return this.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
return (this.status === 'low') ? this.setHigh() : this.setLow();
@ -105,15 +107,13 @@ DigitalPin.prototype.toggle = function() {
// Creates the GPIO file to read/write from
DigitalPin.prototype._createGPIOPin = function() {
var _this = this;
FS.writeFile(this._exportPath(), this.pinNum, function(err) {
if (err) {
_this.emit('error', 'Error while creating pin files');
this.emit('error', 'Error while creating pin files');
} else {
_this._openPin();
this._openPin();
}
});
}.bind(this));
};
DigitalPin.prototype._openPin = function() {
@ -135,20 +135,22 @@ DigitalPin.prototype._setMode = function(mode, emitConnect) {
this.mode = mode;
var _this = this,
data = (mode === 'w') ? GPIO_WRITE : GPIO_READ;
var data = (mode === 'w') ? GPIO_WRITE : GPIO_READ;
FS.writeFile(this._directionPath(), data, function(err) {
_this._setModeCallback(err, emitConnect);
});
this._setModeCallback(err, emitConnect);
}.bind(this));
};
DigitalPin.prototype._setModeCallback = function(err, emitConnect) {
if (err) {
this.emit('error', "Setting up pin direction failed");
} else {
this.ready = true;
if (emitConnect) { this.emit('connect', this.mode); }
return this.emit('error', "Setting up pin direction failed");
}
this.ready = true;
if (emitConnect) {
this.emit('connect', this.mode);
}
};