Rename util.js to uhk.js and refactor the dependent scripts to utilize its API.

This commit is contained in:
László Monda
2017-01-15 01:31:23 +01:00
parent e83a7b8cf6
commit 339602178a
7 changed files with 34 additions and 158 deletions

View File

@@ -1,34 +1,15 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; 'use strict';
var usb = require('usb'); let uhk = require('./uhk');
var util = require('./util'); let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
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];
var state = 1; var state = 1;
setInterval(function() { setInterval(function() {
state = state ? 0 : 1 state = state ? 0 : 1
console.log('Sending ', state); 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) { if (err) {
console.error("USB error: %s", err); console.error("USB error: %s", err);
process.exit(1); process.exit(1);
@@ -38,7 +19,7 @@ setInterval(function() {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
console.log('Received', util.bufferToString(receivedBuffer)); console.log('Received', uhk.bufferToString(receivedBuffer));
}) })
}); });
}, 500) }, 500)

View File

@@ -1,30 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; let uhk = require('./uhk');
let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
var usb = require('usb'); var payload = new Buffer([uhk.usbCommands.jumpToBootloader]);
var util = require('./util'); console.log('Sending ', uhk.bufferToString(payload));
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));
endpointOut.transfer(payload, function(err) { endpointOut.transfer(payload, function(err) {
if (err) { if (err) {
console.error("USB error: %s", err); console.error("USB error: %s", err);
@@ -35,6 +14,6 @@ endpointOut.transfer(payload, function(err) {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
console.log('Received', util.bufferToString(receivedBuffer)); console.log('Received', uhk.bufferToString(receivedBuffer));
}) })
}); });

View File

