Remove @self references in favor of just @ and fix set mode to udpate the obj attr when actually changing the mode.
This commit is contained in:
parent
1fd0a13347
commit
0ae37390fe
|
@ -36,7 +36,6 @@
|
|||
LOW = 0;
|
||||
|
||||
function DigitalPin(opts) {
|
||||
this.self = this;
|
||||
this.pinNum = opts.pin;
|
||||
this.status = 'low';
|
||||
this.ready = false;
|
||||
|
@ -75,14 +74,14 @@
|
|||
DigitalPin.prototype.digitalWrite = function(value) {
|
||||
var _this = this;
|
||||
if (this.mode !== 'w') {
|
||||
this.self._setMode('w');
|
||||
this._setMode('w');
|
||||
}
|
||||
this.status = value === 1 ? 'high' : 'low';
|
||||
return FS.writeFile(this._valuePath(), value, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
|
||||
return _this.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
|
||||
} else {
|
||||
return _this.self.emit('digitalWrite', value);
|
||||
return _this.emit('digitalWrite', value);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -91,34 +90,34 @@
|
|||
var readData,
|
||||
_this = this;
|
||||
if (this.mode !== 'r') {
|
||||
this.self._setMode('r');
|
||||
this._setMode('r');
|
||||
}
|
||||
readData = null;
|
||||
return setInterval(function() {
|
||||
return FS.readFile(_this._valuePath(), function(err, data) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', "Error occurred while reading from pin " + _this.pinNum);
|
||||
return _this.emit('error', "Error occurred while reading from pin " + _this.pinNum);
|
||||
} else {
|
||||
readData = data;
|
||||
return _this.self.emit('digitalRead', data);
|
||||
return _this.emit('digitalRead', data);
|
||||
}
|
||||
});
|
||||
}, interval);
|
||||
};
|
||||
|
||||
DigitalPin.prototype.setHigh = function() {
|
||||
return this.self.digitalWrite(1);
|
||||
return this.digitalWrite(1);
|
||||
};
|
||||
|
||||
DigitalPin.prototype.setLow = function() {
|
||||
return this.self.digitalWrite(0);
|
||||
return this.digitalWrite(0);
|
||||
};
|
||||
|
||||
DigitalPin.prototype.toggle = function() {
|
||||
if (this.status === 'low') {
|
||||
return this.self.setHigh();
|
||||
return this.setHigh();
|
||||
} else {
|
||||
return this.self.setLow();
|
||||
return this.setLow();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -126,7 +125,7 @@
|
|||
var _this = this;
|
||||
return FS.writeFile(this._exportPath(), "" + this.pinNum, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', 'Error while creating pin files');
|
||||
return _this.emit('error', 'Error while creating pin files');
|
||||
} else {
|
||||
return _this._openPin();
|
||||
}
|
||||
|
@ -134,15 +133,15 @@
|
|||
};
|
||||
|
||||
DigitalPin.prototype._openPin = function() {
|
||||
this.self._setMode(this.mode, true);
|
||||
return this.self.emit('open');
|
||||
this._setMode(this.mode, true);
|
||||
return this.emit('open');
|
||||
};
|
||||
|
||||
DigitalPin.prototype._closeCallback = function(err) {
|
||||
if (err) {
|
||||
return this.self.emit('error', 'Error while closing pin files');
|
||||
return this.emit('error', 'Error while closing pin files');
|
||||
} else {
|
||||
return this.self.emit('close', this.pinNum);
|
||||
return this.emit('close', this.pinNum);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -151,6 +150,7 @@
|
|||
if (emitConnect == null) {
|
||||
emitConnect = false;
|
||||
}
|
||||
this.mode = mode;
|
||||
if (mode === 'w') {
|
||||
return FS.writeFile(this._directionPath(), GPIO_DIRECTION_WRITE, function(err) {
|
||||
return _this._setModeCallback(err, emitConnect);
|
||||
|
@ -164,11 +164,11 @@
|
|||
|
||||
DigitalPin.prototype._setModeCallback = function(err, emitConnect) {
|
||||
if (err) {
|
||||
return this.self.emit('error', "Setting up pin direction failed");
|
||||
return this.emit('error', "Setting up pin direction failed");
|
||||
} else {
|
||||
this.ready = true;
|
||||
if (emitConnect) {
|
||||
return this.self.emit('connect', mode);
|
||||
return this.emit('connect', this.mode);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -25,7 +25,6 @@ namespace 'Cylon.IO', ->
|
|||
LOW = 0
|
||||
|
||||
constructor: (opts) ->
|
||||
@self = this
|
||||
@pinNum = opts.pin
|
||||
@status = 'low'
|
||||
@ready = false
|
||||
|
@ -51,66 +50,67 @@ namespace 'Cylon.IO', ->
|
|||
@_closeCallback(false)
|
||||
|
||||
digitalWrite: (value) ->
|
||||
@self._setMode('w') unless @mode == 'w'
|
||||
@_setMode('w') unless @mode == 'w'
|
||||
@status = if (value == 1) then 'high' else 'low'
|
||||
|
||||
FS.writeFile(@_valuePath(), value, (err) =>
|
||||
if (err)
|
||||
@self.emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
|
||||
@emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
|
||||
else
|
||||
@self.emit('digitalWrite', value)
|
||||
@emit('digitalWrite', value)
|
||||
)
|
||||
|
||||
# Reads the pin input every interval amount of time:
|
||||
# params:
|
||||
# interval: amount in miliseconds
|
||||
digitalRead: (interval) ->
|
||||
@self._setMode('r') unless @mode == 'r'
|
||||
@_setMode('r') unless @mode == 'r'
|
||||
readData = null
|
||||
|
||||
setInterval(() =>
|
||||
FS.readFile(@_valuePath(), (err, data) =>
|
||||
if err
|
||||
@self.emit('error', "Error occurred while reading from pin #{ @pinNum }")
|
||||
@emit('error', "Error occurred while reading from pin #{ @pinNum }")
|
||||
else
|
||||
readData = data
|
||||
@self.emit('digitalRead', data)
|
||||
@emit('digitalRead', data)
|
||||
)
|
||||
, interval)
|
||||
|
||||
setHigh: ->
|
||||
@self.digitalWrite(1)
|
||||
@digitalWrite(1)
|
||||
|
||||
setLow: ->
|
||||
@self.digitalWrite(0)
|
||||
@digitalWrite(0)
|
||||
|
||||
toggle: ->
|
||||
if @status == 'low'
|
||||
@self.setHigh()
|
||||
@setHigh()
|
||||
else
|
||||
@self.setLow()
|
||||
@setLow()
|
||||
|
||||
# Creates the GPIO file to read/write from
|
||||
_createGPIOPin: () ->
|
||||
FS.writeFile(@_exportPath(), "#{ @pinNum }", (err) =>
|
||||
if(err)
|
||||
@self.emit('error', 'Error while creating pin files')
|
||||
@emit('error', 'Error while creating pin files')
|
||||
else
|
||||
@_openPin()
|
||||
)
|
||||
|
||||
_openPin: () ->
|
||||
@self._setMode(@mode, true)
|
||||
@self.emit('open')
|
||||
@_setMode(@mode, true)
|
||||
@emit('open')
|
||||
|
||||
_closeCallback: (err) ->
|
||||
if(err)
|
||||
@self.emit('error', 'Error while closing pin files')
|
||||
@emit('error', 'Error while closing pin files')
|
||||
else
|
||||
@self.emit('close', @pinNum)
|
||||
@emit('close', @pinNum)
|
||||
|
||||
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
|
||||
_setMode: (mode, emitConnect = false) ->
|
||||
@mode = mode
|
||||
if mode == 'w'
|
||||
FS.writeFile(@_directionPath(), GPIO_DIRECTION_WRITE, (err) => @_setModeCallback(err, emitConnect) )
|
||||
else if mode =='r'
|
||||
|
@ -118,10 +118,10 @@ namespace 'Cylon.IO', ->
|
|||
|
||||
_setModeCallback: (err, emitConnect) ->
|
||||
if (err)
|
||||
@self.emit('error', "Setting up pin direction failed")
|
||||
@emit('error', "Setting up pin direction failed")
|
||||
else
|
||||
@ready = true
|
||||
@self.emit('connect', mode) if emitConnect
|
||||
@emit('connect', @mode) if emitConnect
|
||||
|
||||
_directionPath: () ->
|
||||
"#{ @_pinPath() }/direction"
|
||||
|
|
Loading…
Reference in New Issue