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,44 +1,46 @@
const seq = {};
seq.id = 'sequence';
seq.grid = [];
seq.gridLoops = 1;
seq.arr = [];
seq.loops = 1;
seq.size = 24;
seq.time = 0;
seq.running = false;
/******
Sequence Object
*******/
seq.init = function () {
seq.listen();
'use strict';
let seq = {};
class Sequence {
constructor() {
this.id = 'sequence';
this.grid = [];
this.gridLoops = 1;
this.arr = [];
this.loops = 1;
this.size = 24;
this.time = 0;
this.running = false;
}
seq.listen = function () {
ipcRenderer.on(seq.id, seq.listener);
init() {
this.listen();
}
seq.listener = function (event, arg) {
listen() {
ipcRenderer.on(this.id, this.listener.bind(this));
}
listener(event, arg) {
//console.log(JSON.stringify(arg))
if (arg.start) {
if (typeof arg.loop !== 'undefined' && typeof arg.step !== 'undefined') {
seq.activeStep(arg.step);
log.info(`Step ${arg.step + 1}/${seq.arr.length}, Loop ${arg.loop + 1}/${seq.loops}`, 'SERIAL', true);
} else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text(gui.fmtZero(arg.loop + 1, 6));
} else {
seq.progress(0, 0);
this.activeStep(arg.step);
log.info(`Step ${arg.step + 1}/${this.arr.length}, Loop ${arg.loop + 1}/${this.loops}`, 'SERIAL', true);
}
} 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') {
//console.log(JSON.stringify(arg))
seq.progress(arg.step + 1, arg.loop);
seq.inactiveAll();
} else if (typeof arg.loop !== 'undefined') {
this.progress(arg.step + 1, arg.loop);
this.inactiveAll();
}
else if (typeof arg.loop !== 'undefined') {
$('#loop_current').text('');
} else {
}
else {
gui.overlay(false);
gui.spinner(false);
log.info('Sequence stopped', 'SERIAL', true);
@ -46,101 +48,85 @@ seq.listener = function (event, arg) {
}
return event.returnValue = true;
}
seq.progress = function (step, loop) {
progress(step, loop) {
const elem = $('.progress-bar');
const len = seq.arr.length;
const total = len * seq.loops;
const len = this.arr.length;
const total = len * this.loops;
let pos = (loop * len) + step;
let progress = 0;
if (pos > 0 && total > 0) {
progress = (pos / total) * 100;
}
//console.log(`${progress}%`)
elem.attr('aria-valuenow', progress);
elem.css('width', `${progress}%`);
}
seq.activeStep = function (x) {
seq.inactiveAll();
//console.log(`.row input[x=${x + ''}]`)
$(`.row input[x=${x + ''}]`).addClass('h');
$(`#numbers div[x=${x + ''}]`).addClass('h');
activeStep(x) {
const step = String(x);
this.inactiveAll();
$(`.row input[x=${step}]`).addClass('h');
$(`#numbers div[x=${step}]`).addClass('h');
}
seq.inactiveAll = function () {
inactiveAll() {
$('.row input').removeClass('h');
$('#numbers div').removeClass('h');
}
seq.stop = function (s) {
'use strict';
ipcRenderer.send(seq.id, { stop : true });
stop() {
ipcRenderer.send(this.id, { stop: true });
$('#loop_current').text('');
};
}
//start the sequencer from the grid
seq.start = function () {
'use strict';
seq.time = +new Date();
seq.arr = seq.grid;
seq.loops = seq.gridLoops;
ipcRenderer.send(seq.id, { start : true });
};
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
seq.exec = function (arr, loops) {
'use strict';
seq.time = +new Date();
seq.arr = arr;
seq.loops = loops;
ipcRenderer.send(seq.id, { start : true, arr, loops });
};
seq.set = function (x, cmd) {
'use strict';
exec(arr, loops) {
this.time = +new Date();
this.arr = arr;
this.loops = loops;
ipcRenderer.send(this.id, { start: true, arr, loops });
}
set(x, cmd) {
let increase = 0;
if (x >= seq.grid.length + 1) {
increase = x - seq.grid.length;
if (x >= this.grid.length + 1) {
increase = x - this.grid.length;
for (let i = 0; i < increase; i++) {
seq.grid.push({});
this.grid.push({});
}
}
if (!seq.grid[x]) seq.grid[x] = {};
seq.grid[x].x = x;
seq.grid[x].cmd = cmd;
if (!this.grid[x])
this.grid[x] = {};
this.grid[x].x = x;
this.grid[x].cmd = cmd;
if (cmd.indexOf('C') !== -1) {
seq.grid[x].light = light.color;
} else {
if (seq.grid[x].light) {
delete seq.grid[x].light;
this.grid[x].light = light.color;
}
else {
if (this.grid[x].light) {
delete this.grid[x].light;
}
}
//set
ipcRenderer.send(seq.id, { set : [ seq.grid[x] ] });
ipcRenderer.send(this.id, { set: [this.grid[x]] });
//update grid?
}
seq.unsetAll = function () {
const len = seq.grid.length;
unsetAll() {
const len = this.grid.length;
const steps = [];
for (let i = 0; i < len; i++) {
if (typeof seq.grid[i] !== 'undefined') {
if (typeof this.grid[i] !== 'undefined') {
steps.push(i);
}
}
ipcRenderer.send(seq.id, { unset : steps });
seq.grid = [];
ipcRenderer.send(this.id, { unset: steps });
this.grid = [];
}
seq.unset = function (x) {
'use strict';
seq.grid[x] = undefined
ipcRenderer.send(seq.id, { unset : [ x ]});
unset(x) {
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()
@ -148,42 +134,38 @@ seq.unset = function (x) {
* @param {integer} x Step in sequence
* @param {array} rgb Light value in RGB
**/
seq.setLight = function (x, rgb) {
'use strict';
setLight(x, rgb) {
let color = rgb.join(',');
seq.grid[x].light = color;
ipcRenderer.send(seq.id, { x, cmd : seq.grid[x].cmd, light : color });
};
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
*/
seq.setLoops = function (count) {
'use strict';
seq.gridLoops = count;
seq.stats();
ipcRenderer.send(seq.id, { loops : seq.gridLoops })
};
seq.stats = function () {
'use strict';
setLoops(count) {
this.gridLoops = count;
this.stats();
ipcRenderer.send(this.id, { loops: this.gridLoops });
}
stats() {
let ms = 0;
let c = '';
let cam_total = 0;
let proj_total = 0;
let real_total = seq.grid.filter(function (elem) {
if (elem === undefined) {
let real_total = this.grid.filter((elem) => {
if (elem == undefined) {
return false;
}
return true;
});
//timing
for (let step of seq.grid) {
c = seq.cmd;
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;
@ -202,7 +184,6 @@ seq.stats = function () {
ms += cfg.arduino.serialDelay;
}
ms += cfg.arduino.sequenceDelay;
if (c === cfg.cmd.camera_forward || c === cfg.cmd.black_forward) {
cam_total++;
}
@ -216,39 +197,33 @@ seq.stats = function () {
proj_total--;
}
}
//timing
ms = ms * seq.gridLoops;
ms = ms * this.gridLoops;
if (ms < 2000) {
$('#seq_stats .timing span').text(ms + 'ms');
} else {
}
else {
$('#seq_stats .timing span').text(humanizeDuration(ms));
}
//ending frames
cam_total = cam_total * seq.gridLoops;
proj_total = proj_total * seq.gridLoops;
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 * seq.gridLoops);
$('#seq_stats .seq_count span').text(real_total.length * this.gridLoops);
return ms;
};
seq.clear = function () {
'use strict';
seq.size = 24;
seq.unsetAll();
seq.progress(0, 0);
};
seq.cancel = function () {
gui.spinner(true, `Cancelling sequence...`);
seq.running = false;
seq.stop();
}
clear() {
this.size = 24;
this.unsetAll();
this.progress(0, 0);
}
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-app",
"version": "1.4.8",
"version": "1.4.9",
"description": "GUI for the mcopy small gauge film optical printer platform",
"main": "main.js",
"scripts": {

View File

@ -1,3 +1,4 @@
#!/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/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",
"version": "1.4.8",
"version": "1.4.9",
"description": "CLI for controlling the mcopy optical printer platform",
"main": "index.js",
"scripts": {

View File

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

2
package-lock.json generated
View File

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

View File

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