diff --git a/usb/blink-test-led.js b/usb/blink-test-led.js index bec6258a..c45b222d 100755 --- a/usb/blink-test-led.js +++ b/usb/blink-test-led.js @@ -1,34 +1,15 @@ #!/usr/bin/env node 'use strict'; -var usb = require('usb'); -var util = require('./util'); - -var vid = 0x16d3; -var pid = 0x05ea; -var test_led_command_id = 2; - -var device = usb.findByIds(vid, pid); -device.open(); - -var usbInterface = device.interface(0); - -// https://github.com/tessel/node-usb/issues/147 -// The function 'isKernelDriverActive' is not available on Windows and not even needed. -if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) { - usbInterface.detachKernelDriver(); -} -usbInterface.claim(); - -var endpointIn = usbInterface.endpoints[0]; -var endpointOut = usbInterface.endpoints[1]; +let uhk = require('./uhk'); +let [endpointIn, endpointOut] = uhk.getUsbEndpoints(); var state = 1; setInterval(function() { state = state ? 0 : 1 console.log('Sending ', state); - endpointOut.transfer(new Buffer([test_led_command_id, state]), function(err) { + endpointOut.transfer(new Buffer([uhk.usbCommands.setTestLed, state]), function(err) { if (err) { console.error("USB error: %s", err); process.exit(1); @@ -38,7 +19,7 @@ setInterval(function() { console.error("USB error: %s", err2); process.exit(2); } - console.log('Received', util.bufferToString(receivedBuffer)); + console.log('Received', uhk.bufferToString(receivedBuffer)); }) }); }, 500) diff --git a/usb/jump-to-bootloader.js b/usb/jump-to-bootloader.js index 76a1c28d..2986ebd4 100755 --- a/usb/jump-to-bootloader.js +++ b/usb/jump-to-bootloader.js @@ -1,30 +1,9 @@ #!/usr/bin/env node -'use strict'; +let uhk = require('./uhk'); +let [endpointIn, endpointOut] = uhk.getUsbEndpoints(); -var usb = require('usb'); -var util = require('./util'); - -var vid = 0x16d3; -var pid = 0x05ea; -var jumpToBootloaderCommandId = 1; - -var device = usb.findByIds(vid, pid); -device.open(); - -var usbInterface = device.interface(0); - -// https://github.com/tessel/node-usb/issues/147 -// The function 'isKernelDriverActive' is not available on Windows and not even needed. -if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) { - usbInterface.detachKernelDriver(); -} -usbInterface.claim(); - -var endpointIn = usbInterface.endpoints[0]; -var endpointOut = usbInterface.endpoints[1]; - -var payload = new Buffer([jumpToBootloaderCommandId]); -console.log('Sending ', util.bufferToString(payload)); +var payload = new Buffer([uhk.usbCommands.jumpToBootloader]); +console.log('Sending ', uhk.bufferToString(payload)); endpointOut.transfer(payload, function(err) { if (err) { console.error("USB error: %s", err); @@ -35,6 +14,6 @@ endpointOut.transfer(payload, function(err) { console.error("USB error: %s", err2); process.exit(2); } - console.log('Received', util.bufferToString(receivedBuffer)); + console.log('Received', uhk.bufferToString(receivedBuffer)); }) }); diff --git a/usb/pulse-leds.js b/usb/pulse-leds.js index d98b98c1..3fdd349d 100755 --- a/usb/pulse-leds.js +++ b/usb/pulse-leds.js @@ -1,32 +1,12 @@ #!/usr/bin/env node -'use strict'; +let uhk = require('./uhk'); +let [endpointIn, endpointOut] = uhk.getUsbEndpoints(); -var usb = require('usb'); -var util = require('./util'); - -var vid = 0x16d3; -var pid = 0x05ea; var ledMatrixSize = 144; var ledCountToUpdatePerCommand = ledMatrixSize / 3; -var writeLedDriverCommandId = 3; var leftLedDriverAddress = 0b1110100; var rightLedDriverAddress = 0b1110111; -var device = usb.findByIds(vid, pid); -device.open(); - -var usbInterface = device.interface(0); - -// https://github.com/tessel/node-usb/issues/147 -// The function 'isKernelDriverActive' is not available on Windows and not even needed. -if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) { - usbInterface.detachKernelDriver(); -} -usbInterface.claim(); - -var endpointIn = usbInterface.endpoints[0]; -var endpointOut = usbInterface.endpoints[1]; - var state = 1; var ledsLeft = new Buffer(ledMatrixSize); @@ -39,7 +19,7 @@ var matrixId = 0; var initLedCommands = [ [ // only enable the LEDs that are actually in the matrix - writeLedDriverCommandId, + uhk.usbCommands.writeLedDriver, leftLedDriverAddress, 19, 0, @@ -54,7 +34,7 @@ var initLedCommands = [ 0, 0b00011111, ], [ // only enable the LEDs that are actually in the matrix - writeLedDriverCommandId, + uhk.usbCommands.writeLedDriver, rightLedDriverAddress, 19, 0, @@ -68,9 +48,9 @@ var initLedCommands = [ 0, 0, 0, 0, ], - [writeLedDriverCommandId, leftLedDriverAddress, 2, 0xfd, 0x0b], // switch to function page - [writeLedDriverCommandId, leftLedDriverAddress, 2, 0xc2, 0xff], // enable the ghost image prevention bit - [writeLedDriverCommandId, leftLedDriverAddress, 2, 0xfd, 0x00], // switch to page 0 + [uhk.usbCommands.writeLedDriver, leftLedDriverAddress, 2, 0xfd, 0x0b], // switch to function page + [uhk.usbCommands.writeLedDriver, leftLedDriverAddress, 2, 0xc2, 0xff], // enable the ghost image prevention bit + [uhk.usbCommands.writeLedDriver, leftLedDriverAddress, 2, 0xfd, 0x00], // switch to page 0 ] var ledCommandId = 0; @@ -97,14 +77,14 @@ function updateLeds() { // console.log('update') var buffer = Buffer.concat([ new Buffer([ - writeLedDriverCommandId, + uhk.usbCommands.writeLedDriver, matrixId ? rightLedDriverAddress : leftLedDriverAddress, ledCountToUpdatePerCommand, 0x24 + ledIndex ]), (matrixId ? ledsRight : ledsLeft).slice(ledIndex, ledIndex + ledCountToUpdatePerCommand) ]); - console.log('iter: '+letterIdx+' out:', util.bufferToString(buffer)) + console.log('iter: '+letterIdx+' out:', uhk.bufferToString(buffer)) endpointOut.transfer(buffer, function(err) { if (err) { console.error("USB error: %s", err); @@ -116,7 +96,7 @@ function updateLeds() { console.error("USB error: %s", err2); process.exit(2); } -// console.log('Received', util.bufferToString(receivedBuffer)); +// console.log('Received', uhk.bufferToString(receivedBuffer)); ledIndex += ledCountToUpdatePerCommand; if (ledIndex >= ledMatrixSize) { diff --git a/usb/read-eeprom.js b/usb/read-eeprom.js index a5dd6975..a6378af7 100755 --- a/usb/read-eeprom.js +++ b/usb/read-eeprom.js @@ -1,30 +1,9 @@ #!/usr/bin/env node -'use strict'; +let uhk = require('./uhk'); +let [endpointIn, endpointOut] = uhk.getUsbEndpoints(); -var usb = require('usb'); -var util = require('./util'); - -var vid = 0x16d3; -var pid = 0x05ea; -var readEepromCommandId = 6; - -var device = usb.findByIds(vid, pid); -device.open(); - -var usbInterface = device.interface(0); - -// https://github.com/tessel/node-usb/issues/147 -// The function 'isKernelDriverActive' is not available on Windows and not even needed. -if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) { - usbInterface.detachKernelDriver(); -} -usbInterface.claim(); - -var endpointIn = usbInterface.endpoints[0]; -var endpointOut = usbInterface.endpoints[1]; - -var payload = new Buffer([readEepromCommandId, 63, 0x00, 0x00]); -console.log('Sending ', util.bufferToString(payload)); +var payload = new Buffer([uhk.usbCommands.readEeprom, 63, 0x00, 0x00]); +console.log('Sending ', uhk.bufferToString(payload)); endpointOut.transfer(payload, function(err) { if (err) { console.error("USB error: %s", err); @@ -35,6 +14,6 @@ endpointOut.transfer(payload, function(err) { console.error("USB error: %s", err2); process.exit(2); } - console.log('Received', util.bufferToString(receivedBuffer)); + console.log('Received', uhk.bufferToString(receivedBuffer)); }) }); diff --git a/usb/read-merge-sensor.js b/usb/read-merge-sensor.js index 5ee5ff28..c3671265 100755 --- a/usb/read-merge-sensor.js +++ b/usb/read-merge-sensor.js @@ -1,33 +1,11 @@ #!/usr/bin/env node -'use strict'; - -var usb = require('usb'); -var util = require('./util'); - -var vid = 0x16d3; -var pid = 0x05ea; -var readMergeSensorCommandId = 7; - -var device = usb.findByIds(vid, pid); -device.open(); - -var usbInterface = device.interface(0); - -// https://github.com/tessel/node-usb/issues/147 -// The function 'isKernelDriverActive' is not available on Windows and not even needed. -if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) { - usbInterface.detachKernelDriver(); -} -usbInterface.claim(); - -var endpointIn = usbInterface.endpoints[0]; -var endpointOut = usbInterface.endpoints[1]; +let uhk = require('./uhk'); +let [endpointIn, endpointOut] = uhk.getUsbEndpoints(); var arg = process.argv[2] || ''; - function readMergeSensor() { - var payload = new Buffer([readMergeSensorCommandId]); - console.log('Sending ', util.bufferToString(payload)); + var payload = new Buffer([uhk.usbCommands.readMergeSensor]); + console.log('Sending ', uhk.bufferToString(payload)); endpointOut.transfer(payload, function(err) { if (err) { console.error("USB error: %s", err); @@ -38,7 +16,7 @@ function readMergeSensor() { console.error("USB error: %s", err2); process.exit(2); } - console.log('Received', util.bufferToString(receivedBuffer)); + console.log('Received', uhk.bufferToString(receivedBuffer)); setTimeout(readMergeSensor, 500) var areHalvesMerged = receivedBuffer[1] === 1; console.log('The keyboards halves are ' + (areHalvesMerged ? 'merged' : 'split')) diff --git a/usb/util.js b/usb/uhk.js similarity index 100% rename from usb/util.js rename to usb/uhk.js diff --git a/usb/write-eeprom.js b/usb/write-eeprom.js index 9b0796a2..7327c369 100755 --- a/usb/write-eeprom.js +++ b/usb/write-eeprom.js @@ -1,27 +1,6 @@ #!/usr/bin/env node -'use strict'; - -var usb = require('usb'); -var util = require('./util'); - -var vid = 0x16d3; -var pid = 0x05ea; -var writeEepromCommandId = 5; - -var device = usb.findByIds(vid, pid); -device.open(); - -var usbInterface = device.interface(0); - -// https://github.com/tessel/node-usb/issues/147 -// The function 'isKernelDriverActive' is not available on Windows and not even needed. -if (process.platform !== 'win32' && usbInterface.isKernelDriverActive()) { - usbInterface.detachKernelDriver(); -} -usbInterface.claim(); - -var endpointIn = usbInterface.endpoints[0]; -var endpointOut = usbInterface.endpoints[1]; +let uhk = require('./uhk'); +let [endpointIn, endpointOut] = uhk.getUsbEndpoints(); var arg = process.argv[2] || ''; if (arg.length === 0) { @@ -29,8 +8,8 @@ if (arg.length === 0) { process.exit(1); } -var payload = Buffer.concat([new Buffer([writeEepromCommandId, arg.length+2, 0x00, 0x00]), new Buffer(arg, 'utf8')]); -console.log('Sending ', util.bufferToString(payload)); +var payload = Buffer.concat([new Buffer([uhk.usbCommands.writeEeprom, arg.length+2, 0x00, 0x00]), new Buffer(arg, 'utf8')]); +console.log('Sending ', uhk.bufferToString(payload)); endpointOut.transfer(payload, function(err) { if (err) { console.error("USB error: %s", err); @@ -41,6 +20,6 @@ endpointOut.transfer(payload, function(err) { console.error("USB error: %s", err2); process.exit(2); } - console.log('Received', util.bufferToString(receivedBuffer)); + console.log('Received', uhk.bufferToString(receivedBuffer)); }) });