Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions examples/connect_spheros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env node
/**
* Creates virtual serial ports in /tmp for each Sphero port and holds the
* bluetooth ones open. This is helpful for avoiding connection errors and
* interference from iOS devices when connecting multiple Spheros.
*
* Requires the 'socat' command-line tool to create the virtual serial ports.
*/
var spawn = require('child_process').spawn;
var extname = require('path').extname;

// where to store the virtual ports while running
var virtual_port_dir = '/tmp/';

// socat displays this when successfully connected
var success_str = 'starting data transfer loop';

// socat binary
//var socat = './testsocat.sh';
var socat = 'socat';

// terminal colors
red   = '\033[31m';
blue  = '\033[34m';
green  = '\033[32m';
reset = '\033[0m';

var paths = process.argv.slice(2);
var numPaths = paths.length;
if(paths.length === 0) {
console.log('usage connectSpheros.js [sphero serial port paths]');
process.exit(1);
}
console.log('Connecting ' + numPaths + ' paired Spheros');

var connectPort = function(index, path, callback) {
var self;
var virtual_port = virtual_port_dir + extname(path).slice(1);
console.log('Starting connection to ' + path + ' at ' + virtual_port);

// Run socat to connect virtual serial port to real one
child = spawn(socat, [
'-d','-d','-d', // Extra debugging
'pty,link=' + virtual_port + ',raw,echo=0', // Create virtual serial port
'file:' + path // Connect to Sphero serial port
]);

child.on('exit', function() {
if(index in activeStreams) {
// We lost an active connection
console.log(red + 'Connection lost for ' + path + reset);
} else {
console.log('Failed to connect to ' + path);
}

delete activeStreams[index];

// Restart self
children[self.index] = connectPort(index, path, callback);
});

var handleOutput = function(buffer) {
var data = buffer.toString();
//console.log(index + ' stdout: ' + data);

if(data.search('starting data transfer loop') !== -1) {
self.ready = true;

console.log(self.path, ': stream is now active at ' + self.port);
callback();

activeStreams[index] = true;
if(Object.keys(activeStreams).length === numPaths) {
console.log('\n' + green + 'READY TO GO!!!' + reset + '\n');
}
}
};
child.stdout.on('data', handleOutput);
child.stderr.on('data', handleOutput);

self = {
index: index,
path: path,
port: virtual_port,
child: child,
ready: false
};
return self;
};

var children = Object.create(null);
var activeStreams = Object.create(null);

// connect one at a time, waiting for callback
var i = 0;
var connectNext = function() {
if(!paths.length) { return; }

i = i + 1;
var path = paths.shift();

children[i] = connectPort(i, path, connectNext);
};
connectNext();

process.on('SIGINT', function() {
process.exit();
});
process.on('exit', function() {
for(var i=0; i<numPaths; i++) {
console.log('Killing ' + i);
children[i].child.kill();
}
});

71 changes: 71 additions & 0 deletions examples/orient_spheros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node
/**
* Allows you to re-orient multiple spheros by temporarily disabling the
* auto-stabilization and turning it back on again after a keypress.
*/
var Sphero = require("../lib/sphero.js").Sphero;

var paths = process.argv.slice(2);
if(paths.length === 0) {
console.log('usage orientSpheros.js [paths to sphero ports]');
process.exit(1);
}
console.log('Orienting ' + paths.length + ' paired Spheros');

var orient = function(path) {
var sphero = new Sphero(path);
var deviceName;

sphero.getBluetoothInfo(function(name, id) {
console.log("Device name is " + name + ' / ' + id);
deviceName = name;
})
// Turn off stabilization
.setStabilization(0x00, function() {
console.log(deviceName + ': stabilization turned off');
})
// Turn on back led
.setBackLED(255, function() {
console.log(deviceName + ': back LED on');
})
.setColor(0, 255, 0, function() {
console.log(deviceName + ' : set color to green');
})
;

return sphero;
};

var numReset = 0;
var reset = function(sphero) {
// Turn sphero to heading 0 so it reorients to the same place as the others
sphero.setHeading(0, function() {

// Turn on stabilization
sphero.setStabilization(0x01, function() {
console.log('Reset sphero ' + numReset);
sphero.disconnect();

numReset = numReset + 1;

if(numReset === paths.length) {
console.log("Finished orientation.");
process.exit(0);
}
});
});
};

var spheros = [];
paths.forEach(function(path) {
spheros.push(orient(path));
});

console.log('Press any key to complete orientation. > ');
var stdin = process.openStdin();
stdin.once('data', function() {
spheros.forEach(function(sphero) {
reset(sphero);
});
});

36 changes: 36 additions & 0 deletions examples/setup_sphero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node
var Sphero = require("../lib/sphero.js").Sphero;

var run = function(sphero) {

sphero.getBluetoothInfo(function(name, id) {
console.log("Device name is " + name + ' / ' + id);
})
.setAutoReconnect(false, 5, function() {
console.log("Turned off auto reconnect");
})
.send({
device: sphero.devices.core,
command: 0x25,
data: new Buffer([0xFF, 0xFF]),
success: function(packet) {
console.log("Set timeout to 65535 seconds");
sphero.disconnect();
process.exit(0);
}
});
};


var sphero;
if(process.argv.length === 2) {
sphero = new Sphero();
}
else if(process.argv.length === 3) {
sphero = new Sphero(process.argv[2]);
} else {
console.log('usage: setup_sphero.js [path to sphero port]');
process.exit(1);
}

run(sphero);
Loading