Skip to content

Instantly share code, notes, and snippets.

@parshap
Last active December 28, 2015 22:59
Show Gist options
  • Save parshap/7575843 to your computer and use it in GitHub Desktop.
Save parshap/7575843 to your computer and use it in GitHub Desktop.
sphero "slow-motion hot potato" game hacked on at nodebots sf spherofest 11/20/2013 with @tc
var spheron = require('spheron');
var sphero = spheron.sphero();
var spheroPort = '/dev/cu.Sphero-BYR-RN-SPP';
sphero.on('open', function() {
console.log("opened");
var maskX = 0x1000,
maskY = 0x0800,
maskZ = 0x0400,
mask = maskX | maskY | maskZ;
sphero.setStabalisation(0);
sphero.setDataStreaming(40, 1, mask, 0);
});
console.log("opening...");
sphero.open(spheroPort);
sphero.on("packet", function(packet) {
if (packet.ID_CODE === 0x03) {
var data = readGyro(packet.DATA);
sphero.emit("gyro", data);
}
});
sphero.on("gyro", function(data) {
sphero.emit("move", getMoveVal(data));
});
var updater = createUpdater();
sphero.on("move", function(move) {
updater(move);
});
function createUpdater() {
var dead = false;
return function(val) {
if (dead) {
sphero.setRGB(0xff0000, 0);
return;
}
if (val < 1000) {
sphero.setRGB(0x0000ff, 0);
}
else if (val < 5000) {
sphero.setRGB(0x00ff00, 0);
}
else {
dead = true;
resetState();
}
};
function resetState() {
setTimeout(function() {
dead = false;
}, 6000);
}
}
function getMoveVal(data) {
return Math.abs(data.x) + Math.abs(data.y) + Math.abs(data.z);
}
function readGyro(data) {
return {
x: data.readInt16BE(0),
y: data.readInt16BE(2),
z: data.readInt16BE(4),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment