-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
272 lines (238 loc) · 8.17 KB
/
index.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Demo of Ring-Con with WebHID</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Helvetica;
background-color: #DCD9D8;
}
.manage {
position: absolute;
}
.area_a {
width: 100%;
height: 80vh;
display: grid;
place-items: center;
font-size: 60vh;
}
.area_a span {
background: linear-gradient(to right, #EF0007 25%, #FA950A 75%);
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}
.area_b {
position: absolute;
bottom: 0;
width: 100%;
display: grid;
place-items: center;
background-color: #F81331;
transition: height 0.15s ease-out;
}
.strain_value {
position: absolute;
bottom: 0;
right: 0;
padding: 1vh;
color: #FFFFFF;
font-size: 5vh;
}
</style>
</head>
<body>
<div id="app">
<div class="manage">
<button type="button" @click="connect" :disabled="state.connected">
Connect Ring-Con
</button>
<span v-if="state.error" v-text="state.error"></span>
</div>
<div class="area_a">
<span v-text="state.count"></span>
</div>
<div v-if="state.connected" class="area_b" :style="{ height: heightOfAreaB }"></div>
<div class="strain_value" v-text="state.strainValue"></div>
</div>
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<script>
Vue.createApp({
setup() {
const state = Vue.reactive({
connected: false,
error: null,
count: 0,
strainValue: 0,
neutralStrainValue: 0,
});
const connectRingCon = async () => {
const defineSendReportAsyncFunction = ({
subcommand,
expectedReport,
timeoutErrorMessage = 'timeout.'
}) => {
return device => new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
device.removeEventListener('inputreport', checkInputReport);
reject(new Error(timeoutErrorMessage));
}, 5000);
const checkInputReport = event => {
if (event.reportId !== 0x21) {
return;
}
const data = new Uint8Array(event.data.buffer);
for (const [key, value] of Object.entries(expectedReport)) {
if (data[key - 1] !== value) {
return;
}
}
device.removeEventListener('inputreport', checkInputReport);
clearTimeout(timeoutId);
setTimeout(resolve, 50);
};
device.addEventListener('inputreport', checkInputReport);
device.sendReport(0x01, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, ...subcommand
]));
});
};
const set_input_report_mode_to_0x30 = defineSendReportAsyncFunction({
subcommand: [0x03, 0x30],
expectedReport: {
14: 0x03
},
});
const enabling_MCU_data_22_1 = defineSendReportAsyncFunction({
subcommand: [0x22, 0x01],
expectedReport: {
13: 0x80,
14: 0x22
},
});
const enabling_MCU_data_21_21_1_1 = defineSendReportAsyncFunction({
subcommand: [0x21, 0x21, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF3
],
expectedReport: {
14: 0x21
},
});
const get_ext_data_59 = defineSendReportAsyncFunction({
subcommand: [0x59],
expectedReport: {
14: 0x59,
16: 0x20
},
timeoutErrorMessage: 'ring-con not found.',
});
const get_ext_dev_in_format_config_5C = defineSendReportAsyncFunction({
subcommand: [0x5C, 0x06, 0x03, 0x25, 0x06, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x16, 0xED, 0x34, 0x36,
0x00, 0x00, 0x00, 0x0A, 0x64, 0x0B, 0xE6, 0xA9, 0x22, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x90, 0xA8, 0xE1, 0x34, 0x36
],
expectedReport: {
14: 0x5C
},
});
const start_external_polling_5A = defineSendReportAsyncFunction({
subcommand: [0x5A, 0x04, 0x01, 0x01, 0x02],
expectedReport: {
14: 0x5A
},
});
const blinkLed = defineSendReportAsyncFunction({
subcommand: [0x30, 0x90],
expectedReport: {
14: 0x30
},
});
const extractStrainValueFromBuffer = buffer => {
return new DataView(buffer, 38, 2).getInt16(0, true);
};
const getStrainValue = device => new Promise(resolve => {
const checkInputReport = event => {
if (event.reportId === 0x30) {
const strainValue = extractStrainValueFromBuffer(event.data.buffer);
if (strainValue !== 0x0000) {
device.removeEventListener('inputreport', checkInputReport);
resolve(strainValue);
}
}
}
device.addEventListener('inputreport', checkInputReport);
});
const [device] = await navigator.hid.requestDevice({
filters: [{
vendorId: 0x057e, // Nintendo vendor ID
productId: 0x2007, // joy-con R
}],
});
if (!device) {
throw new Error('device not found.');
}
if (!device.opened) {
await device.open();
}
await set_input_report_mode_to_0x30(device);
await enabling_MCU_data_22_1(device);
await enabling_MCU_data_21_21_1_1(device);
await get_ext_data_59(device);
await get_ext_dev_in_format_config_5C(device);
await start_external_polling_5A(device);
const NEUTRAL_STRAIN_VALUE = await getStrainValue(device);
const NEUTRAL_STRAIN_RADIUS = 0x0200;
const NEUTRAL_STRAIN_RADIUS_MARGIN = 0x0010;
console.log('Ready!');
console.log('NEUTRAL_STRAIN_VALUE:', NEUTRAL_STRAIN_VALUE);
await blinkLed(device);
state.connected = true;
state.error = null;
state.neutralStrainValue = NEUTRAL_STRAIN_VALUE;
let isPressing = false;
device.addEventListener('inputreport', event => {
if (event.reportId === 0x30) {
isPressing = (strainValue => {
state.strainValue = strainValue;
if (isPressing) {
if (NEUTRAL_STRAIN_VALUE - NEUTRAL_STRAIN_RADIUS + NEUTRAL_STRAIN_RADIUS_MARGIN <=
strainValue && strainValue <= NEUTRAL_STRAIN_VALUE + NEUTRAL_STRAIN_RADIUS -
NEUTRAL_STRAIN_RADIUS_MARGIN) {
return false;
}
return true;
}
if (strainValue < NEUTRAL_STRAIN_VALUE - NEUTRAL_STRAIN_RADIUS) {
state.count--;
return true;
} else if (NEUTRAL_STRAIN_VALUE + NEUTRAL_STRAIN_RADIUS < strainValue) {
state.count++;
return true;
}
return false;
})(extractStrainValueFromBuffer(event.data.buffer));
}
});
};
return {
state,
connect() {
connectRingCon().catch(err => {
console.error(err);
state.error = err;
});
},
heightOfAreaB: Vue.computed(() => {
return ((state.strainValue - state.neutralStrainValue) / 256 + 12) + 'vh';
})
};
},
}).mount('#app');
</script>
</body>
</html>