Merge pull request #24 from hybridgroup/20-raspi-adaptor

20 raspi adaptor
This commit is contained in:
Ron Evans 2013-11-06 13:19:04 -08:00
commit 7bcb799ca6
7 changed files with 325 additions and 0 deletions

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

@ -0,0 +1,162 @@
/*
* Linux IO DigitalPin
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
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(_super) {
var GPIO_DIRECTION_READ, GPIO_DIRECTION_WRITE, GPIO_PATH, HIGH, LOW;
__extends(DigitalPin, _super);
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 = 'low';
this.ready = false;
this.mode = opts.mode;
}
DigitalPin.prototype.connect = function(mode) {
var _this = this;
if (mode == null) {
mode = null;
}
if (this.mode == null) {
this.mode = mode;
}
return FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum, function(err) {
if (err) {
return _this.self.emit('error', 'Error while creating pin files');
} else {
_this.self._setMode(_this.mode, true);
return _this.self.emit('open');
}
});
};
DigitalPin.prototype.close = function() {
var _this = this;
return FS.writeFile("" + GPIO_PATH + "/unexport", "" + this.pinNum, function(err) {
if (err) {
return _this.self.emit('error', 'Error while closing pin files');
} else {
return _this.self.emit('close');
}
});
};
DigitalPin.prototype.digitalWrite = function(value) {
var _this = this;
if (this.mode !== 'w') {
this.self._setMode('w');
}
this.status = value === 1 ? 'high' : 'low';
return FS.writeFile(this.pinFile, value, function(err) {
if (err) {
return _this.self.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
} else {
return _this.self.emit('digitalWrite', value);
}
});
};
DigitalPin.prototype.digitalRead = function(interval) {
var readData,
_this = this;
if (this.mode !== 'r') {
this.self._setMode('r');
}
readData = null;
return setInterval(function() {
return FS.readFile(_this.pinFile, function(err, data) {
if (err) {
return _this.self.emit('error', "Error occurred while reading from pin " + _this.pinNum);
} else {
readData = data;
return _this.self.emit('digitalRead', data);
}
});
}, interval);
};
DigitalPin.prototype._setMode = function(mode, emitConnect) {
var _this = this;
if (emitConnect == null) {
emitConnect = false;
}
if (mode === 'w') {
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_WRITE, function(err) {
if (err) {
return _this.self.emit('error', "Setting up pin direction failed");
} else {
_this.pinFile = "" + GPIO_PATH + "/gpio" + _this.pinNum + "/value";
_this.ready = true;
if (emitConnect) {
return _this.self.emit('connect', mode);
}
}
});
} else if (mode === 'r') {
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_READ, function(err) {
if (err) {
return _this.self.emit('error', "Setting up pin direction failed");
} else {
_this.pinFile = "" + GPIO_PATH + "/gpio" + _this.pinNum + "/value";
_this.ready = true;
if (emitConnect) {
return _this.self.emit('connect', mode);
}
}
});
}
};
DigitalPin.prototype.setHigh = function() {
return this.self.digitalWrite(1);
};
DigitalPin.prototype.setLow = function() {
return this.self.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.self.setHigh();
} else {
return this.self.setLow();
}
};
return DigitalPin;
})(EventEmitter);
});
}).call(this);

2
dist/robot.js vendored
View File

@ -17,6 +17,8 @@
require('./basestar');
require('./digital-pin');
namespace = require('node-namespace');
Connection = require("./connection");

18
examples/pure_gpio.coffee Normal file
View File

@ -0,0 +1,18 @@
require('../dist/digital-pin')
pin4 = new Cylon.IO.DigitalPin(pin: 4, mode: 'w')
pin4.on('open', (data) ->
console.log("Pin files have been created")
)
pin4.on('digitalWrite', (value) ->
console.log("Value writen to pin -> #{ value }")
)
pin4.on('connect', (data) ->
console.log("Pin mode has been setup!")
pin4.setHigh()
)
pin4.connect()

View File

@ -0,0 +1,15 @@
Cylon = require('..')
# Initialize the robot
Cylon.robot
connection:
name: 'raspi', adaptor: 'raspi'
device:
name: 'led', driver: 'led', pin: 11
work: (my) ->
# we do our thing here
every 1.second(), -> my.led.toggle()
.start()

View File

@ -0,0 +1,17 @@
Cylon = require('..')
# Initialize the robot
Cylon.robot
connection:
name: 'raspi', adaptor: 'raspi'
devices:
[
{name: 'led', driver: 'led', pin: 11},
{name: 'button', driver: 'button', pin: 7}
]
work: (my) ->
my.button.on 'push', () -> my.led.toggle()
.start()

110
src/digital-pin.coffee Normal file
View File

@ -0,0 +1,110 @@
###
* Linux IO DigitalPin
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
###
'use strict';
FS = require('fs')
EventEmitter = require('events').EventEmitter
namespace = require 'node-namespace'
# DigitalPin class to interface with linux GPIO in raspi and beaglebone
#
namespace 'Cylon.IO', ->
class @DigitalPin extends EventEmitter
GPIO_PATH = "/sys/class/gpio"
GPIO_DIRECTION_READ = "in"
GPIO_DIRECTION_WRITE = "out"
HIGH = 1
LOW = 0
constructor: (opts) ->
@self = this
@pinNum = opts.pin
@status = 'low'
@ready = false
@mode = opts.mode
connect: (mode = null) ->
@mode ?= mode
# Creates the GPIO file to read/write from
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) =>
if(err)
@self.emit('error', 'Error while creating pin files')
else
@self._setMode(@mode, true)
@self.emit('open')
)
close: ->
FS.writeFile("#{ GPIO_PATH }/unexport", "#{ @pinNum }", (err) =>
if(err)
@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'
FS.writeFile(@pinFile, value, (err) =>
if (err)
@self.emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
else
@self.emit('digitalWrite', value)
)
digitalRead: (interval) ->
@self._setMode('r') unless @mode == 'r'
readData = null
setInterval(() =>
FS.readFile(@pinFile, (err, data) =>
if err
@self.emit('error', "Error occurred while reading from pin #{ @pinNum }")
else
readData = data
@self.emit('digitalRead', data)
)
, interval)
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
_setMode: (mode, emitConnect = false) ->
if mode == 'w'
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) =>
if (err)
@self.emit('error', "Setting up pin direction failed")
else
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
@ready = true
@self.emit('connect', mode) if emitConnect
)
else if mode =='r'
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) =>
if (err)
@self.emit('error', "Setting up pin direction failed")
else
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
@ready = true
@self.emit('connect', mode) if emitConnect
)
setHigh: ->
@self.digitalWrite(1)
setLow: ->
@self.digitalWrite(0)
toggle: ->
if @status == 'low'
@self.setHigh()
else
@self.setLow()

View File

@ -10,6 +10,7 @@
require './cylon'
require './basestar'
require './digital-pin'
namespace = require 'node-namespace'