@@ -1,32 +1,12 @@
#!/usr/bin/env node #!/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 ledMatrixSize = 144;
var ledCountToUpdatePerCommand = ledMatrixSize / 3; var ledCountToUpdatePerCommand = ledMatrixSize / 3;
var writeLedDriverCommandId = 3;
var leftLedDriverAddress = 0b1110100; var leftLedDriverAddress = 0b1110100;
var rightLedDriverAddress = 0b1110111; 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 state = 1;
var ledsLeft = new Buffer(ledMatrixSize); var ledsLeft = new Buffer(ledMatrixSize);
@@ -39,7 +19,7 @@ var matrixId = 0;
var initLedCommands = [ var initLedCommands = [
[ // only enable the LEDs that are actually in the matrix [ // only enable the LEDs that are actually in the matrix
writeLedDriverCommandId, uhk.usbCommands.writeLedDriver,
leftLedDriverAddress, leftLedDriverAddress,
19, 19,
0, 0,
@@ -54,7 +34,7 @@ var initLedCommands = [
0, 0b00011111, 0, 0b00011111,
], ],
[ // only enable the LEDs that are actually in the matrix [ // only enable the LEDs that are actually in the matrix
writeLedDriverCommandId, uhk.usbCommands.writeLedDriver,
rightLedDriverAddress, rightLedDriverAddress,
19, 19,
0, 0,
@@ -68,9 +48,9 @@ var initLedCommands = [
0, 0, 0, 0,
0, 0, 0, 0,
], ],
[writeLedDriverCommandId, leftLedDriverAddress, 2, 0xfd, 0x0b], // switch to function page [uhk.usbCommands.writeLedDriver, leftLedDriverAddress, 2, 0xfd, 0x0b], // switch to function page
[writeLedDriverCommandId, leftLedDriverAddress, 2, 0xc2, 0xff], // enable the ghost image prevention bit [uhk.usbCommands.writeLedDriver, 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, 0x00], // switch to page 0
] ]
var ledCommandId = 0; var ledCommandId = 0;
@@ -97,14 +77,14 @@ function updateLeds() {
// console.log('update') // console.log('update')
var buffer = Buffer.concat([ var buffer = Buffer.concat([
new Buffer([ new Buffer([
writeLedDriverCommandId, uhk.usbCommands.writeLedDriver,
matrixId ? rightLedDriverAddress : leftLedDriverAddress, matrixId ? rightLedDriverAddress : leftLedDriverAddress,
ledCountToUpdatePerCommand, ledCountToUpdatePerCommand,
0x24 + ledIndex 0x24 + ledIndex
]), ]),
(matrixId ? ledsRight : ledsLeft).slice(ledIndex, ledIndex + ledCountToUpdatePerCommand) (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) { endpointOut.transfer(buffer, function(err) {
if (err) { if (err) {
console.error("USB error: %s", err); console.error("USB error: %s", err);
@@ -116,7 +96,7 @@ function updateLeds() {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
// console.log('Received', util.bufferToString(receivedBuffer)); // console.log('Received', uhk.bufferToString(receivedBuffer));
ledIndex += ledCountToUpdatePerCommand; ledIndex += ledCountToUpdatePerCommand;
if (ledIndex >= ledMatrixSize) { if (ledIndex >= ledMatrixSize) {

View File

@@ -1,30 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; let uhk = require('./uhk');
let [endpointIn, endpointOut] = uhk.getUsbEndpoints();
var usb = require('usb'); var payload = new Buffer([uhk.usbCommands.readEeprom, 63, 0x00, 0x00]);
var util = require('./util'); console.log('Sending ', uhk.bufferToString(payload));
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));
endpointOut.transfer(payload, function(err) { endpointOut.transfer(payload, function(err) {
if (err) { if (err) {
console.error("USB error: %s", err); console.error("USB error: %s", err);
@@ -35,6 +14,6 @@ endpointOut.transfer(payload, function(err) {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
console.log('Received', util.bufferToString(receivedBuffer)); console.log('Received', uhk.bufferToString(receivedBuffer));
}) })
}); });

View File

@@ -1,33 +1,11 @@
#!/usr/bin/env node #!/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 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];
var arg = process.argv[2] || ''; var arg = process.argv[2] || '';
function readMergeSensor() { function readMergeSensor() {
var payload = new Buffer([readMergeSensorCommandId]); var payload = new Buffer([uhk.usbCommands.readMergeSensor]);
console.log('Sending ', util.bufferToString(payload)); console.log('Sending ', uhk.bufferToString(payload));
endpointOut.transfer(payload, function(err) { endpointOut.transfer(payload, function(err) {
if (err) { if (err) {
console.error("USB error: %s", err); console.error("USB error: %s", err);
@@ -38,7 +16,7 @@ function readMergeSensor() {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
console.log('Received', util.bufferToString(receivedBuffer)); console.log('Received', uhk.bufferToString(receivedBuffer));
setTimeout(readMergeSensor, 500) setTimeout(readMergeSensor, 500)
var areHalvesMerged = receivedBuffer[1] === 1; var areHalvesMerged = receivedBuffer[1] === 1;
console.log('The keyboards halves are ' + (areHalvesMerged ? 'merged' : 'split')) console.log('The keyboards halves are ' + (areHalvesMerged ? 'merged' : 'split'))

View File

@@ -1,27 +1,6 @@
#!/usr/bin/env node #!/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 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];
var arg = process.argv[2] || ''; var arg = process.argv[2] || '';
if (arg.length === 0) { if (arg.length === 0) {
@@ -29,8 +8,8 @@ if (arg.length === 0) {
process.exit(1); process.exit(1);
} }
var payload = Buffer.concat([new Buffer([writeEepromCommandId, arg.length+2, 0x00, 0x00]), new Buffer(arg, 'utf8')]); var payload = Buffer.concat([new Buffer([uhk.usbCommands.writeEeprom, arg.length+2, 0x00, 0x00]), new Buffer(arg, 'utf8')]);
console.log('Sending ', util.bufferToString(payload)); console.log('Sending ', uhk.bufferToString(payload));
endpointOut.transfer(payload, function(err) { endpointOut.transfer(payload, function(err) {
if (err) { if (err) {
console.error("USB error: %s", err); console.error("USB error: %s", err);
@@ -41,6 +20,6 @@ endpointOut.transfer(payload, function(err) {
console.error("USB error: %s", err2); console.error("USB error: %s", err2);
process.exit(2); process.exit(2);
} }
console.log('Received', util.bufferToString(receivedBuffer)); console.log('Received', uhk.bufferToString(receivedBuffer));
}) })
}); });