-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.js
101 lines (81 loc) · 1.87 KB
/
keyboard.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
89
90
91
92
93
94
95
96
97
98
99
100
101
var keypress = require('keypress');
var RollingSpider = require('rolling-spider');
keypress(process.stdin);
var active = true;
var rs = new RollingSpider();
function connect() {
console.log('connecting ...');
rs.connect(connectCallback);
}
function connectCallback(error) {
if (error) {
console.log('error connecting: ' + error);
process.exit(-1);
}
console.log('connected, setting up ...');
rs.setup(setupCallback);
}
function setupCallback() {
console.log('set up, flat trimming and starting ping ...');
rs.flatTrim();
rs.startPing();
rs.flatTrim();
setTimeout(ready, 1000);
}
function ready() {
console.log('ready to receive commands!');
console.log();
clearActive();
}
process.stdin.setRawMode(true);
function clearActive() {
active = false;
}
process.stdin.on('keypress', function (ch, key) {
if (!key) {
return;
}
if (key.ctrl && key.name === 'c') {
process.exit();
}
if (active) {
return;
}
active = true;
var options = {
steps: 2
};
if (key.name === 't') {
console.log('taking off');
rs.takeOff();
} else if (key.name === 'l') {
console.log('landing');
rs.land();
} else if (key.name === 'up') {
console.log('up');
rs.up(options);
} else if (key.name ==='down') {
console.log('down');
rs.down(options);
} else if (key.name === 'left') {
console.log('turn left');
rs.turnLeft(options);
} else if (key.name ==='right') {
console.log('turn right');
rs.turnRight(options);
} else if (key.name === 'w') {
console.log('forward');
rs.forward(options);
} else if (key.name ==='s') {
console.log('backward');
rs.backward(options);
} else if (key.name === 'a') {
console.log('left');
rs.left(options);
} else if (key.name ==='d') {
console.log('right');
rs.right(options);
}
setTimeout(clearActive, 100);
});
connect();