Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: wait for devices in usb script #2442

Merged
merged 6 commits into from
Dec 14, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
import * as path from 'path';
import * as fs from 'fs';
import {
FIRMWARE_UPGRADE_METHODS,
LEFT_HALF_MODULE,
UHK_60_DEVICE,
UHK_60_V2_DEVICE,
UHK_80_DEVICE_LEFT,
} from 'uhk-common';
import {
getCurrentUhkDeviceProduct,
getDeviceFirmwarePath,
getDeviceUserConfigPath,
getFirmwarePackageJson,
getModuleFirmwarePath,
isUhkDeviceConnected,
waitForUhkDeviceConnected,
waitForDevices,
} from 'uhk-usb';

import Uhk, { errorHandler, getUhkDeviceProductFromArg, getDevicesOptions, yargs } from './src/index.js';
Expand All @@ -30,7 +27,7 @@ const devicesOptions = getDevicesOptions(DEVICES);
(async function () {
try {
const argv = yargs
.scriptName('./factory-update.ts')
.scriptName('./factory-update-uhk60.ts')
.usage(`Usage: $0 <firmwarePath> {${devicesOptions} {iso|ansi}`)
.demandCommand(3)
.option('set-serial-number', {
Expand All @@ -57,10 +54,7 @@ const devicesOptions = getDevicesOptions(DEVICES);
process.exit(1);
}


const leftFirmwarePath = uhkDeviceProduct.firmwareUpgradeMethod === FIRMWARE_UPGRADE_METHODS.MCUBOOT
? getDeviceFirmwarePath(UHK_80_DEVICE_LEFT, packageJson)
: getModuleFirmwarePath(LEFT_HALF_MODULE, packageJson);
const leftFirmwarePath = getModuleFirmwarePath(LEFT_HALF_MODULE, packageJson);

if (!fs.existsSync(leftFirmwarePath)) {
console.error('Left firmware path not found!');
Expand All @@ -80,26 +74,11 @@ const devicesOptions = getDevicesOptions(DEVICES);

const { operations } = Uhk(argv);
await operations.updateDeviceFirmware(rightFirmwarePath, uhkDeviceProduct);
if (uhkDeviceProduct.firmwareUpgradeMethod === FIRMWARE_UPGRADE_METHODS.MCUBOOT) {
if (!(await isUhkDeviceConnected(UHK_80_DEVICE_LEFT))) {
console.log(`[DeviceService] Please connect your ${UHK_80_DEVICE_LEFT.logName} keyboard with USB cable.`);
}
await waitForUhkDeviceConnected(UHK_80_DEVICE_LEFT);

await operations.updateFirmwareWithMcuManager(leftFirmwarePath, UHK_80_DEVICE_LEFT);

if (!(await isUhkDeviceConnected(uhkDeviceProduct))) {
console.log(`[DeviceService] Please connect your ${uhkDeviceProduct.logName} keyboard with USB cable.`);
}
await waitForUhkDeviceConnected(uhkDeviceProduct);
}
else {
await operations.updateLeftModuleWithKboot(leftFirmwarePath, uhkDeviceProduct);
}
await waitForDevices(uhkDeviceProduct.keyboard);
await operations.updateLeftModuleWithKboot(leftFirmwarePath, uhkDeviceProduct);
const configBuffer = fs.readFileSync(userConfigPath) as any;
await operations.saveUserConfiguration(configBuffer);
await operations.saveHardwareConfiguration(layout === 'iso', deviceId, argv.setSerialNumber);
await operations.switchKeymap('TES');
console.log('All done!');
} catch (error) {
await errorHandler(error);
Expand Down
7 changes: 5 additions & 2 deletions packages/usb/factory-update-uhk80.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
getDeviceFirmwarePath,
getFirmwarePackageJson,
TmpFirmware,
snooze,
UhkHidDevice,
UhkOperations,
UsbVariables,
waitForDevices,
waitForUhkDeviceConnected,
} from 'uhk-usb';

Expand Down Expand Up @@ -71,7 +71,10 @@ try {

// just wait until devices be ready. After the reenumeration the halves start to communicate with each other
// give them some time to finish
await snooze(5000);
await Promise.all([
waitForDevices(UHK_80_DEVICE.keyboard),
waitForDevices(UHK_80_DEVICE_LEFT.keyboard),
]);

// Need to reload hid devices because after the reenumeration maybe the HID device path changed
connectedDevices = await getHidDevices();
Expand Down
1 change: 1 addition & 0 deletions packages/usb/wait-for-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ while (new Date().getTime() - startTime.getTime() < timeout && !found) {
}

if (found) {
await snooze(1000);
process.exit(0);
}
else {
Expand Down
Loading