Skip to content

Commit

Permalink
add keyboard emulation feature
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Ryan Cooke <[email protected]>
  • Loading branch information
rcooke-warwick committed Jan 24, 2024
1 parent dc56f6a commit 5d767e0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/features/keyboard/implementations/dummy-keyboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class DummyKeyboard implements Keyboard {
constructor(){
}

async setup(): Promise<void> {
console.log(`Dummy keyboard setup`)
}

async pressKey(key: string): Promise<string | void> {
console.log(`Dummy keyboard ${key} pressed!`)
}

async teardown(): Promise<void> {
}

}
36 changes: 36 additions & 0 deletions lib/features/keyboard/implementations/rpi0-keyboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SerialPort } from "serialport";

export class Rpi0Keyboard implements Keyboard {
public DEV_SERIAL = '/dev/ttyUSB0' || process.env.DEV_SERIAL;
public BAUD_RATE = 115200;
public serial : SerialPort;

constructor(){
this.serial = new SerialPort({
path: this.DEV_SERIAL,
baudRate: this.BAUD_RATE,
autoOpen: false,
});

}

async setup(): Promise<void> {
if(this.serial.isOpen){
console.log(`Serial already open!`)
} else {
this.serial.open();
}
}

async pressKey(key: string): Promise<string | void> {
if(this.serial.isOpen){
this.serial.write(`--${key} \r\n`);
} else {
return `Serial connection not open`
}
}

async teardown(): Promise<void> {
}

}
9 changes: 9 additions & 0 deletions lib/features/keyboard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DummyKeyboard } from "./implementations/dummy-keyboard";
import { Rpi0Keyboard } from "./implementations/rpi0-keyboard";

const keyboardImplementations: {[key: string]: Type<Keyboard> } = {
dummyKeyboard: DummyKeyboard,
rpi0Keyboard: Rpi0Keyboard,
};

export { keyboardImplementations }
3 changes: 3 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { videoImplementations } from './features/video';
import { sdMuxImplementations } from './features/sd-mux';
import { serialImplementations } from './features/serial';
import { digitalRelayImplementations } from './features/digitalRelay';
import { keyboardImplementations } from './features/keyboard';

import { flash } from './flashing'

Expand All @@ -16,6 +17,7 @@ export class Autokit {
public sdMux: SdMux;
public serial: Serial;
public digitalRelay: DigitalRelay;
public keyboard: Keyboard;

/**
* @param config - AutokitConfig object, MUST define every implementation. You can use a Dummy one if needed.
Expand All @@ -29,6 +31,7 @@ export class Autokit {
this.serial = new serialImplementations[this.config.serial]();
this.serial = new serialImplementations[this.config.serial]();
this.digitalRelay = new digitalRelayImplementations[this.config.digitalRelay]
this.keyboard = new keyboardImplementations[this.config.keyboard];
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ interface Serial extends Base{
close(): Promise<void>;
}

interface Keyboard extends Base{
pressKey(key: string): Promise<string | void>;
}

// specify which peripherals are in use
interface AutokitConfig{
power: string;
Expand All @@ -51,6 +55,7 @@ interface AutokitConfig{
usbBootPort?: usbPort;
serial: string;
digitalRelay: string;
keyboard: string;
}

// utility from angular
Expand Down

0 comments on commit 5d767e0

Please sign in to comment.