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 = {
//Behind the Wire Arduino profile
arduino_btw: {
description: "Arduino Behind The Wire",
digital : [["RedLight","RedLight"],["GreenLight","GreenLight"],["WhiteLight","WhiteLight"],["UpButton","UpButton"],["DownButton","DownButton"],["OnButton","OnButton"],["OffButton","OffButton"],["LeftButton","LeftButton"],["RightButton","RightButton"]],
analog : [["EngineLeft","EngineLeft"],["EngineRight","EngineRight"],["Throttle","Throttle"]],
nav_lights : [["Red (Left Wing)","RedLight"],["Green (Right Wing)","GreenLight"],["White (Tail)","WhiteLight"]],
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"]]
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 : [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
serial : 9600
}
}
//set default profile to arduino standard-compatible board
......
......@@ -22,17 +22,12 @@
* @fileoverview Helper functions for generating Arduino blocks.
* Originally developed by the author below and modified for Ardublockly
* @author gasolin@gmail.com (Fred Lin)
* The Arduino built in functions syntax can be found in http://arduino.cc/en/Reference/HomePage
*/
'use strict';
//To support syntax defined in http://arduino.cc/en/Reference/HomePage
goog.provide('Blockly.Arduino.loops');
goog.require('Blockly.Arduino');
Blockly.Language.base_map = {
Blockly.Blocks['base_map'] = {
category: 'Math',
helpUrl: 'http://arduino.cc/en/Reference/map',
init: function() {
......@@ -44,60 +39,24 @@ Blockly.Language.base_map = {
.appendTitle("value to [0-")
.setCheck(Number);
this.appendDummyInput("")
.appendTitle("]");
.appendTitle("]");
this.setInputsInline(true);
this.setOutput(true);
this.setTooltip('Re-maps a number from [0-1024] to another.');
}
};
//servo block
//http://www.seeedstudio.com/depot/emax-9g-es08a-high-sensitive-mini-servo-p-760.html?cPath=170_171
Blockly.Language.servo_move = {
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.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.Arduino['base_map'] = function(block) {
var value_num = Blockly.Arduino.valueToCode(block, 'NUM', Blockly.Arduino.ORDER_NONE);
var value_dmax = Blockly.Arduino.valueToCode(block, 'DMAX', Blockly.Arduino.ORDER_ATOMIC);
var code = 'map('+value_num+', 0, 1024, 0, '+value_dmax+')';
return [code, Blockly.Arduino.ORDER_NONE];
};
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',
init: function() {
this.setColour(230);
......@@ -109,64 +68,8 @@ Blockly.Language.serial_print = {
}
};
// define generators
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['serial_print'] = function(block) {
var content = Blockly.Arduino.valueToCode(block, 'CONTENT', Blockly.Arduino.ORDER_ATOMIC) || '0'
Blockly.Arduino.setups_['setup_serial_'+profile.default.serial] = 'Serial.begin('+profile.default.serial+');\n';
......
......@@ -9,15 +9,14 @@
* Description
*/
Blockly.Blocks['io_digitalwrite'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/DigitalWrite',
init: function() {
this.setColour(230);
this.appendDummyInput("")
.appendTitle("DigitalWrite PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.digital), "PIN")
.appendTitle("Stat")
.appendTitle(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
.appendField("DigitalWrite PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN")
.appendField("Stat")
.appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Write digital value to a specific Port');
......@@ -36,13 +35,12 @@ Blockly.Arduino['io_digitalwrite'] = function(block) {
* Description
*/
Blockly.Blocks['io_digitalread'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/DigitalRead',
init: function() {
this.setColour(230);
this.appendDummyInput("")
.appendTitle("DigitalRead PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.digital), "PIN");
.appendField("DigitalRead PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN");
this.setOutput(true, Boolean);
this.setTooltip('');
}
......@@ -60,13 +58,12 @@ Blockly.Arduino['io_digitalread'] = function(block) {
* Description
*/
Blockly.Blocks['io_builtin_led'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/DigitalWrite',
init: function() {
this.setColour(190);
this.appendDummyInput("")
.appendTitle("Build-in LED Stat")
.appendTitle(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
.appendField("Build-in LED Stat")
.appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip('Turn on or off the built in LED');
......@@ -84,15 +81,14 @@ Blockly.Arduino['io_builtin_led'] = function(block) {
* Description
*/
Blockly.Blocks['io_analogwrite'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/AnalogWrite',
init: function() {
this.setColour(230);
this.appendDummyInput("")
.appendTitle("AnalogWrite PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.analog), "PIN");
.appendField("AnalogWrite PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.analog), "PIN");
this.appendValueInput("NUM", Number)
.appendTitle("value")
.appendField("value")
.setCheck(Number);
this.setInputsInline(true);
this.setPreviousStatement(true, null);
......@@ -114,13 +110,12 @@ Blockly.Arduino['io_analogwrite'] = function(block) {
* Description
*/
Blockly.Blocks['io_analogread'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/AnalogRead',
init: function() {
this.setColour(230);
this.appendDummyInput("")
.appendTitle("AnalogRead PIN#")
.appendTitle(new Blockly.FieldDropdown(profile.default.analog), "PIN");
.appendField("AnalogRead PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.analog), "PIN");
this.setOutput(true, Number);
this.setTooltip('Return value between 0 and 1024');
}
......@@ -137,14 +132,13 @@ Blockly.Arduino['io_analogread'] = function(block) {
* Description
*/
Blockly.Blocks['io_highlow'] = {
category: 'Input/Output',
helpUrl: 'http://arduino.cc/en/Reference/Constants',
init: function() {
this.setColour(230);
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.setTooltip(Blockly.LANG_LOGIC_BOOLEAN_TOOLTIP_1);
this.setTooltip("Set a pin state logic High or Low");
}
};
......
......@@ -4,23 +4,22 @@
*/
'use strict';
//TODO: When confirm working, replicate for millis(), micros(), and delayMicroseconds()
/**
* Delay block definition
*/
Blockly.Blocks['time_delay'] = {
category: 'Time',
helpUrl: 'http://arduino.cc/en/Reference/delay',
helpUrl: 'http://arduino.cc/en/Reference/Delay',
init: function() {
this.setColour(120);
this.appendValueInput("DELAY_TIME", Number)
.appendTitle("Delay")
.setCheck(Number);
.appendField("Delay")
.setCheck(Number)
.appendField("Milliseconds");
this.setInputsInline(true);
this.setPreviousStatement(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'] = {
* Delay Arduino code generator
*/
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';
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