Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix: move button logic out into button.js

  • Loading branch information...
commit 12032ad38e5bca0ad79a8c01825db4149ce31aa8 1 parent 1c64de3
@tmpvar authored
Showing with 99 additions and 88 deletions.
  1. +79 −0 lib/button.js
  2. +20 −88 lib/tpad.js
View
79 lib/button.js
@@ -0,0 +1,79 @@
+var
+ EventEmitter = require('events').EventEmitter,
+ color = require('color');
+
+var Button = module.exports = function Button(index, serialport) {
+ EventEmitter.call(this);
+
+ this.index = index;
+ this.history = [];
+ this.serialport = serialport;
+
+ this.init();
+};
+
+Button.prototype = new EventEmitter();
+Button.prototype.activationThreshold = 4000;
+Button.prototype.value = 0;
+
+Button.prototype.init = function() {
+ var that = this;
+
+ this.currentColor = color('#000');
+
+ this.on('raw', function(value) {
+ var event = false;
+ if (!that.pressed && value > that.activationThreshold) {
+ event = 'press';
+ that.pressed = true;
+ } else if (that.pressed && value < that.activationThreshold) {
+ event = 'depress';
+ that.pressed = false;
+ }
+
+ that.history.unshift(value);
+ that.history.length = 5;
+
+ that.value = value;
+ that.emit('pressure', this);
+
+ if (event) {
+ that.emit(event, this);
+ }
+ });
+};
+
+Button.prototype.color = function() {
+ var args = [];
+
+ Array.prototype.push.apply(args, arguments);
+
+ if (args.length === 0) {
+ return this.currentColor;
+ }
+
+ var rgb;
+
+ if (args.length === 1) {
+ args = args.shift();
+ }
+
+ // handle color objects directly
+ if (args && args.rgbArray) {
+ this.currentColor = args;
+ rgb = args.rgb();
+ } else if (typeof args === 'string') {
+ this.currentColor = color(args);
+ rgb = this.currentColor.rgb();
+ } else if (args.length === 3) {
+ this.currentColor = color().rgb(args);
+ rgb = this.currentColor.rgb();
+ } else {
+ return this.currentColor;
+ }
+
+ if (this.serialport) {
+ this.serialport.write([this.index, rgb.r, rgb.g, rgb.b].join(',') + '\n');
+ }
+ return this.currentColor;
+};
View
108 lib/tpad.js
@@ -1,7 +1,7 @@
var EventEmitter = require('events').EventEmitter
, color = require('color')
, serial = require('./serial')
- ;
+ , Button = require('./button');
function Tpad(config) {
@@ -9,6 +9,8 @@ function Tpad(config) {
this.serialport = config.serialport;
this.activationThreshold = config.activationThreshold || 4000;
this.config = config.tpad;
+ this.buttons = [];
+
//
// Hook up serialport events
//
@@ -17,95 +19,28 @@ function Tpad(config) {
});
//
- // Add getters/setters for buttons
+ // Add Buttons
//
-
for (var i=0; i<config.tpad.pads; i++) {
-
- (function(pad) {
- states[pad] = new EventEmitter();
- states[pad]._history = [];
- states[pad].index = pad;
-
-
- states[pad].on('raw', function(value) {
- var event = false;
- if (!states[pad].pressed && value > that.activationThreshold) {
- event = 'press';
- states[pad].pressed = true;
- } else if (states[pad].pressed && value < that.activationThreshold) {
- event = 'depress';
- states[pad].pressed = false;
- }
-
- states[pad]._history.unshift(value);
- states[pad]._history.length = 5;
-
- states[pad].value = value;
- states[pad].emit('pressure', this);
-
- if (event) {
- this.emit(event, this);
- }
- });
-
- states[pad].value = 0;
-
- states[pad].color = function() {
- if (arguments.length === 0 || !arguments[0]) {
- return this._color || color('#000000');
- }
-
- var args = [];
-
- Array.prototype.push.apply(args, arguments);
-
- var rgb;
-
- if (args.length === 1) {
- args = args.shift();
- }
-
- // handle color objects directly
- if (args && args.rgbArray) {
- rgb = args.rgb();
- } else if (typeof args === 'string') {
- if (args[0] !== '#') {
- args = '#' + args;
- }
- this._color = color(args);
- rgb = this._color.rgb();
- }
-
- if (that.serialport) {
- that.serialport.write([pad, rgb.r, rgb.g, rgb.b].join(',') + '\n');
- }
- };
-
- that.__defineGetter__(pad, function() {
- return states[pad];
- });
- that.__defineSetter__(pad, function(val) {
- states[pad].color(val);
- });
- }(i));
+ this.buttons.push(new Button(i, config.serialport));
}
var ret = function(index, color) {
if (color) {
- that[index].color(color);
+ that.buttons[index].color(color);
- // shortcut for tpad('F00');
+ // shortcut for setting all of the pads
+ // to a color
} else if (index.length) {
ret.color(index);
}
- return typeof index !== 'undefined' ? that[index] : that;
+ return typeof index !== 'undefined' ? that.buttons[index] : that;
};
ret.color = function(color) {
- for (var i = 0; i<that.config.pads; i++) {
- that[i].color(color);
- }
+ that.buttons.forEach(function(button) {
+ button.color(color);
+ });
};
ret.animate = function(interval, pads, fn) {
@@ -113,7 +48,7 @@ function Tpad(config) {
var index = 0;
this.timer = setInterval(function() {
- fn && fn(that[pads[index]]);
+ fn && fn(that.buttons[pads[index]]);
index++;
if (index >= pads.length) {
index = 0;
@@ -122,14 +57,11 @@ function Tpad(config) {
};
ret.stop = function() {
- this.color('000000');
clearInterval(this.timer);
}
ret.each = function(fn) {
- for (var i = 0, l = that.config.pads; i<l; i++) {
- fn && fn(that[i], i);
- }
+ fn && that.buttons.forEach(fn);
}
ret.config = that.config;
@@ -146,11 +78,11 @@ Tpad.prototype = {
// parse the line into parts
var
parts = line.split(','),
- pad = parseInt(parts[0], 10),
+ button = parseInt(parts[0], 10),
value = parseInt(parts[1], 10);
// emit the current pressure
- this[pad].emit('raw', value);
+ this.buttons[button].emit('raw', value);
}
};
@@ -164,9 +96,9 @@ module.exports.init = function (options, cb) {
serial.init()
var spinnerTimer = function () {}
-
+
if (!options.noOutput) {
- var spinner = "|/-\\", spinnerPos = 0, spinnerTimer;
+ var spinner = "|/-\\", spinnerPos = 0, spinnerTimer;
serial.on('searching', function() {
process.stdout.write('searching ');
spinnerTimer = setInterval(function() {
@@ -185,7 +117,7 @@ module.exports.init = function (options, cb) {
if (!options.noOutput) {
console.log('\n\nconnected to a', config.tpad.name, 'with', config.tpad.pads, 'pads')
}
-
+
var currentSerialPort = config.serialport
var tpad = new Tpad(config)
@@ -197,5 +129,5 @@ module.exports.init = function (options, cb) {
console.log('disconnected');
}
});
-
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.