Created — forked from mikeal/gist:3095110

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

tmpad color pressure

View test.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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');
});

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

Owner

DONT JUDGE MY HACKY FUNCODE RICK!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.