usb
Version:
Library to access USB devices
87 lines (86 loc) • 3.12 kB
TypeScript
import { UsbDevice, Emitter } from '../index.js';
/**
* USB Options
*/
interface USBOptions {
/**
* Optional `device found` callback function to allow the user to select a device
*/
devicesFound?: (devices: UsbDevice[]) => Promise<UsbDevice | void>;
/**
* Optional array of preconfigured allowed devices
*/
allowedDevices?: USBDeviceFilter[];
/**
* Optional flag to automatically allow all devices
*/
allowAllDevices?: boolean;
/**
* Optional timeout (in milliseconds) to use for the device control transfers
*/
deviceTimeout?: number;
}
/**
* WebUSB class
*
* ### Events
*
* | Name | Event | Description |
* | ---- | ----- | ----------- |
* | `connect` | {@link USBConnectionEvent} | Device connected |
* | `disconnect` | {@link USBConnectionEvent} | Device disconnected |
*/
declare class WebUSB extends EventTarget implements USB {
private options;
protected nativeEmitter: Emitter;
protected authorisedDevices: Set<USBDeviceFilter>;
protected listenerCount: number;
protected eventListeners: Map<string, Set<EventListener>>;
protected knownDevices: Map<string, UsbDevice>;
constructor(options?: USBOptions);
private deviceConnectCallback;
private deviceDisconnectCallback;
addEventListener(type: 'connect' | 'disconnect', listener: (this: this, ev: USBConnectionEvent) => void): void;
addEventListener(type: 'connect' | 'disconnect', listener: EventListener): void;
removeEventListener(type: 'connect' | 'disconnect', callback: (this: this, ev: USBConnectionEvent) => void): void;
removeEventListener(type: 'connect' | 'disconnect', callback: EventListener): void;
private _onconnect;
set onconnect(fn: (ev: USBConnectionEvent) => void);
private _ondisconnect;
set ondisconnect(fn: (ev: USBConnectionEvent) => void);
/**
* Requests a single Web USB device
* @param options The options to use when scanning
* @returns Promise containing the selected device
*/
requestDevice(options?: USBDeviceRequestOptions): Promise<UsbDevice>;
/**
* Gets all allowed Web USB devices which are connected
* @returns Promise containing an array of devices
*/
getDevices(): Promise<UsbDevice[]>;
/**
* Convenience method to get the first device with the specified VID and PID, or `undefined` if no such device is present.
* @param vid
* @param pid
*/
findDeviceByIds(vid: number, pid: number): Promise<UsbDevice | undefined>;
/**
* Convenience method to get the device with the specified serial number, or `undefined` if no such device is present.
* @param serialNumber
*/
findDeviceBySerial(serialNumber: string): Promise<UsbDevice | undefined>;
private loadDevices;
private quickFilter;
private filterDevice;
private isAuthorisedDevice;
}
/**
* Default USB object (allows all devices by default)
*/
declare const usb: WebUSB;
/**
* Default WebUSB object (mimics navigator.usb)
*/
declare const webusb: WebUSB | USB;
export { usb, webusb, WebUSB, USBOptions, };