Extract reenumerate() as an async function in reenumerate.js
This commit is contained in:
@@ -23,29 +23,41 @@ if (enumerationModeId === undefined) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Trying to reenumerate as ${enumerationMode}...`);
|
||||
setInterval(() => {
|
||||
pollingTimeoutMs -= pollingIntervalMs;
|
||||
function reenumerate(enumerationModeId, bootloaderTimeoutMs) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(`Trying to reenumerate as ${enumerationMode}...`);
|
||||
const intervalId = setInterval(() => {
|
||||
pollingTimeoutMs -= pollingIntervalMs;
|
||||
|
||||
const foundDevice = HID.devices().find(device =>
|
||||
device.vendorId === uhk.vendorId && device.productId === uhk.enumerationModeIdToProductId[enumerationModeId]);
|
||||
const foundDevice = HID.devices().find(device =>
|
||||
device.vendorId === uhk.vendorId && device.productId === uhk.enumerationModeIdToProductId[enumerationModeId]);
|
||||
|
||||
if (foundDevice) {
|
||||
console.log(`${enumerationMode} is up`);
|
||||
process.exit(0);
|
||||
}
|
||||
if (foundDevice) {
|
||||
console.log(`${enumerationMode} is up`);
|
||||
resolve();
|
||||
clearInterval(intervalId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pollingTimeoutMs <= 0) {
|
||||
console.log(`Couldn't reenumerate as ${enumerationMode}`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (pollingTimeoutMs <= 0) {
|
||||
console.log(`Couldn't reenumerate as ${enumerationMode}`);
|
||||
reject();
|
||||
clearInterval(intervalId);
|
||||
return;
|
||||
}
|
||||
|
||||
let device = uhk.getUhkDevice();
|
||||
if (device && !jumped) {
|
||||
console.log(`UHK found, reenumerating as ${enumerationMode}`);
|
||||
let message = new Buffer([uhk.usbCommands.reenumerate, enumerationModeId, ...uhk.uint32ToArray(bootloaderTimeoutMs)]);
|
||||
device.write(uhk.getTransferData(message));
|
||||
jumped = true;
|
||||
}
|
||||
let device = uhk.getUhkDevice();
|
||||
if (device && !jumped) {
|
||||
console.log(`UHK found, reenumerating as ${enumerationMode}`);
|
||||
let message = new Buffer([uhk.usbCommands.reenumerate, enumerationModeId, ...uhk.uint32ToArray(bootloaderTimeoutMs)]);
|
||||
device.write(uhk.getTransferData(message));
|
||||
jumped = true;
|
||||
}
|
||||
|
||||
}, pollingIntervalMs);
|
||||
}, pollingIntervalMs);
|
||||
})
|
||||
};
|
||||
|
||||
(async function() {
|
||||
await reenumerate(enumerationModeId, bootloaderTimeoutMs);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user