Commit 25c52c79 authored by carlosperate's avatar carlosperate

Add better control over Arduino code new lines.

parent 681fdf1b
...@@ -128,8 +128,7 @@ Blockly.Arduino.init = function(workspace) { ...@@ -128,8 +128,7 @@ Blockly.Arduino.init = function(workspace) {
Blockly.Arduino.getArduinoType_(variableTypes[varName]) + ' ' + Blockly.Arduino.getArduinoType_(variableTypes[varName]) + ' ' +
varName + ';'); varName + ';');
} }
Blockly.Arduino.definitions_['variables'] = Blockly.Arduino.definitions_['variables'] = variableDeclarations.join('\n');
variableDeclarations.join('\n') + '\n';
}; };
/** /**
...@@ -138,28 +137,32 @@ Blockly.Arduino.init = function(workspace) { ...@@ -138,28 +137,32 @@ Blockly.Arduino.init = function(workspace) {
* @return {string} Completed code. * @return {string} Completed code.
*/ */
Blockly.Arduino.finish = function(code) { Blockly.Arduino.finish = function(code) {
// Indent every line. // Convert the includes, definitions, and functions dictionaries into lists
//TODO: revise regexs var includes = [], definitions = [], functions = [];
code = ' ' + code.replace(/\n/g, '\n ');
code = code.replace(/\n\s+$/, '\n');
code = 'void loop() {\n' + code + '\n}';
// Convert the includes, definitions, functions, and setup dicts into lists
var includes = [], definitions = [], functions = [], setups = [];
var userSetupCode= '';
for (var name in Blockly.Arduino.includes_) { for (var name in Blockly.Arduino.includes_) {
includes.push(Blockly.Arduino.includes_[name]); includes.push(Blockly.Arduino.includes_[name]);
} }
if (includes.length) {
includes.push('\n');
}
for (var name in Blockly.Arduino.definitions_) { for (var name in Blockly.Arduino.definitions_) {
definitions.push(Blockly.Arduino.definitions_[name]); definitions.push(Blockly.Arduino.definitions_[name]);
} }
if (definitions.length) {
definitions.push('\n');
}
for (var name in Blockly.Arduino.codeFunctions_) { for (var name in Blockly.Arduino.codeFunctions_) {
functions.push(Blockly.Arduino.codeFunctions_[name]); functions.push(Blockly.Arduino.codeFunctions_[name]);
} }
for (var name in Blockly.Arduino.userFunctions_) { for (var name in Blockly.Arduino.userFunctions_) {
functions.push(Blockly.Arduino.userFunctions_[name]); functions.push(Blockly.Arduino.userFunctions_[name]);
} }
// userSetupCode is added at the end of the setup function if (functions.length) {
functions.push('\n');
}
// userSetupCode added at the end of the setup function without leading spaces
var setups = [''], userSetupCode= '';
if (Blockly.Arduino.setups_['userSetupCode'] !== undefined) { if (Blockly.Arduino.setups_['userSetupCode'] !== undefined) {
userSetupCode = '\n' + Blockly.Arduino.setups_['userSetupCode']; userSetupCode = '\n' + Blockly.Arduino.setups_['userSetupCode'];
delete Blockly.Arduino.setups_['userSetupCode']; delete Blockly.Arduino.setups_['userSetupCode'];
...@@ -167,11 +170,15 @@ Blockly.Arduino.finish = function(code) { ...@@ -167,11 +170,15 @@ Blockly.Arduino.finish = function(code) {
for (var name in Blockly.Arduino.setups_) { for (var name in Blockly.Arduino.setups_) {
setups.push(Blockly.Arduino.setups_[name]); setups.push(Blockly.Arduino.setups_[name]);
} }
if (userSetupCode) {
setups.push(userSetupCode);
}
var allDefs = includes.join('\n') + '\n\n' + definitions.join('\n') + '\n\n' + var allDefs = includes.join('\n') + definitions.join('\n') +
functions.join('\n\n') + '\n\nvoid setup() {\n ' + functions.join('\n\n');
setups.join('\n ') + '\n' + userSetupCode + '}'; var setup = 'void setup() {' + setups.join('\n ') + '\n}\n\n';
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code; var loop = 'void loop() {\n ' + code.replace(/\n/g, '\n ') + '\n}';
return allDefs + setup + loop;
}; };
/** /**
......
...@@ -57,7 +57,7 @@ Blockly.Arduino['procedures_defreturn'] = function(block) { ...@@ -57,7 +57,7 @@ Blockly.Arduino['procedures_defreturn'] = function(block) {
// Construct code // Construct code
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' + var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
branch + returnValue + '}\n'; branch + returnValue + '}';
code = Blockly.Arduino.scrub_(block, code); code = Blockly.Arduino.scrub_(block, code);
Blockly.Arduino.userFunctions_[funcName] = code; Blockly.Arduino.userFunctions_[funcName] = code;
return null; return null;
...@@ -147,12 +147,12 @@ Blockly.Arduino['arduino_functions'] = function(block) { ...@@ -147,12 +147,12 @@ Blockly.Arduino['arduino_functions'] = function(block) {
} }
var setupBranch = Blockly.Arduino.statementToCode(block, 'SETUP_FUNC'); var setupBranch = Blockly.Arduino.statementToCode(block, 'SETUP_FUNC');
// Remove first spacers as they will be added again in "addSetup()" //var setupCode = Blockly.Arduino.scrub_(block, setupBranch); No comment block
//setupBranch = setupBranch.substring(2); if (setupBranch) {
var setupCode = Blockly.Arduino.scrub_(block, setupBranch); Blockly.Arduino.addSetup('userSetupCode', setupBranch, true);
Blockly.Arduino.addSetup('userSetupCode', setupCode, true); }
var loopBranch = statementToCodeNoTab(block, 'LOOP_FUNC'); var loopBranch = statementToCodeNoTab(block, 'LOOP_FUNC');
var loopcode = Blockly.Arduino.scrub_(block, loopBranch); //var loopcode = Blockly.Arduino.scrub_(block, loopBranch); No comment block
return loopcode; return loopBranch;
}; };
...@@ -46,7 +46,7 @@ Blockly.Arduino['serial_print'] = function(block) { ...@@ -46,7 +46,7 @@ Blockly.Arduino['serial_print'] = function(block) {
Blockly.Arduino['serial_setup'] = function(block) { Blockly.Arduino['serial_setup'] = function(block) {
var serialId = block.getFieldValue('SERIAL_ID'); var serialId = block.getFieldValue('SERIAL_ID');
var serialSpeed = block.getFieldValue('SPEED'); var serialSpeed = block.getFieldValue('SPEED');
var serialSetupCode = serialId + '.begin(' + serialSpeed + ');\n'; var serialSetupCode = serialId + '.begin(' + serialSpeed + ');';
Blockly.Arduino.addSetup('serial_' + serialId, serialSetupCode, true); Blockly.Arduino.addSetup('serial_' + serialId, serialSetupCode, true);
var code = ''; var code = '';
return code; return code;
......
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