Extract waitForKbootIdle()

This commit is contained in:
László Monda
2018-01-31 02:22:41 +01:00
parent df14e2d569
commit 933a715ea5
+30 -15
View File
@@ -1,22 +1,37 @@
#!/usr/bin/env node #!/usr/bin/env node
const uhk = require('./uhk'); const uhk = require('./uhk');
function getCurrentKbootCommand() { let intervalMs = 100;
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.currentKbootCommand]))); let pingMessageInterval = 500;
const response = Buffer.from(device.readSync());
const currentKbootCommand = response[1]; async function waitForKbootIdle(device) {
if (currentKbootCommand == 0) { let timeoutMs = 10000;
console.log('Bootloader pinged.'); return new Promise((resolve, reject) => {
process.exit(0); const intervalId = setInterval(async function() {
} else { const response = await uhk.writeDevice(device, [uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.currentKbootCommand]);
console.log("Cannot ping the bootloader. Please reconnect the left keyboard half. It probably needs several tries, so keep reconnecting until you see this message."); const currentKbootCommand = response[1];
} if (currentKbootCommand == 0) {
console.log('Bootloader pinged.');
resolve();
clearInterval(intervalId);
return;
} else if (timeoutMs % pingMessageInterval === 0) {
console.log("Cannot ping the bootloader. Please reconnect the left keyboard half. It probably needs several tries, so keep reconnecting until you see this message.");
};
timeoutMs -= intervalMs;
if (timeoutMs < 0) {
reject();
clearInterval(intervalId);
return;
}
}, intervalMs);
});
} }
const device = uhk.getUhkDevice(); const device = uhk.getUhkDevice();
getCurrentKbootCommand(); (async function() {
await waitForKbootIdle(device);
setInterval(() => { })();
getCurrentKbootCommand();
}, 500);