Expose uhk.sendUsbPacket() and vastly simplify {write,read}-eeprom.js by using it. Make led_pwm.js use uhk.js instead of the obsoleted util.js file.
This commit is contained in:
86
usb/uhk.js
86
usb/uhk.js
@@ -1,27 +1,63 @@
|
||||
let usb = require('usb');
|
||||
|
||||
exports = module.exports = {
|
||||
getUsbEndpoints: () => {
|
||||
let vid = 0x16d3;
|
||||
let pid = 0x05ea;
|
||||
|
||||
let device = usb.findByIds(vid, pid);
|
||||
device.open();
|
||||
|
||||
let 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();
|
||||
function bufferToString(buffer) {
|
||||
let str = '';
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
let hex = buffer[i].toString(16) + ' ';
|
||||
if (hex.length <= 2) {
|
||||
hex = '0' + hex;
|
||||
}
|
||||
usbInterface.claim();
|
||||
str += hex;
|
||||
};
|
||||
return str;
|
||||
}
|
||||
|
||||
let endpointIn = usbInterface.endpoints[0];
|
||||
let endpointOut = usbInterface.endpoints[1];
|
||||
return [endpointIn, endpointOut];
|
||||
},
|
||||
let usbEndpoints;
|
||||
|
||||
function getUsbEndpoints() {
|
||||
let vid = 0x16d3;
|
||||
let pid = 0x05ea;
|
||||
|
||||
let device = usb.findByIds(vid, pid);
|
||||
device.open();
|
||||
|
||||
let 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();
|
||||
|
||||
let endpointIn = usbInterface.endpoints[0];
|
||||
let endpointOut = usbInterface.endpoints[1];
|
||||
return [endpointIn, endpointOut];
|
||||
}
|
||||
|
||||
function sendUsbPacket(packet) {
|
||||
console.log('Sending: ', bufferToString(packet));
|
||||
|
||||
let [endpointIn, endpointOut] = usbEndpoints || getUsbEndpoints();
|
||||
endpointOut.transfer(packet, function(err) {
|
||||
if (err) {
|
||||
console.error("USB error: %s", err);
|
||||
process.exit(1);
|
||||
}
|
||||
endpointIn.transfer(64, function(err2, receivedBuffer) {
|
||||
if (err2) {
|
||||
console.error("USB error: %s", err2);
|
||||
process.exit(2);
|
||||
}
|
||||
console.log('Received:', bufferToString(receivedBuffer));
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports = module.exports = {
|
||||
bufferToString,
|
||||
getUsbEndpoints,
|
||||
sendUsbPacket,
|
||||
usbCommands: {
|
||||
getProperty: 0,
|
||||
jumpToBootloader: 1,
|
||||
@@ -35,16 +71,4 @@ exports = module.exports = {
|
||||
applyConfig: 9,
|
||||
setLedPwm: 10
|
||||
},
|
||||
|
||||
bufferToString: buffer => {
|
||||
let str = '';
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
let hex = buffer[i].toString(16) + ' ';
|
||||
if (hex.length <= 2) {
|
||||
hex = '0' + hex;
|
||||
}
|
||||
str += hex;
|
||||
};
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user