Skip to content

Commit

Permalink
feat: BLE HID pairing
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb committed Dec 30, 2024
1 parent b93deb7 commit 98cb4c6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/uhk-common/src/models/device-connection-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ export interface DeviceConnectionState {
communicationInterfaceAvailable: boolean;
halvesInfo: HalvesInfo;
hardwareModules?: HardwareModules;
/**
* Each element describe the hostConnection is the user-config are paired to the keyboard or not.
* If the value is 1 then paired.
*/
pairedDevices: number[];
udevRulesInfo: UdevRulesInfo;
}
1 change: 1 addition & 0 deletions packages/uhk-common/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './get-formatted-timestamp.js';
export * from './get-md5-hash-from-file-name.js';
export * from './get-slot-id-name.js';
export * from './helpers.js';
export * from './is-bit-set.js';
export * from './is-device-protocol-support-firmware-checksum.js';
export * from './is-device-protocol-support-git-info.js';
export * from './is-device-protocol-support-status-error.js';
Expand Down
3 changes: 3 additions & 0 deletions packages/uhk-common/src/util/is-bit-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isBitSet(value: number, bitPosition: number): boolean {
return (value & (1 << bitPosition)) !== 0;
}
1 change: 1 addition & 0 deletions packages/uhk-usb/src/models/device-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export interface DeviceState {
activeLayerToggled: boolean;
leftKeyboardHalfSlot: string;
leftModuleSlot: string;
newPairedDevice: boolean;
rightModuleSlot: string;
}
20 changes: 19 additions & 1 deletion packages/uhk-usb/src/uhk-hid-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
DeviceConnectionState,
FIRMWARE_UPGRADE_METHODS,
HalvesInfo,
HOST_CONNECTION_COUNT_MAX,
isBitSet,
isEqualArray,
LeftSlotModules,
LogService,
Expand Down Expand Up @@ -337,6 +339,7 @@ export class UhkHidDevice {
isMacroStatusDirty: false,
leftHalfDetected: false,
multiDevice: await getNumberOfConnectedDevices(this.options) > 1,
pairedDevices: [],
udevRulesInfo: await this.getUdevInfoAsync(),
};

Expand Down Expand Up @@ -414,6 +417,10 @@ export class UhkHidDevice {
const deviceState = await this.getDeviceState();
result.halvesInfo = calculateHalvesState(deviceState);
result.isMacroStatusDirty = deviceState.isMacroStatusDirty;

if (deviceState.newPairedDevice) {
result.pairedDevices = await this.getPairedDevices();
}
} else if (!result.connectedDevice) {
this._device = undefined;
}
Expand Down Expand Up @@ -607,19 +614,21 @@ export class UhkHidDevice {
}

async getDeviceState(): Promise<DeviceState> {
this.logService.usb('[UhkHidDevice] USB[T]: Get device state');
const buffer = await this.write(Buffer.from([UsbCommand.GetDeviceState]));
const activeLayerNumber = buffer[6] & 0x7f;

return {
isEepromBusy: buffer[1] !== 0,
isMacroStatusDirty: buffer[7] !== 0,
areHalvesMerged: (buffer[2] & 0x1) !== 0,
areHalvesMerged: isBitSet(buffer[2], 0),
isLeftHalfConnected: buffer[3] !== 0,
activeLayerNumber,
activeLayerName: LAYER_NUMBER_TO_STRING[activeLayerNumber],
activeLayerToggled: (buffer[6] & 0x80) === 1,
leftKeyboardHalfSlot: MODULE_ID_TO_STRING[buffer[3]],
leftModuleSlot: MODULE_ID_TO_STRING[buffer[4]],
newPairedDevice: isBitSet(buffer[2], 2),
rightModuleSlot: MODULE_ID_TO_STRING[buffer[5]]
};
}
Expand Down Expand Up @@ -672,6 +681,15 @@ export class UhkHidDevice {
}
}

async getPairedDevices(): Promise<number[]> {
this.logService.usb('[UhkHidDevice] USB[T]: Read paired devices');
const command = Buffer.from([UsbCommand.GetProperty, DevicePropertyIds.NewPairings]);
const buffer = await this.write(command);
const pairedDevices = convertBufferToIntArray(buffer);

return pairedDevices.slice(0, HOST_CONNECTION_COUNT_MAX + 1);
}

async getProtocolVersions(): Promise<ProtocolVersions> {
if (this._protocolVersions) {
return this._protocolVersions;
Expand Down
1 change: 1 addition & 0 deletions packages/usb/get-device-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ setInterval(async function () {
`isEepromBusy: ${state.isEepromBusy ? 'yes' : 'no'} | \
isMacroStatusDirty: ${state.isMacroStatusDirty ? 'yes' : 'no'} | \
areHalvesMerged: ${state.areHalvesMerged ? 'yes' : 'no'} | \
newPairedDevice: ${state.newPairedDevice ? 'yes' : 'no'} | \
leftKeyboardHalfSlot:${state.leftKeyboardHalfSlot} | \
leftModuleSlot:${state.leftModuleSlot} | \
rightModuleSlot:${state.rightModuleSlot} | \
Expand Down

0 comments on commit 98cb4c6

Please sign in to comment.