Add UhkBuffer.backtrack() and implement the KeyAction class.

This commit is contained in:
László Monda
2016-03-26 02:52:50 +01:00
parent 32646f3616
commit 9d77d7212b
4 changed files with 55 additions and 10 deletions

30
model/KeyAction.ts Normal file
View File

@@ -0,0 +1,30 @@
/// <reference path="KeystrokeAction.ts" />
class KeyAction {
static fromJsObject(jsObject: any): KeyAction {
switch (jsObject.actionType) {
case 'keyStroke':
let keystrokeAction = new KeystrokeAction();
keystrokeAction.fromJsObject(jsObject);
return keystrokeAction;
default:
throw 'Unknown KeyAction actionType "${jsObject.actionType}"';
}
}
static fromBinary(buffer: UhkBuffer) {
let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack();
if (KeystrokeAction.firstValidScancode <= keyActionFirstByte &&
keyActionFirstByte <= KeystrokeAction.lastValidScancode)
{
let keystrokeAction = new KeystrokeAction();
keystrokeAction.fromBinary(buffer);
return keystrokeAction;
} else {
throw 'Unknown KeyAction first byte "${keyActionFirstByte}"';
}
}
}

View File

@@ -1,9 +1,10 @@
/// <reference path="KeyAction.ts" />
/// <reference path="Serializable.ts" />
class KeystrokeAction implements Serializable {
class KeystrokeAction extends KeyAction implements Serializable {
private static firstValidScancode = 1;
private static lastValidScancode = 231;
static firstValidScancode = 1;
static lastValidScancode = 231;
_scancode: number;
modifierMask: number;

View File

@@ -2,7 +2,7 @@
interface Serializable {
fromJsObject(jsObject: any);
toJsObject(): any;
fromBinary(buffer: UhkBuffer);
toJsObject(): any;
toBinary(buffer: UhkBuffer);
}

View File

@@ -7,16 +7,19 @@ class UhkBuffer {
buffer: Buffer;
offset: number;
bytesToBacktrack: number;
constructor() {
this.offset = 0;
this.bytesToBacktrack = 0;
this.buffer = new Buffer(UhkBuffer.eepromSize);
this.buffer.fill(0);
}
readInt8(): number {
let value = this.buffer.readInt8(this.offset);
this.offset += 1;
this.bytesToBacktrack = 1;
this.offset += this.bytesToBacktrack;
return value;
}
@@ -27,7 +30,8 @@ class UhkBuffer {
readUInt8(): number {
let value = this.buffer.readUInt8(this.offset);
this.offset += 1;
this.bytesToBacktrack = 1;
this.offset += this.bytesToBacktrack;
return value;
}
@@ -38,7 +42,8 @@ class UhkBuffer {
readInt16(): number {
let value = this.buffer.readInt16LE(this.offset);
this.offset += 2;
this.bytesToBacktrack = 2;
this.offset += this.bytesToBacktrack;
return value;
}
@@ -49,7 +54,8 @@ class UhkBuffer {
readUInt16(): number {
let value = this.buffer.readUInt16LE(this.offset);
this.offset += 2;
this.bytesToBacktrack = 2;
this.offset += this.bytesToBacktrack;
return value;
}
@@ -60,7 +66,8 @@ class UhkBuffer {
readInt32(): number {
let value = this.buffer.readInt32LE(this.offset);
this.offset += 4;
this.bytesToBacktrack = 4;
this.offset += this.bytesToBacktrack;
return value;
}
@@ -71,7 +78,8 @@ class UhkBuffer {
readUInt32(): number {
let value = this.buffer.readUInt32LE(this.offset);
this.offset += 4;
this.bytesToBacktrack = 4;
this.offset += this.bytesToBacktrack;
return value;
}
@@ -88,6 +96,7 @@ class UhkBuffer {
}
let str = this.buffer.toString(UhkBuffer.stringEncoding, this.offset, stringByteLength);
this.bytesToBacktrack = stringByteLength;
this.offset += stringByteLength;
return str;
}
@@ -110,4 +119,9 @@ class UhkBuffer {
this.buffer.write(str, this.offset, stringByteLength, UhkBuffer.stringEncoding);
this.offset += stringByteLength;
}
backtrack(): void {
this.offset -= this.bytesToBacktrack;
this.bytesToBacktrack = 0;
}
}