Last active
August 17, 2016 09:32
-
-
Save kevinmehall/4c98d93e16323473b78b to your computer and use it in GitHub Desktop.
Using Tessel CLI as a Node library for USB message passing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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