2018-03-16 15:30:54 +00:00
|
|
|
'use strict';
|
2016-04-12 04:06:23 +00:00
|
|
|
|
2018-03-16 16:57:02 +00:00
|
|
|
/** @module lib/mscript */
|
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
const BLACK = '0,0,0';
|
|
|
|
const CMD = [
|
2016-04-12 04:06:23 +00:00
|
|
|
'CF',
|
|
|
|
'PF',
|
|
|
|
'BF',
|
|
|
|
'CB',
|
|
|
|
'PB',
|
2016-04-18 05:12:04 +00:00
|
|
|
'BB'
|
2016-04-12 04:06:23 +00:00
|
|
|
];
|
2018-06-16 22:50:24 +00:00
|
|
|
const ALTS = {
|
2016-04-12 04:06:23 +00:00
|
|
|
'CF' : ['CAMERA FORWARD', 'CAM FORWARD'],
|
|
|
|
'PF' : ['PROJECTOR FORWARD', 'PROJ FORWARD'],
|
2018-06-16 22:50:24 +00:00
|
|
|
'BF' : ['BLACK FORWARD', 'BLACK', 'BLANK FORWARD', 'BLANK'],
|
2016-04-12 04:06:23 +00:00
|
|
|
'CB' : ['CAMERA BACKWARD', 'CAM BACKWARD', 'CAMERA BACK', 'CAM BACK'],
|
|
|
|
'PB' : ['PROJECTOR FORWARD', 'PROJ FORWARD', 'PROJECTOR BACK', 'PROJ BACK'],
|
2018-03-16 16:57:02 +00:00
|
|
|
'BB' : ['BLACK BACKWARD', 'BLACK BACK', 'BLANK BACK'],
|
|
|
|
'L ' : ['LIGHT', 'COLOR', 'LAMP'],
|
|
|
|
'F ' : ['FADE']
|
2016-04-12 04:06:23 +00:00
|
|
|
};
|
2018-03-16 16:57:02 +00:00
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
class Mscript {
|
|
|
|
constructor () {
|
|
|
|
this.output = {};
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the state of the script
|
|
|
|
*/
|
|
|
|
clear () {
|
|
|
|
this.cam = 0;
|
|
|
|
this.proj = 0;
|
|
|
|
this.color = '';
|
|
|
|
this.loops = [];
|
|
|
|
this.rec = -1;
|
|
|
|
this.output = {};
|
2016-04-12 04:21:19 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
/**
|
|
|
|
* Main function, accepts multi-line string, parses into lines
|
|
|
|
* and interprets the instructions from the text. Returns an array
|
|
|
|
* of steps to be fed into the mcopy.
|
|
|
|
*/
|
|
|
|
interpret (text, callback) {
|
|
|
|
this.clear()
|
2016-04-18 16:23:14 +00:00
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
if (typeof text === 'undefined') {
|
|
|
|
return this.fail('No input');
|
|
|
|
}
|
2016-04-18 16:23:14 +00:00
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
//split string into lines, each containing a command
|
|
|
|
let lines = text.split('\n');
|
|
|
|
|
|
|
|
let two = '';
|
|
|
|
|
|
|
|
let arr = [];
|
|
|
|
|
|
|
|
let light = [];
|
|
|
|
|
|
|
|
let target = 0;// move to target using CAM # or PROJ #
|
|
|
|
|
|
|
|
let dist = 0; //?
|
|
|
|
|
|
|
|
for (let line of lines) {
|
|
|
|
line = line.replace(/\t+/g, ""); //strip tabs
|
|
|
|
line = line.trim(); //remove excess whitespace before and after command
|
|
|
|
two = line.substring(0, 2);
|
|
|
|
if (CMD.indexOf(two) !== -1) {
|
|
|
|
|
|
|
|
if (this.loops.length > 0) {
|
|
|
|
//hold generated arr in state loop array
|
|
|
|
this.loops[this.rec].arr
|
|
|
|
.push.apply(this.loops[this.rec].arr,
|
|
|
|
this.str_to_arr(line,
|
|
|
|
two));
|
|
|
|
this.loops[this.rec].light
|
|
|
|
.push.apply(this.loops[this.rec].light,
|
|
|
|
this.light_to_arr(line,
|
|
|
|
two));
|
2016-04-18 16:23:14 +00:00
|
|
|
} else {
|
2018-06-16 22:50:24 +00:00
|
|
|
arr.push.apply(arr, this.str_to_arr(line, two));
|
|
|
|
light.push.apply(light, this.light_to_arr(line, two))
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (line.substring(0, 4) === 'LOOP') {
|
|
|
|
this.rec++;
|
|
|
|
this.loops[this.rec] = {
|
|
|
|
arr : [],
|
|
|
|
light : [],
|
|
|
|
cam : 0,
|
|
|
|
proj : 0,
|
|
|
|
cmd : line + ''
|
|
|
|
};
|
|
|
|
} else if (line.substring(0, 2) === 'L ') {
|
|
|
|
this.light_state(line);
|
|
|
|
} else if (line.substring(0, 3) === 'END') {
|
|
|
|
for (var x = 0; x < this.loop_count(this.loops[this.rec].cmd); x++) {
|
|
|
|
if (this.rec === 0) {
|
|
|
|
arr.push.apply(arr, this.loops[this.rec].arr);
|
|
|
|
light.push.apply(light, this.loops[this.rec].light);
|
|
|
|
} else if (this.rec >= 1) {
|
|
|
|
this.loops[this.rec - 1].arr
|
|
|
|
.push.apply(this.loops[this.rec - 1].arr,
|
|
|
|
this.loops[this.rec].arr);
|
|
|
|
this.loops[this.rec - 1].light
|
|
|
|
.push.apply(this.loops[this.rec - 1].light,
|
|
|
|
this.loops[this.rec].light);
|
2016-04-18 16:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
this.update('END', this.loop_count(this.loops[this.rec].cmd));
|
|
|
|
delete this.loops[this.rec];
|
|
|
|
this.rec--;
|
|
|
|
} else if (line.substring(0, 3) === 'CAM') { //directly go to that frame (black?)
|
|
|
|
target = parseInt(line.split('CAM ')[1]);
|
|
|
|
if (this.loops.length > 0) {
|
|
|
|
if (target > this.cam) {
|
|
|
|
dist = target - this.cam;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
this.loops[this.rec].arr.push('BF');
|
|
|
|
this.loops[this.rec].light.push(BLACK);
|
|
|
|
this.update('BF');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dist = this.cam - target;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
this.loops[this.rec].arr.push('BB');
|
|
|
|
this.loops[this.rec].light.push(BLACK);
|
|
|
|
this.update('BB');
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 16:23:14 +00:00
|
|
|
} else {
|
2018-06-16 22:50:24 +00:00
|
|
|
if (target > this.cam) {
|
|
|
|
dist = target - this.cam;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
arr.push('BF');
|
|
|
|
light.push(BLACK);
|
|
|
|
this.update('BF');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dist = this.cam - target;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
arr.push('BB');
|
|
|
|
light.push(BLACK);
|
|
|
|
this.update('BB');
|
|
|
|
}
|
2016-04-18 16:23:14 +00:00
|
|
|
}
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
} else if (line.substring(0, 4) === 'PROJ') { //directly go to that frame
|
|
|
|
target = parseInt(line.split('PROJ ')[1]);
|
|
|
|
if (this.loops.length > 0) {
|
|
|
|
if (target > this.proj) {
|
|
|
|
dist = target - this.proj;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
this.loops[this.rec].arr.push('PF');
|
|
|
|
this.loops[this.rec].light.push('');
|
|
|
|
this.update('PF');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dist = this.proj - target;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
this.loops[this.rec].arr.push('PB');
|
|
|
|
this.loops[this.rec].light.push('');
|
|
|
|
this.update('PB');
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 16:23:14 +00:00
|
|
|
} else {
|
2018-06-16 22:50:24 +00:00
|
|
|
if (target > this.proj) {
|
|
|
|
dist = target - this.proj;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
arr.push('PF');
|
|
|
|
light.push('');
|
|
|
|
this.update('PF');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dist = this.proj - target;
|
|
|
|
for (var x = 0; x < dist; x++) {
|
|
|
|
arr.push('PB');
|
|
|
|
light.push('');
|
|
|
|
this.update('PB');
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 16:23:14 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
} else if (line.substring(0, 3) === 'SET') { //set that state
|
|
|
|
if (line.substring(0, 7) === 'SET CAM') {
|
|
|
|
this.cam = parseInt(line.split('SET CAM')[1]);
|
|
|
|
} else if (line.substring(0, 8) === 'SET PROJ') {
|
|
|
|
this.proj = parseInt(line.split('SET PROJ')[1]);
|
2016-04-18 16:23:14 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
} else if (line.substring(0, 1) === '#' || line.substring(0, 2) === '//') {
|
|
|
|
//comments
|
|
|
|
//ignore while parsing
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-18 05:12:04 +00:00
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
|
|
|
|
this.output.success = true;
|
|
|
|
this.output.arr = arr; //all instructions
|
|
|
|
this.output.light = light; //all light instructions
|
|
|
|
this.output.cam = this.cam;
|
|
|
|
this.output.proj = this.proj;
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof callback !== 'undefined') {
|
|
|
|
//should only be invoked by running mscript.tests()
|
|
|
|
callback(this.output);
|
2016-04-18 05:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
last_loop () {
|
|
|
|
return this.loops[this.loops.length - 1];
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
parent_loop () {
|
|
|
|
return this.loops[this.loops.length - 2];
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
loop_count (str) {
|
|
|
|
return parseInt(str.split(' ')[1]);
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
fade_count (str) {
|
|
|
|
return parseInt(str.split(' ')[1]);
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
/**
|
|
|
|
* Increase the state of a specific object, such as the camera/projector,
|
|
|
|
* by the value defined in val
|
|
|
|
*/
|
|
|
|
update (cmd, val = 1) {
|
|
|
|
if (cmd === 'END') {
|
|
|
|
//I don't understand this loop
|
|
|
|
for (let i = 0; i < val; i++) {
|
|
|
|
if (this.rec === 0) {
|
|
|
|
this.cam += this.loops[this.rec].cam;
|
|
|
|
this.proj += this.loops[this.rec].proj;
|
|
|
|
} else if (this.rec >= 1) {
|
|
|
|
this.loops[this.rec - 1].cam += this.loops[this.rec].cam;
|
|
|
|
this.loops[this.rec - 1].proj += this.loops[this.rec].proj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (cmd === 'CF') {
|
|
|
|
if (this.loops.length < 1) {
|
|
|
|
this.cam += val;
|
|
|
|
} else {
|
|
|
|
this.loops[this.rec].cam += val;
|
|
|
|
}
|
|
|
|
} else if (cmd === 'CB') {
|
|
|
|
if (this.loops.length < 1) {
|
|
|
|
this.cam -= val;
|
|
|
|
} else {
|
|
|
|
this.loops[this.rec].cam--;
|
|
|
|
}
|
|
|
|
} else if (cmd === 'PF') {
|
|
|
|
if (this.loops.length < 1) {
|
|
|
|
this.proj += val;
|
|
|
|
} else {
|
|
|
|
this.loops[this.rec].proj += val;
|
|
|
|
}
|
|
|
|
} else if (cmd === 'PB') {
|
|
|
|
if (this.loops.length < 1) {
|
|
|
|
this.proj -= val;
|
|
|
|
} else {
|
|
|
|
this.loops[this.rec].proj--;
|
|
|
|
}
|
|
|
|
} else if (cmd === 'BF') {
|
|
|
|
if (this.loops.length < 1) {
|
|
|
|
this.cam += val;
|
|
|
|
} else {
|
|
|
|
this.loops[this.rec].cam += val;
|
|
|
|
}
|
|
|
|
} else if (cmd === 'BB') {
|
|
|
|
if (this.loops.length < 1) {
|
|
|
|
this.cam -= val;
|
|
|
|
} else {
|
|
|
|
this.loops[this.rec].cam -= val;
|
|
|
|
}
|
|
|
|
} else if (cmd === 'L ') {
|
2016-04-12 04:06:23 +00:00
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
}
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
/**
|
|
|
|
* Split string on command, extract any integers from string
|
|
|
|
*/
|
|
|
|
str_to_arr (str, cmd) {
|
|
|
|
const cnt = str.split(cmd);
|
|
|
|
let c = parseInt(cnt[1]);
|
|
|
|
let arr = [];
|
|
|
|
if (cnt[1] === '') {
|
|
|
|
c = 1;
|
|
|
|
} else {
|
|
|
|
c = parseInt(cnt[1]);
|
|
|
|
}
|
|
|
|
arr = new Array(c).fill(cmd);
|
|
|
|
this.update(cmd, c);
|
|
|
|
return arr;
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
light_to_arr (str, cmd) {
|
|
|
|
const cnt = str.split(cmd);
|
|
|
|
let c = parseInt(cnt[1]);
|
|
|
|
let arr = [];
|
|
|
|
if (cnt[1] === '') {
|
|
|
|
c = 1;
|
|
|
|
} else {
|
|
|
|
c = parseInt(cnt[1]);
|
|
|
|
}
|
|
|
|
for (var i = 0; i < c; i++) {
|
|
|
|
if (cmd === 'CF'
|
|
|
|
|| cmd === 'CB') {
|
|
|
|
arr.push(this.color);
|
|
|
|
} else if (cmd === 'BF'
|
|
|
|
|| cmd === 'BB') {
|
|
|
|
arr.push(BLACK);
|
|
|
|
} else {
|
|
|
|
arr.push('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr;
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
2018-06-16 22:50:24 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
light_state (str) {
|
|
|
|
//add parsers for other color spaces
|
|
|
|
const color = str.replace('L ', '').trim();
|
|
|
|
this.color = color;
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fail (msg) {
|
|
|
|
throw new Error(msg);
|
2016-04-12 04:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 22:50:24 +00:00
|
|
|
module.exports = Mscript;
|
|
|
|
|
2016-04-12 04:38:01 +00:00
|
|
|
|
2016-04-12 04:06:23 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
CAM # - go to camera frame #
|
|
|
|
PROJ # - go to projector frame #
|
|
|
|
|
|
|
|
SET CAM # - sets camera count to #
|
|
|
|
SET PROJ # - sets projector count to #
|
|
|
|
|
|
|
|
LOOP # - begin loop, can nest recursively, # times
|
|
|
|
END LOOP - (or END) closes loop
|
|
|
|
|
2016-04-18 16:23:14 +00:00
|
|
|
L #RGB - sets light to rgb value
|
|
|
|
|
|
|
|
FADE
|
|
|
|
|
2016-04-12 04:06:23 +00:00
|
|
|
CF - Camera forwards
|
|
|
|
PF - Projector forwards
|
|
|
|
BF - Black forwards
|
|
|
|
CB - Camera backwards
|
|
|
|
PB - Projector backwards
|
|
|
|
BB - Black backwards
|
|
|
|
|
|
|
|
*/
|