Commit 4a1bdb5a authored by carlosperate's avatar carlosperate

Add addDeclaration generator function and update codebase to use it.

parent bb486337
......@@ -144,24 +144,15 @@ Blockly.Arduino.finish = function(code) {
code = code.replace(/\n\s+$/, '\n');
code = 'void loop() {\n' + code + '\n}';
// Convert the definitions dictionary into a list
// TODO: remove regex expression once all includes are moved to their own dict
var imports = [];
var definitions = [];
for (var name in Blockly.Arduino.definitions_) {
var def = Blockly.Arduino.definitions_[name];
if (def.match(/^#include/)) {
imports.push(def);
} else {
definitions.push(def);
}
}
// Convert the includes, functions, and setup dictionaries into lists
var includes = [], functions = [], setups = [], userSetupCode= '';
// Convert the includes, definitions, functions, and setup dicts into lists
var includes = [], definitions = [], functions = [], setups = [];
var userSetupCode= '';
for (var name in Blockly.Arduino.includes_) {
includes.push(Blockly.Arduino.includes_[name]);
}
for (var name in Blockly.Arduino.definitions_) {
definitions.push(Blockly.Arduino.definitions_[name]);
}
for (var name in Blockly.Arduino.codeFunctions_) {
functions.push(Blockly.Arduino.codeFunctions_[name]);
}
......@@ -177,10 +168,9 @@ Blockly.Arduino.finish = function(code) {
setups.push(Blockly.Arduino.setups_[name]);
}
var allDefs = includes.join('\n') + '\n' + imports.join('\n') + '\n' +
definitions.join('\n') + '\n\n' + functions.join('\n\n') +
'\n\nvoid setup() {\n ' + setups.join('\n ') + '\n' + userSetupCode +
'}';
var allDefs = includes.join('\n') + '\n\n' + definitions.join('\n') + '\n\n' +
functions.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;
};
......@@ -196,6 +186,18 @@ Blockly.Arduino.addInclude = function(includeTag, code) {
}
};
/**
* Adds a string of code to be declared globally to the sketch.
* Once it is added it will not get overwritten with new code.
* @param {!string} declarationTag Identifier for this declaration code.
* @param {!string} code Code to be added below the includes.
*/
Blockly.Arduino.addDeclaration = function(declarationTag, code) {
if (Blockly.Arduino.definitions_[declarationTag] === undefined) {
Blockly.Arduino.definitions_[declarationTag] = code;
}
};
/**
* Adds a string of code into the Arduino setup() function. It takes an
* identifier to not repeat the same kind of initialisation code from several
......
......@@ -32,9 +32,9 @@ Blockly.Arduino['servo_write'] = function(block) {
Blockly.Arduino.reservePin(
block, pinKey, Blockly.Arduino.PinTypes.SERVO, 'Servo Write');
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pinKey] =
'Servo ' + servoName + ';';
Blockly.Arduino.addInclude('servo', '#include <Servo.h>');
Blockly.Arduino.addDeclaration('servo_' + pinKey, 'Servo ' + servoName + ';');
var setupCode = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true);
......@@ -58,9 +58,9 @@ Blockly.Arduino['servo_read'] = function(block) {
Blockly.Arduino.reservePin(
block, pinKey, Blockly.Arduino.PinTypes.SERVO, 'Servo Read');
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pinKey] =
'Servo ' + servoName + ';';
Blockly.Arduino.addInclude('servo', '#include <Servo.h>');
Blockly.Arduino.addDeclaration('servo_' + pinKey, 'Servo ' + servoName + ';');
var setupCode = servoName + '.attach(' + pinKey + ');';
Blockly.Arduino.addSetup('servo_' + pinKey, setupCode, true);
......
......@@ -36,11 +36,11 @@ Blockly.Arduino['stepper_config'] = function(block) {
Blockly.Arduino.reservePin(block, pin1, pinType, 'Stepper');
Blockly.Arduino.reservePin(block, pin2, pinType, 'Stepper');
Blockly.Arduino.definitions_['define_stepper'] = '#include <Stepper.h>\n';
Blockly.Arduino.addInclude('stepper', '#include <Stepper.h>');
var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' +
pin1 + ', ' + pin2 + ');';
Blockly.Arduino.definitions_['global_stepper_' + stepperName] = globalCode;
Blockly.Arduino.addDeclaration('stepper_' + stepperName, globalCode);
var setupCode = stepperName + '.setSpeed(' + stepperSpeed + ');';
Blockly.Arduino.addSetup('stepper_' + stepperName, setupCode, true);
......
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