Skip to content

Instantly share code, notes, and snippets.

@zankich
Last active August 29, 2015 14:07
Show Gist options
  • Save zankich/a428771e71c2767aa202 to your computer and use it in GitHub Desktop.
Save zankich/a428771e71c2767aa202 to your computer and use it in GitHub Desktop.
var Cylon = require('cylon');
Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
device: { name: 'sphero', driver: 'sphero' },
red: 0,
green: 0,
blue: 255,
shakePower: 0,
currentLevel: 0,
levelColors: ['blue', 'green', 'yellow', 'orange', 'orangered', 'red'],
levelValues: [10, 30, 50, 80, 130, 210],
addShakeDelta: function(x, y) {
var shakeDelta = Math.max(Math.abs(x), Math.abs(y))/50 | 0;
console.log(shakeDelta);
this.shakePower += shakeDelta;
this.red = this.shakePower;
console.log("shakePower: " + this.shakePower);
},
nextLevelReached: function() {
return this.shakePower > this.levelValues[this.currentLevel];
},
verifyMaxPowerReached: function() {
if(this.shakePower > 255) {
this.blue = 255;
this.red = 0;
this.green = 0;
this.currentLevel = 0;
this.shakePower = 0;
console.log('Max Power reached!');
}
},
work: function(my) {
my.sphero.setDataStreaming(['velocity'], { n: 40, m: 1, pcnt: 0 });
my.sphero.on('data', function(data) {
my.addShakeDelta(data[0],data[1]);
});
every((0.4).second(), function() {
//if(my.nextLevelReached()) {
// my.currentLevel += 1;
//}
my.verifyMaxPowerReached();
var hexColor = ((1 << 24) + (my.red << 16) + (my.green << 8) + (my.blue - my.red)) & 0xffffff;
//my.sphero.setColor(my.levelColors[my.currentLevel]);
my.sphero.setRGB(hexColor);
});
}
}).start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment