Change event triggers in callbacks to fat arrows to keep obj scope.
This commit is contained in:
parent
87a75745dd
commit
33f8c95edc
|
@ -36,81 +36,86 @@
|
|||
LOW = 0;
|
||||
|
||||
function DigitalPin(opts) {
|
||||
this.self = this;
|
||||
this.pinNum = opts.pin;
|
||||
this.status = 'low';
|
||||
this.ready = false;
|
||||
}
|
||||
|
||||
DigitalPin.prototype.open = function(mode) {
|
||||
var _this = this;
|
||||
return FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum, function(err) {
|
||||
if (!err) {
|
||||
this.emit('create');
|
||||
return this._setMode(opts.mode);
|
||||
_this.self.emit('create');
|
||||
return _this.self._setMode(opts.mode);
|
||||
} else {
|
||||
console.log('Error while creating pin files ...');
|
||||
return this.emit('error', 'Error while creating pin files');
|
||||
return _this.self.emit('error', 'Error while creating pin files');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
DigitalPin.prototype.digitalWrite = function(value) {
|
||||
var _this = this;
|
||||
if (this.mode !== 'w') {
|
||||
this._setMode('w');
|
||||
this.self._setMode('w');
|
||||
}
|
||||
this.status = value === 1 ? 'high' : 'low';
|
||||
return FS.writeFile(this.pinFile, value, function(err) {
|
||||
if (err) {
|
||||
console.log('ERROR occurred while writing to the Pin File');
|
||||
return this.emit('error', "Error occurred while writing value " + value + " to pin " + this.pinNum);
|
||||
return _this.self.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
|
||||
} else {
|
||||
console.log('Pin File written successfully');
|
||||
return this.emit('digitalWrite', value);
|
||||
return _this.self.emit('digitalWrite', value);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
DigitalPin.prototype.digitalRead = function() {
|
||||
var readData;
|
||||
var readData,
|
||||
_this = this;
|
||||
if (this.mode !== 'r') {
|
||||
this._setMode('r');
|
||||
this.self._setMode('r');
|
||||
}
|
||||
readData = null;
|
||||
FS.readFile(this.pinFile, function(err, data) {
|
||||
if (err) {
|
||||
console.log('ERROR occurred while reading from the Pin');
|
||||
return this.emit('error', "Error occurred while reading from pin " + this.pinNum);
|
||||
return _this.self.emit('error', "Error occurred while reading from pin " + _this.pinNum);
|
||||
} else {
|
||||
readData = data;
|
||||
return this.emit('read', data);
|
||||
return _this.self.emit('read', data);
|
||||
}
|
||||
});
|
||||
return readData;
|
||||
};
|
||||
|
||||
DigitalPin.prototype._setMode = function(mode) {
|
||||
var _this = this;
|
||||
this.mode = mode;
|
||||
if (this.mode === 'w') {
|
||||
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_WRITE, function(err) {
|
||||
if (!err) {
|
||||
console.log('Pin mode(direction) setup...');
|
||||
this.pinFile = "" + GPIO_PATH + "/gpio" + this.pinNum + "/value";
|
||||
this.ready = true;
|
||||
return this.emit('open', mode);
|
||||
_this.pinFile = "" + GPIO_PATH + "/gpio" + _this.pinNum + "/value";
|
||||
_this.ready = true;
|
||||
return _this.self.emit('open', mode);
|
||||
} else {
|
||||
console.log('Error occurred while settingup pin mode(direction)...');
|
||||
return this.emit('error', "Setting up pin direction failed");
|
||||
return _this.self.emit('error', "Setting up pin direction failed");
|
||||
}
|
||||
});
|
||||
} else if (mode === 'r') {
|
||||
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_READ, function(err) {
|
||||
if (!err) {
|
||||
console.log('Pin mode(direction) setup...');
|
||||
this.pinFile = "" + GPIO_PATH + "/gpio" + this.pinNum + "/value";
|
||||
this.ready = true;
|
||||
return this.emit('open', mode);
|
||||
_this.pinFile = "" + GPIO_PATH + "/gpio" + _this.pinNum + "/value";
|
||||
_this.ready = true;
|
||||
return _this.self.emit('open', mode);
|
||||
} else {
|
||||
console.log('Error occurred while settingup pin mode(direction)...');
|
||||
return this.emit('error', "Setting up pin direction failed");
|
||||
return _this.self.emit('error', "Setting up pin direction failed");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -118,9 +123,9 @@
|
|||
|
||||
DigitalPin.prototype.toggle = function() {
|
||||
if (this.status === 'low') {
|
||||
return this.digitalWrite(1);
|
||||
return this.self.digitalWrite(1);
|
||||
} else {
|
||||
return this.digitalWrite(0);
|
||||
return this.self.digitalWrite(0);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -129,4 +134,6 @@
|
|||
})(EventEmitter);
|
||||
});
|
||||
|
||||
module.exports = Cylon.IO.DigitalPin;
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require('../dist/digital-pin')
|
||||
DigitalPin = require('../dist/digital-pin')
|
||||
|
||||
pin4 = new Cylon.IO.DigitalPin(pin: 4)
|
||||
pin4 = new DigitalPin(pin: 4)
|
||||
|
||||
pin4.on('create', (data) ->
|
||||
console.log("Pin files have been created")
|
||||
|
|
|
@ -25,46 +25,47 @@ namespace 'Cylon.IO', ->
|
|||
LOW = 0
|
||||
|
||||
constructor: (opts) ->
|
||||
@self = this
|
||||
@pinNum = opts.pin
|
||||
@status = 'low'
|
||||
@ready = false
|
||||
|
||||
open: (mode) ->
|
||||
# Creates the GPIO file to read/write from
|
||||
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) ->
|
||||
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) =>
|
||||
unless(err)
|
||||
@emit('create')
|
||||
@_setMode(opts.mode)
|
||||
@self.emit('create')
|
||||
@self._setMode(opts.mode)
|
||||
else
|
||||
console.log('Error while creating pin files ...')
|
||||
@emit('error', 'Error while creating pin files')
|
||||
@self.emit('error', 'Error while creating pin files')
|
||||
)
|
||||
|
||||
|
||||
digitalWrite: (value) ->
|
||||
@_setMode('w') unless @mode == 'w'
|
||||
@self._setMode('w') unless @mode == 'w'
|
||||
@status = if (value == 1) then 'high' else 'low'
|
||||
|
||||
FS.writeFile(@pinFile, value, (err) ->
|
||||
FS.writeFile(@pinFile, value, (err) =>
|
||||
if (err)
|
||||
console.log('ERROR occurred while writing to the Pin File')
|
||||
@emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
|
||||
@self.emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
|
||||
else
|
||||
console.log('Pin File written successfully')
|
||||
@emit('digitalWrite', value)
|
||||
@self.emit('digitalWrite', value)
|
||||
)
|
||||
|
||||
digitalRead: ->
|
||||
@_setMode('r') unless @mode == 'r'
|
||||
@self._setMode('r') unless @mode == 'r'
|
||||
readData = null
|
||||
|
||||
FS.readFile(@pinFile, (err, data) ->
|
||||
FS.readFile(@pinFile, (err, data) =>
|
||||
if err
|
||||
console.log('ERROR occurred while reading from the Pin')
|
||||
@emit('error', "Error occurred while reading from pin #{ @pinNum }")
|
||||
@self.emit('error', "Error occurred while reading from pin #{ @pinNum }")
|
||||
else
|
||||
readData = data
|
||||
@emit('read', data)
|
||||
@self.emit('read', data)
|
||||
)
|
||||
|
||||
readData
|
||||
|
@ -73,30 +74,33 @@ namespace 'Cylon.IO', ->
|
|||
_setMode: (mode) ->
|
||||
@mode = mode
|
||||
if @mode == 'w'
|
||||
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) ->
|
||||
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) =>
|
||||
unless (err)
|
||||
console.log('Pin mode(direction) setup...')
|
||||
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
|
||||
@ready = true
|
||||
@emit('open', mode)
|
||||
@self.emit('open', mode)
|
||||
else
|
||||
console.log('Error occurred while settingup pin mode(direction)...')
|
||||
@emit('error', "Setting up pin direction failed")
|
||||
@self.emit('error', "Setting up pin direction failed")
|
||||
)
|
||||
else if mode =='r'
|
||||
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) ->
|
||||
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) =>
|
||||
unless (err)
|
||||
console.log('Pin mode(direction) setup...')
|
||||
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
|
||||
@ready = true
|
||||
@emit('open', mode)
|
||||
@self.emit('open', mode)
|
||||
else
|
||||
console.log('Error occurred while settingup pin mode(direction)...')
|
||||
@emit('error', "Setting up pin direction failed")
|
||||
@self.emit('error', "Setting up pin direction failed")
|
||||
)
|
||||
|
||||
toggle: ->
|
||||
if @status == 'low'
|
||||
@digitalWrite(1)
|
||||
@self.digitalWrite(1)
|
||||
else
|
||||
@digitalWrite(0)
|
||||
@self.digitalWrite(0)
|
||||
|
||||
module.exports = Cylon.IO.DigitalPin
|
||||
|
||||
|
|
Loading…
Reference in New Issue