-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.js
88 lines (70 loc) · 1.94 KB
/
demo.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var keypress = require("keypress");
keypress(process.stdin);
try {
process.stdin.setRawMode(true);
} catch(e) {
console.log("Could not set stdin to raw mode, using nodemon?");
};
var port = process.env.SPHERO_DEV || process.argv[2];
if(!port) {
console.log("Provide serial port to attach to");
console.log("run with");
console.log("SPHERO_DEV=/dev/rfcommX node demo.js");
console.log("or");
console.log("node demo.js /dev/rfcommX");
process.exit(1);
}
var simple = require("./index")(port, 0x77);
var stage = 0;
simple.stop();
function randomColor() {
return Math.round(Math.random() * 254);
}
simple.on("ready", function() {
process.stdin.on("keypress", function(ch, key) {
if(key && key.name === 'up') {
simple.forward();
}
if(key && key.name === 'left') {
simple.left();
}
if(key && key.name === 'right') {
simple.right();
}
if(key && key.name === 'c') {
simple.setColor(randomColor() * randomColor() * randomColor());
}
if(key && key.name === 't') {
simple.toggleBackLight();
}
if(key && key.ctrl && key.name === 'c') {
process.nextTick(function() {
process.exit(1);
});
}
usage();
});
usage();
return;
var repl = require("repl").start("> ");
["left", "right", "forward", "stop"].forEach(function(cmd) {
if(typeof simple[cmd] === "function")
repl.context[cmd] = simple[cmd].bind(simple);
else repl.context[cmd] = simple[cmd];
});
repl.context.simple = simple;
}).on("error", function(err) {
console.log("an error occured");
console.log(err);
});
function usage() {
console.log("");
console.log(" simple-sphero");
console.log(" [BATTERY: " + simple.power + "]");
console.log(" c - change color!");
console.log(" t - toggle backlight");
console.log(" up - drive forward");
console.log(" left - turn 90° left");
console.log(" right - turn 90° right");
console.log("");
}