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