Updated IO and digital pin to use events.

This commit is contained in:
Edgar O Silva 2013-11-04 21:01:29 -06:00
parent 239056fa65
commit 3bdecf665a
3 changed files with 168 additions and 26 deletions

99
dist/digital-pin.js vendored
View File

@ -9,16 +9,22 @@
(function() {
'use strict';
var FS, namespace;
var EventEmitter, FS, namespace,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
FS = require('fs');
EventEmitter = require('events').EventEmitter;
namespace = require('node-namespace');
namespace('Cylon.IO', function() {
return this.DigitalPin = (function() {
return this.DigitalPin = (function(_super) {
var GPIO_DIRECTION_READ, GPIO_DIRECTION_WRITE, GPIO_PATH, HIGH, LOW;
__extends(DigitalPin, _super);
GPIO_PATH = "/sys/class/gpio";
GPIO_DIRECTION_READ = "in";
@ -32,43 +38,108 @@
function DigitalPin(opts) {
this.self = this;
this.pinNum = opts.pin;
this.status = 0;
FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum);
this._setMode(opts.mode);
this.status = 'low';
this.ready = false;
}
DigitalPin.prototype.open = function(mode) {
return FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum, function(err) {
if (!err) {
this.self.emit('create');
return this._setMode(opts.mode);
} else {
console.log('Error while creating pin files ...');
return this.self.emit('error', 'Error while creating pin files');
}
});
};
DigitalPin.prototype.digitalWrite = function(value) {
var status;
if (this.mode !== 'w') {
this._setMode('w');
}
return status = value === 1 ? 'high' : 'low';
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);
} else {
console.log('Pin File written successfully');
return this.emit('digitalWrite', value);
}
});
};
DigitalPin.prototype.digitalRead = function() {
var readData;
if (this.mode !== 'r') {
this._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);
} else {
readData = data;
return this.emit('read', data);
}
});
return readData;
};
DigitalPin.prototype._setMode = function(mode) {
this.mode = mode;
if (this.mode === 'w') {
return FS.open("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", "w", function(err, fd) {
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_WRITE, function(err) {
if (!err) {
return FS.write(fd, GPIO_DIRECTION_WRITE);
console.log('Pin mode(direction) setup...');
this.pinFile = "" + GPIO_PATH + "/gpio" + this.pinNum + "/value";
this.ready = true;
return this.emit('open', mode);
} else {
return console.log("EROR OCCURED: while opening " + GPIO_PATH + "/gpio" + this.pinNum + "/direction");
console.log('Error occurred while settingup pin mode(direction)...');
return this.emit('error', "Setting up pin direction failed");
}
});
} else if (mode === 'r') {
return FS.open("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", "w", function(err, fd) {
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_READ, function(err) {
if (!err) {
return FS.write(fd, GPIO_DIRECTION_READ);
console.log('Pin mode(direction) setup...');
this.pinFile = "" + GPIO_PATH + "/gpio" + this.pinNum + "/value";
this.ready = true;
return this.emit('open', mode);
} else {
return console.log("EROR OCCURED: while opening " + GPIO_PATH + "/gpio" + this.pinNum + "/direction");
console.log('Error occurred while settingup pin mode(direction)...');
return this.emit('error', "Setting up pin direction failed");
}
});
}
};
DigitalPin.prototype.on = function() {
return this.digitalWrite(1);
};
DigitalPin.prototype.off = function() {
return this.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.digitalWrite(1);
} else {
return this.digitalWrite(0);
}
};
DigitalPin.prototype.isOn = DigitalPin.status === 'high';
DigitalPin.prototype.isOff = !DigitalPin.isOn;
return DigitalPin;
})();
})(EventEmitter);
});
}).call(this);

View File

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

View File

@ -8,13 +8,16 @@
'use strict';
FS = require('fs')
EventEmitter = require('events').EventEmitter
namespace = require 'node-namespace'
# IO is class that lays foundation to DigitalPin and I2C in Raspi and beaglebone.
#
namespace 'Cylon.IO', ->
class @DigitalPin
class @DigitalPin extends EventEmitter
GPIO_PATH = "/sys/class/gpio"
GPIO_DIRECTION_READ = "in"
@ -25,31 +28,89 @@ namespace 'Cylon.IO', ->
constructor: (opts) ->
@self = this
@pinNum = opts.pin
@status = 0
@status = 'low'
@ready = false
open: (mode) ->
# Creates the GPIO file to read/write from
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }")
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) ->
unless(err)
@self.emit('create')
@_setMode(opts.mode)
else
console.log('Error while creating pin files ...')
@self.emit('error', 'Error while creating pin files')
)
@_setMode(opts.mode)
digitalWrite: (value) ->
@_setMode('w') unless @mode == 'w'
status = if (value == 1) then 'high' else 'low'
@status = if (value == 1) then 'high' else 'low'
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 }")
else
console.log('Pin File written successfully')
@emit('digitalWrite', value)
)
digitalRead: ->
@_setMode('r') unless @mode == 'r'
readData = null
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 }")
else
readData = data
@emit('read', data)
)
readData
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
_setMode: (mode) ->
@mode = mode
if @mode == 'w'
FS.open("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", "w", (err, fd) ->
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) ->
unless (err)
FS.write(fd, GPIO_DIRECTION_WRITE)
console.log('Pin mode(direction) setup...')
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
@ready = true
@emit('open', mode)
else
console.log("EROR OCCURED: while opening #{ GPIO_PATH }/gpio#{ @pinNum }/direction")
console.log('Error occurred while settingup pin mode(direction)...')
@emit('error', "Setting up pin direction failed")
)
else if mode =='r'
FS.open("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", "w", (err, fd) ->
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) ->
unless (err)
FS.write(fd, GPIO_DIRECTION_READ)
console.log('Pin mode(direction) setup...')
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
@ready = true
@emit('open', mode)
else
console.log("EROR OCCURED: while opening #{ GPIO_PATH }/gpio#{ @pinNum }/direction")
console.log('Error occurred while settingup pin mode(direction)...')
@emit('error', "Setting up pin direction failed")
)
on: ->
@digitalWrite(1)
off: ->
@digitalWrite(0)
toggle: ->
if @status == 'low'
@digitalWrite(1)
else
@digitalWrite(0)
isOn:
(@status == 'high')
isOff:
!@isOn