diff --git a/packages/usb/set-i2c-baud-rate.js b/packages/usb/set-i2c-baud-rate.js new file mode 100755 index 00000000..cb44eaca --- /dev/null +++ b/packages/usb/set-i2c-baud-rate.js @@ -0,0 +1,24 @@ +#!/usr/bin/env node +const path = require('path'); +const uhk = require('./uhk'); +const device = uhk.getUhkDevice(); + +let programName = path.basename(process.argv[1]); + +if (process.argv.length !== 3) { + console.log(`Usage: ${programName} +Adjust the frequency of the main I2C bus via which the right keyboard half communicates with the left half and the add-on modules. +Example values: + * 400000 Hz - Highest speed. This will probably make communication unreliable. Not recommended. + * 100000 Hz - Default speed. Should work in most cases. + * 10000 Hz - Low speed. Should help when connection is spotty. It'll make initial LED display updates visibly slower. +You're free to use any value in between and test the results.`); + process.exit(1); +} + +let bps = process.argv[2]; +let buffer = new Buffer(uhk.pushUint32([uhk.usbCommands.setI2cBaudRate], +bps)); +console.log(buffer); +device.write(uhk.getTransferData(buffer)); +const response = Buffer.from(device.readSync()); +console.log(response); diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index 999d7cf9..45606777 100644 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -14,6 +14,22 @@ function bufferToString(buffer) { return str; } +function getUint16(buffer, offset) { + return (buffer[offset]) + (buffer[offset+1] * 2**8); +} + +function getUint32(buffer, offset) { + return (buffer[offset]) + (buffer[offset+1] * 2**8) + (buffer[offset+2] * 2**16) + (buffer[offset+3] * 2**24); +} + +function pushUint32(array, value) { + array.push((value >> 0) & 0xff); + array.push((value >> 8) & 0xff); + array.push((value >> 16) & 0xff); + array.push((value >> 24) & 0xff); + return array; +} + function getUhkDevice() { const foundDevice = HID.devices().find(device => device.vendorId === 0x1d50 && device.productId === 0x6122 && @@ -57,6 +73,9 @@ let eepromOperations = { exports = module.exports = moduleExports = { bufferToString, + getUint16, + getUint32, + pushUint32, getUhkDevice, getBootloaderDevice, getTransferData, @@ -78,6 +97,7 @@ exports = module.exports = moduleExports = { setLedPwmBrightness : 0x0d, getModuleProperty : 0x0e, getSlaveI2cErrors : 0x0f, + setI2cBaudRate : 0x10, }, enumerationModes: { bootloader: 0,