BirdBrainTechnologies / BlocklyDuino-update

BlocklyDuino integrated with Codebender, with focus on Hummingbird Duo blocks
Apache License 2.0
0 stars 0 forks source link

Problem with procedures #2

Open TomWildenhain opened 9 years ago

TomWildenhain commented 9 years ago

I think there is a glitch in how blocky turns procedures with arguments and return values into Arduino code. The following blocky code: image Produces the following Arduino code: int number1; int number2; Dynamic addTwoNumbers(number1, number2) { return number1 + number2; } void setup() { Serial.begin(9600); } void loop() { Serial.print((addTwoNumbers(3, 4))); Serial.print('\t'); } But when I click "Validate," it generates the following errors: sketch.ino:2:1: error: unknown type name 'Dynamic' Dynamic addTwoNumbers(number1, number2);

sketch.ino:2:23: error: C++ requires a type specifier for all declarations Dynamic addTwoNumbers(number1, number2);

sketch.ino:2:32: error: unknown type name 'number2' Dynamic addTwoNumbers(number1, number2);

sketch.ino:5:1: error: unknown type name 'Dynamic' Dynamic addTwoNumbers(number1, number2) {

sketch.ino:5:40: error: expected ';' after top level declarator Dynamic addTwoNumbers(number1, number2) { The following changes resolve the error: int number1; int number2; int addTwoNumbers(int number1, int number2) { return number1 + number2; } void setup() { Serial.begin(9600); } void loop() { Serial.print((addTwoNumbers(3, 4))); Serial.print('\t'); } So I think the parameters need int added before them, and the return type should be int (or boolean), not Dynamic.

birdbraintech commented 9 years ago

This is a more fundamental issue. It looks as though blockly only supports global variables, making procedures/functions challenging to convert to code properly. Fortunately, if you declare the variables in a function declaration as well as globally, it looks like the Arduino compiler assumes that inside a function, the local variable takes precedence. In the end, all this means is that Blockly is now producing unneeded global variables - not great, but better than not having procedures.

This issue is also discussed on the main blocklyduino site at https://github.com/gasolin/BlocklyDuino/issues/8

Another bug (now solved) is that BlocklyDuino was using a "Dynamic" keyword that doesn't seem to work in the Arduino environment to try to dynamically cast data types. We've altered procedures so that they now only work with ints. It seems as though the Arduino compiler is quite generous in automatically casting other types to ints - for example, floats are truncated, chars are interpreted according to the ASCII code, and bools are just 0 for false and 1 for true. So even if you put a float into our procedure, it won't throw a compiler error (which keeps the goal of ensuring that Blockly programs compile, even if they may not quite behave properly once run).

Our int fix makes procedures a bit more useful than in BlocklyDuino, but it would be great if our procedures could properly handle other data types and not generate extraneous global variables, so I'm labeling this as help wanted.