Add rough USB code that will have to be converted to TypeScript and cleaned up.
This commit is contained in:
101
usb/uhkcmd
Executable file
101
usb/uhkcmd
Executable file
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
var UhkConnection = require('./lib/UhkConnection');
|
||||
var R = require('ramda');
|
||||
var path = require('path');
|
||||
|
||||
var ENUMERATION_MODE_IDS = R.pluck('id', UhkConnection.ENUMERATION_MODES);
|
||||
var commands = getTypistFriendlyObjectKeys(R.keys(UhkConnection.COMMANDS));
|
||||
var args = process.argv.slice(1);
|
||||
var programName = path.basename(args.shift());
|
||||
var commandArg = args.shift();
|
||||
|
||||
if (!R.contains(commandArg, commands)) {
|
||||
console.error('Usage: ' + programName + ' COMMAND [ARG]');
|
||||
console.error(commandArg === undefined
|
||||
? 'No command has been specified.'
|
||||
: 'Command "' + commandArg + '" is invalid.');
|
||||
console.error('Valid commands: ' + commands.join(', '));
|
||||
exitWithError();
|
||||
}
|
||||
|
||||
var command = UhkConnection.COMMANDS[getTypistUnfriendlyObjectKey(commandArg)];
|
||||
var uhkConnection;
|
||||
|
||||
switch (command) {
|
||||
case UhkConnection.COMMANDS.DETECT:
|
||||
uhkConnection = new UhkConnection(/*UhkConnection.LOG_LEVELS.TRANSFER*/);
|
||||
uhkConnection.connect(function(error) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
exitWithError();
|
||||
}
|
||||
});
|
||||
break;
|
||||
case UhkConnection.COMMANDS.REENUMERATE:
|
||||
var enumerationModeArg = args.shift();
|
||||
var enumerationModes = getTypistFriendlyObjectKeys(ENUMERATION_MODE_IDS);
|
||||
if (!R.contains(enumerationModeArg, enumerationModes)) {
|
||||
console.error('Usage: %s %s {%s}', programName, commandArg, enumerationModes.join(' | '));
|
||||
console.error(enumerationModeArg === undefined
|
||||
? 'No enumeration mode has been specified.'
|
||||
: 'Enumeration mode "%s" is invalid.', enumerationModeArg);
|
||||
exitWithError();
|
||||
}
|
||||
var enumerationMode = R.find(R.propEq('id', getTypistUnfriendlyObjectKey(enumerationModeArg)),
|
||||
UhkConnection.ENUMERATION_MODES);
|
||||
sendRequest(command, enumerationMode.enumerationId, function(error, data) {
|
||||
console.log('Reenumerating the UHK in %s mode...', getTypistUnfriendlyObjectKey(enumerationMode.id));
|
||||
uhkConnection.waitUntilDisconnect(function() {}, function() {
|
||||
console.log('UHK disconnected.');
|
||||
uhkConnection.connect(function() {});
|
||||
});
|
||||
}, false);
|
||||
break;
|
||||
case UhkConnection.COMMANDS.READ_EEPROM:
|
||||
sendRequest(command, null, function(error, data) {
|
||||
console.log(data);
|
||||
});
|
||||
break;
|
||||
case UhkConnection.COMMANDS.WRITE_EEPROM:
|
||||
var stringToBeSaved = args.shift();
|
||||
if (!stringToBeSaved) {
|
||||
console.error('A string has to be specified to be saved into the EEPROM.');
|
||||
exitWithError();
|
||||
}
|
||||
sendRequest(command, stringToBeSaved, function(error, data) {});
|
||||
break;
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
function sendRequest(command, arg, callback, shouldReceiveResponse) {
|
||||
if (uhkConnection) {
|
||||
uhkConnection.sendRequest(command, arg, callback, shouldReceiveResponse);
|
||||
} else {
|
||||
uhkConnection = new UhkConnection(/*UhkConnection.LOG_LEVELS.TRANSFER*/);
|
||||
uhkConnection.connect(function(error) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
exitWithError();
|
||||
}
|
||||
|
||||
uhkConnection.sendRequest(command, arg, callback, shouldReceiveResponse);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function getTypistFriendlyObjectKeys(object) {
|
||||
return object.map(function(command) {
|
||||
return command.toLowerCase().replace(/_/g, '-');
|
||||
});
|
||||
}
|
||||
|
||||
function getTypistUnfriendlyObjectKey(key) {
|
||||
return key.toUpperCase().replace(/-/g, '_');
|
||||
}
|
||||
|
||||
function exitWithError() {
|
||||
process.exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user