Commit 056bb99e authored by Carlos Pereira Atencio's avatar Carlos Pereira Atencio Committed by carlospamg

Update to Arduino generators for arduino specific functions:

  Added all time related functions.
  Removed some of the Behind the Wire pin renaming.
  Removed servo functions, this will be added in the future as part of an "arduino libraries" update.
  Updated depreciated function calls.
parent a600871a
...@@ -83,12 +83,9 @@ var profile = { ...@@ -83,12 +83,9 @@ var profile = {
//Behind the Wire Arduino profile //Behind the Wire Arduino profile
arduino_btw: { arduino_btw: {
description: "Arduino Behind The Wire", description: "Arduino Behind The Wire",
digital : [["RedLight","RedLight"],["GreenLight","GreenLight"],["WhiteLight","WhiteLight"],["UpButton","UpButton"],["DownButton","DownButton"],["OnButton","OnButton"],["OffButton","OffButton"],["LeftButton","LeftButton"],["RightButton","RightButton"]], digital : [["1", "1"], ["2", "2"], ["3", "3"], ["4", "4"], ["5", "5"], ["6", "6"], ["7", "7"], ["8", "8"], ["9", "9"], ["10", "10"], ["11", "11"], ["12", "12"], ["13", "13"], ["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
analog : [["EngineLeft","EngineLeft"],["EngineRight","EngineRight"],["Throttle","Throttle"]], analog : [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
nav_lights : [["Red (Left Wing)","RedLight"],["Green (Right Wing)","GreenLight"],["White (Tail)","WhiteLight"]], serial : 9600
buttons : [["Left","LeftButton"],["Right","RightButton"]],
rudder_angle: [["-45","-45"],["-30","-30"],["-15","-15"],["0","0"],["15","15"],["30","30"],["45","45"]],
engines : [["Engine Left","EngineLeft"],["Engine Right","EngineRight"]]
} }
} }
//set default profile to arduino standard-compatible board //set default profile to arduino standard-compatible board
......
...@@ -22,17 +22,12 @@ ...@@ -22,17 +22,12 @@
* @fileoverview Helper functions for generating Arduino blocks. * @fileoverview Helper functions for generating Arduino blocks.
* Originally developed by the author below and modified for Ardublockly * Originally developed by the author below and modified for Ardublockly
* @author gasolin@gmail.com (Fred Lin) * @author gasolin@gmail.com (Fred Lin)
* The Arduino built in functions syntax can be found in http://arduino.cc/en/Reference/HomePage
*/ */
'use strict'; 'use strict';
//To support syntax defined in http://arduino.cc/en/Reference/HomePage
goog.provide('Blockly.Arduino.loops'); Blockly.Blocks['base_map'] = {
goog.require('Blockly.Arduino');
Blockly.Language.base_map = {
category: 'Math', category: 'Math',
helpUrl: 'http://arduino.cc/en/Reference/map', helpUrl: 'http://arduino.cc/en/Reference/map',
init: function() { init: function() {
...@@ -44,60 +39,24 @@ Blockly.Language.base_map = { ...@@ -44,60 +39,24 @@ Blockly.Language.base_map = {
.appendTitle("value to [0-") .appendTitle("value to [0-")
.setCheck(Number); .setCheck(Number);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle("]"); .appendTitle("]");
this.setInputsInline(true); this.setInputsInline(true);
this.setOutput(true); this.setOutput(true);
this.setTooltip('Re-maps a number from [0-1024] to another.'); this.setTooltip('Re-maps a number from [0-1024] to another.');
} }
}; };
Blockly.Arduino['base_map'] = function(block) {
//servo block var value_num = Blockly.Arduino.valueToCode(block, 'NUM', Blockly.Arduino.ORDER_NONE);
//http://www.seeedstudio.com/depot/emax-9g-es08a-high-sensitive-mini-servo-p-760.html?cPath=170_171 var value_dmax = Blockly.Arduino.valueToCode(block, 'DMAX', Blockly.Arduino.ORDER_ATOMIC);
Blockly.Language.servo_move = { var code = 'map('+value_num+', 0, 1024, 0, '+value_dmax+')';
category: 'Servo', return [code, Blockly.Arduino.ORDER_NONE];
helpUrl: 'http://www.arduino.cc/playground/ComponentLib/servo',
init: function() {
this.setColour(190);
this.appendDummyInput("")
.appendTitle("Servo")
.appendTitle(new Blockly.FieldImage("http://www.seeedstudio.com/depot/images/product/a991.jpg", 64, 64))
.appendTitle("PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.digital), "PIN")
this.appendValueInput("DEGREE", Number)
.setCheck(Number)
.setAlign(Blockly.ALIGN_RIGHT)
.appendTitle("Degree (0~180)");
this.appendValueInput("DELAY_TIME", Number)
.setCheck(Number)
.setAlign(Blockly.ALIGN_RIGHT)
.appendTitle("Delay");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('move between 0~180 degree');
}
}; };
Blockly.Language.servo_read_degrees = {
category: 'Servo',
helpUrl: 'http://www.arduino.cc/playground/ComponentLib/servo',
init: function() {
this.setColour(190);
this.appendDummyInput("")
.appendTitle("Servo")
.appendTitle(new Blockly.FieldImage("http://www.seeedstudio.com/depot/images/product/a991.jpg", 64, 64))
.appendTitle("PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.digital), "PIN");
this.appendDummyInput("")
.setAlign(Blockly.ALIGN_RIGHT)
.appendTitle("Read Degrees")
this.setOutput(true, Number);
this.setTooltip('return that degree with the last servo move.');
}
};
Blockly.Language.serial_print = {
category: 'In/Out', Blockly.Blocks['serial_print'] = {
category: 'Serial',
helpUrl: 'http://www.arduino.cc/en/Serial/Print', helpUrl: 'http://www.arduino.cc/en/Serial/Print',
init: function() { init: function() {
this.setColour(230); this.setColour(230);
...@@ -109,64 +68,8 @@ Blockly.Language.serial_print = { ...@@ -109,64 +68,8 @@ Blockly.Language.serial_print = {
} }
}; };
Blockly.Arduino['serial_print'] = function(block) {
// define generators var content = Blockly.Arduino.valueToCode(block, 'CONTENT', Blockly.Arduino.ORDER_ATOMIC) || '0'
Blockly.Arduino = Blockly.Generator.get('Arduino');
Blockly.Arduino.base_map = function() {
var value_num = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_NONE);
var value_dmax = Blockly.Arduino.valueToCode(this, 'DMAX', Blockly.Arduino.ORDER_ATOMIC);
var code = 'map('+value_num+', 0, 1024, 0, '+value_dmax+')';
return [code, Blockly.Arduino.ORDER_NONE];
};
/*
//servo
#include <Servo.h>
Servo servo_11;
void setup() {
servo_11.attach(11);
}
void loop() {
servo_11.write(0);
delay(2000);
servo_11.write(150); //0~180
delay(2000);
}
*/
Blockly.Arduino.servo_move = function() {
var dropdown_pin = this.getTitleValue('PIN');
var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC);
//value_degree = value_degree.replace('(','').replace(')','')
var delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC) || '1000'
//delay_time = delay_time.replace('(','').replace(')','');
Blockly.Arduino.definitions_['define_servo'] = '#include <Servo.h>\n';
Blockly.Arduino.definitions_['var_servo'+dropdown_pin] = 'Servo servo_'+dropdown_pin+';\n';
Blockly.Arduino.setups_['setup_servo_'+dropdown_pin] = 'servo_'+dropdown_pin+'.attach('+dropdown_pin+');\n';
var code = 'servo_'+dropdown_pin+'.write('+value_degree+');\n'+'delay(' + delay_time + ');\n';
return code;
};
Blockly.Arduino.servo_read_degrees = function() {
var dropdown_pin = this.getTitleValue('PIN');
Blockly.Arduino.definitions_['define_servo'] = '#include &lt;Servo.h&gt;\n';
Blockly.Arduino.definitions_['var_servo'+dropdown_pin] = 'Servo servo_'+dropdown_pin+';\n';
Blockly.Arduino.setups_['setup_servo_'+dropdown_pin] = 'servo_'+dropdown_pin+'.attach('+dropdown_pin+');\n';
var code = 'servo_'+dropdown_pin+'.read()';
return code;
};
Blockly.Arduino.serial_print = function() {
var content = Blockly.Arduino.valueToCode(this, 'CONTENT', Blockly.Arduino.ORDER_ATOMIC) || '0'
//content = content.replace('(','').replace(')','');
Blockly.Arduino.setups_['setup_serial_'+profile.default.serial] = 'Serial.begin('+profile.default.serial+');\n'; Blockly.Arduino.setups_['setup_serial_'+profile.default.serial] = 'Serial.begin('+profile.default.serial+');\n';
......
...@@ -9,15 +9,14 @@ ...@@ -9,15 +9,14 @@
* Description * Description
*/ */
Blockly.Blocks['io_digitalwrite'] = { Blockly.Blocks['io_digitalwrite'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/DigitalWrite', helpUrl: 'http://arduino.cc/en/Reference/DigitalWrite',
init: function() { init: function() {
this.setColour(230); this.setColour(230);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle("DigitalWrite PIN#") .appendField("DigitalWrite PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.digital), "PIN") .appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN")
.appendTitle("Stat") .appendField("Stat")
.appendTitle(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT"); .appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
this.setPreviousStatement(true, null); this.setPreviousStatement(true, null);
this.setNextStatement(true, null); this.setNextStatement(true, null);
this.setTooltip('Write digital value to a specific Port'); this.setTooltip('Write digital value to a specific Port');
...@@ -36,13 +35,12 @@ Blockly.Arduino['io_digitalwrite'] = function(block) { ...@@ -36,13 +35,12 @@ Blockly.Arduino['io_digitalwrite'] = function(block) {
* Description * Description
*/ */
Blockly.Blocks['io_digitalread'] = { Blockly.Blocks['io_digitalread'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/DigitalRead', helpUrl: 'http://arduino.cc/en/Reference/DigitalRead',
init: function() { init: function() {
this.setColour(230); this.setColour(230);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle("DigitalRead PIN#") .appendField("DigitalRead PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.digital), "PIN"); .appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN");
this.setOutput(true, Boolean); this.setOutput(true, Boolean);
this.setTooltip(''); this.setTooltip('');
} }
...@@ -60,13 +58,12 @@ Blockly.Arduino['io_digitalread'] = function(block) { ...@@ -60,13 +58,12 @@ Blockly.Arduino['io_digitalread'] = function(block) {
* Description * Description
*/ */
Blockly.Blocks['io_builtin_led'] = { Blockly.Blocks['io_builtin_led'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/DigitalWrite', helpUrl: 'http://arduino.cc/en/Reference/DigitalWrite',
init: function() { init: function() {
this.setColour(190); this.setColour(190);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle("Build-in LED Stat") .appendField("Build-in LED Stat")
.appendTitle(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT"); .appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
this.setPreviousStatement(true, null); this.setPreviousStatement(true, null);
this.setNextStatement(true, null); this.setNextStatement(true, null);
this.setTooltip('Turn on or off the built in LED'); this.setTooltip('Turn on or off the built in LED');
...@@ -84,15 +81,14 @@ Blockly.Arduino['io_builtin_led'] = function(block) { ...@@ -84,15 +81,14 @@ Blockly.Arduino['io_builtin_led'] = function(block) {
* Description * Description
*/ */
Blockly.Blocks['io_analogwrite'] = { Blockly.Blocks['io_analogwrite'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/AnalogWrite', helpUrl: 'http://arduino.cc/en/Reference/AnalogWrite',
init: function() { init: function() {
this.setColour(230); this.setColour(230);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle("AnalogWrite PIN#") .appendField("AnalogWrite PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.analog), "PIN"); .appendField(new Blockly.FieldDropdown(profile.default.analog), "PIN");
this.appendValueInput("NUM", Number) this.appendValueInput("NUM", Number)
.appendTitle("value") .appendField("value")
.setCheck(Number); .setCheck(Number);
this.setInputsInline(true); this.setInputsInline(true);
this.setPreviousStatement(true, null); this.setPreviousStatement(true, null);
...@@ -114,13 +110,12 @@ Blockly.Arduino['io_analogwrite'] = function(block) { ...@@ -114,13 +110,12 @@ Blockly.Arduino['io_analogwrite'] = function(block) {
* Description * Description
*/ */
Blockly.Blocks['io_analogread'] = { Blockly.Blocks['io_analogread'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/AnalogRead', helpUrl: 'http://arduino.cc/en/Reference/AnalogRead',
init: function() { init: function() {
this.setColour(230); this.setColour(230);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle("AnalogRead PIN#") .appendField("AnalogRead PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.analog), "PIN"); .appendField(new Blockly.FieldDropdown(profile.default.analog), "PIN");
this.setOutput(true, Number); this.setOutput(true, Number);
this.setTooltip('Return value between 0 and 1024'); this.setTooltip('Return value between 0 and 1024');
} }
...@@ -137,14 +132,13 @@ Blockly.Arduino['io_analogread'] = function(block) { ...@@ -137,14 +132,13 @@ Blockly.Arduino['io_analogread'] = function(block) {
* Description * Description
*/ */
Blockly.Blocks['io_highlow'] = { Blockly.Blocks['io_highlow'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/Constants', helpUrl: 'http://arduino.cc/en/Reference/Constants',
init: function() { init: function() {
this.setColour(230); this.setColour(230);
this.appendDummyInput("") this.appendDummyInput("")
.appendTitle(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), 'BOOL') .appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), 'BOOL')
this.setOutput(true, Boolean); this.setOutput(true, Boolean);
this.setTooltip(Blockly.LANG_LOGIC_BOOLEAN_TOOLTIP_1); this.setTooltip("Set a pin state logic High or Low");
} }
}; };
......
...@@ -4,23 +4,22 @@ ...@@ -4,23 +4,22 @@
*/ */
'use strict'; 'use strict';
//TODO: When confirm working, replicate for millis(), micros(), and delayMicroseconds()
/** /**
* Delay block definition * Delay block definition
*/ */
Blockly.Blocks['time_delay'] = { Blockly.Blocks['time_delay'] = {
category: 'Time', helpUrl: 'http://arduino.cc/en/Reference/Delay',
helpUrl: 'http://arduino.cc/en/Reference/delay',
init: function() { init: function() {
this.setColour(120); this.setColour(120);
this.appendValueInput("DELAY_TIME", Number) this.appendValueInput("DELAY_TIME", Number)
.appendTitle("Delay") .appendField("Delay")
.setCheck(Number); .setCheck(Number)
.appendField("Milliseconds");
this.setInputsInline(true); this.setInputsInline(true);
this.setPreviousStatement(true, null); this.setPreviousStatement(true, null);
this.setNextStatement(true, null); this.setNextStatement(true, null);
this.setTooltip('Delay specific time'); this.setTooltip('Delay specific time in milliseconds');
} }
}; };
...@@ -28,7 +27,80 @@ Blockly.Blocks['time_delay'] = { ...@@ -28,7 +27,80 @@ Blockly.Blocks['time_delay'] = {
* Delay Arduino code generator * Delay Arduino code generator
*/ */
Blockly.Arduino['time_delay'] = function(block) { Blockly.Arduino['time_delay'] = function(block) {
var delay_time = Blockly.Arduino.valueToCode(block, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC) || '1000' var delay_time = Blockly.Arduino.valueToCode(block, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC) || '1000';
var code = 'delay(' + delay_time + ');\n'; var code = 'delay(' + delay_time + ');\n';
return code; return code;
}; };
/**
* delayMicroseconds block definition
*/
Blockly.Blocks['time_delaymicros'] = {
helpUrl: 'http://arduino.cc/en/Reference/DelayMicroseconds',
init: function() {
this.setColour(120);
this.appendValueInput("DELAY_TIME_MS", Number)
.appendField("Delay")
.setCheck(Number)
.appendField("Microseconds");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Delay specific time in microseconds');
}
};
/**
* delayMicroseconds Arduino code generator
*/
Blockly.Arduino['time_delaymicros'] = function(block) {
var delay_timems = Blockly.Arduino.valueToCode(block, 'DELAY_TIME_MS', Blockly.Arduino.ORDER_ATOMIC) || '1000';
var code = 'delayMicroseconds(' + delay_timems + ');\n';
return code;
};
/**
* millis block definition
*/
Blockly.Blocks['time_millis'] = {
helpUrl: 'http://arduino.cc/en/Reference/Millis',
init: function() {
this.setColour(120);
this.appendDummyInput("")
.appendField("Elapsed Time (Milli)");
this.setOutput(true, Number);
this.setTooltip('Returns the number of milliseconds since the Arduino board began running the current program.');
}
};
/**
* millis Arduino code generator
*/
Blockly.Arduino['time_millis'] = function(block) {
var code = 'millis()';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
/**
* micros block definition
*/
Blockly.Blocks['time_micros'] = {
helpUrl: 'http://arduino.cc/en/Reference/Micros',
init: function() {
this.setColour(120);
this.appendDummyInput("")
.appendField("Elapsed Time (Micro)");
this.setOutput(true, Number);
this.setTooltip('Returns the number of microseconds since the Arduino board began running the current program.');
}
};
/**
* micros Arduino code generator
*/
Blockly.Arduino['time_micros'] = function(block) {
var code = 'micros()';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
\ No newline at end of file
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