Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/shims/node-hid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,29 @@ const ExtendedHID = {
const data = new Uint8Array(arr.slice(1));
await this._hidDevice?._device.sendReport(0, data);
}

async getReportSize(): Promise<number> {
const device = this._hidDevice?._device;

// Look explicitely for a VIA collection. There may be others.
const viaCollection = device?.collections
.find(c => c.usagePage === 0xFF60 && c.usage === 0x61);

// Sum up lengths of all the fields of the to get the size of output report item
const bits = (viaCollection?.outputReports?.[0]?.items ?? []).reduce(
(sum: number, item: HIDReportItem) =>
sum + (item.reportSize ?? 0) * (item.reportCount ?? 0),
0
);

return bits > 0 ? Math.ceil(bits / 8) : 32; // fallback for standard VIA keyboards
}

},
};



const promisify = (cb: Function) => () => {
return new Promise((res, rej) => {
cb((e: any, d: any) => {
Expand Down
14 changes: 13 additions & 1 deletion src/utils/keyboard-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const canConnect = (device: Device) => {

export class KeyboardAPI {
kbAddr: HIDAddress;
private _reportSize?: number;

constructor(path: string) {
this.kbAddr = path;
Expand Down Expand Up @@ -642,9 +643,20 @@ export class KeyboardAPI {
return cache[this.kbAddr].hid;
}

async getReportSize(): Promise<number> {
if (this._reportSize !== undefined) {
return this._reportSize;
}
let size = await this.getHID().getReportSize();

this._reportSize = size;
return size;
}

async _hidCommand(command: Command, bytes: Array<number> = []): Promise<any> {
const commandBytes = [...[COMMAND_START, command], ...bytes];
const paddedArray = new Array(33).fill(0);
const arraySize = await this.getReportSize() + 1;
const paddedArray = new Array(arraySize).fill(0);
commandBytes.forEach((val, idx) => {
paddedArray[idx] = val;
});
Expand Down