ant-plus-next / USBDriverBase
Interface representing the base functionalities of a USB driver. This interface extends EventEmitter to allow for emitting and handling events. It provides methods to open, close, read, write, and manage sensors.
EventEmitter
maxChannels:
number
The maximum number of channels available for communication.
console.log(driver.maxChannels); // Outputs the maximum number of channels available
usedChannels:
number
The number of channels currently in use.
console.log(driver.usedChannels); // Outputs the number of channels in use
attach(
sensor
,forScan
):Promise
<boolean
>
Attaches a sensor to the USB driver and assigns it a channel.
The sensor to attach.
boolean
Whether the sensor is being attached for scanning.
Promise
<boolean
>
A promise that resolves to true if the sensor was successfully attached, otherwise false.
const sensor = new SomeSensor();
const attached = await driver.attach(sensor, true);
console.log(attached); // true if successfully attached
canAttach():
Promise
<boolean
>
Checks if a new sensor can be attached based on the available channels.
Promise
<boolean
>
A promise that resolves to true if a new sensor can be attached, otherwise false.
const canAttach = await driver.canAttach();
console.log(canAttach); // true if another sensor can be attached
canScan():
Promise
<boolean
>
Checks if the USB driver is capable of scanning for devices.
Promise
<boolean
>
A promise that resolves to true if the driver can scan, otherwise false.
const canScan = await driver.canScan();
console.log(canScan); // true if the device can scan
close():
Promise
<void
>
Closes the connection to the USB device.
Promise
<void
>
A promise that resolves when the device is successfully closed.
await driver.close();
console.log("Device closed.");
detach(
sensor
):Promise
<boolean
>
Detaches a sensor from the USB driver.
The sensor to detach.
Promise
<boolean
>
A promise that resolves to true if the sensor was successfully detached, otherwise false.
const detached = await driver.detach(sensor);
console.log(detached); // true if successfully detached
isPresent():
Promise
<boolean
>
Checks if a USB device is currently present and connected.
Promise
<boolean
>
A promise that resolves to true if the device is present, otherwise false.
const isPresent = await driver.isPresent();
console.log(isPresent); // true if the device is present
isScanning():
Promise
<boolean
>
Checks if the USB driver is currently scanning for devices.
Promise
<boolean
>
A promise that resolves to true if the driver is scanning, otherwise false.
const isScanning = await driver.isScanning();
console.log(isScanning); // true if the device is scanning
open():
Promise
<boolean
>
Opens the connection to the USB device.
Promise
<boolean
>
A promise that resolves to true if the device was successfully opened, otherwise false.
const driver: USBDriverBase = new SomeUSBDriver();
const isOpen = await driver.open();
console.log(isOpen); // true if successfully opened, false otherwise
read(
data
):Promise
<void
>
Reads data from the USB device.
Uint8Array
<ArrayBufferLike
>
The data received from the USB device.
Promise
<void
>
A promise that resolves when the data has been processed.
const data = new Uint8Array([0x01, 0x02, 0x03]);
await driver.read(data);
reset():
Promise
<void
>
Resets the USB device and channels.
Promise
<void
>
A promise that resolves when the device has been reset.
await driver.reset();
console.log("Device reset.");
write(
data
):Promise
<void
>
Writes data to the USB device.
Uint8Array
<ArrayBufferLike
>
The data to send to the USB device.
Promise
<void
>
A promise that resolves when the data has been sent.
const data = new Uint8Array([0xA4, 0xB5]);
await driver.write(data);