diff --git a/blockly/blocks/arduino/procedures.js b/blockly/blocks/arduino/procedures.js
index 44f245533d..fa0d937eb7 100644
--- a/blockly/blocks/arduino/procedures.js
+++ b/blockly/blocks/arduino/procedures.js
@@ -39,3 +39,31 @@ Blockly.Blocks['arduino_functions'] = {
return true;
}
};
+
+Blockly.Blocks['arduino_functions_ext'] = {
+ /**
+ * Extended Block for defining the Arduino setup() and loop() functions,
+ * containing also a declare upfront part for declaration before setup.
+ * @this Blockly.Block
+ */
+ init: function() {
+ this.appendDummyInput()
+ .appendField(Blockly.Msg.ARD_FUN_RUN_DECL);
+ this.appendStatementInput('DECLARE_FUNC');
+ this.appendDummyInput()
+ .appendField(Blockly.Msg.ARD_FUN_RUN_SETUP);
+ this.appendStatementInput('SETUP_FUNC');
+ this.appendDummyInput()
+ .appendField(Blockly.Msg.ARD_FUN_RUN_LOOP);
+ this.appendStatementInput('LOOP_FUNC');
+ this.setInputsInline(false);
+ this.setColour(Blockly.Blocks.procedures.HUE);
+ this.setTooltip(Blockly.Msg.ARD_FUN_RUN_TIP);
+ this.setHelpUrl('https://arduino.cc/en/Reference/Loop');
+ this.contextMenu = false;
+ },
+ /** @return {!boolean} True if the block instance is in the workspace. */
+ getArduinoLoopsInstance: function() {
+ return true;
+ }
+};
\ No newline at end of file
diff --git a/blockly/core/procedures.js b/blockly/core/procedures.js
index 3370a95fa5..858cacaf33 100644
--- a/blockly/core/procedures.js
+++ b/blockly/core/procedures.js
@@ -152,7 +152,7 @@ Blockly.Procedures.rename = function(text) {
/**
* Construct the blocks required by the flyout for the procedure category.
- * @param {!Blockly.Workspace} workspace The workspace contianing procedures.
+ * @param {!Blockly.Workspace} workspace The workspace containing procedures.
* @return {!Array.} Array of XML block elements.
*/
Blockly.Procedures.flyoutCategory = function(workspace) {
@@ -172,6 +172,21 @@ Blockly.Procedures.flyoutCategory = function(workspace) {
}
xmlList.push(block);
}
+ if (Blockly.Blocks['arduino_functions_ext']) {
+ //
+ var block = goog.dom.createDom('block');
+ block.setAttribute('type', 'arduino_functions_ext');
+ block.setAttribute('gap', 16);
+ // If this parent block present already in the workspace show as disabled
+ var workspaceTopBlocks = workspace.getTopBlocks();
+ for (var i = 0; i < workspaceTopBlocks.length; i++) {
+ if (workspaceTopBlocks[i].getArduinoLoopsInstance &&
+ workspaceTopBlocks[i].getArduinoLoopsInstance()) {
+ block.setAttribute('disabled', true);
+ }
+ }
+ xmlList.push(block);
+ }
if (Blockly.Blocks['procedures_defnoreturn']) {
//
var block = goog.dom.createDom('block');
diff --git a/blockly/generators/arduino.js b/blockly/generators/arduino.js
index 0e9b713d0f..2bddeed391 100644
--- a/blockly/generators/arduino.js
+++ b/blockly/generators/arduino.js
@@ -162,12 +162,20 @@ Blockly.Arduino.finish = function(code) {
functions.push('\n');
}
+ // userDeclareCode added up front
// userSetupCode added at the end of the setup function without leading spaces
- var setups = [''], userSetupCode= '';
+ var setups = [''], userSetupCode= '', userDeclareCode= '';
+ if (Blockly.Arduino.setups_['userDeclareCode'] !== undefined) {
+ userDeclareCode = '\n' + Blockly.Arduino.setups_['userDeclareCode'];
+ delete Blockly.Arduino.setups_['userDeclareCode'];
+ }
if (Blockly.Arduino.setups_['userSetupCode'] !== undefined) {
userSetupCode = '\n' + Blockly.Arduino.setups_['userSetupCode'];
delete Blockly.Arduino.setups_['userSetupCode'];
}
+ if (userDeclareCode) {
+ setups.push('declareUpFront();');
+ }
for (var name in Blockly.Arduino.setups_) {
setups.push(Blockly.Arduino.setups_[name]);
}
diff --git a/blockly/generators/arduino/procedures.js b/blockly/generators/arduino/procedures.js
index ee0de7f18e..19a08810bd 100644
--- a/blockly/generators/arduino/procedures.js
+++ b/blockly/generators/arduino/procedures.js
@@ -159,3 +159,38 @@ Blockly.Arduino['arduino_functions'] = function(block) {
//var loopcode = Blockly.Arduino.scrub_(block, loopBranch); No comment block
return loopBranch;
};
+
+/**
+ * Code generator to add code into the setup() and loop() functions and a declare block.
+ * Its use is not mandatory, but necessary to add manual code to setup() and upfront declare.
+ * @param {!Blockly.Block} block Block to generate the code from.
+ * @return {string} Completed code.
+ */
+Blockly.Arduino['arduino_functions_ext'] = function(block) {
+ // Edited version of Blockly.Generator.prototype.statementToCode
+ function statementToCodeNoTab(block, name) {
+ var targetBlock = block.getInputTargetBlock(name);
+ var code = Blockly.Arduino.blockToCode(targetBlock);
+ if (!goog.isString(code)) {
+ throw 'Expecting code from statement block "' + targetBlock.type + '".';
+ }
+ return code;
+ }
+
+ var declareBranch = Blockly.Arduino.statementToCode(block, 'DECLARE_FUNC');
+ if (declareBranch) {
+ declareBranch = 'void declareUpFront() {\n' + declareBranch + '\n}'
+ Blockly.Arduino.addDeclaration('userDeclareCode', declareBranch);
+ Blockly.Arduino.addSetup('userDeclareCode', declareBranch);
+ }
+
+ var setupBranch = Blockly.Arduino.statementToCode(block, 'SETUP_FUNC');
+ //var setupCode = Blockly.Arduino.scrub_(block, setupBranch); No comment block
+ if (setupBranch) {
+ Blockly.Arduino.addSetup('userSetupCode', setupBranch, true);
+ }
+
+ var loopBranch = statementToCodeNoTab(block, 'LOOP_FUNC');
+ //var loopcode = Blockly.Arduino.scrub_(block, loopBranch); No comment block
+ return loopBranch;
+};
\ No newline at end of file
diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json
index 1b0f0f4c72..012bde5a7d 100644
--- a/blockly/msg/json/nl.json
+++ b/blockly/msg/json/nl.json
@@ -378,6 +378,7 @@
"ARD_MAP": "Herschaal",
"ARD_MAP_VAL": "waarde naar [0-",
"ARD_MAP_TIP": "Herschaalt een getal in interval [0-1024] naar een ander getal in de gegeven schaal.",
+ "ARD_FUN_RUN_DECL": "Arduino definieer vooraf:",
"ARD_FUN_RUN_SETUP": "Arduino doe eerst:",
"ARD_FUN_RUN_LOOP": "Arduino herhaal voor altijd:",
"ARD_FUN_RUN_TIP": "Definieer de Arduino setup() en loop() functies.",
diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js
index 0011766381..8e4a7514dc 100644
--- a/blockly/msg/messages.js
+++ b/blockly/msg/messages.js
@@ -1124,6 +1124,7 @@ Blockly.Msg.ARD_NOTONE_PIN_TIP = 'Stop generating a tone on a pin';
Blockly.Msg.ARD_MAP = 'Map';
Blockly.Msg.ARD_MAP_VAL = 'value to [0-';
Blockly.Msg.ARD_MAP_TIP = 'Re-maps a number from [0-1024] to another.';
+Blockly.Msg.ARD_FUN_RUN_DECL = 'Arduino define up front:';
Blockly.Msg.ARD_FUN_RUN_SETUP = 'Arduino run first:';
Blockly.Msg.ARD_FUN_RUN_LOOP = 'Arduino loop forever:';
Blockly.Msg.ARD_FUN_RUN_TIP = 'Defines the Arduino setup() and loop() functions.';