Commit 24c42b23 authored by carlosperate's avatar carlosperate

Add optional function block to define Arduino setup() and loops().

parent bd79dc6f
......@@ -55,9 +55,10 @@ There was an error manipulating the sketch data!!
- [ ] Code generator for lists into arrays
- [ ] A lot of blocks go through the entire block tree, which end ups being inefficient. Maybe create a general pass through in the arduino.js file to check everything that needs to be checked in one pass.
- [ ] SPI pin reservation log needs to be refactored for the new board settings
- [ ] Create I2C communication blocks
- [ ] Create I2C communication blocks with hue 190
- [ ] Update the serial print block to specify explicit type (hex, str, int, etc)
- [ ] Look into all the serial functions and decide what else might fit in
- [ ] Allow to add return statement to the Arduino setup()/loop() functions
## Arduino front end
......
......@@ -28,6 +28,7 @@
<script src="../blockly/blocks/procedures.js"></script>
<script src="../blockly/blocks/arduino/io.js"></script>
<script src="../blockly/blocks/arduino/map.js"></script>
<script src="../blockly/blocks/arduino/procedures.js"></script>
<script src="../blockly/blocks/arduino/serial.js"></script>
<script src="../blockly/blocks/arduino/servo.js"></script>
<script src="../blockly/blocks/arduino/spi.js"></script>
......
......@@ -7,8 +7,13 @@ It adds the following features:
* Arduino code generation
* Arduino specific blocks
* Static Typing
* Arduino boards support
* Static typing (working, but incomplete)
* Code warnings
* Arduino generator unit test (incomplete)
* Procedures and flyout core classes modified to include the Arduino setup() and loop() functions
All other changes and fixes have been submitted to the original blockly repository for inclusion into the upstream master branch.
This fork gets frequent upstream pulls to maintain it up to date.
......
/**
* @license Licensed under the Apache License, Version 2.0 (the "License"):
* http://www.apache.org/licenses/LICENSE-2.0
*
* @fileoverview Block for the Arduino functions.
* The Arduino built in functions syntax can be found at:
* https://arduino.cc/en/Reference/HomePage
*
* TODO: This block can be improved to set the new range properly.
*/
'use strict';
goog.provide('Blockly.Blocks.Arduino.procedures');
goog.require('Blockly.Arduino');
/** Common HSV hue for all blocks in this category. */
Blockly.Blocks.Arduino.procedures.HUE = 290;
Blockly.Blocks['arduino_functions'] = {
/**
* Block for defining the Arduino setup() and loop() functions.
* @this Blockly.Block
*/
init: function() {
this.appendDummyInput()
.appendField("Arduino run once:");
this.appendStatementInput("SETUP_FUNC");
this.appendDummyInput()
.appendField("Arduino loop forever:");
this.appendStatementInput("LOOP_FUNC");
this.setInputsInline(false);
this.setColour(Blockly.Blocks.Arduino.procedures.HUE);
this.setTooltip('Defines the Arduino setup() and loop() functions.');
this.setHelpUrl('https://arduino.cc/en/Reference/Loop');
},
/** Removes all the option to disable the block context menu */
customContextMenu: function(options) {
// Remove all options from the original reference (=[] won't work)
options.splice(0, options.length);
},
/** @return True if the a block instance is present in the workspace */
getArduinoLoopsInstance: function() {
return true;
}
};
......@@ -643,7 +643,9 @@ Blockly.Flyout.prototype.filterForCapacity_ = function() {
for (var i = 0, block; block = blocks[i]; i++) {
var allBlocks = block.getDescendants();
var disabled = allBlocks.length > remainingCapacity;
block.setDisabled(disabled);
if (!block.disabled) {
block.setDisabled(disabled);
}
}
};
......
......@@ -170,6 +170,21 @@ Blockly.Procedures.rename = function(text) {
* @param {!Blockly.Workspace} workspace The flyout's workspace.
*/
Blockly.Procedures.flyoutCategory = function(blocks, gaps, margin, workspace) {
if (Blockly.Blocks['arduino_functions']) {
var block = Blockly.Block.obtain(workspace, 'arduino_functions');
block.initSvg();
// If there is a block in the workspace show as disabled
var workspaceBlocks = Blockly.mainWorkspace.getAllBlocks();
for (var x = 0, length_ = workspaceBlocks.length; x < length_; x++) {
var getArduinoLoopsInstance = workspaceBlocks[x].getArduinoLoopsInstance;
if (getArduinoLoopsInstance) {
block.setDisabled(getArduinoLoopsInstance.call(workspaceBlocks[x]));
block.updateDisabled();
}
}
blocks.push(block);
gaps.push(margin * 2);
}
if (Blockly.Blocks['procedures_defnoreturn']) {
var block = Blockly.Block.obtain(workspace, 'procedures_defnoreturn');
block.initSvg();
......
......@@ -19,7 +19,7 @@ goog.require('Blockly.Arduino');
* Code generator to create a function with a return value (X).
* Arduino code: void functionname { return X }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {null} There is no code added to loop
* @return {null} There is no code added to loop.
*/
Blockly.Arduino['procedures_defreturn'] = function(block) {
var funcName = Blockly.Arduino.variableDB_.getName(
......@@ -43,15 +43,15 @@ Blockly.Arduino['procedures_defreturn'] = function(block) {
// Get arguments with type
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = block.getArgType(block.arguments_[x]) + ' ' +
args[x] = block.getArgType(block.arguments_[x]) + ' ' +
Blockly.Arduino.variableDB_.getName(block.arguments_[x],
Blockly.Variables.NAME_TYPE);
}
// Ger return type
var returnType = Blockly.StaticTyping.blocklyType.UNSPECIFIED
if (this.getReturnType) {
returnType = this.getReturnType();
// Get return type
var returnType = Blockly.StaticTyping.blocklyType.UNSPECIFIED;
if (block.getReturnType) {
returnType = block.getReturnType();
}
returnType = Blockly.Arduino.getArduinoType_(returnType);
......@@ -59,7 +59,7 @@ Blockly.Arduino['procedures_defreturn'] = function(block) {
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
branch + returnValue + '}\n';
code = Blockly.Arduino.scrub_(block, code);
Blockly.Arduino.definitions_[funcName] = code;
Blockly.Arduino.userFunctions_[funcName] = code;
return null;
};
......@@ -69,7 +69,7 @@ Blockly.Arduino['procedures_defreturn'] = function(block) {
* type.
* Arduino code: void functionname { }
*/
Blockly.Arduino['procedures_defnoreturn'] =
Blockly.Arduino['procedures_defnoreturn'] =
Blockly.Arduino['procedures_defreturn'];
/**
......@@ -128,3 +128,31 @@ Blockly.Arduino['procedures_ifreturn'] = function(block) {
code += '}\n';
return code;
};
/**
* Code generator to add code into the setup() and loop() functions.
* Its use is not mandatory, but necessary to add manual code to setup().
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['arduino_functions'] = 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 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);
var loopBranch = statementToCodeNoTab(block, 'LOOP_FUNC');
var loopcode = Blockly.Arduino.scrub_(block, loopBranch);
return loopcode;
};
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