Permalink
Browse files

Land: Mikeal's initial cleanup

  • Loading branch information...
2 parents 92ee03e + 6344847 commit bc3b2423b64ae2a3e56eeffcc424315753c201c1 @tmpvar committed Aug 11, 2012
Showing with 150 additions and 4 deletions.
  1. +27 −0 demos/animate.js
  2. +74 −0 demos/color-pressure.js
  3. 0 index.js
  4. +49 −4 lib/tpad.js
View
@@ -0,0 +1,27 @@
+var tpad = require('../lib/tpad');
+
+tpad.init(function (err, tpad) {
+ if (err) throw err
+
+ var colors =
+ [ 'CC0033'
+ , '990099'
+ , '66CC66'
+ , '333399'
+ , '00E5EE'
+ ]
+
+ var pnum = 0
+ , cindex = 0
+ ;
+ tpad.animate(300, [0,1,2,3], function(pad) {
+ tpad.color('000') // turn all the lights off
+ pad.color(colors[cindex]) // turn the current pad to a color
+ pnum += 1
+ if (pnum === 4) {
+ pnum = 0
+ if (cindex === colors.length - 1) cindex = 0
+ else cindex += 1
+ }
+ });
+})
View
@@ -0,0 +1,74 @@
+var tpad = require('../lib/tpad')
+ , color = require('color')
+ ;
+
+tpad.init(function (err, tpad) {
+ function initStats() {
+ return {
+ 0: {values: [], last: 0},
+ 1: {values: [], last: 0},
+ 2: {values: [], last: 0},
+ 3: {values: [], last: 0}
+ }
+ }
+
+ var padstats = initStats()
+
+ function average(ary) {
+ var sum = 0
+ for (var i = 0; i < ary.length; i++) {
+ sum += parseInt(ary[i])
+ }
+ return sum/(ary.length)
+ }
+
+ function difference(num1, num2) {
+ num1 = Math.abs(num1)
+ num2 = Math.abs(num2)
+ return (num1 > num2) ? num1 - num2 : num2 - num1
+ }
+
+ function translate(n, sourceLow, sourceHigh, targetLow, targetHigh) {
+ return (n - sourceLow) / (sourceHigh - sourceLow) * (targetHigh - targetLow) + targetLow
+ }
+
+ var bucketsize = 5
+ var colorchangethreshhold = 5
+ var lowerpressure = 2000
+ var upperpressure = 18000
+ var lastTouch
+
+ tpad.each(function(pad, index) {
+
+ setInterval(function() {
+ if ( (new Date() - lastTouch) > 200 ) {
+ pad.color('#609FFF')
+ padstats = initStats()
+ }
+ }, 199)
+
+ pad.on('pressure', function(p) {
+ lastTouch = new Date()
+ var bucket = padstats[p.index]
+ if (bucket.values.length > bucketsize) bucket.values.pop()
+ if (p.value < lowerpressure) p.value = lowerpressure
+ if (p.value > upperpressure) p .value = upperpressure
+ var num = translate(p.value, lowerpressure, upperpressure, 0, 255)
+ num = Math.floor(num)
+ bucket.values.unshift(num)
+ var avg = Math.floor(average(bucket.values))
+ var val = bucket.last
+ if (difference(avg, bucket.last) > colorchangethreshhold) val = avg
+ var c = color({r: val, g: 255 - val, b: 255})
+ console.log(val, c.hexString())
+ p.color(c.hexString())
+ bucket.last = val
+ });
+
+ pad.on('depress', function(p) {
+ padstats = initStats()
+ });
+
+ });
+
+});
View
No changes.
View
@@ -1,6 +1,7 @@
-var
- EventEmitter = require('events').EventEmitter;
- color = require('color');
+var EventEmitter = require('events').EventEmitter
+ , color = require('color')
+ , serial = require('./serial')
+ ;
function Tpad(config) {
@@ -153,4 +154,48 @@ Tpad.prototype = {
}
};
-module.exports = Tpad;
+module.exports = function (options) { return new Tpad(options) };
+
+module.exports.init = function (options, cb) {
+ if (!cb) {
+ cb = options
+ options = {}
+ }
+ serial.init()
+
+ var spinnerTimer = function () {}
+
+ if (!options.noOutput) {
+ var spinner = "|/-\\", spinnerPos = 0, spinnerTimer;
+ serial.on('searching', function() {
+ process.stdout.write('searching ');
+ spinnerTimer = setInterval(function() {
+ process.stdout.write(spinner[spinnerPos]);
+ spinnerPos++;
+ if (spinnerPos >= spinner.length) {
+ spinnerPos = 0;
+ }
+ }, 50);
+ });
+ }
+
+ serial.on('connected', function(config) {
+ clearInterval(spinnerTimer)
+
+ 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)
+
+ cb(null, tpad)
+ });
+
+ serial.on('disconnected', function() {
+ if (!options.noOutput) {
+ console.log('disconnected');
+ }
+ });
+
+}

0 comments on commit bc3b242

Please sign in to comment.