Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Forked from mikeal/gist:3095110
Created July 12, 2012 02:36
Show Gist options
  • Save max-mapper/3095317 to your computer and use it in GitHub Desktop.
Save max-mapper/3095317 to your computer and use it in GitHub Desktop.
tmpad color pressure
var serial = require('./lib/serial')
, Tpad = require('./lib/tpad')
, color = require('color')
;
serial.init()
var spinnerTimer = function () {}
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)
console.log('\n\nconnected to a', config.tpad.name, 'with', config.tpad.pads, 'pads')
var currentSerialPort = config.serialport
var tpad = new Tpad(config)
var colors =
[ '#1B28E0'
, '#E30B0B'
, '#07FA34'
, '#FAA507'
]
var index = 0
, count = 0
;
// tpad.animate(200, [0,1,2,3], function(pad) {
// tpad.color('000'); // turn all the lights off
// pad.color(colors[index]); // turn the current pad red
// count++
// if (count === 4) {
// if (index === 3) index = 0
// else index++
// count = 0
// }
// });
var padstats = initStats()
function initStats() {
return {
0: {values: [], last: 0},
1: {values: [], last: 0},
2: {values: [], last: 0},
3: {values: [], last: 0}
}
}
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()
});
});
});
serial.on('disconnected', function() {
console.log('disconnected');
});
@rwaldron
Copy link

psst. line 67 "sum/ary.length" ;)

@max-mapper
Copy link
Author

DONT JUDGE MY HACKY FUNCODE RICK!

@rwaldron
Copy link

🐒
🐶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment