From 76799bd66d33fab092c221a02027197126660316 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Wed, 13 Jul 2022 08:28:18 -0400 Subject: [PATCH] Capper work --- data/cfg.json | 4 +- src/capper/index.ts | 107 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/capper/index.ts diff --git a/data/cfg.json b/data/cfg.json index e1642cd..52b86ca 100644 --- a/data/cfg.json +++ b/data/cfg.json @@ -183,8 +183,8 @@ "camera_capper_identifier" : "8", "camera_capper_projector_identifier" : "9", "camera_capper_projectors_identifier" : "0", - "cap_on" : "A", - "cap_off" : "B" + "capper_on" : "A", + "capper_off" : "B" } } } diff --git a/src/capper/index.ts b/src/capper/index.ts new file mode 100644 index 0000000..4c324a3 --- /dev/null +++ b/src/capper/index.ts @@ -0,0 +1,107 @@ +'use strict'; + +import { Intval } from 'intval'; +import { Processing } from 'processing'; +import { delay } from 'delay'; + +/** class representing capper functions **/ + +class Capper { + private state : any = { + capper : false + }; + private arduino : Arduino = null; + private log : any; + private cfg : any; + private filmout : any; + private ui : any; + private ipc : any; + private id : string = 'capper'; + /** + * + **/ + constructor (arduino : Arduino, cfg : any, ui : any, filmout : any) { + this.arduino = arduino; + this.cfg = cfg; + this.ui = ui; + this.filmout = filmout; + this.init(); + } + + /** + * + **/ + private async init () { + const Log = require('log'); + this.log = await Log({ label : this.id }); + this.ipc = require('electron').ipcMain; + this.listen(); + } + + /** + * + **/ + private listen () { + this.ipc.on(this.id, this.listener.bind(this)); + } + + /** + * + **/ + public async capper (state : boolean, id : string) { + let cmd : string; + let ms : number; + + if (state) { + cmd = this.cfg.arduino.cmd[`${this.id}_on`]; + } else { + cmd = this.cfg.arduino.cmd[`${this.id}_off`]; + } + + this.state.capper = state; + + try { + ms = await this.arduino.send(this.id, cmd); + } catch (err) { + this.log.error(err); + } + return await this.end(cmd, id, ms); + } + + + /** + * + **/ + private async listener (event : any, arg : any) { + if (typeof arg.capper !== 'undefined') { + try { + await this.capper(arg.capper, arg.id) + } catch (err) { + this.log.error(err) + } + } + event.returnValue = true + } + + /** + * + **/ + private async end (cmd : string, id : string, ms : number) { + let message = ''; + + if (cmd === this.cfg.arduino.cmd.capper_on) { + message = 'Capper set to ON'; + } else if (cmd === this.cfg.arduino.cmd.capper_off) { + message = 'Capper set to OFF'; + } + + message += ` ${ms}ms` + + this.log.info(message); + this.ui.send(this.id, {cmd: cmd, id : id, ms: ms}); + } +} + +module.exports = function (arduino : Arduino, cfg : any, ui : any, filmout: any) { + return new Capper(arduino, cfg, ui, filmout); +} \ No newline at end of file