spacemouse-webhid
Version:
An npm module for interfacing with the SpaceMouse devices in a browser
44 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestSpaceMice = requestSpaceMice;
exports.getOpenedSpaceMice = getOpenedSpaceMice;
exports.setupSpaceMouse = setupSpaceMouse;
const core_1 = require("@spacemouse-lib/core");
const web_hid_wrapper_js_1 = require("./web-hid-wrapper.js");
/** Prompts the user for which SpaceMouse device to select */
async function requestSpaceMice() {
return window.navigator.hid.requestDevice({
filters: Object.values(core_1.PRODUCTS).map((product) => ({
vendorId: product.vendorId,
productId: product.productId,
})),
});
}
/**
* Reopen previously selected devices.
* The browser remembers what the user previously allowed your site to access, and this will open those without the request dialog
*/
async function getOpenedSpaceMice() {
return await window.navigator.hid.getDevices();
}
/** Sets up a connection to a HID device (the SpaceMouse device) */
async function setupSpaceMouse(browserDevice) {
if (!browserDevice?.collections?.length)
throw Error(`device collections is empty`);
if (!browserDevice.productId)
throw Error(`Device has no productId!`);
if (!browserDevice.opened) {
await browserDevice.open();
}
const deviceWrap = new web_hid_wrapper_js_1.WebHIDDevice(browserDevice);
const spaceMouse = new core_1.SpaceMouse(deviceWrap, {
product: browserDevice.productName,
vendorId: browserDevice.vendorId,
productId: browserDevice.productId,
interface: null, // todo: Check what to use here (collection.usage?)
}, undefined);
// Wait for the device to initialize:
await spaceMouse.init();
return spaceMouse;
}
//# sourceMappingURL=methods.js.map