Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

basic repl and serialport detection

  • Loading branch information...
commit 965f29a2a0c578a05f90caa0cbc075cdb68722c4 1 parent 71dad94
@tmpvar authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
51 bin/tpad
@@ -0,0 +1,51 @@
+#!/usr/bin/env node
+
+var
+ c = require('ansi')(process.stdout),
+ argv = require('optimist').argv,
+ version = require(__dirname + '/../package.json').version,
+ repl = require('../lib/repl'),
+ serial = require('../lib/serial'),
+ cleanup = c.reset.bind(c),
+ currentRepl;
+
+c.hex('CCFF00');
+
+
+console.log([
+'_/_/_/_/_/ _/_/_/ _/_/ _/_/_/',
+' _/ _/ _/ _/ _/ _/ _/',
+' _/ _/_/_/ _/_/_/_/ _/ _/',
+' _/ _/ _/ _/ _/ _/',
+'_/ _/ _/ _/ _/_/_/'].join('\n'));
+
+c.hex('aaaaaa');
+
+console.log('version:', version);
+
+c.reset();
+
+c.green();
+
+serial.init();
+
+serial.on('searching', function() {
+ console.log('searching..');
+});
+
+serial.on('connected', function(data) {
+ c.green();
+ console.log('connected');
+ currentRepl = repl.start(data);
+});
+
+serial.on('disconnected', function() {
+ c.red();
+ console.log('disconnected');
+ currentRepl.close();
+ currentRepl = null;
+});
+
+
+process.on('unhandledException', cleanup);
+process.on('exit', cleanup);
View
0  index.js
No changes.
View
12 lib/repl.js
@@ -0,0 +1,12 @@
+var repl = require('repl'), start = repl.start;
+
+repl.start = function() {
+
+ var server = start.apply(this, arguments);
+ server.stop = function() {
+ server.rli.close();
+ }
+ return server;
+};
+
+module.exports = repl;
View
81 lib/serial.js
@@ -0,0 +1,81 @@
+var
+ serialport = require('serialport'),
+ glob = require('glob'),
+ EventEmitter = require('events').EventEmitter,
+ async = require('async');
+
+
+var serial = module.exports = new EventEmitter();
+
+serial.pollInterval = 500;
+serial.search = function(fn) {
+ this.emit('searching');
+
+ var
+ that = this,
+ timer = setInterval(function poll() {
+
+ glob('/dev/tty.usb*', function(err, matches) {
+
+ var found = false, sp;
+ if (matches && matches.length) {
+ async.forEach(matches, function(match, collect) {
+
+ // attempt to create a serial port
+ sp = new serialport.SerialPort(match, {
+ parser : serialport.parsers.readline('\n')
+ });
+
+ sp.on('error', function() {
+ console.log('error', arguments);
+ });
+
+ var header = "", expected = 'tpad';
+ sp.on('data', function(data) {
+ header += data.toString();
+
+ if (header.length > expected.length) {
+
+ if (header.substring(0, expected.length) !== expected) {
+ // not found, collect nothing;
+ sp.close();
+ collect();
+ } else {
+ sp.removeAllListeners();
+ collect(null, sp)
+ }
+ } else {
+
+ }
+ });
+
+ }, function(err, sp) {
+ if (err || !sp) {
+ setTimeout(poll, that.pollInterval);
+ } else {
+ fn(null, sp);
+ }
+ });
+
+ });
+ } else {
+ setTimeout(poll, that.pollInterval);
+ }
+
+ }, that.pollInterval);
+};
+
+serial.init = function() {
+ var that = this;
+ this.search(function(err, sp) {
+ that.sp = sp;
+ that.emit('connected')
+ });
+};
+
+
+
+
+
+
+
View
21 package.json
@@ -0,0 +1,21 @@
+{
+ "author": "Elijah Insua <tmpvar@gmail.com> (http://tmpvar.com)",
+ "name": "tpad",
+ "description": "hackable pressure sensitive buttons",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/tmpvar/tpad.git"
+ },
+ "dependencies": {
+ "serialport": "~0.7.4",
+ "ansi": "~0.1.2",
+ "glob": "~3.1.10",
+ "async": "~0.1.22"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.