From 859f550f446f7b9c5a2f262564606dec310d1ed9 Mon Sep 17 00:00:00 2001 From: Lautaro Petaccio Date: Thu, 23 Jun 2016 12:26:17 -0300 Subject: [PATCH 1/3] Refactorizacion de codigo - desacoplamiento de navbar y simulator --- src/app/home/navbar/navbar.js | 127 +++------------------------- src/app/home/simulator/simulator.js | 121 ++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 115 deletions(-) diff --git a/src/app/home/navbar/navbar.js b/src/app/home/navbar/navbar.js index 639c2b4..b36cde3 100644 --- a/src/app/home/navbar/navbar.js +++ b/src/app/home/navbar/navbar.js @@ -36,49 +36,33 @@ this.HomeNavbar = (function() { } module.onClickRun = function(event) { - if(me.execution.hasFinished()) { - var code = Blockly.JavaScript.workspaceToCode(HomeBlockly.workspace); - if(!code.length) { - module.notifyEmptyProgram(); - return; - } - me.state.pause(); - me.state.change(me.state.PLAY); - var onCompleted = function() { - $('.btn_run').show(); - $('.btn_pause').hide(); - $('.btn_stop').addClass('disabled'); - } - me.execution.onCompleted = onCompleted; - me.interpreter = new Interpreter(code, initApi); + var onCompleted = function() { + $('.btn_run').show(); + $('.btn_pause').hide(); + $('.btn_stop').addClass('disabled'); + } + var onRun = function() { $('.btn_pause').show(); $('.btn_stop').removeClass('disabled'); - me.state.resume(); - me.execution.run(); - } else if (me.execution.isPaused()) { - $('.btn_pause').show(); - me.execution.run(); + $('.btn_run').hide(); } - $('.btn_run').hide(); + HomeSimulator.run(onRun, onCompleted); event.stopPropagation(); } module.onClickPause = function(event) { - if(me.execution.isRunning()) { + HomeSimulator.pause(function() { $('.btn_run').show(); $('.btn_pause').hide(); - me.execution.pause(); - } + }); } module.onClickStop = function(event) { - if(!me.execution.hasFinished()) { + HomeSimulator.stop(function() { $('.btn_run').show(); $('.btn_pause').hide(); $('.btn_stop').addClass('disabled'); - me.execution.finish(); - me.state.change(me.state.PLAY); - } + }); } module.onClickSave = function(event) { @@ -99,93 +83,6 @@ this.HomeNavbar = (function() { } } - module.notifyEmptyProgram = _.throttle(function() { - toastr.info('No hay código para ejecutar.', 'Programa vacío', {timeOut: 2000}); - }, 3000, {trailing: false}); - - module.notifySaturationLeftWheel = _.throttle(function() { - notifySaturation('La potencia de la rueda izquierda está fuera del rango [-100, 100].'); - }, 3000, {trailing: false}); - - module.notifySaturationRightWheel = _.throttle(function() { - notifySaturation('La potencia de la rueda derecha está fuera del rango [-100, 100].'); - }, 3000, {trailing: false}); - - function notifySaturation(mensaje) { - toastr.warning(mensaje, 'Potencia fuera de límite', {timeOut: 2000}); - } - return module; - function initApi(interpreter, scope) { - interpreter.robotInstructions = new Array(); - interpreter.robotSensors = {left : 0, - right: 0, - front: 0, - back : 0}; - /* Add an API function for the motor function */ - var wrapper = function(leftWheelValue, rightWheelValue, durationValue) { - if (leftWheelValue.data < -100 || leftWheelValue.data > 100) { - leftWheelValue.data = Math.max(-100, Math.min(100, leftWheelValue.data)); - module.notifySaturationLeftWheel(); - } - if (rightWheelValue.data < -100 || rightWheelValue.data > 100) { - rightWheelValue.data = Math.max(-100, Math.min(100, rightWheelValue.data)); - module.notifySaturationRightWheel(); - } - return interpreter.createPrimitive( - interpreter.robotInstructions.push( - {action : 'motor', - leftWheel : leftWheelValue.data, - rightWheel : rightWheelValue.data, - duration : durationValue.data - }) - ); - }; - interpreter.setProperty(scope, 'motor', - interpreter.createNativeFunction(wrapper)); - - /* Add an API function for the sensor function */ - wrapper = function(sensorName, callback) { - interpreter.robotInstructions.push({ - action : 'sensor', - sensorName : sensorName, - sensorResultCallback : callback - }); - }; - interpreter.setProperty(scope, 'sensor', - interpreter.createAsyncFunction(wrapper)); - - /* Add an API function for the tracer enabler */ - wrapper = function(enabled) { - interpreter.robotInstructions.push( - {action : 'tracer_status', - enabled: enabled - }); - return interpreter.createPrimitive(null); - }; - interpreter.setProperty(scope, 'tracer', - interpreter.createNativeFunction(wrapper)); - - /* Add an API function for the tracer colour */ - wrapper = function(colour) { - interpreter.robotInstructions.push( - {action : 'tracer_colour', - colour: colour - }) - return interpreter.createPrimitive(null); - }; - interpreter.setProperty(scope, 'tracer_colour', - interpreter.createNativeFunction(wrapper)); - - wrapper = function(text) { - console.log("Console log: "); - console.log(text.data); - return interpreter.createPrimitive(null); - }; - interpreter.setProperty(scope, 'console_log', - interpreter.createNativeFunction(wrapper)); - - } - })(); diff --git a/src/app/home/simulator/simulator.js b/src/app/home/simulator/simulator.js index 0df1190..dcc03b7 100644 --- a/src/app/home/simulator/simulator.js +++ b/src/app/home/simulator/simulator.js @@ -50,6 +50,127 @@ this.HomeSimulator = (function() { canvasElement.height(screenHeight); } + module.pause = function(onPause) { + if(me.execution.isRunning()) { + onPause(); + me.execution.pause(); + } + } + + module.stop = function(onStop) { + if(!me.execution.hasFinished()) { + onStop(); + me.execution.finish(); + me.state.change(me.state.PLAY); + } + } + + module.run = function(onRun, onCompleted) { + if(me.execution.hasFinished()) { + var code = Blockly.JavaScript.workspaceToCode(HomeBlockly.workspace); + if(!code.length) { + module.notifyEmptyProgram(); + return; + } + me.state.pause(); + me.state.change(me.state.PLAY); + me.execution.onCompleted = onCompleted; + me.interpreter = new Interpreter(code, initApi); + me.state.resume(); + } + if(!me.execution.isRunning()) { + onRun(); + me.execution.run(); + } + } + + module.notifyEmptyProgram = _.throttle(function() { + toastr.info('No hay código para ejecutar.', 'Programa vacío', {timeOut: 2000}); + }, 3000, {trailing: false}); + + module.notifySaturationLeftWheel = _.throttle(function() { + notifySaturation('La potencia de la rueda izquierda está fuera del rango [-100, 100].'); + }, 3000, {trailing: false}); + + module.notifySaturationRightWheel = _.throttle(function() { + notifySaturation('La potencia de la rueda derecha está fuera del rango [-100, 100].'); + }, 3000, {trailing: false}); + + function notifySaturation(mensaje) { + toastr.warning(mensaje, 'Potencia fuera de límite', {timeOut: 2000}); + } + + function initApi(interpreter, scope) { + interpreter.robotInstructions = new Array(); + interpreter.robotSensors = {left : 0, + right: 0, + front: 0, + back : 0}; + /* Add an API function for the motor function */ + var wrapper = function(leftWheelValue, rightWheelValue, durationValue) { + if (leftWheelValue.data < -100 || leftWheelValue.data > 100) { + leftWheelValue.data = Math.max(-100, Math.min(100, leftWheelValue.data)); + module.notifySaturationLeftWheel(); + } + if (rightWheelValue.data < -100 || rightWheelValue.data > 100) { + rightWheelValue.data = Math.max(-100, Math.min(100, rightWheelValue.data)); + module.notifySaturationRightWheel(); + } + return interpreter.createPrimitive( + interpreter.robotInstructions.push( + {action : 'motor', + leftWheel : leftWheelValue.data, + rightWheel : rightWheelValue.data, + duration : durationValue.data + }) + ); + }; + interpreter.setProperty(scope, 'motor', + interpreter.createNativeFunction(wrapper)); + + /* Add an API function for the sensor function */ + wrapper = function(sensorName, callback) { + interpreter.robotInstructions.push({ + action : 'sensor', + sensorName : sensorName, + sensorResultCallback : callback + }); + }; + interpreter.setProperty(scope, 'sensor', + interpreter.createAsyncFunction(wrapper)); + + /* Add an API function for the tracer enabler */ + wrapper = function(enabled) { + interpreter.robotInstructions.push( + {action : 'tracer_status', + enabled: enabled + }); + return interpreter.createPrimitive(null); + }; + interpreter.setProperty(scope, 'tracer', + interpreter.createNativeFunction(wrapper)); + + /* Add an API function for the tracer colour */ + wrapper = function(colour) { + interpreter.robotInstructions.push( + {action : 'tracer_colour', + colour: colour + }) + return interpreter.createPrimitive(null); + }; + interpreter.setProperty(scope, 'tracer_colour', + interpreter.createNativeFunction(wrapper)); + + wrapper = function(text) { + console.log("Console log: "); + console.log(text.data); + return interpreter.createPrimitive(null); + }; + interpreter.setProperty(scope, 'console_log', + interpreter.createNativeFunction(wrapper)); + + } + return module; })(); From 2b8b6f66685394f1fdddf66569f4820b27ac5512 Mon Sep 17 00:00:00 2001 From: Lautaro Petaccio Date: Thu, 23 Jun 2016 12:51:29 -0300 Subject: [PATCH 2/3] Refactoring de la inicializacion del interprete JS --- .../home/simulator/simulator-interpreter.js | 81 +++++++++++++++++++ src/app/home/simulator/simulator.js | 73 +---------------- 2 files changed, 82 insertions(+), 72 deletions(-) create mode 100644 src/app/home/simulator/simulator-interpreter.js diff --git a/src/app/home/simulator/simulator-interpreter.js b/src/app/home/simulator/simulator-interpreter.js new file mode 100644 index 0000000..3079bfa --- /dev/null +++ b/src/app/home/simulator/simulator-interpreter.js @@ -0,0 +1,81 @@ +"use strict"; + +this.SimulatorInterpreter = (function() { + + var module = {}; + + module.createInterpreter = function(code) { + return new Interpreter(code, initApi); + } + + function initApi(interpreter, scope) { + interpreter.robotInstructions = new Array(); + + /* Add an API function for the motor function */ + var motorWrapper = function(leftWheelValue, rightWheelValue, durationValue) { + if (leftWheelValue.data < -100 || leftWheelValue.data > 100) { + leftWheelValue.data = Math.max(-100, Math.min(100, leftWheelValue.data)); + module.notifySaturationLeftWheel(); + } + if (rightWheelValue.data < -100 || rightWheelValue.data > 100) { + rightWheelValue.data = Math.max(-100, Math.min(100, rightWheelValue.data)); + module.notifySaturationRightWheel(); + } + return interpreter.createPrimitive( + interpreter.robotInstructions.push( + {action : 'motor', + leftWheel : leftWheelValue.data, + rightWheel : rightWheelValue.data, + duration : durationValue.data + }) + ); + }; + + /* Add an API function for the sensor function */ + var sensorWrapper = function(sensorName, callback) { + interpreter.robotInstructions.push({ + action : 'sensor', + sensorName : sensorName, + sensorResultCallback : callback + }); + }; + + /* Add an API function for the tracer enabler */ + var tracerEnableWrapper = function(enabled) { + interpreter.robotInstructions.push( + {action : 'tracer_status', + enabled: enabled + }); + return interpreter.createPrimitive(null); + }; + + /* Add an API function for the tracer colour */ + var tracerColourWrapper = function(colour) { + interpreter.robotInstructions.push( + {action : 'tracer_colour', + colour: colour + }) + return interpreter.createPrimitive(null); + }; + + var consoleLogWrapper = function(text) { + console.log("Console log: "); + console.log(text.data); + return interpreter.createPrimitive(null); + }; + + interpreter.setProperty(scope, 'sensor', + interpreter.createAsyncFunction(sensorWrapper)); + interpreter.setProperty(scope, 'motor', + interpreter.createNativeFunction(motorWrapper)); + interpreter.setProperty(scope, 'console_log', + interpreter.createNativeFunction(consoleLogWrapper)); + interpreter.setProperty(scope, 'tracer_colour', + interpreter.createNativeFunction(tracerColourWrapper)); + interpreter.setProperty(scope, 'tracer', + interpreter.createNativeFunction(tracerEnableWrapper)); + } + + return module; + +})(); \ No newline at end of file diff --git a/src/app/home/simulator/simulator.js b/src/app/home/simulator/simulator.js index dcc03b7..781067c 100644 --- a/src/app/home/simulator/simulator.js +++ b/src/app/home/simulator/simulator.js @@ -75,7 +75,7 @@ this.HomeSimulator = (function() { me.state.pause(); me.state.change(me.state.PLAY); me.execution.onCompleted = onCompleted; - me.interpreter = new Interpreter(code, initApi); + me.interpreter = SimulatorInterpreter.createInterpreter(code); me.state.resume(); } if(!me.execution.isRunning()) { @@ -100,77 +100,6 @@ this.HomeSimulator = (function() { toastr.warning(mensaje, 'Potencia fuera de límite', {timeOut: 2000}); } - function initApi(interpreter, scope) { - interpreter.robotInstructions = new Array(); - interpreter.robotSensors = {left : 0, - right: 0, - front: 0, - back : 0}; - /* Add an API function for the motor function */ - var wrapper = function(leftWheelValue, rightWheelValue, durationValue) { - if (leftWheelValue.data < -100 || leftWheelValue.data > 100) { - leftWheelValue.data = Math.max(-100, Math.min(100, leftWheelValue.data)); - module.notifySaturationLeftWheel(); - } - if (rightWheelValue.data < -100 || rightWheelValue.data > 100) { - rightWheelValue.data = Math.max(-100, Math.min(100, rightWheelValue.data)); - module.notifySaturationRightWheel(); - } - return interpreter.createPrimitive( - interpreter.robotInstructions.push( - {action : 'motor', - leftWheel : leftWheelValue.data, - rightWheel : rightWheelValue.data, - duration : durationValue.data - }) - ); - }; - interpreter.setProperty(scope, 'motor', - interpreter.createNativeFunction(wrapper)); - - /* Add an API function for the sensor function */ - wrapper = function(sensorName, callback) { - interpreter.robotInstructions.push({ - action : 'sensor', - sensorName : sensorName, - sensorResultCallback : callback - }); - }; - interpreter.setProperty(scope, 'sensor', - interpreter.createAsyncFunction(wrapper)); - - /* Add an API function for the tracer enabler */ - wrapper = function(enabled) { - interpreter.robotInstructions.push( - {action : 'tracer_status', - enabled: enabled - }); - return interpreter.createPrimitive(null); - }; - interpreter.setProperty(scope, 'tracer', - interpreter.createNativeFunction(wrapper)); - - /* Add an API function for the tracer colour */ - wrapper = function(colour) { - interpreter.robotInstructions.push( - {action : 'tracer_colour', - colour: colour - }) - return interpreter.createPrimitive(null); - }; - interpreter.setProperty(scope, 'tracer_colour', - interpreter.createNativeFunction(wrapper)); - - wrapper = function(text) { - console.log("Console log: "); - console.log(text.data); - return interpreter.createPrimitive(null); - }; - interpreter.setProperty(scope, 'console_log', - interpreter.createNativeFunction(wrapper)); - - } - return module; })(); From 4fa676fdd927fe19e4500762085920c93783f3b0 Mon Sep 17 00:00:00 2001 From: Lautaro Petaccio Date: Thu, 30 Jun 2016 21:06:13 -0300 Subject: [PATCH 3/3] Si se cambia el codigo al pausar, se reinicia la ejecucion --- src/app/home/simulator/simulator.js | 35 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/app/home/simulator/simulator.js b/src/app/home/simulator/simulator.js index 781067c..b823b10 100644 --- a/src/app/home/simulator/simulator.js +++ b/src/app/home/simulator/simulator.js @@ -37,23 +37,36 @@ this.HomeSimulator = (function() { } module.onContainerResize = function() { - var screenElement = $("#simulator-screen"); - var canvasElement = $("#simulator-screen > canvas"); - var screenWrapperElement = screenElement.parent(); - var screenWrapperWidth = screenWrapperElement.width(); - var screenWrapperHeight = screenWrapperElement.height(); - var screenWidth = Math.min(screenWrapperWidth, screenWrapperHeight); - var screenHeight = screenWidth; - screenElement.width(screenWidth); - screenElement.height(screenHeight); - canvasElement.width(screenWidth); - canvasElement.height(screenHeight); + var screenElement = $("#simulator-screen"); + var canvasElement = $("#simulator-screen > canvas"); + var screenWrapperElement = screenElement.parent(); + var screenWrapperWidth = screenWrapperElement.width(); + var screenWrapperHeight = screenWrapperElement.height(); + var screenWidth = Math.min(screenWrapperWidth, screenWrapperHeight); + var screenHeight = screenWidth; + screenElement.width(screenWidth); + screenElement.height(screenHeight); + canvasElement.width(screenWidth); + canvasElement.height(screenHeight); + } + + module.onWorkspaceChange = function(event) { + if(event.type == Blockly.Events.CHANGE || + event.type == Blockly.Events.CREATE || + event.type == Blockly.Events.DELETE) { + /* If the code was modified, finish the execution */ + me.execution.finish(); + /* Remove the event listener */ + HomeBlockly.workspace.removeChangeListener(module.onWorkspaceChange); + } } module.pause = function(onPause) { if(me.execution.isRunning()) { onPause(); me.execution.pause(); + /* While being paused, check if the code was modified */ + HomeBlockly.workspace.addChangeListener(module.onWorkspaceChange); } }