From dddd119949afc5945ce80b233aeb65556c88440c Mon Sep 17 00:00:00 2001 From: ricardo Date: Tue, 22 Apr 2014 17:57:26 -0400 Subject: [PATCH] created a config module to apply default options and grant access to current options. --- gruntfile.js | 6 ++- lib/analogs.js | 11 ++++-- lib/buttons.js | 7 +++- lib/config.js | 48 +++++++++++++++++++++++ lib/controller.js | 17 ++++---- lib/dualshock.js | 31 +++------------ lib/gyro.js | 11 ++++-- lib/status.js | 9 +++-- package.json | 3 +- test/analog.tests.js | 11 +++++- test/buttons.tests.js | 8 +++- test/config.tests.js | 90 +++++++++++++++++++++++++++++++++++++++++++ test/gyro.tests.js | 11 +++++- test/status.tests.js | 6 ++- 14 files changed, 215 insertions(+), 54 deletions(-) create mode 100644 lib/config.js create mode 100644 test/config.tests.js diff --git a/gruntfile.js b/gruntfile.js index 5f6e8e2..31bf3e4 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -14,7 +14,11 @@ module.exports = function(grunt) { files: files, options: { node: true, - globals: {} + globals: { + describe: true, + it: true, + beforeEach: true + } } }, mochaTest: { diff --git a/lib/analogs.js b/lib/analogs.js index 9e8ebaa..31a0e1a 100644 --- a/lib/analogs.js +++ b/lib/analogs.js @@ -1,14 +1,17 @@ +'use strict'; // Module dependencies. var Utilities = require('./utilities'), - Smoothing = require('./smoothing'); + Smoothing = require('./smoothing'), + config = require('./config'); //Proccess Analog stick events. -var Analogs = function(eventEmmiter, analogConfiguration, smoothInput) { - 'use strict'; +var Analogs = function(eventEmmiter) { var varianceThreshhold = 1, outputSmoothing = new Smoothing(smoothInput), - ds3Utilities = new Utilities(); + ds3Utilities = new Utilities(), + analogConfiguration = config.getControllerConfig().analogSticks, + smoothInput = config.getOptions().analogStickSmoothing; //Private methods var processStick = function(analogStick, data) { diff --git a/lib/buttons.js b/lib/buttons.js index 9a5ab8b..ad925a8 100644 --- a/lib/buttons.js +++ b/lib/buttons.js @@ -1,8 +1,11 @@ +'use strict'; // Module dependencies. +var config = require('./config'); //Proccess button events. -var Buttons = function(eventEmmiter, buttonConfiguration) { - 'use strict'; +var Buttons = function(eventEmmiter) { + + var buttonConfiguration = config.getControllerConfig().buttons; // convert strings to numbers, e.g. "0x01" to 0x01 // must be converted because JSON doesn't allow numbers with leading zeros diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..8838c40 --- /dev/null +++ b/lib/config.js @@ -0,0 +1,48 @@ +'use strict'; +// Module dependencies. +var _ = require('lodash'); +// we will expose these objects via the manager. +var options, + controllerConfig; + +//provides access to the current options and configs. +var config = { + setOptions: function(opts) { + //no options were passed + options = opts || {}; + + // Defaults: + var defaultValues = { + config: "dualShock3", + accelerometerSmoothing: true, + analogStickSmoothing: false + }; + var defaults = _.partialRight(_.assign, function(a, b) { + return typeof a == 'undefined' ? b : a; + }); + + defaults(options, defaultValues); + + var controllerConfiguration; + //use passed config or load from built-in configs + if (typeof options.config === "object") { + controllerConfiguration = options.config; + } else { + controllerConfiguration = require('./../controllerConfigurations/' + options.config); + } + + //set the current controllerConfiguration + config.setControllerConfig(controllerConfiguration); + }, + getOptions: function() { + return options; + }, + setControllerConfig: function(config) { + controllerConfig = config; + }, + getControllerConfig: function() { + return controllerConfig; + } +}; + +module.exports = config; diff --git a/lib/controller.js b/lib/controller.js index ec1a061..a3e0c53 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -8,17 +8,20 @@ var util = require('util'), Buttons = require('./buttons'), Status = require('./status'), HID = require('node-hid'), - LinuxConnector = require('./linuxConnector'); + LinuxConnector = require('./linuxConnector'), + config = require('./config'); //generic controller object, it will need a controller Configuration with a buttons array passed into its connect function. -var Controller = function(controllerConfig, options) { +var Controller = function() { 'use strict'; - var device = null, - analogs = new Analogs(this, controllerConfig.analogSticks, options.analogStickSmoothing), - buttons = new Buttons(this, controllerConfig.buttons), - gyro = new Gyro(this, controllerConfig.motionInputs, options.accelerometerSmoothing), - status = new Status(this, controllerConfig.status); + //get the options and controller configuration. + controllerConfig = config.getControllerConfig(), + options = config.getOptions(), + analogs = new Analogs(this), + buttons = new Buttons(this), + gyro = new Gyro(this), + status = new Status(this); //Private methods diff --git a/lib/dualshock.js b/lib/dualshock.js index bb48e43..13fd175 100644 --- a/lib/dualshock.js +++ b/lib/dualshock.js @@ -1,6 +1,6 @@ // Module dependencies. -var controller = require('./controller.js'); - +var Controller = require('./controller.js'), + config = require('./config'); // This is the app entry point. // options you can pass: @@ -12,30 +12,11 @@ var controller = require('./controller.js'); var dualShock = function(options) { 'use strict'; - //no options were passed - options = options || {}; - - //default to dualshock3 - options.config = options.config || "dualShock3"; - - //default motionSmoothing is turned on - options.accelerometerSmoothing = options.accelerometerSmoothing === undefined ? true : options.accelerometerSmoothing; - - //defaults analogStickSmoothing is turned off - options.analogStickSmoothing = options.analogStickSmoothing === undefined ? false : options.analogStickSmoothing; - - var controllerConfiguration; - - //use passed config or load from built-in configs - if (typeof options.config === "object") { - controllerConfiguration = options.config; - } else { - controllerConfiguration = require('./../controllerConfigurations/' + options.config); - } + //set the current options + config.setOptions(options); - //loads the configuration; - var ds = new controller(controllerConfiguration, options); - return ds; + //returns the controller. + return new Controller(); }; module.exports = dualShock; diff --git a/lib/gyro.js b/lib/gyro.js index 29cac71..de677a6 100644 --- a/lib/gyro.js +++ b/lib/gyro.js @@ -1,14 +1,17 @@ +'use strict'; // Module dependencies. var Utilities = require('./utilities'), - Smoothing = require('./smoothing'); + Smoothing = require('./smoothing'), + config = require('./config'); //Proccess button events. -var motionProcessor = function(eventEmmiter, motionInputConfiguration, smoothInput) { - 'use strict'; +var motionProcessor = function(eventEmmiter) { var varianceThreshhold = 1, outputSmoothing = new Smoothing(smoothInput), - utilities = new Utilities(); + utilities = new Utilities(), + motionInputConfiguration = config.getControllerConfig().motionInputs, + smoothInput = config.getOptions().accelerometerSmoothing; //Private methods //data corrections so that each dirrection has a 0 throug x value diff --git a/lib/status.js b/lib/status.js index 2be68df..acaae06 100644 --- a/lib/status.js +++ b/lib/status.js @@ -1,10 +1,13 @@ +'use strict'; // Module dependencies. +var config = require('./config'); + //Proccess button events. -var Status = function(eventEmmiter, statusConfiguration) { - 'use strict'; +var Status = function(eventEmmiter) { - var buffer = {}; + var buffer = {}, + statusConfiguration = config.getControllerConfig().status; var processControllerStatus = function(category, data) { var statusValue; diff --git a/package.json b/package.json index 7ec944e..082fc25 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "dependencies": { "node-hid": "^0.3", "colors": "^0.6", - "joystick": "^0.1.1" + "joystick": "^0.1.1", + "lodash": "^2.4.1" }, "devDependencies": { "grunt": "^0.4", diff --git a/test/analog.tests.js b/test/analog.tests.js index 12745ed..a1424c3 100644 --- a/test/analog.tests.js +++ b/test/analog.tests.js @@ -2,7 +2,8 @@ var Analogs = require('../lib/analogs'), assert = require('assert'), sinon = require('sinon'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + config = require('../lib/config'); describe('the Analogs component', function() { 'use strict'; @@ -23,7 +24,13 @@ describe('the Analogs component', function() { beforeEach(function() { emitter = new EventEmitter(); - analogs = new Analogs(emitter, mockConfig, false); + config.setOptions({ + analogStickSmoothing: false + }); + config.setControllerConfig({ + analogSticks: mockConfig + }); + analogs = new Analogs(emitter); spy = new sinon.spy(); }); diff --git a/test/buttons.tests.js b/test/buttons.tests.js index 030a132..963e452 100644 --- a/test/buttons.tests.js +++ b/test/buttons.tests.js @@ -2,7 +2,8 @@ var Buttons = require('../lib/buttons'), assert = require('assert'), sinon = require('sinon'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + config = require('../lib/config'); describe('the Buttons component', function() { 'use strict'; @@ -34,7 +35,10 @@ describe('the Buttons component', function() { beforeEach(function() { emitter = new EventEmitter(); - buttons = new Buttons(emitter, mockConfig); + config.setControllerConfig({ + buttons: mockConfig + }); + buttons = new Buttons(emitter); spy = new sinon.spy(); }); diff --git a/test/config.tests.js b/test/config.tests.js new file mode 100644 index 0000000..bafc92a --- /dev/null +++ b/test/config.tests.js @@ -0,0 +1,90 @@ +'use strict'; + +var assert = require('assert'); + +describe('The Config component', function() { + var mockConfig = { + vendorId: 1556, + productId: 616 + }, + mockOptions = { + config: 'dualShock3', + accelerometerSmoothing: true, + logging: false + }, + instance = [{ + name: 'setOptions' + }, { + name: 'getOptions' + }, { + name: 'setControllerConfig' + }, { + name: 'getControllerConfig' + }], + defaultOptionsInstance = [{ + name: 'config' + }, { + name: 'accelerometerSmoothing' + }, { + name: 'analogStickSmoothing' + }], + configA, + configB; + + beforeEach(function() { + configA = require('../lib/config'); + configB = require('../lib/config'); + }); + + + describe('object instance', function() { + it('should have the following shape', function() { + instance.forEach(function(method) { + assert.equal(typeof configA[method.name], 'function'); + }); + }); + }); + + describe('option methods', function() { + it('should be able to save options', function() { + configA.setOptions(mockOptions); + assert.equal(configA.getOptions(), mockOptions); + }); + + it('should provide a single object accross instances', function() { + configA.setOptions(mockOptions); + assert.equal(configA.getOptions(), configB.getOptions()); + }); + }); + + describe('controllerConfig methods', function() { + it('should be able to save controllerConfig settings', function() { + configA.setControllerConfig(mockConfig); + //change the object + mockConfig.vendorId = 22; + assert.equal(configA.getControllerConfig(), mockConfig); + }); + + it('should provide a single object accross instances', function() { + configA.setControllerConfig(mockConfig); + assert.equal(configA.getControllerConfig(), configB.getControllerConfig()); + }); + }); + + describe('default values', function() { + beforeEach(function() { + configA.setOptions(); + }); + it('should apply default values', function() { + var ops = configA.getOptions(); + defaultOptionsInstance.forEach(function(property) { + assert.notEqual(ops[property.name], void 0); + }); + }); + it('should load default config', function() { + var controllerConfig = configA.getControllerConfig(); + assert.notEqual(controllerConfig, null); + assert.notEqual(controllerConfig, void 0); + }); + }); +}); diff --git a/test/gyro.tests.js b/test/gyro.tests.js index 5ab226c..0ab0ade 100644 --- a/test/gyro.tests.js +++ b/test/gyro.tests.js @@ -2,7 +2,8 @@ var Gyro = require('../lib/gyro'), assert = require('assert'), sinon = require('sinon'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + config = require('../lib/config'); describe('the Gyro component', function() { 'use strict'; @@ -24,7 +25,13 @@ describe('the Gyro component', function() { beforeEach(function() { emitter = new EventEmitter(); spy = sinon.spy(); - gyro = new Gyro(emitter, mockConfig, true); + config.setOptions({ + accelerometerSmoothing: false + }); + config.setControllerConfig({ + motionInputs: mockConfig + }); + gyro = new Gyro(emitter); }); describe('object instance', function() { diff --git a/test/status.tests.js b/test/status.tests.js index 3b6e104..32b7f33 100644 --- a/test/status.tests.js +++ b/test/status.tests.js @@ -2,7 +2,8 @@ var Status = require('../lib/status'), assert = require('assert'), sinon = require('sinon'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + config = require('../lib/config'); describe('the status component', function() { var mockConfig = [{ @@ -31,6 +32,9 @@ describe('the status component', function() { beforeEach(function() { emitter = new EventEmitter(); spy = sinon.spy(); + config.setControllerConfig({ + status: mockConfig + }); status = new Status(emitter, mockConfig); });