Commit ab0cbe56 authored by carlosperate's avatar carlosperate

Update IO block with new generator functions & slightly different colour.

parent eb625d19
......@@ -12,8 +12,8 @@ goog.provide('Blockly.Blocks.Arduino.io');
goog.require('Blockly.Arduino');
Blockly.Blocks.Arduino.io.HUE = 230;
/** Common HSV hue for all blocks in this category. */
Blockly.Blocks.Arduino.io.HUE = 250;
Blockly.Blocks['io_digitalwrite'] = {
/**
......@@ -26,7 +26,7 @@ Blockly.Blocks['io_digitalwrite'] = {
this.appendDummyInput('')
.appendField('set digital pin#')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'PIN')
Blockly.Arduino.Boards.selected.digitalPins), 'PIN');
this.appendValueInput('STATE', Blockly.StaticTyping.blocklyType.BOOLEAN)
.appendField('to')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN);
......@@ -39,7 +39,10 @@ Blockly.Blocks['io_digitalwrite'] = {
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
},
/** Updates the content of the the pin related fields. */
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'PIN', 'digitalPins');
......@@ -61,11 +64,14 @@ Blockly.Blocks['io_digitalread'] = {
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setTooltip('Reads the digital value of a pin.');
},
/** Retrieves the type of return value for the block, an integer. */
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
},
/** Updates the content of the the pin related fields. */
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'PIN', 'digitalPins');
......@@ -83,7 +89,7 @@ Blockly.Blocks['io_builtin_led'] = {
this.appendDummyInput('')
.appendField('set LED')
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.builtinLed), 'BUILT_IN_LED')
Blockly.Arduino.Boards.selected.builtinLed), 'BUILT_IN_LED');
this.appendValueInput('STATE', Blockly.StaticTyping.blocklyType.BOOLEAN)
.appendField('to')
.setCheck(Blockly.StaticTyping.blocklyType.BOOLEAN);
......@@ -92,13 +98,14 @@ Blockly.Blocks['io_builtin_led'] = {
this.setNextStatement(true, null);
this.setTooltip('Turn on or off the built in LED.');
},
/**
* Retrieves the type of return value for the block, in this case an integer.
*/
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
},
/** Updates the content of the the pin related fields. */
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'BUILT_IN_LED', 'builtinLed');
......@@ -125,7 +132,10 @@ Blockly.Blocks['io_analogwrite'] = {
this.setNextStatement(true, null);
this.setTooltip('Write analog value between 0 and 255 to a specific Port.');
},
/** Updates the content of the the pin related fields. */
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(this, 'PIN', 'analogPins');
}
......@@ -146,13 +156,14 @@ Blockly.Blocks['io_analogread'] = {
this.setOutput(true, Blockly.StaticTyping.blocklyType.NUMBER);
this.setTooltip('Return value between 0 and 1024.');
},
/**
* Retrieves the type of return value for the block, in this case an integer.
*/
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
},
/** Updates the content of the the pin related fields. */
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(this, 'PIN', 'analogPins');
}
......@@ -169,13 +180,11 @@ Blockly.Blocks['io_highlow'] = {
this.appendDummyInput('')
.appendField(
new Blockly.FieldDropdown([['HIGH', 'HIGH'], ['LOW', 'LOW']]),
'STATE')
'STATE');
this.setOutput(true, Blockly.StaticTyping.blocklyType.BOOLEAN);
this.setTooltip('Set a pin state logic High or Low.');
},
/**
* Retrieves the type of return value for the block, in this case a boolean.
*/
/** @return {!string} The type of return value for the block, an integer. */
getType: function() {
return Blockly.StaticTyping.blocklyType.INTEGER;
}
......
......@@ -21,30 +21,17 @@ goog.require('Blockly.Arduino');
* @return {string} Completed code.
*/
Blockly.Arduino['io_digitalwrite'] = function(block) {
var pinKey = block.getFieldValue('PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.OUTPUT;
var stateInput = Blockly.Arduino.valueToCode(
var pin = block.getFieldValue('PIN');
var stateOutput = Blockly.Arduino.valueToCode(
block, 'STATE', Blockly.Arduino.ORDER_ATOMIC) || '0';
var setUpKey = 'setup_io_' + pinKey;
var pinMode = 'pinMode(' + pinKey + ', OUTPUT);';
var code = 'digitalWrite(' + pinKey + ',' + stateInput + ');\n'
// If the IO has been configured already temporarily change it and restore it
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
Blockly.Arduino.setups_[setUpKey] = pinMode;
block.setWarningText(null);
}
Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'Digital Write');
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
var code = 'digitalWrite(' + pin + ',' + stateOutput + ');\n';
return code;
};
......@@ -56,28 +43,14 @@ Blockly.Arduino['io_digitalwrite'] = function(block) {
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['io_digitalread'] = function(block) {
var pinKey = block.getFieldValue('PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.INPUT;
var setUpKey = 'setup_io_' + pinKey;
var pinMode = 'pinMode(' + pinKey + ', INPUT);';
var code = 'digitalRead(' + pinKey + ')';
// If the IO has been configured already temporarily change it and restore it
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
Blockly.Arduino.setups_[setUpKey] = pinMode;
block.setWarningText(null);
}
var pin = block.getFieldValue('PIN');
Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.INPUT, 'Digital Read');
var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
var code = 'digitalRead(' + pin + ')';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
......@@ -89,31 +62,17 @@ Blockly.Arduino['io_digitalread'] = function(block) {
* @return {string} Completed code.
*/
Blockly.Arduino['io_builtin_led'] = function(block) {
var pinKey = block.getFieldValue('BUILT_IN_LED');
var pinType = Blockly.Arduino.Boards.pinTypes.OUTPUT;
var stateInput = Blockly.Arduino.valueToCode(
var pin = block.getFieldValue('BUILT_IN_LED');
var stateOutput = Blockly.Arduino.valueToCode(
block, 'STATE', Blockly.Arduino.ORDER_ATOMIC) || '0';
var setUpKey = 'setup_io_' + pinKey;
var pinMode = 'pinMode(' + pinKey + ', ' + pinType + ');';
var code = 'digitalWrite(' + pinKey + ',' + stateInput + ');\n'
// If the IO has been configured already temporarily change it and restore it
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin ' + pinKey + ' already used as ' +
Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
Blockly.Arduino.setups_[setUpKey] = pinMode;
block.setWarningText(null);
}
Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'Set LED');
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
var code = 'digitalWrite(' + pin + ',' + stateOutput + ');\n';
return code;
};
......@@ -125,30 +84,17 @@ Blockly.Arduino['io_builtin_led'] = function(block) {
* @return {string} Completed code.
*/
Blockly.Arduino['io_analogwrite'] = function(block) {
var pinKey = block.getFieldValue('PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.OUTPUT;
var value_num = Blockly.Arduino.valueToCode(
var pin = block.getFieldValue('PIN');
var stateOutput = Blockly.Arduino.valueToCode(
block, 'NUM', Blockly.Arduino.ORDER_ATOMIC) || '0';
var setUpKey = 'setup_io_' + pinKey;
var pinMode = 'pinMode(' + pinKey + ', OUTPUT);';
var code = 'analogWrite(' + pinKey + ',' + value_num + ');\n'
// If the IO has been configured already temporarily change it and restore it
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
Blockly.Arduino.setups_[setUpKey] = pinMode;
block.setWarningText(null);
}
Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.OUTPUT, 'Analogue Write');
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
var code = 'analogWrite(' + pin + ',' + stateOutput + ');\n';
return code;
};
......@@ -160,32 +106,17 @@ Blockly.Arduino['io_analogwrite'] = function(block) {
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['io_analogread'] = function(block) {
var pinKey = block.getFieldValue('PIN');
var pinType = Blockly.Arduino.Boards.pinTypes.INPUT;
var setUpKey = 'setup_io_' + pinKey;
var pinMode = 'pinMode(' + pinKey + ', ' + pinType + ');';
var code = 'analogRead(' + pinKey + ')';
// If the IO has been configured already temporarily change it and restore it
if (pinKey in Blockly.Arduino.pins_) {
if (Blockly.Arduino.pins_[pinKey] != pinType) {
block.setWarningText(
'Pin already used as ' + Blockly.Arduino.pins_[pinKey]);
} else {
block.setWarningText(null);
}
} else {
// First time this IO pin is used, so configure it
Blockly.Arduino.pins_[pinKey] = pinType;
Blockly.Arduino.setups_[setUpKey] = pinMode;
block.setWarningText(null);
}
var pin = block.getFieldValue('PIN');
Blockly.Arduino.reservePin(
block, pin, Blockly.Arduino.Boards.pinTypes.INPUT, 'Analogue Read');
var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
var code = 'analogRead(' + pin + ')';
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
/**
* Value for defining a digital pin state.
* Arduino code: loop { HIGH / LOW }
......@@ -193,7 +124,6 @@ Blockly.Arduino['io_analogread'] = function(block) {
* @return {array} Completed code with order of operation.
*/
Blockly.Arduino['io_highlow'] = function(block) {
// Boolean values HIGH and LOW.
var code = (block.getFieldValue('STATE') == 'HIGH') ? 'HIGH' : 'LOW';
var code = block.getFieldValue('STATE');
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