Commit 61a16040 authored by carlosperate's avatar carlosperate

Updated arduino text code generator.

Fix a couple of issues with previous block generator.
Removed unnecessary blocks for arduino from the toolbox.
Print and prompt use the Serial peripheral.
parent ec36ed8d
......@@ -93,28 +93,6 @@
</block>
<block type="text_length"></block>
<block type="text_isEmpty"></block>
<block type="text_indexOf">
<value name="VALUE">
<block type="variables_get">
<field name="VAR" class="textVar">text</field>
</block>
</value>
</block>
<block type="text_charAt">
<value name="VALUE">
<block type="variables_get">
<field name="VAR" class="textVar">text</field>
</block>
</value>
</block>
<block type="text_getSubstring">
<value name="STRING">
<block type="variables_get">
<field name="VAR" class="textVar">text</field>
</block>
</value>
</block>
<block type="text_changeCase"></block>
<block type="text_trim"></block>
<block type="text_print"></block>
<block type="text_prompt_ext">
......
/**
* @license
* Visual Blocks Language
* @license Licensed under the Apache License, Version 2.0 (the "License"):
* http://www.apache.org/licenses/LICENSE-2.0
*
* Copyright 2012 Google Inc.
* https://blockly.googlecode.com/
* @fileoverview Arduino code generator for the text blocks.
* Partially implements the Serial interface in Arduino:
* http://arduino.cc/en/Reference/Serial
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating Arduino for text blocks.
* Implements the Serial interface in Arduino: http://arduino.cc/en/Reference/Serial
* TODO: Too many calls to String constructor, which consumes a lot of
* resources. Once type identification is implemented this will need an
* update.
* TODO: Serial peripheral and speed are hard-coded, should require serial
* setup block.
*/
'use strict';
......@@ -29,67 +19,230 @@ goog.provide('Blockly.Arduino.text');
goog.require('Blockly.Arduino');
/**
* Code generator for a literal String (X).
* Arduino code: loop { "X" }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['text'] = function(block) {
// Text value.
var code = Blockly.Arduino.quote_(block.getFieldValue('TEXT'));
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
/**
* Code generator for a String concatenation (X...Y). This string can be made
* up of any number of elements of any type.
* This block uses a mutator.
* String construction info: http://arduino.cc/en/Reference/StringConstructor
* Arduino code: loop { "String(X)" + ... + "String(Y)" }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['text_join'] = function(block) {
// Create a string made up of any number of elements of any type.
var code;
if (block.itemCount_ == 0) {
return ['\'\'', Blockly.Arduino.ORDER_ATOMIC];
return ['""', Blockly.Arduino.ORDER_ATOMIC];
} else if (block.itemCount_ == 1) {
var argument0 = Blockly.Arduino.valueToCode(block, 'ADD0',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '\'\'';
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '""';
code = 'String(' + argument0 + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
} else {
var argument;
code = [];
code[0] = (Blockly.Arduino.valueToCode(block, 'ADD0',
Blockly.Arduino.ORDER_NONE) || '\'\'');
for (var n = 1; n < block.itemCount_; n++) {
code[n] = '+' + (Blockly.Arduino.valueToCode(block, 'ADD' + n,
Blockly.Arduino.ORDER_NONE) || '\'\'');
for (var n = 0; n < block.itemCount_; n++) {
argument = Blockly.Arduino.valueToCode(
block, 'ADD' + n, Blockly.Arduino.ORDER_NONE);
if (argument == '') {
code[n] = '""';
} else {
code[n] = 'String(' + argument +')';
}
}
code = code.join('');
code = code.join(' + ');
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
}
};
/**
* Code generator for appending text (Y) to a variable in place (X).
* String constructor info: http://arduino.cc/en/Reference/StringConstructor
* Arduino code: loop { X += String(Y) }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['text_append'] = function(block) {
// Append to a variable in place.
var varName = Blockly.Arduino.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.Arduino.valueToCode(block, 'TEXT',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '\'\'';
Blockly.Arduino.ORDER_UNARY_POSTFIX);
if (argument0 == '') {
argument0 = '""';
} else {
argument0 = 'String(' + argument0 + ')';
}
return varName + ' += ' + argument0 + ';\n';
};
/**
* Code generator to get the length of a string (X).
* String length info: http://arduino.cc/en/Reference/StringLength
* Arduino code: loop { String(X).length() }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['text_length'] = function(block) {
// String length.
var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '\'\'';
return ['((String)' + argument0 + ').length()', Blockly.Arduino.ORDER_UNARY_POSTFIX];
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '""';
var code = 'String(' + argument0 + ').length()';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
/**
* Code generator to test if a string (X) is null/empty.
* String length info: http://arduino.cc/en/Reference/StringLength
* Arduino code: boolean isStringEmpty(...) { ... }
* loop { isStringEmpty(X) }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['text_isEmpty'] = function(block) {
// TODO: Is the string null?
var func = [];
func.push('boolean isStringEmpty(String msg) {');
func.push(' if(msg.length()==0) {');
func.push(' return true;');
func.push(' } else {');
func.push(' return false;');
func.push(' }');
func.push('}\n\n');
func.push('boolean isStringEmpty(String msg) {');
func.push(' if (msg.length() == 0) {');
func.push(' return true;');
func.push(' } else {');
func.push(' return false;');
func.push(' }');
func.push('}');
Blockly.Arduino.definitions_['is_string_empty'] = func.join('\n');
var argument0 = Blockly.Arduino.valueToCode(block, 'VALUE',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '\'\'';
return ['isStringEmpty(' + argument0 + ')', Blockly.Arduino.ORDER_UNARY_POSTFIX];
Blockly.Arduino.ORDER_UNARY_POSTFIX);
if (argument0 == '') {
argument0 = '""';
} else {
argument0 = 'String(' + argument0 + ')';
}
var code = 'isStringEmpty(' + argument0 + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
/**
* Code generator to trim spaces from a string (X).
* String trim info: http://arduino.cc/en/Tutorial/StringLengthTrim
* Arduino code: loop { String(X).trim() }
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['text_trim'] = function(block) {
// Trim spaces.
Blockly.Arduino.text_trim.OPERATORS = {
LEFT: '.trim()',
RIGHT: '.trim()',
BOTH: '.trim()'
};
var mode = block.getFieldValue('MODE');
var operator = Blockly.Arduino.text_trim.OPERATORS[mode];
var argument0 = Blockly.Arduino.valueToCode(this, 'TEXT',
Blockly.Arduino.ORDER_UNARY_POSTFIX);
if (argument0 == '') {
argument0 = '""';
} else {
argument0 = 'String(' + argument0 + ')';
}
return [argument0 + operator, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
/**
* Code generator to trim spaces from a string (X).
* Serial info: http://arduino.cc/en/Reference/Serial
* Arduino code: setup { Serial.begin(9600); }
* loop { Serial.print(String(X)) }
* TODO: The serial peripheral and speed hard-coded into the code.
* Need to change to request a serial setup block.
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['text_print'] = function(block) {
var serialId = profile.default.serial[0][1];
Blockly.Arduino.setups_['setup_serial_' + serialId] =
serialId + '.begin(9600);';
var argument0 = Blockly.Arduino.valueToCode(block, 'TEXT',
Blockly.Arduino.ORDER_NONE);
if (argument0 == '') {
argument0 = '""';
} else {
argument0 = 'String(' + argument0 + ')';
}
return serialId + '.print(' + argument0 + ');\n';
};
/**
* Code generator to prompt the user with a string (X) and request input.
* Serial info: http://arduino.cc/en/Reference/Serial
* Arduino code: getUserInputPrompt(...) { ... }
* loop { getUserInputPrompt("X")) }
* TODO: The serial peripheral and speed hard-coded into the code.
* Need to change to request a serial setup block.
* @param {!Blockly.Block} block Block to generate the code from.
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['text_prompt_ext'] = function(block) {
// Get the first Serial peripheral of arduino board
var serialId = profile.default.serial[0][1];
// The function code changes based on reading a number or string
var func = [];
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
if (toNumber) {
func.push('int getUserInputPrompt(String msg) {');
} else {
func.push('String getUserInputPrompt(String msg) {');
}
func.push(' ' + serialId + '.print(msg+\'\\n\');');
func.push(' boolean stringComplete = false;');
if (toNumber) {
func.push(' int content = ' + serialId + '.parseInt();');
} else {
func.push(' String content = "";');
}
func.push(' while (stringComplete == false) {');
func.push(' if (' + serialId + '.available()) {');
if (toNumber) {
func.push(' content = ' + serialId + '.parseInt();');
func.push(' stringComplete = true;');
} else {
func.push(' char readChar = (char)' + serialId + '.read();');
func.push(' content += readChar;');
func.push(' if (readChar == \'\\n\' || readChar == \'\\r\') {');
func.push(' stringComplete = true;');
func.push(' }');
}
func.push(' }');
func.push(' }');
func.push(' return content;');
func.push('}\n\n');
Blockly.Arduino.definitions_['define_string_return'] = func.join('\n');
Blockly.Arduino.setups_['setup_serial_' + serialId] =
serialId + '.begin(9600);';
var msg = Blockly.Arduino.valueToCode(block, 'TEXT',
Blockly.Arduino.ORDER_NONE) || '\'\'';
var code = 'getUserInputPrompt(' + msg + ')';
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
/* ********************************************************************** *
* The rest of the blocks have been left as it is, as they are be removed *
* from the toolbox and not used for Arduino code. *
* ********************************************************************** */
Blockly.Arduino['text_endString'] = function(block) {
// Return a leading or trailing substring.
var first = block.getFieldValue('END') == 'FIRST';
......@@ -262,28 +415,6 @@ Blockly.Arduino['text_changeCase'] = function(block) {
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
Blockly.Arduino['text_trim'] = function(block) {
// Trim spaces.
Blockly.Arduino.text_trim.OPERATORS = {
LEFT: '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
RIGHT: '.replaceFirst(new RegExp(r\'\\s+$\'), \'\')',
BOTH: '.trim()'
};
var mode = block.getFieldValue('MODE');
var operator = Blockly.Arduino.text_trim.OPERATORS[mode];
var argument0 = Blockly.Arduino.valueToCode(this, 'TEXT',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || '\'\'';
return [argument0 + operator, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
Blockly.Arduino['text_print'] = function(block) {
// Print statement.
Blockly.Arduino.setups_['serial_begin'] = 'Serial.begin(9600);';
var argument0 = Blockly.Arduino.valueToCode(block, 'TEXT',
Blockly.Arduino.ORDER_NONE) || '\'\'';
return 'Serial.print(' + argument0 + ');\n';
};
Blockly.Arduino['text_prompt'] = function(block) {
// Prompt function.
Blockly.Arduino.setups_['serial_begin'] = 'Serial.begin(9600);';
......@@ -295,28 +426,3 @@ Blockly.Arduino['text_prompt'] = function(block) {
}
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
Blockly.Arduino['text_prompt_ext'] = function(block) {
// Prompt function (external message).
var func = [];
func.push('String getUserInputPrompt(String msg) {');
func.push(' Serial.print(msg);\n');
func.push(' String content = "";');
func.push(' while(Serial.available()) {');
func.push(' content.concat(Serial.read());');
func.push(' }');
func.push(' return content;');
func.push('}\n\n');
Blockly.Arduino.definitions_['define_string_return'] = func.join('\n');
Blockly.Arduino.setups_['serial_begin'] = 'Serial.begin(9600);';
var msg = Blockly.Arduino.valueToCode(block, 'TEXT',
Blockly.Arduino.ORDER_NONE) || '\'\'';
var code = 'getUserInputPrompt(' + msg + ')';
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
if (toNumber) {
code = 'parseFloat(' + code + ')';
}
return [code, Blockly.Arduino.ORDER_UNARY_POSTFIX];
};
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