@blackmagic-controller/node
Version:
An npm module for interfacing with the Blackmagic usb/bluetooth controllers in node
89 lines • 3.13 kB
JavaScript
import { DEVICE_MODELS, VENDOR_ID } from '@blackmagic-controller/core';
import * as HID from 'node-hid';
import { NodeHIDDevice } from './hid-device.js';
import { BlackmagicControllerNode } from './wrapper.js';
export { VENDOR_ID, BlackmagicControllerModelId, getBlackmagicControllerName, } from '@blackmagic-controller/core';
/**
* Scan for and list detected devices
*/
export async function listBlackmagicControllers() {
const devices = {};
for (const dev of await HID.devicesAsync()) {
if (dev.path && !devices[dev.path]) {
const info = getBlackmagicControllerDeviceInfo(dev);
if (info)
devices[dev.path] = info;
}
}
return Object.values(devices);
}
/**
* If the provided device is a supported blackmagic controller, get the info about it
*/
export function getBlackmagicControllerDeviceInfo(dev) {
const model = DEVICE_MODELS.find((m) => m.productIds.includes(dev.productId));
if (model && dev.vendorId === VENDOR_ID && dev.path) {
return {
model: model.id,
path: dev.path,
serialNumber: dev.serialNumber,
};
}
else {
return null;
}
}
/**
* Get the info of a device if the given path is a supported blackmagic controller
*/
export async function getBlackmagicControllerInfo(path) {
const allDevices = await listBlackmagicControllers();
return allDevices.find((dev) => dev.path === path);
}
/**
* Open a supported blackmagic controller
* @param devicePath The path of the device to open.
* @param userOptions Options to customise the device behvaiour
*/
export async function openBlackmagicController(devicePath, userOptions) {
// const options: Required<OpenBlackmagicControllerOptions> = {
// ...userOptions,
// }
let hidDevice;
let model;
try {
hidDevice = await HID.HIDAsync.open(devicePath);
const deviceInfo = await hidDevice.getDeviceInfo();
model = DEVICE_MODELS.find((m) => deviceInfo.vendorId === VENDOR_ID && m.productIds.includes(deviceInfo.productId));
if (!model) {
throw new Error(`Device path at "${devicePath}" is not a supported Blackmagic controller.`);
}
}
catch (e) {
if (hidDevice)
await hidDevice.close().catch(() => null); // Suppress error
throw e;
}
let device;
try {
device = new NodeHIDDevice(hidDevice);
// Perform authentication if requried by the model
let nextAuthMaxDelay = null;
if (model.authenticate) {
nextAuthMaxDelay = await model.authenticate(device);
}
const fullOptions = {
// ...options,
nextAuthMaxDelay,
authenticate: model.authenticate ?? null,
};
const rawSteamdeck = model.factory(device, fullOptions);
return new BlackmagicControllerNode(rawSteamdeck, userOptions?.clearOnClose ?? false);
}
catch (e) {
if (device)
await device.close().catch(() => null); // Suppress error
throw e;
}
}
//# sourceMappingURL=index.js.map