feat(config): Read / write hardware configuration area (#423)

* add write-hca.js

* refactor: Move config serializer into the uhk-common package

* refactor: Move getTransferBuffers into the uhk-usb package

* refactor: delete obsoleted classes

* build: add uhk-usb build command

* refactor: move eeprom transfer to uhk-usb package

* fix: Fix write-hca.js

* feat: load hardware config from the device and

* style: fix ts lint errors

* build: fix rxjs dependency resolve

* test: Add jasmine unit test framework to the tet serializer

* fix(user-config): A "type": "basic", properties to the "keystroke" action types

* feat(usb): set chmod+x on write-hca.js

* feat(usb): Create USB logger

* style: Fix type

* build: Add chalk to dependencies.

Chalk will colorize the output
This commit is contained in:
Róbert Kiss
2017-09-26 18:57:27 +02:00
committed by László Monda
parent 1122784bdb
commit 9294bede50
130 changed files with 9108 additions and 1991 deletions

34
packages/usb/logger.js Normal file
View File

@@ -0,0 +1,34 @@
const {LogService, LogRegExps} = require('uhk-common');
const chalk = require('chalk');
class Logger extends LogService {
error(...args) {
console.error(args);
}
debug(...args) {
const msg = args.join(' ');
if (LogRegExps.writeRegExp.test(msg)) {
console.log(chalk.blue(msg));
} else if (LogRegExps.readRegExp.test(msg)) {
console.log(chalk.green(msg));
} else if (LogRegExps.errorRegExp.test(msg)) {
console.log(chalk.red(msg));
}else if (LogRegExps.transferRegExp.test(msg)) {
console.log(chalk.yellow(msg));
} else {
console.log(...args);
}
}
silly(...args) {
console.log(args);
}
info(...args) {
console.info(args);
}
}
module.exports = Logger;

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,16 @@
"description": "Agent preliminary USB code",
"main": "UhkConnection.js",
"license": "GPL-3.0",
"scripts": {
"build": "tsc"
},
"devDependencies": {
"@types/node": "8.0.28"
},
"dependencies": {
"node-hid": "0.5.4"
"chalk": "^2.1.0",
"node-hid": "0.5.4",
"uhk-common": "1.0.0",
"uhk-usb": "1.0.0"
}
}

View File

@@ -1,4 +1,6 @@
const HID = require('node-hid');
// const debug = process.env.DEBUG;
const debug = true;
function bufferToString(buffer) {
let str = '';
@@ -78,8 +80,10 @@ exports = module.exports = moduleExports = {
writeUserConfig: 3,
},
leftLedDriverAddress: 0b1110100,
rightLedDriverAddress: 0b1110111
}
rightLedDriverAddress: 0b1110111,
sendLog: sendLog,
readLog: readLog
};
function convertBufferToIntArray(buffer) {
return Array.prototype.slice.call(buffer, 0)
@@ -101,3 +105,18 @@ function getTransferData(buffer) {
return data
}
function readLog(buffer) {
writeLog('USB[R]: ', buffer)
}
function sendLog(buffer) {
writeLog('USB[W]: ', buffer)
}
function writeLog(prefix, buffer) {
if (!debug) {
return;
}
console.log(prefix + bufferToString(buffer))
}

57
packages/usb/write-hca.js Executable file
View File

@@ -0,0 +1,57 @@
#!/usr/bin/env node
const {HardwareConfiguration, UhkBuffer} = require('uhk-common');
const {EepromTransfer, UhkHidDevice, UsbCommand} = require('uhk-usb');
const Logger = require('./logger');
if (process.argv.length < 3) {
console.log(`use: write-hca <layout> <manufactureId>
- layout: iso or ansi
- manufactureId: max 32 bit integer
`);
process.exit(1);
}
const layout = process.argv[2];
if (layout !== 'iso' && layout !== 'ansi') {
console.log('Invalid layout. Layout should be on of: iso, ansi');
process.exit(1);
}
const uuid = Number.parseInt(process.argv[3]);
if (isNaN(uuid)) {
console.log('Manufacture Id is not a integer');
process.exit(1);
}
const hardwareConfig = new HardwareConfiguration();
hardwareConfig.signature = 'UHK';
hardwareConfig.dataModelVersion = 0;
hardwareConfig.hardwareId = 0;
hardwareConfig.uuid = uuid;
hardwareConfig.brandId = 0;
hardwareConfig.isIso = layout === 'iso';
hardwareConfig.hasBacklighting = false;
const logger = new Logger();
async function writeHca() {
const device = new UhkHidDevice(logger);
const hardwareBuffer = new UhkBuffer();
hardwareConfig.toBinary(hardwareBuffer);
const buffer = hardwareBuffer.buffer.slice(0, 60);
const fragments = UhkHidDevice.getTransferBuffers(UsbCommand.WriteHardwareConfig, buffer);
logger.debug('USB[T]: Write hardware configuration to keyboard');
for (const fragment of fragments) {
await device.write(fragment);
}
logger.debug('USB[T]: Write hardware configuration to EEPROM');
await device.writeConfigToEeprom(EepromTransfer.WriteHardwareConfig);
}
writeHca()
.catch((err)=>{
console.error(err);
});