Commit cde5c9c6 authored by Karan Goel's avatar Karan Goel
Browse files

remove duplicate files

parent f0a7cfa5
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
// The `tessel` module built-in to the Tessel firmware for access to hardware
var tessel = require('tessel');
//When a message is received from the computer, this event triggers.
process.on('message', function(msg) {
console.log('----------------');
console.log("[Tessel] Message from PC:", msg);
});
var counter = 0;
// Keep the event loop alive
process.ref();
\ No newline at end of file
{
"name": "usb_messages",
"version": "0.0.0",
"description": "Tessel USB communication example",
"main": "host.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/tessel/docs"
},
"author": "Technical Machine",
"license": "Public Domain",
"bugs": {
"url": "https://github.com/tessel/docs/issues"
}
}
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
// When we `require('tessel')` here, this is the library out of `node_modules`
// for USB communication. It's not the same as the `tessel` module built into
// the Tessel firmware that code on the Tessel uses for hardware access --
// that's only available to JS that executes on the Tessel itself.
var tessel = require('tessel');
var neurosky = require('node-neurosky');
var script = require.resolve('./device/index.js');
var opts = {
// Stop existing script, if any
stop: true,
// Serial number (`undefined` picks the first one)
serial: process.env.TESSEL_SERIAL,
};
// create a mindwave client
var client = neurosky.createClient({
appName: 'My Great Application',
appKey: '1234567890abcdef...'
});
// connect to tessel client
client.connect();
var args = [];
// `tessel.findTessel` finds a Tessel attached to this computer and connects.
tessel.findTessel(opts, function(err, device) {
console.log('Connected to: ', device);
if (err) throw err;
// Once we've found a Tessel, we tell it to run our script. This works just
// like `tessel run` and bundles the `device/` directory. It bundles only
// `device/` and not the host code because `device/` has its own
// `package.json`.
device.run(script, args, {}, function () {
// Connect the stdout and stderr of the process running on Tessel to
// the console, so that our `console.log` messages show.
device.stdout.resume();
device.stdout.pipe(process.stdout);
device.stderr.resume();
device.stderr.pipe(process.stderr);
var count = 0;
// `device.send(msg)` sends an object to Tessel. It supports JSON
// compatible objects as messages, with the addition that `Buffer`s,
// `Date`s and cyclic data structures are also handled properly.
//setInterval(function(){
//device.send({count:count++, data: {obj: 'demo'}})
//}, 4000);
// when mindwave gives you data
client.on('data',function(data){
device.send(data);
});
// Exit cleanly on Ctrl+C.
process.on('SIGINT', function() {
// Try to stop the process on the Tessel
device.stop();
setTimeout(function () {
// But if that fails, just exit
logs.info('Script aborted');
process.exit(131);
}, 200);
});
// When the script on Tessel exits, shut down
// USB communications and exit
device.once('script-stop', function (code) {
device.close(function () {
process.exit(code);
});
});
});
});
\ No newline at end of file
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
// This is a demo of programtically checking multiple tessels hooked
// up to the same computer.
// When we `require('tessel')` here, this is the library out of `node_modules`
// for USB communication. It's not the same as the `tessel` module built into
// the Tessel firmware that code on the Tessel uses for hardware access --
// that's only available to JS that executes on the Tessel itself.
var tessel = require('tessel');
var async = require('async');
var script = require.resolve('./device/index.js');
// list all Tessels attached to this computer
tessel.listDevices(function(err, devices){
if (err) throw new Error(err);
// the devices object consists of the usb descriptor,
// the serial number, and the version info of the Tessel
var funcQueue = [];
var count = 0;
devices.forEach(function(device){
console.log("Found device", device.serialNumber);
funcQueue.push(function(callback){
loadCode(device.serialNumber, function(){
callback(null);
});
})
})
async.waterfall(funcQueue, function(err, res){
console.log('Done loading code');
});
})
function loadCode(serialNumber, next){
var opts = {
// Stop existing script, if any
stop: true,
// Serial number
serial: serialNumber,
};
var args = [];
// `tessel.findTessel` finds a Tessel attached to this computer and connects.
tessel.findTessel(opts, function(err, device) {
if (err) throw err;
// Once we've found a Tessel, we tell it to run our script. This works just
// like `tessel run` and bundles the `device/` directory. It bundles only
// `device/` and not the host code because `device/` has its own
// `package.json`.
device.run(script, args, {}, function () {
// Connect the stdout and stderr of the process running on Tessel to
// the console, so that our `console.log` messages show.
device.stdout.resume();
device.stdout.pipe(process.stdout);
device.stderr.resume();
device.stderr.pipe(process.stderr);
var count = 0;
console.log('device initial', device.serialNumber);
// `device.send(msg)` sends an object to Tessel. It supports JSON
// compatible objects as messages, with the addition that `Buffer`s,
// `Date`s and cyclic data structures are also handled properly.
setInterval(function(){
console.log("device", device.serialNumber);
device.send({count:count++, data: {obj: 'demo'}})
}, 4000);
// `device.on('message', function (msg) { ... })` receives an event
// when an object is received from Tessel.
device.on('message', function (m) {
console.log('[PC] Message from Tessel:', m);
});
// Exit cleanly on Ctrl+C.
process.on('SIGINT', function() {
// Try to stop the process on the Tessel
device.stop();
setTimeout(function () {
// But if that fails, just exit
logs.info('Script aborted');
process.exit(131);
}, 200);
});
// When the script on Tessel exits, shut down
// USB communications and exit
device.once('script-stop', function (code) {
device.close(function () {
process.exit(code);
});
});
next();
});
});
}
{
"name": "usb_messages",
"version": "0.0.0",
"description": "Tessel USB communication example",
"main": "host.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/tessel/docs"
},
"author": "Technical Machine",
"license": "Public Domain",
"bugs": {
"url": "https://github.com/tessel/docs/issues"
},
"dependencies": {
"async": "~0.9.0",
"node-neurosky": "0.0.1",
"tessel": "~0.3.9"
}
}
# USB communication between Tessel and Node on your PC
In addition to loading code, you can also use the USB connection to Tessel to pass data between your Tessel and Node.js on your PC. Combine your Tessel's easy access to hardware and your computer's processing power to do things neither device can do alone.
Messages are JavaScript objects. It uses [structured clone](https://github.com/tcr/structured-clone), which beyond JSON, supports cyclic structures and `Buffer`, `RegExp`, and `Date` objects.
We've built an example for you to expand on:
```
git clone https://github.com/tessel/docs.git
cd docs/tutorials/usb_messages
npm install
node host.js
```
The directory structure looks like:
```
./
/host.js -- script that runs on the computer
/package.json
/node_modules -- npm modules for the computer
/tessel -- the Tessel host-side USB module
/device -- this entire directory will be deployed to Tessel
/package.json
/node_modules -- all the libs that run ON Tessel go here (camera, ambient, IR, etc.)
/device.js -- the main script that runs on Tessel
-- other JS files for Tessel go here
```
Both Tessel and Node run JS, but you don't want all your host-side code on the Tessel. The host and device each have their own `node_modules` for npm packages. Only `devices/` is sent to the Tessel, because it has its own `package.json`.
The `tessel` command line tool functionality is also exposed as a library.
`host.js` uses that library to take the place of `tessel run`, and not only
deploys your code to the Tessel, but also communicates with it.
The Tessel code, `device/index.js`, simply demonstrates sending and receiving messages.
To try this example, run `node host.js`. It sends the device-side code on the Tessel as it starts.
## Multiple Tessels on one computer
**Note only works on Linux & Mac due to Windows hotplug issues**
This deploys the same script to multiple Tessels and listens for output from each one.
```
git clone https://github.com/tessel/docs.git
cd docs/tutorials/usb_messages
npm install
node multi_host.js
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment