Make the first byte of KeyAction items purely denote their type. Make the binary representation of KeyAction items variable-length.

This commit is contained in:
László Monda
2016-03-30 17:19:09 +02:00
parent 4bd1c8b3e9
commit 5b6f62c113
9 changed files with 116 additions and 65 deletions

View File

@@ -1,27 +1,15 @@
// A KeyAction is composed of 2 bytes in the RAM of the UHK: id byte, and param byte.
// Id denotes the subclass of the KeyAction and param is subclass-specific.
// A KeyAction is composed of 3 bytes in the RAM and up to 3 bytes in the EEPROM of the UHK.
// The first byte denotes the subclass of the KeyAction and its length in the EEPROM.
enum KeyActionId {
NoneAction = 0,
KeyStrokeActionFirst = TypeChecker.firstValidScancode, // 1
// Intermediary scancodes = 2 to 230
KeyStrokeActionLast = TypeChecker.lastValidScancode, // 231
SwitchLayerAction = 232,
SwitchKeymapAction = 233,
MouseAction = 234,
PlayMacroAction = 235,
DualRoleActionLeftCtrlLongPressAction = 236,
DualRoleActionLeftShiftLongPressAction = 237,
DualRoleActionLeftAltLongPressAction = 238,
DualRoleActionLeftSuperLongPressAction = 239,
DualRoleActionRightCtrlLongPressAction = 240,
DualRoleActionRightShiftLongPressAction = 241,
DualRoleActionRightAltLongPressAction = 242,
DualRoleActionRightSuperLongPressAction = 243,
DualRoleActionModLongPressAction = 244,
DualRoleActionFnLongPressAction = 245,
DualRoleActionMouseLongPressAction = 246
// Let's leave space for further layers - additional actions should descend from 255
NoneAction = 0,
KeystrokeAction = 1,
KeystrokeWithModifiersAction = 2,
DualRoleKeystrokeAction = 3,
SwitchLayerAction = 4,
SwitchKeymapAction = 5,
MouseAction = 6,
PlayMacroAction = 7
}
class KeyAction {
@@ -43,14 +31,28 @@ class KeyAction {
let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack();
if (TypeChecker.isScancodeValid(keyActionFirstByte)) {
return new KeystrokeAction().fromBinary(buffer);
} else if (keyActionFirstByte === KeyActionId.MouseAction) {
return new MouseAction().fromBinary(buffer);
} else if (keyActionFirstByte === KeyActionId.NoneAction) {
return new NoneAction().fromBinary(buffer);
} else {
throw 'Invalid KeyAction first byte "${keyActionFirstByte}"';
switch (keyActionFirstByte) {
case KeyActionId.KeystrokeAction:
return new KeystrokeAction().fromBinary(buffer);
case KeyActionId.MouseAction:
return new MouseAction().fromBinary(buffer);
case KeyActionId.NoneAction:
return new NoneAction().fromBinary(buffer);
default:
throw 'Invalid KeyAction first byte "${keyActionFirstByte}"';
}
}
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
if (jsObject.keyActionType !== keyActionTypeString) {
throw 'Invalid ${classname}.id: ${jsObject.keyActionType}';
}
}
readAndAssertKeyActionId(buffer: UhkBuffer, keyActionIdParam: KeyActionId, classname: string) {
let readKeyActionId = buffer.readUInt8();
if (readKeyActionId !== keyActionIdParam) {
throw 'Invalid ${classname} first byte: ${keyActionId}';
}
}
}