This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
55 lines (50 loc) · 1.78 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var keypress = require('keypress');
var VirtualSphero = require("./main");
var virtualSphero = new VirtualSphero(8081, "*");
// キーにテストする内容を割り当てる
var testKeys = {
up: ["roll", 100, 0],
right: ["roll", 100, 90],
down: ["roll", 100, 180],
left: ["roll", 100, 270],
space: ["roll", 0, 0],
r: ["randomColor", null],
a: function() {
console.log("set random color");
var randomColors = ["red", "blue", "yellow", "green", "purple", "orange"];
virtualSphero.command("color", [randomColors[Math.floor(Math.random() * randomColors.length)]]);
},
b: function() {
console.log("add sphero");
virtualSphero.addSphero("Sphero" + new Date().getTime().toString());
},
d: function() {
console.log("remove sphero");
var spheroNames = virtualSphero.getNames();
virtualSphero.removeSphero(spheroNames[Math.floor(Math.random() * spheroNames.length)]);
},
g: function() {
console.log("get sphero names " + virtualSphero.getNames());
}
}
keypress(process.stdin);
process.stdin.on('keypress', function (ch, key) {
if (key && typeof testKeys[key.name] !== "undefined") {
if (Array.isArray(testKeys[key.name])) {
var args = testKeys[key.name];
console.log("orb." + args[0] + "(" + args.slice(1).map(arg => "\"" + arg + "\"").join(", ") + ");");
virtualSphero.virtualSpheroNames.forEach(spheroName => {
virtualSphero.command(spheroName, args[0], args.slice(1));
});
} else if (typeof testKeys[key.name] === "function") {
console.log(key.name + " is assigned to custom function");
testKeys[key.name]();
}
} else if (key && key.ctrl && key.name === "c") {
process.exit();
} else {
console.log('got "keypress"', key);
}
});
process.stdin.setRawMode(true);
process.stdin.resume();