Commit b26b7884 authored by carlosperate's avatar carlosperate

Fix analogWrite to list only PWM pins and warn if input is out of range.

Fixes #30
parent bd5bfff6
...@@ -124,7 +124,7 @@ Blockly.Blocks['io_analogwrite'] = { ...@@ -124,7 +124,7 @@ Blockly.Blocks['io_analogwrite'] = {
this.appendDummyInput('') this.appendDummyInput('')
.appendField('set analogue pin#') .appendField('set analogue pin#')
.appendField(new Blockly.FieldDropdown( .appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.analogPins), 'PIN'); Blockly.Arduino.Boards.selected.pwmPins), 'PIN');
this.appendValueInput('NUM', Blockly.StaticTyping.BlocklyType.NUMBER) this.appendValueInput('NUM', Blockly.StaticTyping.BlocklyType.NUMBER)
.appendField('to') .appendField('to')
.setCheck(Blockly.StaticTyping.BlocklyType.NUMBER); .setCheck(Blockly.StaticTyping.BlocklyType.NUMBER);
...@@ -138,7 +138,7 @@ Blockly.Blocks['io_analogwrite'] = { ...@@ -138,7 +138,7 @@ Blockly.Blocks['io_analogwrite'] = {
* @this Blockly.Block * @this Blockly.Block
*/ */
updateFields: function() { updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(this, 'PIN', 'analogPins'); Blockly.Arduino.Boards.refreshBlockFieldDropdown(this, 'PIN', 'pwmPins');
}, },
/** @return {!string} The type of input value for the block, an integer. */ /** @return {!string} The type of input value for the block, an integer. */
getBlockType: function() { getBlockType: function() {
......
...@@ -94,6 +94,14 @@ Blockly.Arduino['io_analogwrite'] = function(block) { ...@@ -94,6 +94,14 @@ Blockly.Arduino['io_analogwrite'] = function(block) {
var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);'; var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false); Blockly.Arduino.addSetup('io_' + pin, pinSetupCode, false);
// Warn if the input value is out of range
if ( (stateOutput < 0) || (stateOutput > 255)) {
block.setWarningText('The analgue value must be between 0 and 255',
'pwm_value');
} else {
block.setWarningText(null, 'pwm_value');
}
var code = 'analogWrite(' + pin + ',' + stateOutput + ');\n'; var code = 'analogWrite(' + pin + ',' + stateOutput + ');\n';
return code; return code;
}; };
......
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