Skip to content

Commit

Permalink
New release, make the linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
tomayac committed Sep 26, 2022
1 parent b048016 commit 518a5d3
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 98 deletions.
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "joy-con-webhid",
"version": "0.3.1",
"version": "0.4.0",
"description": "Joy-Con over WebHID ",
"scripts": {
"start": "npx http-server",
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://github.com/tomayac/joy-con-webhid#readme",
"devDependencies": {
"eslint": "^8.23.1",
"eslint": "^8.24.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.5.0",
"http-server": "^14.1.1",
Expand Down
118 changes: 68 additions & 50 deletions src/connectRingCon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,106 @@ export const connectRingCon = async (device) => {
const defineSendReportAsyncFunction = ({
subcommand,
expectedReport,
timeoutErrorMessage = 'timeout.'
timeoutErrorMessage = 'timeout.',
}) => {
return device => new Promise(async (resolve, reject) => {
const timeoutId = setTimeout(() => {
device.removeEventListener('inputreport', checkInputReport);
reject(new Error(timeoutErrorMessage));
}, 5000);
return (device) =>
new Promise(async (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) {
const checkInputReport = (event) => {
if (event.reportId !== 0x21) {
return;
}
}

device.removeEventListener('inputreport', checkInputReport);
clearTimeout(timeoutId);
setTimeout(resolve, 50);
};
device.addEventListener('inputreport', checkInputReport);
await device.sendReport(0x01, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, ...subcommand
]));
//resolve();
});
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);
await device.sendReport(
0x01,
new Uint8Array([
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
...subcommand,
])
);
// resolve();
});
};

const set_input_report_mode_to_0x30 = defineSendReportAsyncFunction({
// eslint-disable-next-line no-unused-vars
const setInputReportModeTo0x30 = defineSendReportAsyncFunction({
subcommand: [0x03, 0x30],
expectedReport: {
14: 0x03
14: 0x03,
},
});
const enabling_MCU_data_22_1 = defineSendReportAsyncFunction({
const enablingMCUData221 = defineSendReportAsyncFunction({
subcommand: [0x22, 0x01],
expectedReport: {
13: 0x80,
14: 0x22
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
const enablingMCUData212111 = 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
14: 0x21,
},
});
const get_ext_data_59 = defineSendReportAsyncFunction({
const getExtData59 = defineSendReportAsyncFunction({
subcommand: [0x59],
expectedReport: {
14: 0x59,
16: 0x20
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
const getExtDevInFormatConfig5C = 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
14: 0x5c,
},
});
const start_external_polling_5A = defineSendReportAsyncFunction({
subcommand: [0x5A, 0x04, 0x01, 0x01, 0x02],
const startExternalPolling5A = defineSendReportAsyncFunction({
subcommand: [0x5a, 0x04, 0x01, 0x01, 0x02],
expectedReport: {
14: 0x5A
14: 0x5a,
},
});

//await set_input_report_mode_to_0x30(device); // same as enableStandardFullMode
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);
// await set_input_report_mode_to_0x30(device); // same as enableStandardFullMode
await enablingMCUData221(device);
await enablingMCUData212111(device);
await getExtData59(device);
await getExtDevInFormatConfig5C(device);
await startExternalPolling5A(device);
};
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ const getDeviceID = (device) => {

const addDevice = async (device) => {
const id = getDeviceID(device);
console.log(`HID connected: ${id} ${device.productId.toString(16)} ${device.productName}`);
console.log(
`HID connected: ${id} ${device.productId.toString(16)} ${
device.productName
}`
);
connectedJoyCons.set(id, await connectDevice(device));
};

const removeDevice = async (device) => {
const id = getDeviceID(device);
console.log(`HID disconnected: ${id} ${device.productId.toString(16)} ${device.productName}`);
console.log(
`HID disconnected: ${id} ${device.productId.toString(16)} ${
device.productName
}`
);
connectedJoyCons.delete(id);
};

Expand Down Expand Up @@ -97,7 +105,7 @@ const connectDevice = async (device) => {
}
}
if (!joyCon) {
//console.log(device.productId.toString(16), device.productName);
// console.log(device.productId.toString(16), device.productName);
joyCon = new GeneralController(device); // for other controllers
}
await joyCon.open();
Expand Down
33 changes: 20 additions & 13 deletions src/joycon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as PacketParser from './parse.js';
import { connectRingCon } from "./connectRingCon.js";
import { connectRingCon } from './connectRingCon.js';

/**
* Concatenates two typed arrays.
Expand Down Expand Up @@ -261,7 +261,7 @@ class JoyCon extends EventTarget {
* @memberof JoyCon
* @seeAlso https://github.com/mascii/demo-of-ring-con-with-web-hid
*/
async enableRingCon() {
async enableRingCon() {
/*
const cmds = [
[0x22, 0x01], // enabling_MCU_data_22_1
Expand Down Expand Up @@ -291,15 +291,18 @@ class JoyCon extends EventTarget {
* @memberof JoyCon
*/
async enableUSBHIDJoystickReport() {
const usb = this.device.collections[0].outputReports.find(r => r.reportId == 0x80) != null;
const usb =
this.device.collections[0].outputReports.find(
(r) => r.reportId == 0x80
) != null;
if (usb) {
await this.device.sendReport(0x80, new Uint8Array([0x01]));
await this.device.sendReport(0x80, new Uint8Array([0x02]));
await this.device.sendReport(0x01, new Uint8Array([0x03]));
await this.device.sendReport(0x80, new Uint8Array([0x04]));
}
}

/**
* Send a rumble signal to Joy-Con.
*
Expand Down Expand Up @@ -364,22 +367,26 @@ class JoyCon extends EventTarget {

/**
* set LED state.
* @param {int} n position(0-3)
*
* @memberof JoyCon
*/
async setLEDState(n) {
async setLEDState(n) {
const NO_RUMBLE = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
const subcommand = [0x30, n];
await this.device.sendReport(0x01, new Uint8Array([...NO_RUMBLE, 0, ...subcommand]));
await this.device.sendReport(
0x01,
new Uint8Array([...NO_RUMBLE, 0, ...subcommand])
);
}

/**
* set LED.
*
* @memberof JoyCon
* @param n position(0-3)
* @param {int} n position(0-3)
*/
async setLED(n) {
async setLED(n) {
this.ledstate |= 1 << n;
await this.setLEDState(this.ledstate);
}
Expand All @@ -388,9 +395,9 @@ class JoyCon extends EventTarget {
* reset LED.
*
* @memberof JoyCon
* @param n position(0-3)
* @param {int} n position(0-3)
*/
async resetLED(n) {
async resetLED(n) {
this.ledstate &= ~((1 << n) | (1 << (4 + n)));
await this.setLEDState(this.ledstate);
}
Expand All @@ -399,9 +406,9 @@ class JoyCon extends EventTarget {
* blink LED.
*
* @memberof JoyCon
* @param n position(0-3)
* @param {int} n position(0-3)
*/
async blinkLED(n) {
async blinkLED(n) {
this.ledstate &= ~(1 << n);
this.ledstate |= 1 << (4 + n);
await this.setLEDState(this.ledstate);
Expand Down Expand Up @@ -641,7 +648,7 @@ class GeneralController extends JoyCon {
* @param {*} packet
* @memberof GeneralController
*/
_receiveInputEvent(packet) {
_receiveInputEvent(packet) {
this.dispatchEvent(new CustomEvent('hidinput', { detail: packet }));
}
}
Expand Down
Loading

0 comments on commit 518a5d3

Please sign in to comment.