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