Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing merge conflict in package.json

  • Loading branch information...
commit 7b4fab732be0184b41f3a0409363148f7b1cf3a1 2 parents 1ab9e5c + 301f998
@mikeal mikeal authored
View
23 README.md
@@ -10,7 +10,7 @@
`tpad(index, color)` - change the color of a pad
-`tpad.color('#FF0')` or `tpad('F00')` - set the color of all the leds
+`tpad.color('#FF0')` - set the color of all the leds
##### pad operations
@@ -18,9 +18,9 @@
`tpad(index).on([pressure,press,depress], function(pad) {})` - bind to events
-`tpad(index).color('#fff');` - change the color of an individual pad
+`tpad(index).color('#fff');` - change the color of an individual button
-`tpad(0).on('pressure', function(p) { p.color(color().hsv((p.value/18000)*360, 100, 100)); })` - change the color of pad[0] based on the amount of pressure applied
+`tpad(0).on('pressure', function(p) { p.color(color().hsv((p.value/18000)*360, 100, 100)); })` - change the color of button[0] based on the amount of pressure applied
##### pre-baked functionality
@@ -29,12 +29,27 @@ _very_ basic animation
```javascript
-tpad.animate(milliseconds, [0,1,2,3], function(pad) {
+tpad.animate(100, [0,1,2,3], function(pad) {
tpad.color('000'); // turn all the lights off
pad.color('F00'); // turn the current pad red
});
```
+or shift on hsv!
+
+```javascript
+
+var a = 0;
+tpad.animate(100, [0,1,3,2], function(p) {
+ a+=70;
+ if(a>360) {
+ a=0;
+ }
+
+ p.color(color().hsv(a, 100, 100));
+});
+```
+
and to stop the animation `tpad.stop()`
calling `tpad.animate` again will first stop the currently running animation
View
2  bin/tpad
@@ -99,7 +99,7 @@ if (!command) {
// allow the user to specify a development version of the tpad firmware
config.location = argv._[1] || null;
-
+ config.debug = !!argv.debug;
flash(config);
});
View
152 firmware/index.js
@@ -7,7 +7,9 @@ var
tar = require('tar'),
zlib = require('zlib'),
exec = require('child_process').exec,
- semver = require('semver');
+ semver = require('semver'),
+ lufacdc = require('chip.avr.lufacdc'),
+ SerialPort = require('serialport').SerialPort;
function error() {
console.log(arguments);
@@ -20,96 +22,56 @@ var firmware = module.exports = function(options) {
// TODO: ensure you can specify a firmware dir
if (!options.location) {
- firmware.fetch(function(err, folder) {
- firmware.build(folder, function(err) {
- firmware.flash(folder, options);
- });
+ firmware.fetch(options, function(err, hexData) {
+ firmware.flash(hexData, options);
});
} else {
- console.log('Using firmware', path.resolve(options.location));
- firmware.build(firmwareFolder, function(err) {
- firmware.flash(firmwareFolder, options);
- })
+ var hexFilePath = path.resolve(options.location);
+ console.log('Using firmware', hexFilePath);
+ fs.readFile(hexFilePath, function(err, data) {
+ firmware.flash(data.toString(), options);
+ });
}
};
-firmware.fetch = function(fn) {
+firmware.fetch = function(options, fn) {
// get the latest firmware
console.log('finding the latest firmware');
request('https://api.github.com/repos/tmpvar/tpad-firmware/git/refs/tags', function(err, res) {
- var
- data = JSON.parse(res.body),
- first = true,
- passed = 0,
- where = 0,
- latest, sha, shaDir, size, url, req;
+ if (err) {
+ return fn(err);
+ }
+
+ var data = JSON.parse(res.body);
// find the latest version
data.sort(function(a, b) {
return (semver.gt(a, b)) ? -1 : 1;
});
- latest = data[0];
- sha = latest.object.sha.substring(0,7);
- url = 'https://github.com/tmpvar/tpad-firmware/tarball/' + sha;
- shaDir = __dirname + '/tmpvar-tpad-firmware-' + sha
-
- fs.exists(shaDir, function(exists) {
- if (!exists) {
- req = request(url, sha);
-
- console.log('found ' + latest.ref.replace('refs/tags/',''));
-
- console.log('downloading and extracting...');
+ var latest = data[0];
+ var tag = latest.ref.replace('refs/tags/', '');
+ var tpad = options.tpad.name;
- req.on('data', function(data) {
- passed += data.length;
- var pos = Math.floor((passed/size)*100)/10;
+ var url = 'https://raw.github.com/tmpvar/tpad-firmware/' + tag +
+ '/tpad/' + tpad + '/' + tpad + '.hex';
- if (first) {
- size = req.response.headers['content-length'];
- first = false;
-
- // update progress
- } else if (Math.floor(pos) > where){
- process.stdout.write('#');
- where = pos;
- }
- });
-
- req.pipe(zlib.Unzip())
- .pipe(tar.Extract({ path: __dirname })).on('end', function() {
- console.log(' done');
- fn(null, shaDir);
- });
-
- } else {
- fn(null, shaDir);
+ console.log('fetching', url)
+ request(url, function(err, res) {
+ if (err) {
+ return fn(err);
}
- });
- });
-};
-// build all of the firmware
-firmware.build = function(dir, fn) {
- console.log('building...');
-
- exec('make', {
- cwd : dir
- }, function (error, stdout, stderr) {
- if (error) {
- throw error;
- } else {
- fn();
- }
+ fn(null, res.body.toString());
+ });
});
};
// Basically wait for the known tpad connection to drop
-// wait 2 seconds then run avrdude
-firmware.waitForReset = function(dir, options, fn) {
+// wait 2 seconds then run chip.avr.lufacdc's flash mechanism
+firmware.waitForReset = function(options, fn) {
// attempt a software reset (as of firmware 0.0.2)
if (options.tpad.version && semver.gte(options.tpad.version, '0.0.2')) {
@@ -129,14 +91,9 @@ firmware.waitForReset = function(dir, options, fn) {
state = 1;
setTimeout(tick, 100);
} else if (state === 1) {
- console.log('\ntpad is booting..');
-
- // we don't want to mess with the pad while it's being flashed
- // so close the serial connection for good measure.
options.serialport.close();
-
setTimeout(function() {
- fn(dir + '/tpad/' + options.tpad.name);
+ fn();
}, 500);
} else {
setTimeout(tick, 100);
@@ -145,19 +102,42 @@ firmware.waitForReset = function(dir, options, fn) {
}, 100);
};
-firmware.flash = function(dir, options, fn) {
- firmware.waitForReset(dir,options, function(tpadFirmwareDir) {
- console.log('flashing...');
- var env = process.env;
- env.AVRDUDE_PORT = options.serialport.port;
-
- exec('make avrdude', {
- cwd : tpadFirmwareDir,
- env : env
- }, function (err, stdout, stderr) {
- if (err) { throw err; }
- console.log('OK!')
- process.exit();
+firmware.flash = function(hexData, options, fn) {
+ firmware.waitForReset(options, function(err) {
+ if (err) {
+ fn && fn(err);
+ return;
+ }
+
+ var sp = new SerialPort(options.serialport.port);
+
+ sp.on('open', function() {
+ lufacdc.init(sp, options, function (err, flasher) {
+ if (err) {
+ throw err;
+ }
+
+ console.log('\nerasing chip')
+ flasher.erase(function() {
+
+ console.log('programming');
+ flasher.program(hexData, function(err) {
+ if (err) throw err;
+
+ console.log('verifying')
+ flasher.verify(function(err) {
+ if (err) {
+ throw err
+ }
+ flasher.fuseCheck(function(err) {
+ if (err) throw err;
+ console.log('OK!');
+ process.exit();
+ });
+ });
+ });
+ });
+ });
});
});
};
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');
}
});
-
+
}
View
14 package.json
@@ -2,12 +2,14 @@
"author": "Elijah Insua <tmpvar@gmail.com> (http://tmpvar.com)",
"name": "tpad",
"description": "an array of hackable pressure sensitive buttons",
- "version": "0.0.3",
- "contributors" : [{
- "name" : "Mikeal Rogers",
- "email" : "mikeal.rogers@gmail.com>"
- }],
- "repository" : {
+ "version": "0.0.4",
+ "contributors": [
+ {
+ "name": "Mikeal Rogers",
+ "email": "mikeal.rogers@gmail.com>"
+ }
+ ],
+ "repository": {
"type": "git",
"url": "git://github.com/tmpvar/tpad.git"
},
Please sign in to comment.
Something went wrong with that request. Please try again.