Adde pure GPIO example and updated digital pin lib

This commit is contained in:
Edgar O Silva 2013-11-04 17:59:40 -06:00
parent 6d694f85b5
commit 831fbe4069
4 changed files with 117 additions and 5 deletions

74
dist/digital-pin.js vendored Normal file
View File

@ -0,0 +1,74 @@
/*
* Linux IO DigitalPin
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var FS, namespace;
FS = require('fs');
namespace = require('node-namespace');
namespace('Cylon.IO', function() {
return this.DigitalPin = (function() {
var GPIO_DIRECTION_READ, GPIO_DIRECTION_WRITE, GPIO_PATH, HIGH, LOW;
GPIO_PATH = "/sys/class/gpio";
GPIO_DIRECTION_READ = "in";
GPIO_DIRECTION_WRITE = "out";
HIGH = 1;
LOW = 0;
function DigitalPin(opts) {
this.self = this;
this.pinNum = opts.pin;
this.status = 0;
FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum);
this._setMode(opts.mode);
}
DigitalPin.prototype.digitalWrite = function(value) {
var status;
if (this.mode !== 'w') {
setMode('w');
}
return status = value === 1 ? 'high' : 'low';
};
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) {
if (!err) {
return FS.write(fd, GPIO_DIRECTION_WRITE);
} else {
return console.log("EROR OCCURED: while opening " + GPIO_PATH + "/gpio" + this.pinNum + "/direction");
}
});
} else if (mode === 'r') {
return FS.open("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", "w", function(err, fd) {
if (!err) {
return FS.write(fd, GPIO_DIRECTION_READ);
} else {
return console.log("EROR OCCURED: while opening " + GPIO_PATH + "/gpio" + this.pinNum + "/direction");
}
});
}
};
return DigitalPin;
})();
});
}).call(this);

27
dist/io.js vendored Normal file
View File

@ -0,0 +1,27 @@
/*
* Linux IO
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var namespace;
namespace = require('node-namespace');
namespace('Cylon', function() {
return this.IO = (function() {
function IO(opts) {
this.self = this;
}
return IO;
})();
});
}).call(this);

View File

@ -0,0 +1,5 @@
require ('./digital-pin')
pin4 = new DigitalPin(pin: 4)
pin4.digitalWrite(1)

View File

@ -24,9 +24,18 @@ namespace 'Cylon.IO', ->
constructor: (opts) ->
@self = this
@pinNum = opts.pin
@pinFile = ''
@status = 0
# Creates the GPIO file to read/write from
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }")
@_setMode(opts.mode)
digitalWrite: (value) ->
setMode('w') unless @mode == 'w'
status = if (value == 1) then 'high' else 'low'
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
_setMode: (mode) ->
@mode = mode
if @mode == 'w'
@ -36,13 +45,10 @@ namespace 'Cylon.IO', ->
else
console.log("EROR OCCURED: while opening #{ GPIO_PATH }/gpio#{ @pinNum }/direction")
)
@pinFile = FS.open("#{ GPIO_PATH }/gpio#{ @pinNum }/value", "w")
elsif mode =='r'
else if mode =='r'
FS.open("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", "w", (err, fd) ->
unless (err)
FS.write(fd, GPIO_DIRECTION_READ)
else
console.log("EROR OCCURED: while opening #{ GPIO_PATH }/gpio#{ @pinNum }/direction")
)
@pinFile = FS.open("#{ GPIO_PATH }/gpio#{ @pinNum }/value", "r")
end