Permalink
Browse files

Changes to exported API and the addition of an init function to reduc…

…e boiler. Also, an animation demo.
  • Loading branch information...
1 parent 3ed7c74 commit 0dbd919c63de77ce27efa57ce828df583fec7bd3 @mikeal mikeal committed Aug 10, 2012
Showing with 76 additions and 4 deletions.
  1. +27 −0 demos/animate.js
  2. 0 index.js
  3. +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
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 0dbd919

Please sign in to comment.