Added close for GPIO and updated negative ifs

This commit is contained in:
Edgar O Silva 2013-11-04 23:06:11 -06:00
parent 33f8c95edc
commit 3dec21c459
3 changed files with 48 additions and 36 deletions

40
dist/digital-pin.js vendored
View File

@ -45,12 +45,24 @@
DigitalPin.prototype.open = function(mode) {
var _this = this;
return FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum, function(err) {
if (!err) {
_this.self.emit('create');
return _this.self._setMode(opts.mode);
} else {
if (err) {
console.log('Error while creating pin files ...');
return _this.self.emit('error', 'Error while creating pin files');
} else {
_this.self.emit('open');
return _this.self._setMode(mode);
}
});
};
DigitalPin.prototype.close = function() {
var _this = this;
return FS.writeFile("" + GPIO_PATH + "/unexport", "" + this.pinNum, function(err) {
if (err) {
console.log('Error while closing pin files ...');
return _this.self.emit('error', 'Error while closing pin files');
} else {
return _this.self.emit('close');
}
});
};
@ -96,26 +108,24 @@
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...');
if (err) {
console.log('Error occurred while settingup pin mode(direction)...');
return _this.self.emit('error', "Setting up pin direction failed");
} else {
_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.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...');
if (err) {
console.log('Error occurred while settingup pin mode(direction)...');
return _this.self.emit('error', "Setting up pin direction failed");
} else {
_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.self.emit('error', "Setting up pin direction failed");
}
});
}
@ -134,6 +144,4 @@
})(EventEmitter);
});
module.exports = Cylon.IO.DigitalPin;
}).call(this);

View File

@ -1,13 +1,13 @@
DigitalPin = require('../dist/digital-pin')
require('../dist/digital-pin')
pin4 = new DigitalPin(pin: 4)
pin4 = new Cylon.IO.DigitalPin(pin: 4)
pin4.on('create', (data) ->
console.log("Pin files have been created")
)
pin4.on('open', (data) ->
console.log("Pin ready for writing!")
console.log("Pin mode has been setup!")
pin4.digitalWrite(1)
)

View File

@ -33,15 +33,24 @@ namespace 'Cylon.IO', ->
open: (mode) ->
# Creates the GPIO file to read/write from
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) =>
unless(err)
@self.emit('create')
@self._setMode(opts.mode)
else
if(err)
console.log('Error while creating pin files ...')
@self.emit('error', 'Error while creating pin files')
else
@self.emit('open')
@self._setMode(mode)
)
close: ->
FS.writeFile("#{ GPIO_PATH }/unexport", "#{ @pinNum }", (err) =>
if(err)
console.log('Error while closing pin files ...')
@self.emit('error', 'Error while closing pin files')
else
@self.emit('close')
)
digitalWrite: (value) ->
@self._setMode('w') unless @mode == 'w'
@status = if (value == 1) then 'high' else 'low'
@ -75,25 +84,23 @@ namespace 'Cylon.IO', ->
@mode = mode
if @mode == 'w'
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) =>
unless (err)
console.log('Pin mode(direction) setup...')
if (err)
console.log('Error occurred while settingup pin mode(direction)...')
@self.emit('error', "Setting up pin direction failed")
else
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
@ready = true
@self.emit('open', mode)
else
console.log('Error occurred while settingup pin mode(direction)...')
@self.emit('error', "Setting up pin direction failed")
)
else if mode =='r'
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) =>
unless (err)
console.log('Pin mode(direction) setup...')
if (err)
console.log('Error occurred while settingup pin mode(direction)...')
@self.emit('error', "Setting up pin direction failed")
else
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
@ready = true
@self.emit('open', mode)
else
console.log('Error occurred while settingup pin mode(direction)...')
@self.emit('error', "Setting up pin direction failed")
)
toggle: ->
@ -101,6 +108,3 @@ namespace 'Cylon.IO', ->
@self.digitalWrite(1)
else
@self.digitalWrite(0)
module.exports = Cylon.IO.DigitalPin