-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (68 loc) · 1.86 KB
/
index.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
const keypress = require('keypress');
const logUpdate = require('log-update');
const HubControl = require('./src/hub-control');
const inputs = require('./src/input-modes');
const deviceInfo = {
ports: {
A: { action: '', angle: 0 },
B: { action: '', angle: 0 },
AB: { action: '', angle: 0 },
C: { action: '', angle: 0 },
D: { action: '', angle: 0 },
LED: { action: '', angle: 0 },
},
tilt: { roll: 0, pitch: 0 },
distance: 0,
rssi: 0,
color: '',
error: '',
connected: false
};
const controlData = {
input: null,
speed: 0,
turnAngle: 0,
tilt: { roll: 0, pitch: 0 },
forceState: null,
updateInputMode: null
};
let uiUpdaterInteral = null;
let selectedInputMode = inputs.arcadeDrive;
function printUI() {
const datas = [JSON.stringify(deviceInfo, null, 1), JSON.stringify(controlData, null, 1)];
logUpdate(datas);
}
keypress(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', async (str, key) => {
if (!key || key.name === 'return' || key.name === 'enter') {
return;
} else if (key.ctrl && key.name === 'c') {
clearInterval(uiUpdaterInteral);
console.log('Disconnecting...');
await hubControl.disconnect();
console.log('Disconnected');
process.exit();
} else {
controlData.input = key.name;
selectedInputMode(controlData);
if (controlData.forceState){
hubControl.setNextState(controlData.forceState);
controlData.forceState = null;
}
if (controlData.updateInputMode){
selectedInputMode = controlData.updateInputMode;
controlData.updateInputMode = null;
}
printUI();
}
});
const hubControl = new HubControl(deviceInfo, controlData);
hubControl.setNextState('Manual');
hubControl.start().then(() => {
console.log('\x1Bc');
uiUpdaterInteral = setInterval(() => {
printUI();
hubControl.update();
}, 100);
});