Commit 41debd97 authored by carlosperate's avatar carlosperate

Enforce setup code ordering and move all codebase to use it.

parent ab0cbe56
......@@ -145,7 +145,7 @@ Blockly.Arduino.finish = function(code) {
}
// Convert the includes, functions, and setup dictionaries into lists
var includes = [], functions = [], setups = [];
var includes = [], functions = [], setups = [], userSetupCode= '';
for (var name in Blockly.Arduino.includes_) {
includes.push(Blockly.Arduino.includes_[name]);
}
......@@ -155,13 +155,19 @@ Blockly.Arduino.finish = function(code) {
for (var name in Blockly.Arduino.userFunctions_) {
functions.push(Blockly.Arduino.userFunctions_[name]);
}
// userSetupCode is added at the end of the setup function
if (Blockly.Arduino.setups_['userSetupCode'] !== undefined) {
userSetupCode = '\n' + Blockly.Arduino.setups_['userSetupCode'];
delete Blockly.Arduino.setups_['userSetupCode'];
}
for (var name in Blockly.Arduino.setups_) {
setups.push(Blockly.Arduino.setups_[name]);
}
var allDefs = includes.join('\n') + '\n' + imports.join('\n') + '\n' +
definitions.join('\n') + '\n\n' + functions.join('\n\n') +
'\n\nvoid setup() {\n ' + setups.join('\n ') + '\n}';
'\n\nvoid setup() {\n ' + setups.join('\n ') + '\n' + userSetupCode +
'}';
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
};
......@@ -199,9 +205,8 @@ Blockly.Arduino.addSetup = function(setupTag, code, overwrite) {
* function name) to only keep a single copy even if multiple blocks might
* request this function to be created.
* A function (and its code) will only be added on first request.
* @param {!string} functionName Identifier for the function.
* @param {!string} preferedName Identifier for the function.
* @param {!string} code Code to be included in the setup() function.
* @param {boolean=} overwrite Description.
* @return {!string} A unique function name based on input name.
*/
Blockly.Arduino.addFunction = function(preferedName, code) {
......@@ -217,15 +222,17 @@ Blockly.Arduino.addFunction = function(preferedName, code) {
/**
* Description.
* @param {!Blockly.Block}
* @param {!Blockly.Block} block Description.
* @param {!string} pin Description.
* @param {!string} pinType Description.
* @param {!string} warningTag Description.
*/
Blockly.Arduino.reservePin = function(block, pin, pinType, warningTag) {
if (Blockly.Arduino.pins_[pin] !== undefined) {
if (Blockly.Arduino.pins_[pin] != pinType) {
block.setWarningText(
'Pin ' + pin + ' is needed for ' + warningTag + ' as pin ' + pinType +
'. Already used as ' + Blockly.Arduino.pins_[pin] + ' instead.',
warningTag);
'. Already used as ' + Blockly.Arduino.pins_[pin] + '.', warningTag);
} else {
block.setWarningText(null, warningTag);
}
......
......@@ -148,9 +148,9 @@ Blockly.Arduino['arduino_functions'] = function(block) {
var setupBranch = Blockly.Arduino.statementToCode(block, 'SETUP_FUNC');
// Remove first spacers as they will be added again in "addSetup()"
setupBranch = setupBranch.substring(2);
var setupcode = Blockly.Arduino.scrub_(block, setupBranch);
Blockly.Arduino.addSetup('userSetupCode', setupcode, true);
//setupBranch = setupBranch.substring(2);
var setupCode = Blockly.Arduino.scrub_(block, setupBranch);
Blockly.Arduino.addSetup('userSetupCode', setupCode, true);
var loopBranch = statementToCodeNoTab(block, 'LOOP_FUNC');
var loopcode = Blockly.Arduino.scrub_(block, loopBranch);
......
......@@ -18,29 +18,34 @@ goog.require('Blockly.Arduino');
/**
* Code generator to set an angle (Y) value to a servo PWM pin (X).
* Arduino code: #include <Servo.h>
* Servo myServo_X;
* setup { myServo_X.attach(X); }
* loop { myServo_X.write(Y); }
* Servo myServoX;
* setup { myServoX.attach(X); }
* loop { myServoX.write(Y); }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['servo_write'] = function(block) {
var pinKey = block.getFieldValue('SERVO_PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.SERVO;
var servoAngle = Blockly.Arduino.valueToCode(block, 'SERVO_ANGLE', Blockly.Arduino.ORDER_ATOMIC) || '90';
var servoAngle = Blockly.Arduino.valueToCode(
block, 'SERVO_ANGLE', Blockly.Arduino.ORDER_ATOMIC) || '90';
var servoName = 'myServo_' + pinKey;
var servoName = 'myServo' + pinKey;
var code = servoName + '.write(' + servoAngle + ');\n';
// Maintain the setup regardless of pin conflict, warning should be enough
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pinKey] = 'Servo ' + servoName + ';';
Blockly.Arduino.setups_['setup_servo_' + pinKey] = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.definitions_['global_servo_' + pinKey] =
'Servo ' + servoName + ';';
var setupCode = servoName + '.attach(' + pinKey + ');'
Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true);
// If the IO has been configured already set a block warning for the user
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText('Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
......@@ -56,9 +61,9 @@ Blockly.Arduino['servo_write'] = function(block) {
/**
* Code generator to read an angle value from a servo PWM pin (X).
* Arduino code: #include <Servo.h>
* Servo myServo_X;
* setup { myServo_X.attach(X); }
* loop { myServo_X.read(); }
* Servo myServoX;
* setup { myServoX.attach(X); }
* loop { myServoX.read(); }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
......@@ -66,18 +71,22 @@ Blockly.Arduino['servo_read'] = function(block) {
var pinKey = block.getFieldValue('SERVO_PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.SERVO;
var servoName = 'myServo_' + pinKey;
var servoName = 'myServo' + pinKey;
var code = servoName + '.read()';
// Maintain the setup regardless of pin conflict, warning should be enough
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pinKey] = 'Servo ' + servoName + ';';
Blockly.Arduino.setups_['setup_servo_' + pinKey] = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.definitions_['global_servo_' + pinKey] =
'Servo ' + servoName + ';';
var setupCode = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true);
// If the IO has been configured already set a block warning for the user
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText('Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
......
......@@ -31,9 +31,9 @@ Blockly.Arduino['spi_setup'] = function(block) {
var spiMode = block.getFieldValue('SPI_MODE');
Blockly.Arduino.addInclude('spi', '#include <SPI.h>');
Blockly.Arduino.addSetup('setup_spi_order',
Blockly.Arduino.addSetup('spi_order',
spiId + '.setBitOrder(' + spiShift + ');', true);
Blockly.Arduino.addSetup('setup_spi_mode',
Blockly.Arduino.addSetup('spi_mode',
spiId + '.setDataMode(' + spiMode + ');', true);
Blockly.Arduino.addSetup('spi_div',
spiId + '.setClockDivider(' + spiClockDivide + ');', true);
......@@ -114,7 +114,7 @@ Blockly.Arduino['spi_transfer_return'] = function(block) {
' digitalWrite(' + spiSs + ', HIGH);',
' spiReturn = ' + spiId + '.transfer(' + spiData + ');',
' digitalWrite(' + spiSs + ', LOW);',
' return spiReturn;'
' return spiReturn;',
'}'];
var functionName = Blockly.Arduino.addFunction(
'spiReturnSlave' + spiSs, func.join('\n'));
......
......@@ -33,14 +33,14 @@ Blockly.Arduino['stepper_config'] = function(block) {
var stepperSpeed = Blockly.Arduino.valueToCode(block, 'STEPPER_SPEED',
Blockly.Arduino.ORDER_ATOMIC) || '90';
var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' +
pin1 + ', ' + pin2 + ');';
Blockly.Arduino.definitions_['define_stepper'] = '#include <Stepper.h>\n';
var setupCode = stepperName + '.setSpeed(' + stepperSpeed + ');';
Blockly.Arduino.addSetup('stepper_' + stepperName, setupCode, true);
// Maintain the setup regardless of pin conflict, warning should be enough
Blockly.Arduino.definitions_['define_stepper'] = '#include <Stepper.h>\n';
var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' +
pin1 + ', ' + pin2 + ');';
Blockly.Arduino.definitions_['global_stepper_' + stepperName] = globalCode;
Blockly.Arduino.setups_['setup_stepper_' + stepperName] = setupCode;
// If the IO has been configured already set a block warning for the user
var warningText = '';
......
......@@ -418,7 +418,9 @@ Blockly.Arduino['text_changeCase'] = function(block) {
Blockly.Arduino['text_prompt'] = function(block) {
// Prompt function.
Blockly.Arduino.setups_['serial_begin'] = 'Serial.begin(9600);';
var serialId = Blockly.Arduino.Boards.selected.serial[0][1];
var setupCode = serialId + '.begin(9600);';
Blockly.Arduino.addSetup('serial_' + serialId, setupCode, false);
var msg = Blockly.Arduino.quote_(block.getFieldValue('TEXT'));
var code = 'Serial.print(' + msg + ');\n';
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment