Skip to content

Commit

Permalink
Revert al último commit pre-SdC. Los nuevos commits estan en branch dev.
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmansi committed Jul 19, 2016
1 parent d5cd6d1 commit 2837f88
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 167 deletions.
127 changes: 115 additions & 12 deletions src/app/home/navbar/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,49 @@ this.HomeNavbar = (function() {
}

module.onClickRun = function(event) {
var onCompleted = function() {
$('.btn_run').show();
$('.btn_pause').hide();
$('.btn_stop').addClass('disabled');
}
var onRun = function() {
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);
$('.btn_pause').show();
$('.btn_stop').removeClass('disabled');
$('.btn_run').hide();
me.state.resume();
me.execution.run();
} else if (me.execution.isPaused()) {
$('.btn_pause').show();
me.execution.run();
}
HomeSimulator.run(onRun, onCompleted);
$('.btn_run').hide();
event.stopPropagation();
}

module.onClickPause = function(event) {
HomeSimulator.pause(function() {
if(me.execution.isRunning()) {
$('.btn_run').show();
$('.btn_pause').hide();
});
me.execution.pause();
}
}

module.onClickStop = function(event) {
HomeSimulator.stop(function() {
if(!me.execution.hasFinished()) {
$('.btn_run').show();
$('.btn_pause').hide();
$('.btn_stop').addClass('disabled');
});
me.execution.finish();
me.state.change(me.state.PLAY);
}
}

module.onClickSave = function(event) {
Expand All @@ -83,6 +99,93 @@ 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));

}

})();
81 changes: 0 additions & 81 deletions src/app/home/simulator/simulator-interpreter.js

This file was deleted.

85 changes: 11 additions & 74 deletions src/app/home/simulator/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,80 +37,17 @@ 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);
}

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);
}
}

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 = SimulatorInterpreter.createInterpreter(code);
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});
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);
}

return module;
Expand Down

0 comments on commit 2837f88

Please sign in to comment.