diff --git a/packages/usb/eeprom.js b/packages/usb/eeprom.js index 3b903231..20da1abd 100755 --- a/packages/usb/eeprom.js +++ b/packages/usb/eeprom.js @@ -9,23 +9,5 @@ if (eepromTransfer === undefined) { process.exit(1); } -const device = uhk.getUhkDevice(); -device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransfer, eepromTransfer.operation, eepromTransfer.configBuffer]))); -const buffer = Buffer.from(device.readSync()); -const responseCode = buffer[0]; -if (responseCode !== 0) { - console.error(`Write user config to eeprom failed. Response code: ${responseCode}`); - process.exit(1); -} +// const buffer = await uhk.writeDevice(device, [uhk.usbCommands.launchEepromTransfer, eepromTransfer.operation, eepromTransfer.configBuffer]); -function waitUntilKeyboardBusy() { - - device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getDeviceState]))); - const keyboardStateBuffer = Buffer.from(device.readSync()); - - if (keyboardStateBuffer[1] === 1) { - setTimeout(waitUntilKeyboardBusy, 200); - } -} - -waitUntilKeyboardBusy(); diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 4b6cbc20..651e52aa 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -276,7 +276,7 @@ async function updateFirmwares(firmwarePath) { await uhk.updateModuleFirmware(uhk.moduleSlotToI2cAddress.leftHalf, uhk.moduleSlotToId.leftHalf, `${firmwarePath}/modules/uhk60-left.bin`); } -async function writeUserConfig(device, configBuffer, isHardwareConfig) { +async function writeConfig(device, configBuffer, isHardwareConfig) { const chunkSize = 60; let offset = 0; let chunkSizeToRead; @@ -303,6 +303,19 @@ async function writeUserConfig(device, configBuffer, isHardwareConfig) { } } +async function applyConfig(device) { + await uhk.writeDevice(device, [uhk.usbCommands.applyConfig]); +} + +async function launchEepromTransfer(device, operation, configBuffer) { + const buffer = await uhk.writeDevice(device, [uhk.usbCommands.launchEepromTransfer, operation, configBuffer]); + isBusy = true; + do { + const buffer = uhk.writeDevice(device, [uhk.usbCommands.getDeviceState]); + isBusy = buffer[1] === 1; + } while (isBusy); +}; + uhk = exports = module.exports = moduleExports = { bufferToString, getUint16, @@ -324,7 +337,9 @@ uhk = exports = module.exports = moduleExports = { waitForKbootIdle, updateModuleFirmware, updateFirmwares, - writeUserConfig, + writeConfig, + applyConfig, + launchEepromTransfer, usbCommands: { getDeviceProperty : 0x00, reenumerate : 0x01, diff --git a/packages/usb/update-firmwares.js b/packages/usb/update-firmwares.js index fc3bc03d..880d0f8d 100755 --- a/packages/usb/update-firmwares.js +++ b/packages/usb/update-firmwares.js @@ -1,4 +1,5 @@ #!/usr/bin/env node +const fs = require('fs'); const program = require('commander'); const tmp = require('tmp'); const decompress = require('decompress'); @@ -32,9 +33,11 @@ require('shelljs/global'); await uhk.updateFirmwares(firmwarePath); if (program.overwriteUserConfig) { - exec(`${__dirname}/write-config.js ${firmwarePath}/devices/uhk60-right/config.bin`); - exec(`${__dirname}/apply-config.js`); - exec(`${__dirname}/eeprom.js writeUserConfig`); + const device = uhk.getUhkDevice(); + const configBuffer = fs.readFileSync(`${firmwarePath}/devices/uhk60-right/config.bin`); + await uhk.writeConfig(device, configBuffer, false); + await uhk.applyConfig(device); + await uhk.launchEepromTransfer(device, uhk.eepromOperations.write, uhk.eepromTransfer.writeUserConfig); } config.verbose = false; diff --git a/packages/usb/write-config.js b/packages/usb/write-config.js index 75b81f37..73c782e8 100755 --- a/packages/usb/write-config.js +++ b/packages/usb/write-config.js @@ -20,7 +20,7 @@ const uhk = require('./uhk'); const configBin = program.args[0]; const isHardwareConfig = program.hardwareConfig; const configTypeString = isHardwareConfig ? 'hardware' : 'user'; - let configBuffer = fs.readFileSync(configBin); + const configBuffer = fs.readFileSync(configBin); await uhk.writeUserConfig(device, configBuffer, isHardwareConfig); -})(); \ No newline at end of file +})();