Commit 662aa5a5 authored by Carlos's avatar Carlos Committed by carlospamg

Added Arduino Servo library blocks and code generator

parent a867bf01
...@@ -41,15 +41,15 @@ codepage.messages = function(opt_data, opt_ignored, opt_ijData) { ...@@ -41,15 +41,15 @@ codepage.messages = function(opt_data, opt_ignored, opt_ijData) {
codepage.start = function(opt_data, opt_ignored, opt_ijData) { codepage.start = function(opt_data, opt_ignored, opt_ijData) {
return codepage.messages(null, null, opt_ijData) + '<script type="text/javascript" src="../../blockly_uncompressed.js"><\/script><script type="text/javascript" src="../../blocks_compressed.js"><\/script><script type="text/javascript" src="../../generators/arduino.js"><\/script><script type="text/javascript" src="../../generators/arduino/base.js"><\/script><script type="text/javascript" src="../../generators/arduino/io.js"><\/script><script type="text/javascript" src="../../generators/arduino/lists.js"><\/script><script type="text/javascript" src="../../generators/arduino/logic.js"><\/script><script type="text/javascript" src="../../generators/arduino/loops.js"><\/script><script type="text/javascript" src="../../generators/arduino/math.js"><\/script><script type="text/javascript" src="../../generators/arduino/procedures.js"><\/script><script type="text/javascript" src="../../generators/arduino/text.js"><\/script><script type="text/javascript" src="../../generators/arduino/time.js"><\/script><script type="text/javascript" src="../../generators/arduino/variables.js"><\/script><script type="text/javascript" src="../../' + soy.$$escapeHtml(opt_ijData.langSrc) + '"><\/script><table width="100%" height="100%"><tr><td><h1><span id="title"><a href="../index.html?lang=' + soy.$$escapeHtml(opt_ijData.lang) + '">Blockly</a> : Code</span></h1></td><td class="farSide"><select id="languageMenu"></select></td></tr><tr><td colspan=2><table width="100%"><tr id="tabRow" height="1em"><td id="tab_blocks" class="tabon">Blocks</td><td class="tabmin">&nbsp;</td><td id="tab_arduino" class="taboff">Arduino</td><td class="tabmin">&nbsp;</td><td id="tab_xml" class="taboff">XML</td><td class="tabmax"><button id="trashButton" class="notext" title="Discard all blocks."><img src=\'../../media/1x1.gif\' class="trash icon21"></button> <button id="linkButton" class="notext" title="Save and link to blocks."><img src=\'../../media/1x1.gif\' class="link icon21"></button> <button id="runButton" class="notext primary" title="Run the program defined by the blocks in the workspace."><img src=\'../../media/1x1.gif\' class="run icon21"></button></td></tr></table></td></tr><tr><td height="99%" colspan=2 id="content_area">' + codepage.toolbox(null, null, opt_ijData) + '</td></tr></table><div id="content_blocks" class="content"></div><pre id="content_arduino" class="content"></pre><textarea id="content_xml" class="content" wrap="off"></textarea>' + apps.dialog(null, null, opt_ijData) + apps.storageDialog(null, null, opt_ijData); return codepage.messages(null, null, opt_ijData) + '<script type="text/javascript" src="../../blockly_uncompressed.js"><\/script><script type="text/javascript" src="../../blocks_compressed.js"><\/script><script type="text/javascript" src="../../generators/arduino.js"><\/script><script type="text/javascript" src="../../generators/arduino/base.js"><\/script><script type="text/javascript" src="../../generators/arduino/io.js"><\/script><script type="text/javascript" src="../../generators/arduino/lists.js"><\/script><script type="text/javascript" src="../../generators/arduino/logic.js"><\/script><script type="text/javascript" src="../../generators/arduino/loops.js"><\/script><script type="text/javascript" src="../../generators/arduino/math.js"><\/script><script type="text/javascript" src="../../generators/arduino/procedures.js"><\/script><script type="text/javascript" src="../../generators/arduino/servo.js"><\/script><script type="text/javascript" src="../../generators/arduino/text.js"><\/script><script type="text/javascript" src="../../generators/arduino/time.js"><\/script><script type="text/javascript" src="../../generators/arduino/variables.js"><\/script><script type="text/javascript" src="../../' + soy.$$escapeHtml(opt_ijData.langSrc) + '"><\/script><table width="100%" height="100%"><tr><td><h1><span id="title"><a href="../index.html?lang=' + soy.$$escapeHtml(opt_ijData.lang) + '">Blockly</a> : Code</span></h1></td><td class="farSide"><select id="languageMenu"></select></td></tr><tr><td colspan=2><table width="100%"><tr id="tabRow" height="1em"><td id="tab_blocks" class="tabon">Blocks</td><td class="tabmin">&nbsp;</td><td id="tab_arduino" class="taboff">Arduino</td><td class="tabmin">&nbsp;</td><td id="tab_xml" class="taboff">XML</td><td class="tabmax"><button id="trashButton" class="notext" title="Discard all blocks."><img src=\'../../media/1x1.gif\' class="trash icon21"></button> <button id="linkButton" class="notext" title="Save and link to blocks."><img src=\'../../media/1x1.gif\' class="link icon21"></button> <button id="runButton" class="notext primary" title="Run the program defined by the blocks in the workspace."><img src=\'../../media/1x1.gif\' class="run icon21"></button></td></tr></table></td></tr><tr><td height="99%" colspan=2 id="content_area">' + codepage.toolbox(null, null, opt_ijData) + '</td></tr></table><div id="content_blocks" class="content"></div><pre id="content_arduino" class="content"></pre><textarea id="content_xml" class="content" wrap="off"></textarea>' + apps.dialog(null, null, opt_ijData) + apps.storageDialog(null, null, opt_ijData);
}; };
codepage.toolbox = function(opt_data, opt_ignored, opt_ijData) { codepage.toolbox = function(opt_data, opt_ignored, opt_ijData) {
return '<xml id="toolbox" style="display: none"><category name="Logic"><block type="controls_if"></block><block type="logic_compare"></block><block type="logic_operation"></block><block type="logic_negate"></block><block type="logic_boolean"></block><block type="logic_null"></block><block type="logic_ternary"></block></category><category name="Loops"><block type="controls_repeat_ext"><value name="TIMES"><block type="math_number"><field name="NUM">10</field></block></value></block><block type="controls_whileUntil"></block><block type="controls_for"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">10</field></block></value><value name="BY"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="controls_forEach"></block><block type="controls_flow_statements"></block></category><category name="Math"><block type="math_number"></block><block type="math_arithmetic"></block><block type="math_single"></block><block type="math_trig"></block><block type="math_constant"></block><block type="math_number_property"></block><block type="math_change"><value name="DELTA"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="math_round"></block><block type="math_on_list"></block><block type="math_modulo"></block><block type="math_constrain"><value name="LOW"><block type="math_number"><field name="NUM">1</field></block></value><value name="HIGH"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_int"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_float"></block><block type="base_map"></block></category><category name="Text"><block type="text"></block><block type="text_join"></block><block type="text_append"><value name="TEXT"><block type="text"></block></value></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">text</field></block></value></block><block type="text_charAt"><value name="VALUE"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_getSubstring"><value name="STRING"><block type="variables_get"><field name="VAR">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"><value name="TEXT"><block type="text"></block></value></block></category><category name="Lists"><block type="lists_create_empty"></block><block type="lists_create_with"></block><block type="lists_repeat"><value name="NUM"><block type="math_number"><field name="NUM">5</field></block></value></block><block type="lists_length"></block><block type="lists_isEmpty"></block><block type="lists_indexOf"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getIndex"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_setIndex"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getSublist"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block></category><category name="Variables"><block type="variables_get"></block><block type="variables_set"></block><block type="variables_set_type"></block></category><category name="Functions" custom="PROCEDURE"></category><category name="Input/Output"><block type="io_digitalwrite"></block><block type="io_digitalread"></block><block type="io_builtin_led"></block><block type="io_analogwrite"></block><block type="io_analogread"></block><block type="io_highlow"></block></category><category name="Time"><block type="time_delay"></block><block type="time_delaymicros"></block><block type="time_millis"></block><block type="time_micros"></block></category></xml>'; return '<xml id="toolbox" style="display: none"><category name="Logic"><block type="controls_if"></block><block type="logic_compare"></block><block type="logic_operation"></block><block type="logic_negate"></block><block type="logic_boolean"></block><block type="logic_null"></block><block type="logic_ternary"></block></category><category name="Loops"><block type="controls_repeat_ext"><value name="TIMES"><block type="math_number"><field name="NUM">10</field></block></value></block><block type="controls_whileUntil"></block><block type="controls_for"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">10</field></block></value><value name="BY"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="controls_forEach"></block><block type="controls_flow_statements"></block></category><category name="Math"><block type="math_number"></block><block type="math_arithmetic"></block><block type="math_single"></block><block type="math_trig"></block><block type="math_constant"></block><block type="math_number_property"></block><block type="math_change"><value name="DELTA"><block type="math_number"><field name="NUM">1</field></block></value></block><block type="math_round"></block><block type="math_on_list"></block><block type="math_modulo"></block><block type="math_constrain"><value name="LOW"><block type="math_number"><field name="NUM">1</field></block></value><value name="HIGH"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_int"><value name="FROM"><block type="math_number"><field name="NUM">1</field></block></value><value name="TO"><block type="math_number"><field name="NUM">100</field></block></value></block><block type="math_random_float"></block><block type="base_map"></block></category><category name="Text"><block type="text"></block><block type="text_join"></block><block type="text_append"><value name="TEXT"><block type="text"></block></value></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">text</field></block></value></block><block type="text_charAt"><value name="VALUE"><block type="variables_get"><field name="VAR">text</field></block></value></block><block type="text_getSubstring"><value name="STRING"><block type="variables_get"><field name="VAR">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"><value name="TEXT"><block type="text"></block></value></block></category><category name="Lists"><block type="lists_create_empty"></block><block type="lists_create_with"></block><block type="lists_repeat"><value name="NUM"><block type="math_number"><field name="NUM">5</field></block></value></block><block type="lists_length"></block><block type="lists_isEmpty"></block><block type="lists_indexOf"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getIndex"><value name="VALUE"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_setIndex"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block><block type="lists_getSublist"><value name="LIST"><block type="variables_get"><field name="VAR">list</field></block></value></block></category><category name="Variables"><block type="variables_get"></block><block type="variables_set"></block><block type="variables_set_type"></block></category><category name="Functions" custom="PROCEDURE"></category><category name="Input/Output"><block type="io_digitalwrite"></block><block type="io_digitalread"></block><block type="io_builtin_led"></block><block type="io_analogwrite"></block><block type="io_analogread"></block><block type="io_highlow"></block></category><category name="Time"><block type="time_delay"></block><block type="time_delaymicros"></block><block type="time_millis"></block><block type="time_micros"></block></category><category name="Servo"><block type="servo_write"></block><block type="servo_read"></block></category></xml>';
}; };
codepage.readonly = function(opt_data, opt_ignored, opt_ijData) { codepage.readonly = function(opt_data, opt_ignored, opt_ijData) {
return codepage.messages(null, null, opt_ijData) + '<script type="text/javascript" src="../../blockly_compressed.js"><\/script><script type="text/javascript" src="../../blocks_compressed.js"><\/script><script type="text/javascript" src="../../generators/arduino.js"><\/script><script type="text/javascript" src="../../generators/arduino/base.js"><\/script><script type="text/javascript" src="../../generators/arduino/io.js"><\/script><script type="text/javascript" src="../../generators/arduino/lists.js"><\/script><script type="text/javascript" src="../../generators/arduino/logic.js"><\/script><script type="text/javascript" src="../../generators/arduino/loops.js"><\/script><script type="text/javascript" src="../../generators/arduino/math.js"><\/script><script type="text/javascript" src="../../generators/arduino/procedures.js"><\/script><script type="text/javascript" src="../../generators/arduino/text.js"><\/script><script type="text/javascript" src="../../generators/arduino/time.js"><\/script><script type="text/javascript" src="../../generators/arduino/variables.js"><\/script><script type="text/javascript" src="../../' + soy.$$escapeHtml(opt_ijData.langSrc) + '"><\/script><div id="blockly"></div>'; return codepage.messages(null, null, opt_ijData) + '<script type="text/javascript" src="../../blockly_compressed.js"><\/script><script type="text/javascript" src="../../blocks_compressed.js"><\/script><script type="text/javascript" src="../../generators/arduino.js"><\/script><script type="text/javascript" src="../../generators/arduino/base.js"><\/script><script type="text/javascript" src="../../generators/arduino/io.js"><\/script><script type="text/javascript" src="../../generators/arduino/lists.js"><\/script><script type="text/javascript" src="../../generators/arduino/logic.js"><\/script><script type="text/javascript" src="../../generators/arduino/loops.js"><\/script><script type="text/javascript" src="../../generators/arduino/math.js"><\/script><script type="text/javascript" src="../../generators/arduino/procedures.js"><\/script><script type="text/javascript" src="../../generators/arduino/servo.js"><\/script><script type="text/javascript" src="../../generators/arduino/text.js"><\/script><script type="text/javascript" src="../../generators/arduino/time.js"><\/script><script type="text/javascript" src="../../generators/arduino/variables.js"><\/script><script type="text/javascript" src="../../' + soy.$$escapeHtml(opt_ijData.langSrc) + '"><\/script><div id="blockly"></div>';
}; };
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
<script type="text/javascript" src="../../generators/arduino/loops.js"></script> <script type="text/javascript" src="../../generators/arduino/loops.js"></script>
<script type="text/javascript" src="../../generators/arduino/math.js"></script> <script type="text/javascript" src="../../generators/arduino/math.js"></script>
<script type="text/javascript" src="../../generators/arduino/procedures.js"></script> <script type="text/javascript" src="../../generators/arduino/procedures.js"></script>
<script type="text/javascript" src="../../generators/arduino/servo.js"></script>
<script type="text/javascript" src="../../generators/arduino/text.js"></script> <script type="text/javascript" src="../../generators/arduino/text.js"></script>
<script type="text/javascript" src="../../generators/arduino/time.js"></script> <script type="text/javascript" src="../../generators/arduino/time.js"></script>
<script type="text/javascript" src="../../generators/arduino/variables.js"></script> <script type="text/javascript" src="../../generators/arduino/variables.js"></script>
...@@ -283,6 +284,10 @@ ...@@ -283,6 +284,10 @@
<block type="time_millis"></block> <block type="time_millis"></block>
<block type="time_micros"></block> <block type="time_micros"></block>
</category> </category>
<category name="{msg meaning="Apps.catServo" desc="IBID"}Servo{/msg}">
<block type="servo_write"></block>
<block type="servo_read"></block>
</category>
</xml> </xml>
{/template} {/template}
...@@ -302,6 +307,7 @@ ...@@ -302,6 +307,7 @@
<script type="text/javascript" src="../../generators/arduino/loops.js"></script> <script type="text/javascript" src="../../generators/arduino/loops.js"></script>
<script type="text/javascript" src="../../generators/arduino/math.js"></script> <script type="text/javascript" src="../../generators/arduino/math.js"></script>
<script type="text/javascript" src="../../generators/arduino/procedures.js"></script> <script type="text/javascript" src="../../generators/arduino/procedures.js"></script>
<script type="text/javascript" src="../../generators/arduino/servo.js"></script>
<script type="text/javascript" src="../../generators/arduino/text.js"></script> <script type="text/javascript" src="../../generators/arduino/text.js"></script>
<script type="text/javascript" src="../../generators/arduino/time.js"></script> <script type="text/javascript" src="../../generators/arduino/time.js"></script>
<script type="text/javascript" src="../../generators/arduino/variables.js"></script> <script type="text/javascript" src="../../generators/arduino/variables.js"></script>
......
...@@ -76,7 +76,7 @@ var profile = { ...@@ -76,7 +76,7 @@ var profile = {
interrupt: [["Int_1", "1"], ["Int_2", "2"], ["Int_3", "3"], ["Int_4", "4"], ["Int_5", "5"]], interrupt: [["Int_1", "1"], ["Int_2", "2"], ["Int_3", "3"], ["Int_4", "4"], ["Int_5", "5"]],
serial : [["300", "300"], ["600", "600"], ["1200", "1200"], ["2400", "2400"], ["4800", "4800"], ["9600", "9600"], ["14400", "14400"], ["19200", "19200"], ["28800", "28800"], ["31250", "31250"], ["38400", "38400"],["57600", "57600"], ["115200", "115200"]], serial : [["300", "300"], ["600", "600"], ["1200", "1200"], ["2400", "2400"], ["4800", "4800"], ["9600", "9600"], ["14400", "14400"], ["19200", "19200"], ["28800", "28800"], ["31250", "31250"], ["38400", "38400"],["57600", "57600"], ["115200", "115200"]],
builtin_led: [["BUILTIN_1", "13"]], builtin_led: [["BUILTIN_1", "13"]],
pin_types: { INPUT: "INPUT", OUTPUT: "OUTPUT", PWM: "PWM" }, pin_types: { INPUT: "INPUT", OUTPUT: "OUTPUT", PWM: "PWM", SERVO: "SERVO" },
types : [["void", "void"], ["Boolean", "boolean"], ["Character", "char"], ["Unsigned Character", "unsigned char"], ["Byte", "byte"], ["Integer", "int"], ["Unsigned Integer", "unsigned int"], ["Word", "word"], ["Long", "long"], ["Unsigned Long", "unsigned long"], ["Short", "short"], ["Float", "float"], ["Double", "double"], ["String", "String"], ["Char Array", "string"], ["Array", "array"]] types : [["void", "void"], ["Boolean", "boolean"], ["Character", "char"], ["Unsigned Character", "unsigned char"], ["Byte", "byte"], ["Integer", "int"], ["Unsigned Integer", "unsigned int"], ["Word", "word"], ["Long", "long"], ["Unsigned Long", "unsigned long"], ["Short", "short"], ["Float", "float"], ["Double", "double"], ["String", "String"], ["Char Array", "string"], ["Array", "array"]]
}, },
arduino_mega:{ arduino_mega:{
...@@ -215,7 +215,7 @@ Blockly.Arduino.finish = function(code) { ...@@ -215,7 +215,7 @@ Blockly.Arduino.finish = function(code) {
} }
var allDefs = imports.join('\n') + definitions.join('\n') + var allDefs = imports.join('\n') + definitions.join('\n') +
'\nvoid setup() {\n '+ setups.join('\n ') + '\n}'; '\n\nvoid setup() {\n '+ setups.join('\n ') + '\n}';
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code; return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
}; };
......
/**
* @fileoverview Blocks extension for Arduino Servo library.
* The Arduino Servo functions syntax can be found in http://arduino.cc/en/reference/servo
*/
'use strict';
goog.provide('Blockly.Arduino.servo');
goog.require('Blockly.Arduino');
/**
* Description
*/
Blockly.Blocks['servo_write'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/ServoWrite');
this.setColour(50);
this.appendDummyInput('')
.appendField('Set SERVO from Pin')
.appendField(new Blockly.FieldDropdown(profile.default.pwm), 'SERVO_PIN');
this.setInputsInline(false);
this.appendValueInput('SERVO_ANGLE', 'Number')
.setCheck('Number')
.appendField("to");
this.appendDummyInput('')
.appendField('Degrees (0-180)');
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Set a Servo to an specified angle');
}
};
/**
* Description
*/
Blockly.Arduino['servo_write'] = function(block) {
var pin_key = block.getFieldValue('SERVO_PIN');
var pin_type = profile.default.pin_types.SERVO;
var servo_name = 'myServo_' + pin_key;
var servo_angle = Blockly.Arduino.valueToCode(block, 'SERVO_ANGLE', Blockly.Arduino.ORDER_ATOMIC) || '90';
var code = servo_name + '.write(' + servo_angle + ');\n';
// Maintain the setup regardless of pin conflict, warning should be enough
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pin_key] = 'Servo ' + servo_name + ';';
Blockly.Arduino.setups_['setup_servo_' + pin_key] = servo_name + '.attach(' + pin_key + ');';
// If the IO has been configured already set a block warning for the user
if (pin_key in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pin_key] != pin_type) {
block.setWarningText('Pin alredy used as ' + Blockly.Arduino.pins_[pin_key]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pin_key] = pin_type;
block.setWarningText(null);
}
return code;
};
/**
* Description
*/
Blockly.Blocks['servo_read'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/ServoRead');
this.setColour(50);
this.appendDummyInput('')
.appendField('Read SERVO from PIN#')
.appendField(new Blockly.FieldDropdown(profile.default.pwm), 'SERVO_PIN');
this.setOutput(true, 'Number');
this.setTooltip('Read a Servo angle');
}
};
/**
* Description
*/
Blockly.Arduino['servo_read'] = function(block) {
var pin_key = block.getFieldValue('SERVO_PIN');
var pin_type = profile.default.pin_types.SERVO;
var servo_name = 'myServo_' + pin_key;
var code = servo_name + '.read()';
// Maintain the setup regardless of pin conflict, warning should be enough
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['global_servo_' + pin_key] = 'Servo ' + servo_name + ';';
Blockly.Arduino.setups_['setup_servo_' + pin_key] = servo_name + '.attach(' + pin_key + ');';
// If the IO has been configured already set a block warning for the user
if (pin_key in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pin_key] != pin_type) {
block.setWarningText('Pin alredy used as ' + Blockly.Arduino.pins_[pin_key]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pin_key] = pin_type;
block.setWarningText(null);
}
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
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