Commit a867bf01 authored by Carlos's avatar Carlos Committed by carlospamg

Improved IO block generator:

It now checks if the pin has been used by a previous block to be in a different configuration (Input, output, pwm, etc) and if it has, it alerts the user using a warning.
This was implemented, instead of temporarily changing the pin status because and output can not keep state as an input, and an input cannot be inlined into a line of code if the pinMode has to be changed before and after.
parent 194183e0
......@@ -71,9 +71,12 @@ var profile = {
arduino: {
description: "Arduino Uno standard-compatible board",
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"]],
analog : [["A0", "A0"], ["A1", "A1"], ["A2", "A2"], ["A3", "A3"], ["A4", "A4"], ["A5", "A5"]],
pwm : [["3", "3"], ["5", "5"], ["6", "6"], ["9", "9"], ["10", "10"], ["11", "11"]],
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"]],
builtin_led: [["BUILTIN_1", "13"]],
pin_types: { INPUT: "INPUT", OUTPUT: "OUTPUT", PWM: "PWM" },
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:{
......@@ -105,6 +108,8 @@ Blockly.Arduino.init = function() {
Blockly.Arduino.definitions_ = Object.create(null);
// Create a dictionary of setups to be printed before the code.
Blockly.Arduino.setups_ = Object.create(null);
// Create a dictionary of pins to check if their use conflict
Blockly.Arduino.pins_ = Object.create(null);
if (Blockly.Variables) {
if (!Blockly.Arduino.variableDB_) {
......
......@@ -4,6 +4,10 @@
*/
'use strict';
goog.provide('Blockly.Arduino.IO');
goog.require('Blockly.Arduino');
/**
* Description
......@@ -12,11 +16,11 @@ Blockly.Blocks['io_digitalwrite'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/DigitalWrite');
this.setColour(230);
this.appendDummyInput("")
.appendField("DigitalWrite PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN")
this.appendDummyInput('')
.appendField('DigitalWrite PIN#')
.appendField(new Blockly.FieldDropdown(profile.default.digital), 'PIN')
.appendField("Stat")
.appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "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');
......@@ -24,10 +28,27 @@ Blockly.Blocks['io_digitalwrite'] = {
};
Blockly.Arduino['io_digitalwrite'] = function(block) {
var dropdown_pin = block.getFieldValue('PIN');
var dropdown_stat = block.getFieldValue('STAT');
Blockly.Arduino.setups_['setup_output_'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
var code = 'digitalWrite('+dropdown_pin+','+dropdown_stat+');\n'
var pin_key = block.getFieldValue('PIN');
var pin_type = profile.default.pin_types.OUTPUT;
var dropdown_state = block.getFieldValue('STAT');
var set_up_key = 'setup_io_' + pin_key;
var pin_mode = 'pinMode(' + pin_key + ', OUTPUT);';
var code = 'digitalWrite(' + pin_key + ',' + dropdown_state + ');\n'
// If the IO has been configured already temporarily change it and restore it
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;
Blockly.Arduino.setups_[set_up_key] = pin_mode;
block.setWarningText(null);
}
return code;
};
......@@ -38,18 +59,35 @@ Blockly.Blocks['io_digitalread'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/DigitalRead');
this.setColour(230);
this.appendDummyInput("")
.appendField("DigitalRead PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.digital), "PIN");
this.appendDummyInput('')
.appendField('DigitalRead PIN#')
.appendField(new Blockly.FieldDropdown(profile.default.digital), 'PIN');
this.setOutput(true, 'Boolean');
this.setTooltip('');
}
};
Blockly.Arduino['io_digitalread'] = function(block) {
var dropdown_pin = block.getFieldValue('PIN');
Blockly.Arduino.setups_['setup_input_'+dropdown_pin] = 'pinMode('+dropdown_pin+', INPUT);';
var code = 'digitalRead('+dropdown_pin+')';
var pin_key = block.getFieldValue('PIN');
var pin_type = profile.default.pin_types.INPUT;
var set_up_key = 'setup_io_' + pin_key;
var pin_mode = 'pinMode(' + pin_key + ', INPUT);';
var code = 'digitalRead(' + pin_key + ')';
// If the IO has been configured already temporarily change it and restore it
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;
Blockly.Arduino.setups_[set_up_key] = pin_mode;
block.setWarningText(null);
}
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
......@@ -61,9 +99,11 @@ Blockly.Blocks['io_builtin_led'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/DigitalWrite');
this.setColour(190);
this.appendDummyInput("")
.appendField("Build-in LED Stat")
.appendField(new Blockly.FieldDropdown([["HIGH", "HIGH"], ["LOW", "LOW"]]), "STAT");
this.appendDummyInput('')
.appendField('Set LED')
.appendField(new Blockly.FieldDropdown(profile.default.builtin_led), 'BUILT_IN_LED')
.appendField("to")
.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');
......@@ -71,9 +111,27 @@ Blockly.Blocks['io_builtin_led'] = {
};
Blockly.Arduino['io_builtin_led'] = function(block) {
var pin_key = block.getFieldValue('BUILT_IN_LED');
var pin_type = profile.default.pin_types.OUTPUT;
var dropdown_stat = block.getFieldValue('STAT');
Blockly.Arduino.setups_['setup_output_LED_BUILTIN'] = 'pinMode(LED_BUILTIN, OUTPUT);';
var code = 'digitalWrite(LED_BUILTIN,'+dropdown_stat+');\n'
var set_up_key = 'setup_io_' + pin_key;
var pin_mode = 'pinMode(' + pin_key + ', ' + pin_type + ');';
var code = 'digitalWrite(' + pin_key + ',' + dropdown_stat + ');\n'
// If the IO has been configured already temporarily change it and restore it
if (pin_key in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pin_key] != pin_type) {
block.setWarningText('Pin ' + pin_key + ' 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;
Blockly.Arduino.setups_[set_up_key] = pin_mode;
block.setWarningText(null);
}
return code;
};
......@@ -84,11 +142,11 @@ Blockly.Blocks['io_analogwrite'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/AnalogWrite');
this.setColour(230);
this.appendDummyInput("")
.appendField("AnalogWrite PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.analog), "PIN");
this.appendValueInput("NUM", 'Number')
.appendField("value")
this.appendDummyInput('')
.appendField('AnalogWrite PIN#')
.appendField(new Blockly.FieldDropdown(profile.default.analog), 'PIN');
this.appendValueInput('NUM', 'Number')
.appendField('value')
.setCheck('Number');
this.setInputsInline(true);
this.setPreviousStatement(true, null);
......@@ -98,11 +156,27 @@ Blockly.Blocks['io_analogwrite'] = {
};
Blockly.Arduino['io_analogwrite'] = function(block) {
var dropdown_pin = block.getFieldValue('PIN');
//var dropdown_stat = this.getTitleValue('STAT');
var value_num = Blockly.Arduino.valueToCode(block, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
//Blockly.Arduino.setups_['setup_output'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
var code = 'analogWrite('+dropdown_pin+','+value_num+');\n';
var pin_key = block.getFieldValue('PIN');
var pin_type = profile.default.pin_types.OUTPUT;
var value_num = Blockly.Arduino.valueToCode(block, 'NUM', Blockly.Arduino.ORDER_ATOMIC) || '0';
var set_up_key = 'setup_io_' + pin_key;
var pin_mode = 'pinMode(' + pin_key + ', OUTPUT);';
var code = 'analogWrite(' + pin_key + ',' + value_num + ');\n'
// If the IO has been configured already temporarily change it and restore it
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;
Blockly.Arduino.setups_[set_up_key] = pin_mode;
block.setWarningText(null);
}
return code;
};
......@@ -113,18 +187,35 @@ Blockly.Blocks['io_analogread'] = {
init: function() {
this.setHelpUrl('http://arduino.cc/en/Reference/AnalogRead');
this.setColour(230);
this.appendDummyInput("")
.appendField("AnalogRead PIN#")
.appendField(new Blockly.FieldDropdown(profile.default.analog), "PIN");
this.appendDummyInput('')
.appendField('AnalogRead PIN#')
.appendField(new Blockly.FieldDropdown(profile.default.analog), 'PIN');
this.setOutput(true, 'Number');
this.setTooltip('Return value between 0 and 1024');
}
};
Blockly.Arduino['io_analogread'] = function(block) {
var dropdown_pin = block.getFieldValue('PIN');
//Blockly.Arduino.setups_['setup_input_'+dropdown_pin] = 'pinMode('+dropdown_pin+', INPUT);';
var code = 'analogRead('+dropdown_pin+')';
var pin_key = block.getFieldValue('PIN');
var pin_type = profile.default.pin_types.INPUT;
var set_up_key = 'setup_io_' + pin_key;
var pin_mode = 'pinMode(' + pin_key + ', ' + pin_type + ');';
var code = 'analogRead(' + pin_key + ')';
// If the IO has been configured already temporarily change it and restore it
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;
Blockly.Arduino.setups_[set_up_key] = pin_mode;
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