Skip to content

Commit

Permalink
feat: basic keyboard manager
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixah committed Jan 21, 2022
1 parent 0f5cc16 commit 18e7684
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/classes/KeyboardManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import TV from './tv';

export default class KeyboardManager {
row: number;
col: number;
tv: TV;
modifiers: string[];
keyboard = [
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ','],
['Shift', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '.', 'Backspace'],
['Special', 'Left', 'Right', ' ', ' ', ' ', '-', '_', 'Enter', 'Enter'],
];

constructor(tv: TV) {
this.row = 0;
this.col = 0;
this.modifiers = [];
this.tv = tv;
}

async send(input: string): Promise<void> {
const characters = input.split('');
for (const char of characters) {
await this.sendKey(char);
}
}

reset(): void {
this.row = 0;
this.col = 0;
this.modifiers = [];
}

async sendKey(key: string): Promise<void> {
const [row, col] = this.keyToRowCol(key);
await this.setCol(col);
await this.setRow(row);
await this.tv.ok();
}

keyToRowCol(key: string): [number, number] {
let row = 0;
let col = 0;
for (let i = 0; i < this.keyboard.length; i++) {
const rowKeys = this.keyboard[i];
for (let j = 0; j < rowKeys.length; j++) {
if (rowKeys[j] === key) {
row = i;
col = j;
break;
}
}
}
return [row, col];
}
async setRow(row: number): Promise<void> {
const diff = row - this.row;
if (diff > 0) {
await this.tv.down(diff);
} else if (diff < 0) {
await this.tv.up(-diff);
}
this.row = row;
}

async setCol(col: number): Promise<void> {
const diff = col - this.col;
if (diff > 0) {
await this.tv.right(diff);
} else if (diff < 0) {
await this.tv.left(-diff);
}
this.col = col;
}
}
4 changes: 4 additions & 0 deletions src/classes/tv.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import KeyboardManager from './KeyboardManager';

require('isomorphic-fetch');

export default class TV {
ip: string;
backendURL: string;
keyboard: KeyboardManager;

constructor(ip: string) {
this.ip = ip;
this.backendURL = process.env.SONY_BACKEND_URL || 'http://localhost:3000';
this.keyboard = new KeyboardManager(this);
}

sendIRCC(command: string, times?: number): Promise<Response[]> {
Expand Down

0 comments on commit 18e7684

Please sign in to comment.