Skip to content

Instantly share code, notes, and snippets.

@kevinmehall
Last active August 17, 2016 09:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmehall/4c98d93e16323473b78b to your computer and use it in GitHub Desktop.
Save kevinmehall/4c98d93e16323473b78b to your computer and use it in GitHub Desktop.
Using Tessel CLI as a Node library for USB message passing
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
process.on('message', function(msg) {
console.log('reply');
process.send(msg);
});
process.ref();
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
var tessel = require('tessel');
var script = '/device.js';
tessel.findTessel(null, true, function(err, client) {
if (err) throw err;
client.run(__dirname + script, ['tessel', script], {
single: true,
}, function () {
client.stdout.resume();
client.stdout.pipe(process.stdout);
client.stderr.resume();
client.stderr.pipe(process.stderr);
console.info('Running script...');
var count = 0;
setInterval(function(){
console.log('ping')
client.interface.writeProcessMessage({count:count++, data: {foo: 'bar'}})
client.once('message', function (m) {
console.log('pong', m.count);
});
}, 1000);
// Stop on Ctrl+C.
process.on('SIGINT', function() {
setTimeout(function () {
logs.info('Script aborted');
process.exit(131);
}, 200);
client.stop();
});
client.once('script-stop', function (code) {
client.close(function () {
process.exit(code);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment