Skip to content

Commit

Permalink
created a config module to apply default options and grant access to …
Browse files Browse the repository at this point in the history
…current options.
  • Loading branch information
ricardo committed Apr 22, 2014
1 parent c0cc9a0 commit dddd119
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 54 deletions.
6 changes: 5 additions & 1 deletion gruntfile.js
Expand Up @@ -14,7 +14,11 @@ module.exports = function(grunt) {
files: files,
options: {
node: true,
globals: {}
globals: {
describe: true,
it: true,
beforeEach: true
}
}
},
mochaTest: {
Expand Down
11 changes: 7 additions & 4 deletions 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) {
Expand Down
7 changes: 5 additions & 2 deletions 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
Expand Down
48 changes: 48 additions & 0 deletions 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;
17 changes: 10 additions & 7 deletions lib/controller.js
Expand Up @@ -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
Expand Down
31 changes: 6 additions & 25 deletions 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:
Expand All @@ -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;
11 changes: 7 additions & 4 deletions 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
Expand Down
9 changes: 6 additions & 3 deletions 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;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions test/analog.tests.js
Expand Up @@ -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';
Expand All @@ -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();
});

Expand Down
8 changes: 6 additions & 2 deletions test/buttons.tests.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});

Expand Down
90 changes: 90 additions & 0 deletions 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);
});
});
});
11 changes: 9 additions & 2 deletions test/gyro.tests.js
Expand Up @@ -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';
Expand All @@ -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() {
Expand Down

0 comments on commit dddd119

Please sign in to comment.