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