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

A declare block using an Arduino function #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions blockly/blocks/arduino/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
17 changes: 16 additions & 1 deletion blockly/core/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<!Element>} Array of XML block elements.
*/
Blockly.Procedures.flyoutCategory = function(workspace) {
Expand All @@ -172,6 +172,21 @@ Blockly.Procedures.flyoutCategory = function(workspace) {
}
xmlList.push(block);
}
if (Blockly.Blocks['arduino_functions_ext']) {
// <block type="arduino_functions" gap="16"></block>
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']) {
// <block type="procedures_defnoreturn" gap="16"></block>
var block = goog.dom.createDom('block');
Expand Down
10 changes: 9 additions & 1 deletion blockly/generators/arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
35 changes: 35 additions & 0 deletions blockly/generators/arduino/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions blockly/msg/json/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
1 change: 1 addition & 0 deletions blockly/msg/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down