From f9b7260be6fc0c83f15db59b19296719b9507995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sun, 1 Apr 2018 01:23:50 +0200 Subject: [PATCH] Extract uhk.writeUserConfig() --- packages/usb/uhk.js | 28 ++++++++++++++++++ packages/usb/write-config.js | 56 +++++++++++------------------------- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 340965a8..4b6cbc20 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -276,6 +276,33 @@ async function updateFirmwares(firmwarePath) { await uhk.updateModuleFirmware(uhk.moduleSlotToI2cAddress.leftHalf, uhk.moduleSlotToId.leftHalf, `${firmwarePath}/modules/uhk60-left.bin`); } +async function writeUserConfig(device, configBuffer, isHardwareConfig) { + const chunkSize = 60; + let offset = 0; + let chunkSizeToRead; + let buffer = await uhk.writeDevice(device, [uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]); + const hardwareConfigMaxSize = buffer[1] + (buffer[2]<<8); + const userConfigMaxSize = buffer[3] + (buffer[4]<<8); + const configMaxSize = isHardwareConfig ? hardwareConfigMaxSize : userConfigMaxSize; + const configSize = Math.min(configMaxSize, configBuffer.length); + + while (offset < configSize) { + const usbCommand = isHardwareConfig ? uhk.usbCommands.writeHardwareConfig : uhk.usbCommands.writeStagingUserConfig; + chunkSizeToRead = Math.min(chunkSize, configSize - offset); + + if (chunkSizeToRead === 0) { + break; + } + + buffer = [ + ...[usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8], + ...configBuffer.slice(offset, offset+chunkSizeToRead) + ]; + await uhk.writeDevice(device, buffer) + offset += chunkSizeToRead; + } +} + uhk = exports = module.exports = moduleExports = { bufferToString, getUint16, @@ -297,6 +324,7 @@ uhk = exports = module.exports = moduleExports = { waitForKbootIdle, updateModuleFirmware, updateFirmwares, + writeUserConfig, usbCommands: { getDeviceProperty : 0x00, reenumerate : 0x01, diff --git a/packages/usb/write-config.js b/packages/usb/write-config.js index 9c6404c0..75b81f37 100755 --- a/packages/usb/write-config.js +++ b/packages/usb/write-config.js @@ -2,47 +2,25 @@ const program = require('commander'); const fs = require('fs'); const uhk = require('./uhk'); -const device = uhk.getUhkDevice(); -require('shelljs/global'); - -program - .usage(`config.bin`) - .option('-h, --hardware-config', 'Write the hardware config instead of the user config') - .parse(process.argv); - -if (program.args.length == 0) { - console.error('No binary config file specified.'); - exit(1); -} - -const configBin = program.args[0]; -const chunkSize = 60; -const isHardwareConfig = program.hardwareConfig; -const configTypeString = isHardwareConfig ? 'hardware' : 'user'; -let offset = 0; -let configBuffer = fs.readFileSync(configBin); -let chunkSizeToRead; (async function() { - let buffer = await uhk.writeDevice(device, [uhk.usbCommands.getDeviceProperty, uhk.devicePropertyIds.configSizes]); - const hardwareConfigMaxSize = buffer[1] + (buffer[2]<<8); - const userConfigMaxSize = buffer[3] + (buffer[4]<<8); - const configMaxSize = isHardwareConfig ? hardwareConfigMaxSize : userConfigMaxSize; - const configSize = Math.min(configMaxSize, configBuffer.length); + const device = uhk.getUhkDevice(); + require('shelljs/global'); - while (offset < configSize) { - const usbCommand = isHardwareConfig ? uhk.usbCommands.writeHardwareConfig : uhk.usbCommands.writeStagingUserConfig; - chunkSizeToRead = Math.min(chunkSize, configSize - offset); + program + .usage(`config.bin`) + .option('-h, --hardware-config', 'Write the hardware config instead of the user config') + .parse(process.argv); - if (chunkSizeToRead === 0) { - break; - } - - buffer = [ - ...[usbCommand, chunkSizeToRead, offset & 0xff, offset >> 8], - ...configBuffer.slice(offset, offset+chunkSizeToRead) - ]; - await uhk.writeDevice(device, buffer) - offset += chunkSizeToRead; + if (program.args.length == 0) { + console.error('No binary config file specified.'); + exit(1); } -})(); + + const configBin = program.args[0]; + const isHardwareConfig = program.hardwareConfig; + const configTypeString = isHardwareConfig ? 'hardware' : 'user'; + let configBuffer = fs.readFileSync(configBin); + + await uhk.writeUserConfig(device, configBuffer, isHardwareConfig); +})(); \ No newline at end of file