2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* device
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 02:33:43 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-10-25 05:25:42 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
require('./cylon');
|
|
|
|
require('./driver');
|
2014-03-04 04:28:19 +08:00
|
|
|
require('./utils');
|
|
|
|
|
|
|
|
var namespace = require('node-namespace'),
|
|
|
|
EventEmitter = require('events').EventEmitter;
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// The Artoo::Device class represents the interface to
|
|
|
|
// a specific individual hardware devices. Examples would be a digital
|
|
|
|
// thermometer connected to an Arduino, or a Sphero's accelerometer
|
2014-02-28 02:33:43 +08:00
|
|
|
namespace("Cylon", function() {
|
|
|
|
this.Device = (function(klass) {
|
|
|
|
subclass(Device, klass);
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Creates a new Device
|
|
|
|
//
|
|
|
|
// opts - object containing Device params
|
|
|
|
// name - string name of the device
|
|
|
|
// pin - string pin of the device
|
|
|
|
// robot - parent Robot to the device
|
|
|
|
// connection - connection to the device
|
|
|
|
// driver - string name of the module the device driver logic lives in
|
|
|
|
//
|
|
|
|
// Returns a new Device
|
2014-02-28 02:33:43 +08:00
|
|
|
function Device(opts) {
|
2014-03-04 04:28:19 +08:00
|
|
|
if (opts == null) { opts = {}; }
|
|
|
|
|
|
|
|
this.stop = bind(this.stop, this);
|
|
|
|
this.start = bind(this.start, this);
|
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
this.self = this;
|
|
|
|
this.robot = opts.robot;
|
|
|
|
this.name = opts.name;
|
|
|
|
this.pin = opts.pin;
|
|
|
|
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
|
|
|
this.driver = this.initDriver(opts);
|
2014-03-04 04:28:19 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
|
|
|
|
}
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Starts the device driver
|
|
|
|
//
|
|
|
|
// callback - callback function to be executed by the driver start
|
|
|
|
//
|
|
|
|
// Returns result of supplied callback
|
2014-02-28 02:33:43 +08:00
|
|
|
Device.prototype.start = function(callback) {
|
2014-03-04 04:28:19 +08:00
|
|
|
var msg = "Starting device " + this.name;
|
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
if (this.pin != null) {
|
|
|
|
msg += " on pin " + this.pin;
|
|
|
|
}
|
2014-03-04 04:28:19 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
Logger.info(msg);
|
|
|
|
return this.driver.start(callback);
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Stops the device driver
|
|
|
|
//
|
|
|
|
// Returns result of supplied callback
|
2014-02-28 02:33:43 +08:00
|
|
|
Device.prototype.stop = function() {
|
2014-03-04 04:28:19 +08:00
|
|
|
Logger.info("Stopping device " + this.name);
|
2014-02-28 02:33:43 +08:00
|
|
|
return this.driver.stop();
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Exports basic data for the Connection
|
|
|
|
//
|
|
|
|
// Returns an Object containing Connection data
|
2014-02-28 02:33:43 +08:00
|
|
|
Device.prototype.data = function() {
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
driver: this.driver.constructor.name || this.driver.name,
|
|
|
|
pin: this.pin != null ? this.pin.toString : null,
|
|
|
|
connection: this.connection.data(),
|
|
|
|
commands: this.driver.commands()
|
2013-11-05 04:16:42 +08:00
|
|
|
};
|
2014-02-28 02:33:43 +08:00
|
|
|
};
|
2013-11-05 04:16:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Retrieves the connections from the parent Robot instances
|
|
|
|
//
|
|
|
|
// c - name of the connection to fetch
|
|
|
|
//
|
|
|
|
// Returns a Connection instance
|
2014-02-28 02:33:43 +08:00
|
|
|
Device.prototype.determineConnection = function(c) {
|
2014-03-04 04:28:19 +08:00
|
|
|
if (c) { return this.robot.connections[c]; }
|
2014-02-28 02:33:43 +08:00
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Returns a default Connection to use
|
|
|
|
//
|
|
|
|
// Returns a Connection instance
|
2014-02-28 02:33:43 +08:00
|
|
|
Device.prototype.defaultConnection = function() {
|
2014-03-04 04:28:19 +08:00
|
|
|
var first = 0;
|
|
|
|
|
|
|
|
for (var c in this.robot.connections) {
|
|
|
|
var connection = this.robot.connections[c];
|
|
|
|
first || (first = connection);
|
2014-02-28 02:33:43 +08:00
|
|
|
}
|
2014-03-04 04:28:19 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
return first;
|
|
|
|
};
|
2013-11-05 04:16:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: sets up driver with @robot
|
|
|
|
//
|
|
|
|
// opts - object containing options when initializing driver
|
|
|
|
// driver - name of the driver to intt()
|
|
|
|
//
|
|
|
|
// Returns the set-up driver
|
2014-02-28 02:33:43 +08:00
|
|
|
Device.prototype.initDriver = function(opts) {
|
2014-03-04 04:28:19 +08:00
|
|
|
if (opts == null) { opts = {}; }
|
2014-02-28 02:33:43 +08:00
|
|
|
Logger.debug("Loading driver '" + opts.driver + "'");
|
|
|
|
return this.robot.initDriver(opts.driver, this.self, opts);
|
|
|
|
};
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
return Device;
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
})(EventEmitter);
|
|
|
|
});
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 02:33:43 +08:00
|
|
|
module.exports = Cylon.Device;
|