diff --git a/lib/features/keyboard/implementations/dummy-keyboard/index.ts b/lib/features/keyboard/implementations/dummy-keyboard/index.ts new file mode 100644 index 0000000..3deb615 --- /dev/null +++ b/lib/features/keyboard/implementations/dummy-keyboard/index.ts @@ -0,0 +1,16 @@ +export class DummyKeyboard implements Keyboard { + constructor(){ + } + + async setup(): Promise { + console.log(`Dummy keyboard setup`) + } + + async pressKey(key: string): Promise { + console.log(`Dummy keyboard ${key} pressed!`) + } + + async teardown(): Promise { + } + +} diff --git a/lib/features/keyboard/implementations/rpi0-keyboard/index.ts b/lib/features/keyboard/implementations/rpi0-keyboard/index.ts new file mode 100644 index 0000000..2ae7b3f --- /dev/null +++ b/lib/features/keyboard/implementations/rpi0-keyboard/index.ts @@ -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 { + if(this.serial.isOpen){ + console.log(`Serial already open!`) + } else { + this.serial.open(); + } + } + + async pressKey(key: string): Promise { + if(this.serial.isOpen){ + this.serial.write(`--${key} \r\n`); + } else { + return `Serial connection not open` + } + } + + async teardown(): Promise { + } + +} diff --git a/lib/features/keyboard/index.ts b/lib/features/keyboard/index.ts new file mode 100644 index 0000000..5ff0c74 --- /dev/null +++ b/lib/features/keyboard/index.ts @@ -0,0 +1,9 @@ +import { DummyKeyboard } from "./implementations/dummy-keyboard"; +import { Rpi0Keyboard } from "./implementations/rpi0-keyboard"; + +const keyboardImplementations: {[key: string]: Type } = { + dummyKeyboard: DummyKeyboard, + rpi0Keyboard: Rpi0Keyboard, +}; + +export { keyboardImplementations } \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index 21be759..f50b73e 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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' @@ -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. @@ -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]; } /** diff --git a/lib/interfaces.d.ts b/lib/interfaces.d.ts index 5755d1c..fe1ae27 100644 --- a/lib/interfaces.d.ts +++ b/lib/interfaces.d.ts @@ -42,6 +42,10 @@ interface Serial extends Base{ close(): Promise; } +interface Keyboard extends Base{ + pressKey(key: string): Promise; +} + // specify which peripherals are in use interface AutokitConfig{ power: string; @@ -51,6 +55,7 @@ interface AutokitConfig{ usbBootPort?: usbPort; serial: string; digitalRelay: string; + keyboard: string; } // utility from angular