Commit 03e3bef4 authored by Carlos's avatar Carlos

Added Arduino Servo library blocks and code generator

parent 865d8971
This diff is collapsed.
...@@ -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