Refactored seq.js to Typescript and fixed stats for 1 cam 1 proj. Will need further work to add in multiple devices. Resolves #6.

This commit is contained in:
mmcwilliams 2019-05-28 13:33:16 -04:00
parent c942d89572
commit 4ea390897a
8 changed files with 505 additions and 258 deletions

View File

@ -1,200 +1,182 @@
const seq = {}; 'use strict';
seq.id = 'sequence'; let seq = {};
seq.grid = []; class Sequence {
seq.gridLoops = 1; constructor() {
seq.arr = []; this.id = 'sequence';
seq.loops = 1; this.grid = [];
seq.size = 24; this.gridLoops = 1;
seq.time = 0; this.arr = [];
seq.running = false; this.loops = 1;
this.size = 24;
/****** this.time = 0;
Sequence Object this.running = false;
*******/ }
init() {
seq.init = function () { this.listen();
seq.listen(); }
} listen() {
ipcRenderer.on(this.id, this.listener.bind(this));
seq.listen = function () { }
ipcRenderer.on(seq.id, seq.listener); listener(event, arg) {
}
seq.listener = function (event, arg) {
//console.log(JSON.stringify(arg)) //console.log(JSON.stringify(arg))
if (arg.start) { if (arg.start) {
if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') { if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') {
seq.activeStep(arg.step); this.activeStep(arg.step);
log.info(`Step ${arg.step + 1}/${seq.arr.length}, Loop ${arg.loop + 1}/${seq.loops}`, 'SERIAL', true); log.info(`Step ${arg.step + 1}/${this.arr.length}, Loop ${arg.loop + 1}/${this.loops}`, 'SERIAL', true);
} else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text(gui.fmtZero(arg.loop + 1, 6));
} else {
seq.progress(0, 0);
} }
} else if (arg.stop) { else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text(gui.fmtZero(arg.loop + 1, 6));
}
else {
this.progress(0, 0);
}
}
else if (arg.stop) {
if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') { if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') {
//console.log(JSON.stringify(arg)) //console.log(JSON.stringify(arg))
seq.progress(arg.step + 1, arg.loop); this.progress(arg.step + 1, arg.loop);
seq.inactiveAll(); this.inactiveAll();
} else if (typeof arg.loop !== 'undefined') { }
else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text(''); $('#loop_current').text('');
} else { }
else {
gui.overlay(false); gui.overlay(false);
gui.spinner(false); gui.spinner(false);
log.info('Sequence stopped', 'SERIAL', true); log.info('Sequence stopped', 'SERIAL', true);
} }
} }
return event.returnValue = true; return event.returnValue = true;
} }
progress(step, loop) {
seq.progress = function (step, loop) {
const elem = $('.progress-bar'); const elem = $('.progress-bar');
const len = seq.arr.length; const len = this.arr.length;
const total = len * seq.loops; const total = len * this.loops;
let pos = (loop * len) + step; let pos = (loop * len) + step;
let progress = 0; let progress = 0;
if (pos > 0 && total > 0) { if (pos > 0 && total > 0) {
progress = (pos / total) * 100; progress = (pos / total) * 100;
} }
//console.log(`${progress}%`)
elem.attr('aria-valuenow', progress); elem.attr('aria-valuenow', progress);
elem.css('width', `${progress}%`); elem.css('width', `${progress}%`);
} }
activeStep(x) {
seq.activeStep = function (x) { const step = String(x);
seq.inactiveAll(); this.inactiveAll();
//console.log(`.row input[x=${x + ''}]`) $(`.row input[x=${step}]`).addClass('h');
$(`.row input[x=${x + ''}]`).addClass('h'); $(`#numbers div[x=${step}]`).addClass('h');
$(`#numbers div[x=${x + ''}]`).addClass('h'); }
} inactiveAll() {
seq.inactiveAll = function () {
$('.row input').removeClass('h'); $('.row input').removeClass('h');
$('#numbers div').removeClass('h'); $('#numbers div').removeClass('h');
} }
stop() {
seq.stop = function (s) { ipcRenderer.send(this.id, { stop: true });
'use strict';
ipcRenderer.send(seq.id, { stop : true });
$('#loop_current').text(''); $('#loop_current').text('');
}; }
//start the sequencer from the grid //start the sequencer from the grid
seq.start = function () { start() {
'use strict'; this.time = +new Date();
seq.time = +new Date(); this.arr = JSON.parse(JSON.stringify(this.grid));
seq.arr = seq.grid; this.loops = this.gridLoops + 0;
seq.loops = seq.gridLoops; ipcRenderer.send(this.id, { start: true });
ipcRenderer.send(seq.id, { start : true }); }
}; //start a pre-set sequence, not using the gui
exec(arr, loops) {
//start a pre-set sequence, not using the gui this.time = +new Date();
seq.exec = function (arr, loops) { this.arr = arr;
'use strict'; this.loops = loops;
seq.time = +new Date(); ipcRenderer.send(this.id, { start: true, arr, loops });
seq.arr = arr; }
seq.loops = loops; set(x, cmd) {
ipcRenderer.send(seq.id, { start : true, arr, loops });
};
seq.set = function (x, cmd) {
'use strict';
let increase = 0; let increase = 0;
if (x >= seq.grid.length + 1) { if (x >= this.grid.length + 1) {
increase = x - seq.grid.length; increase = x - this.grid.length;
for (let i = 0; i < increase; i++) { for (let i = 0; i < increase; i++) {
seq.grid.push({}); this.grid.push({});
} }
} }
if (!seq.grid[x]) seq.grid[x] = {}; if (!this.grid[x])
seq.grid[x].x = x; this.grid[x] = {};
seq.grid[x].cmd = cmd; this.grid[x].x = x;
this.grid[x].cmd = cmd;
if (cmd.indexOf('C') !== -1) { if (cmd.indexOf('C') !== -1) {
seq.grid[x].light = light.color; this.grid[x].light = light.color;
} else { }
if (seq.grid[x].light) { else {
delete seq.grid[x].light; if (this.grid[x].light) {
delete this.grid[x].light;
} }
} }
//set //set
ipcRenderer.send(seq.id, { set : [ seq.grid[x] ] }); ipcRenderer.send(this.id, { set: [this.grid[x]] });
//update grid? //update grid?
} }
unsetAll() {
seq.unsetAll = function () { const len = this.grid.length;
const len = seq.grid.length;
const steps = []; const steps = [];
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
if (typeof seq.grid[i] !== 'undefined') { if (typeof this.grid[i] !== 'undefined') {
steps.push(i); steps.push(i);
} }
} }
ipcRenderer.send(seq.id, { unset : steps }); ipcRenderer.send(this.id, { unset: steps });
seq.grid = []; this.grid = [];
} }
unset(x) {
seq.unset = function (x) { this.grid[x] = undefined; //revist this
'use strict'; ipcRenderer.send(this.id, { unset: [x] });
seq.grid[x] = undefined }
ipcRenderer.send(seq.id, { unset : [ x ]}); /**
}
/**
* Set the light value at a specific step and then update * Set the light value at a specific step and then update
* GUI grid via .state() * GUI grid via .state()
* *
* @param {integer} x Step in sequence * @param {integer} x Step in sequence
* @param {array} rgb Light value in RGB * @param {array} rgb Light value in RGB
**/ **/
seq.setLight = function (x, rgb) { setLight(x, rgb) {
'use strict';
let color = rgb.join(','); let color = rgb.join(',');
seq.grid[x].light = color; this.grid[x].light = color;
ipcRenderer.send(seq.id, { x, cmd : seq.grid[x].cmd, light : color }); ipcRenderer.send(this.id, { x, cmd: this.grid[x].cmd, light: color });
}; }
/**
/**
* Function bound to the change event on the loop counter * Function bound to the change event on the loop counter
* input element * input element
* *
* @param {integer} count Integer to set loops to * @param {integer} count Integer to set loops to
*/ */
seq.setLoops = function (count) { setLoops(count) {
'use strict'; this.gridLoops = count;
seq.gridLoops = count; this.stats();
seq.stats(); ipcRenderer.send(this.id, { loops: this.gridLoops });
ipcRenderer.send(seq.id, { loops : seq.gridLoops }) }
}; stats() {
seq.stats = function () {
'use strict';
let ms = 0; let ms = 0;
let c = ''; let c = '';
let cam_total = 0; let cam_total = 0;
let proj_total = 0; let proj_total = 0;
let real_total = seq.grid.filter(function (elem) { let real_total = this.grid.filter((elem) => {
if (elem === undefined) { if (elem == undefined) {
return false; return false;
} }
return true; return true;
}); });
//timing //timing
for (let step of seq.grid) { for (let step of this.grid) {
c = seq.cmd; if (!step)
if (c === cfg.cmd.camera_forward || c === cfg.cmd.camera_backward){ continue;
c = step.cmd;
if (c === cfg.cmd.camera_forward || c === cfg.cmd.camera_backward) {
ms += cfg.arduino.cam.time; ms += cfg.arduino.cam.time;
ms += cfg.arduino.cam.delay; ms += cfg.arduino.cam.delay;
ms += cfg.arduino.serialDelay; ms += cfg.arduino.serialDelay;
} }
if (c === cfg.cmd.projector_forward || c === cfg.cmd.projector_backward){ if (c === cfg.cmd.projector_forward || c === cfg.cmd.projector_backward) {
ms += cfg.arduino.proj.time; ms += cfg.arduino.proj.time;
ms += cfg.arduino.proj.delay; ms += cfg.arduino.proj.delay;
ms += cfg.arduino.serialDelay; ms += cfg.arduino.serialDelay;
} }
if (c === cfg.cmd.black_forward || c === cfg.cmd.black_backward){ if (c === cfg.cmd.black_forward || c === cfg.cmd.black_backward) {
ms += cfg.arduino.black.before; ms += cfg.arduino.black.before;
ms += cfg.arduino.black.after; ms += cfg.arduino.black.after;
ms += cfg.arduino.cam.time; ms += cfg.arduino.cam.time;
@ -202,7 +184,6 @@ seq.stats = function () {
ms += cfg.arduino.serialDelay; ms += cfg.arduino.serialDelay;
} }
ms += cfg.arduino.sequenceDelay; ms += cfg.arduino.sequenceDelay;
if (c === cfg.cmd.camera_forward || c === cfg.cmd.black_forward) { if (c === cfg.cmd.camera_forward || c === cfg.cmd.black_forward) {
cam_total++; cam_total++;
} }
@ -216,39 +197,33 @@ seq.stats = function () {
proj_total--; proj_total--;
} }
} }
//timing //timing
ms = ms * seq.gridLoops; ms = ms * this.gridLoops;
if (ms < 2000) { if (ms < 2000) {
$('#seq_stats .timing span').text(ms + 'ms'); $('#seq_stats .timing span').text(ms + 'ms');
} else { }
else {
$('#seq_stats .timing span').text(humanizeDuration(ms)); $('#seq_stats .timing span').text(humanizeDuration(ms));
} }
//ending frames //ending frames
cam_total = cam_total * seq.gridLoops; cam_total = cam_total * this.gridLoops;
proj_total = proj_total * seq.gridLoops; proj_total = proj_total * this.gridLoops;
$('#seq_stats .cam_end span').text(gui.fmtZero(cam.pos + cam_total, 6)); $('#seq_stats .cam_end span').text(gui.fmtZero(cam.pos + cam_total, 6));
$('#seq_stats .proj_end span').text(gui.fmtZero(proj.pos + proj_total, 6)); $('#seq_stats .proj_end span').text(gui.fmtZero(proj.pos + proj_total, 6));
//count //count
$('#seq_stats .seq_count span').text(real_total.length * seq.gridLoops); $('#seq_stats .seq_count span').text(real_total.length * this.gridLoops);
return ms; return ms;
}; }
clear() {
seq.clear = function () { this.size = 24;
'use strict'; this.unsetAll();
seq.size = 24; this.progress(0, 0);
seq.unsetAll(); }
seq.progress(0, 0); cancel() {
};
seq.cancel = function () {
gui.spinner(true, `Cancelling sequence...`); gui.spinner(true, `Cancelling sequence...`);
seq.running = false; this.running = false;
seq.stop(); this.stop();
}
} }
seq = new Sequence();
module.exports = seq; module.exports = seq;

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy-app", "name": "mcopy-app",
"version": "1.4.8", "version": "1.4.9",
"description": "GUI for the mcopy small gauge film optical printer platform", "description": "GUI for the mcopy small gauge film optical printer platform",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {

View File

@ -1,3 +1,4 @@
#!/bin/sh #!/bin/sh
./node_modules/.bin/tsc ./src/lib/ui/grid.ts --outFile ./lib/ui/grid.js --noImplicitAny --lib ES2017 --lib ES2016 --lib dom -t ES2016 ./node_modules/.bin/tsc ./src/lib/ui/grid.ts --outFile ./lib/ui/grid.js --noImplicitAny --lib ES2017 --lib ES2016 --lib dom -t ES2016
./node_modules/.bin/tsc ./src/lib/ui/seq.ts --outFile ./lib/ui/seq.js --noImplicitAny --lib ES2017 --lib ES2016 --lib dom -t ES2016

271
app/src/lib/ui/seq.ts Normal file
View File

@ -0,0 +1,271 @@
'use strict'
/// <reference path ="jquery.d.ts"/>
declare var gui : any;
declare var grid : any;
declare var light : any;
declare var cfg : any;
declare var log : any;
declare var w2popup : any;
declare var cam : any;
declare var proj : any;
declare var ipcRenderer : any;
declare var humanizeDuration : Function;
interface Arg {
loop : number;
step : number;
stop : boolean;
start : boolean;
}
interface Step {
cmd : string;
light: string;
x : number;
}
let seq : any = {};
class Sequence {
private id : string = 'sequence';
public grid : any[] = [];
public gridLoops : number = 1;
public arr : any[] = [];
public loops : number = 1;
public size : number = 24;
private time : number = 0;
private running : boolean = false;
constructor () {
}
public init () {
this.listen();
}
private listen () {
ipcRenderer.on(this.id, this.listener.bind(this))
}
private listener (event : Event, arg : Arg) {
//console.log(JSON.stringify(arg))
if (arg.start) {
if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') {
this.activeStep(arg.step);
log.info(`Step ${arg.step + 1}/${this.arr.length}, Loop ${arg.loop + 1}/${this.loops}`, 'SERIAL', true);
} else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text(gui.fmtZero(arg.loop + 1, 6));
} else {
this.progress(0, 0);
}
} else if (arg.stop) {
if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') {
//console.log(JSON.stringify(arg))
this.progress(arg.step + 1, arg.loop);
this.inactiveAll();
} else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text('');
} else {
gui.overlay(false);
gui.spinner(false);
log.info('Sequence stopped', 'SERIAL', true);
}
}
return event.returnValue = true;
}
private progress (step : number, loop : number) {
const elem : any = $('.progress-bar');
const len : number = this.arr.length;
const total : number = len * this.loops;
let pos : number = (loop * len) + step;
let progress : number = 0;
if (pos > 0 && total > 0) {
progress = (pos / total) * 100;
}
elem.attr('aria-valuenow', progress);
elem.css('width', `${progress}%`);
}
private activeStep (x : number) {
const step : string = String(x);
this.inactiveAll();
$(`.row input[x=${step}]`).addClass('h');
$(`#numbers div[x=${step}]`).addClass('h');
}
private inactiveAll () {
$('.row input').removeClass('h');
$('#numbers div').removeClass('h');
}
public stop () {
ipcRenderer.send(this.id, { stop : true });
$('#loop_current').text('');
}
//start the sequencer from the grid
public start () {
this.time = +new Date();
this.arr = JSON.parse(JSON.stringify(this.grid));
this.loops = this.gridLoops + 0;
ipcRenderer.send(this.id, { start : true });
}
//start a pre-set sequence, not using the gui
public exec (arr : any[], loops : number) {
this.time = +new Date();
this.arr = arr;
this.loops = loops;
ipcRenderer.send(this.id, { start : true, arr, loops });
}
public set (x : number, cmd : string) {
let increase : number = 0;
if (x >= this.grid.length + 1) {
increase = x - this.grid.length;
for (let i : number = 0; i < increase; i++) {
this.grid.push({});
}
}
if (!this.grid[x]) this.grid[x] = {};
this.grid[x].x = x;
this.grid[x].cmd = cmd;
if (cmd.indexOf('C') !== -1) {
this.grid[x].light = light.color;
} else {
if (this.grid[x].light) {
delete this.grid[x].light;
}
}
//set
ipcRenderer.send(this.id, { set : [ this.grid[x] ] });
//update grid?
}
public unsetAll () {
const len : number = this.grid.length;
const steps : number[] = [];
for (let i : number = 0; i < len; i++) {
if (typeof this.grid[i] !== 'undefined') {
steps.push(i);
}
}
ipcRenderer.send(this.id, { unset : steps });
this.grid = [];
}
public unset (x : number) {
this.grid[x] = undefined; //revist this
ipcRenderer.send(this.id, { unset : [ x ]});
}
/**
* Set the light value at a specific step and then update
* GUI grid via .state()
*
* @param {integer} x Step in sequence
* @param {array} rgb Light value in RGB
**/
public setLight (x : number, rgb : number[]) {
let color : string = rgb.join(',');
this.grid[x].light = color;
ipcRenderer.send(this.id, { x, cmd : this.grid[x].cmd, light : color });
}
/**
* Function bound to the change event on the loop counter
* input element
*
* @param {integer} count Integer to set loops to
*/
public setLoops (count : number) {
this.gridLoops = count;
this.stats();
ipcRenderer.send(this.id, { loops : this.gridLoops })
}
public stats () {
let ms : number = 0;
let c : string = '';
let cam_total : number = 0;
let proj_total : number = 0;
let real_total : Step[] = this.grid.filter((elem : any) => {
if (elem == undefined) {
return false;
}
return true;
});
//timing
for (let step of this.grid) {
if (!step) continue
c = step.cmd;
if (c === cfg.cmd.camera_forward || c === cfg.cmd.camera_backward){
ms += cfg.arduino.cam.time;
ms += cfg.arduino.cam.delay;
ms += cfg.arduino.serialDelay;
}
if (c === cfg.cmd.projector_forward || c === cfg.cmd.projector_backward){
ms += cfg.arduino.proj.time;
ms += cfg.arduino.proj.delay;
ms += cfg.arduino.serialDelay;
}
if (c === cfg.cmd.black_forward || c === cfg.cmd.black_backward){
ms += cfg.arduino.black.before;
ms += cfg.arduino.black.after;
ms += cfg.arduino.cam.time;
ms += cfg.arduino.cam.delay;
ms += cfg.arduino.serialDelay;
}
ms += cfg.arduino.sequenceDelay;
if (c === cfg.cmd.camera_forward || c === cfg.cmd.black_forward) {
cam_total++;
}
if (c === cfg.cmd.camera_backward || c === cfg.cmd.black_backward) {
cam_total--;
}
if (c === cfg.cmd.projector_forward) {
proj_total++;
}
if (c === cfg.cmd.projector_backward) {
proj_total--;
}
}
//timing
ms = ms * this.gridLoops;
if (ms < 2000) {
$('#seq_stats .timing span').text(ms + 'ms');
} else {
$('#seq_stats .timing span').text(humanizeDuration(ms));
}
//ending frames
cam_total = cam_total * this.gridLoops;
proj_total = proj_total * this.gridLoops;
$('#seq_stats .cam_end span').text(gui.fmtZero(cam.pos + cam_total, 6));
$('#seq_stats .proj_end span').text(gui.fmtZero(proj.pos + proj_total, 6));
//count
$('#seq_stats .seq_count span').text(real_total.length * this.gridLoops);
return ms;
}
public clear () {
this.size = 24;
this.unsetAll();
this.progress(0, 0);
}
public cancel () {
gui.spinner(true, `Cancelling sequence...`);
this.running = false;
this.stop();
}
}
seq = new Sequence();
module.exports = seq;

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy-cli", "name": "mcopy-cli",
"version": "1.4.8", "version": "1.4.9",
"description": "CLI for controlling the mcopy optical printer platform", "description": "CLI for controlling the mcopy optical printer platform",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -1,5 +1,5 @@
{ {
"version": "1.4.8", "version": "1.4.9",
"ext_port": 1111, "ext_port": 1111,
"profiles": { "profiles": {
"mcopy": { "mcopy": {

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy", "name": "mcopy",
"version": "1.4.8", "version": "1.4.9",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy", "name": "mcopy",
"version": "1.4.8", "version": "1.4.9",
"description": "Small gauge film optical printer platform", "description": "Small gauge film optical printer platform",
"main": "build.js", "main": "build.js",
"directories": { "directories": {