Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring #70

Merged
merged 3 commits into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 12 additions & 115 deletions src/app/home/navbar/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));

}

})();
81 changes: 81 additions & 0 deletions src/app/home/simulator/simulator-interpreter.js
Original file line number Diff line number Diff line change
@@ -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;

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

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

return module;
Expand Down