-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (116 loc) · 3.75 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const usb = require('usb');
const Promise = require('bluebird');
const handler = require('./handler.js');
const keyboard = require('./keyboard.js');
// usb.setDebugLevel(4);
const DEBUG = false;
// Steam controller object
class SteamControllerInfos {
get devHandle() {
return this.iDevHandle;
}
set devHandle(dH) {
this.iDevHandle = dH;
}
get interfaceNum() {
return this.iInterfaceNum
}
set interfaceNum(iN) {
this.iInterfaceNum = iN
}
}
const SteamController = {
openAndClaim: function() {
let controller = new SteamControllerInfos();
let devHandle;
//Open Steam Controller device
if((devHandle = usb.findByIds(0x28DE, 0x1102)) != null){ // Wired Steam Controller
// console.log('Found wired Steam Controller');
controller.devHandle = devHandle;
controller.interfaceNum = 2;
}
else if((devHandle = usb.findByIds(0x28DE, 0x1142)) != null){ // Steam Controller dongle
// console.log('Found Steam Dongle, will attempt to use the first Steam Controller');
controller.devHandle = devHandle;
controller.interfaceNum = 1;
}
else{
console.log('No device found');
return false;
}
controller.devHandle.open();
//On Linux, detach the module if needed
if(controller.devHandle.__isKernelDriverActive(1)) {
controller.devHandle.__detachKernelDriver(1);
}
if(controller.devHandle.__isKernelDriverActive(2)) {
controller.devHandle.__detachKernelDriver(2);
}
//Claim the USB interface controlling the haptic actuators
try {
let r = controller.devHandle.interface(controller.interfaceNum);
r.claim();
} catch(e) {
console.error(e);
controller.devHandle.close();
return;
}
return controller;
},
close: function(controller) {
controller.devHandle.interface(controller.interfaceNum).release((error) => {
if(!controller.devHandle.__isKernelDriverActive(1)) {
controller.devHandle.__attachKernelDriver(1);
}
if(!controller.devHandle.__isKernelDriverActive(2)) {
controller.devHandle.__attachKernelDriver(2);
}
controller.devHandle.close();
});
}
}
const steamController = SteamController.openAndClaim();
// handler.playNote(steamController, 0, 32, NOTE_LENGTH)
// .then(() => {
// return new Promise((resolve, reject) => {
// setTimeout(resolve, NOTE_LENGTH * MILLISECONDS);
// });
// })
// .then(() => {
// console.log('Done!');
// })
// .catch(console.error)
// .then(() => {
// return SteamController.close(steamController);
// })
// .catch(console.error);
const noteHandler = new handler.NoteHandler(steamController);
noteHandler.start.bind(noteHandler)()
.catch((error) => {
if(DEBUG) {
console.error(error);
}
})
.finally(() => {
SteamController.close(steamController);
process.exit();
});
keyboard(noteHandler);
// noteHandler.left = 6;
//
//
// handler.sleep(300)
// .then(() => noteHandler.left = 2)
// .then(() => handler.sleep(300))
// .then(() => noteHandler.right = 0)
// .then(() => handler.sleep(250))
// .then(() => noteHandler.left = 5)
// .then(() => noteHandler.right = 11)
// .then(() => handler.sleep(250))
// .then(() => noteHandler.leftOctave = 3)
// .then(() => noteHandler.rightOctave = 1)
// .then(() => handler.sleep(200))
// .then(() => {
// return noteHandler.stop();
// })
// .catch(console.error);