-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpipe.js
88 lines (66 loc) · 1.84 KB
/
pipe.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
// pipe.js passes data from WiimoteInput.py to VESCRecieve.py
// Since cwiid is a Python 2 library and pyvesc is a Python 3 library,
// this is needed as they must be seperate files.
var fs = require('fs'); //require filesystem module
const {PythonShell} = require('python-shell'); // Import python-shell
var wiiFilePath = 'WiimoteInput.py'; // Input file name
var vescFilePath = 'VESCRecieve.py'; // Output file name
// Depending on the button pressed on Wiimote, different signals are passed
// to VESCRecieve.py for VESC control.
function handleWiiData(message){
switch (message) {
case 'LEFT':
sendData('LEFT');
break;
case 'RIGHT':
sendData('RIGHT');
break;
case 'UP':
sendData('UP');
break;
case 'DOWN':
sendData('DOWN');
break;
case '1':
sendData('1');
break;
case '2':
sendData('2');
break;
case 'A':
sendData('A');
break;
case 'B':
sendData('B');
break;
case 'HOME':
sendData('HOME');
break;
case 'MINUS':
sendData('MINUS');
break;
case 'PLUS':
sendData('PLUS');
break;
}
}
// Function to monitor if input is passed from WiimoteInput.py
function monitorWiimote(){
let options = {
pythonPath: 'python2', // Specifiying that python2 should be used
scriptPath: './'
};
let pyshell = new PythonShell(wiiFilePath, options);
pyshell.on('message', function (message) {
// Received a message sent from the Python script (a simple "print" statement)
handleWiiData(message);
});
// End the input stream and allow the process to exit
pyshell.end(function (err,code,signal) {
monitorWiimote() // Relaunch since disconnected
});
}
function sendData(data){
console.log(data);
}
monitorWiimote(); // Run WiimoteInput.py and send any output to handleWiiData()