diff --git a/dist/digital-pin.js b/dist/digital-pin.js index 42e2e9b..9a8d58a 100644 --- a/dist/digital-pin.js +++ b/dist/digital-pin.js @@ -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); diff --git a/examples/pure_gpio.coffee b/examples/pure_gpio.coffee index b593604..9a76940 100644 --- a/examples/pure_gpio.coffee +++ b/examples/pure_gpio.coffee @@ -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) ) diff --git a/src/digital-pin.coffee b/src/digital-pin.coffee index 391036d..c14047b 100644 --- a/src/digital-pin.coffee +++ b/src/digital-pin.coffee @@ -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 -