cylon/examples/sphero_shakeometer/sphero_shakeometer.js

59 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-12-14 08:19:25 +08:00
"use strict";
var Cylon = require("cylon");
2014-10-04 02:29:08 +08:00
Cylon.robot({
2014-11-26 08:01:31 +08:00
connections: {
2014-12-14 08:19:25 +08:00
sphero: { adaptor: "sphero", port: "/dev/tty.Sphero-YBW-RN-SPP" }
2014-11-26 08:01:31 +08:00
},
devices: {
2014-12-14 08:19:25 +08:00
sphero: { driver: "sphero" }
2014-11-26 08:01:31 +08:00
},
2014-10-04 02:29:08 +08:00
work: function(my) {
var max = 0;
var changingColor = false;
2014-12-14 08:19:25 +08:00
my.sphero.setDataStreaming(["velocity"], { n: 40, m: 1, pcnt: 0 });
my.sphero.on("data", function(data) {
2015-04-15 12:49:12 +08:00
if (!changingColor) {
2014-12-14 08:19:25 +08:00
var x = Math.abs(data[0]),
y = Math.abs(data[1]);
if (x > max) {
max = x;
}
2014-10-04 02:29:08 +08:00
2014-12-14 08:19:25 +08:00
if (y > max) {
max = y;
}
2014-10-04 02:29:08 +08:00
}
});
every((0.6).second(), function() {
changingColor = true;
if (max < 10) {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("white");
2014-10-04 02:29:08 +08:00
} else if (max < 100) {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("lightyellow");
2014-10-04 02:29:08 +08:00
} else if (max < 150) {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("yellow");
2014-10-04 02:29:08 +08:00
} else if (max < 250) {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("orange");
2014-10-04 02:29:08 +08:00
} else if (max < 350) {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("orangered");
2014-10-04 02:29:08 +08:00
} else if (max < 450) {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("red");
2014-10-04 02:29:08 +08:00
} else {
2014-12-14 08:19:25 +08:00
my.sphero.setColor("darkred");
2014-10-04 02:29:08 +08:00
}
max = 0;
changingColor = false;
});
}
}).start();