Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable pins #70

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions blockly/blocks/arduino/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ goog.require('Blockly.Types');
/** Common HSV hue for all blocks in this category. */
Blockly.Blocks.io.HUE = 250;

function dynamicDigPins() {
var options = [];
var pins = Blockly.Arduino.Boards.selected.digitalPins;
// now we add variables of type DIGPIN
// how to obtain the workspace here ?? Hack it for now...
var workspace = Blockly.getMainWorkspace();
var typedvar = Blockly.StaticTypingArduino.typedVariables(workspace, Blockly.Types.DIGPIN);
for (var ind in typedvar) {
options.push([typedvar[ind], typedvar[ind]]);
}
return options.concat(pins);
}

Blockly.Blocks['io_digitalwrite'] = {
/**
* Block for creating a 'set pin' to a state.
Expand All @@ -30,8 +43,7 @@ Blockly.Blocks['io_digitalwrite'] = {
this.setColour(Blockly.Blocks.io.HUE);
this.appendValueInput('STATE')
.appendField(Blockly.Msg.ARD_DIGITALWRITE)
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'PIN')
.appendField(new Blockly.FieldDropdown(dynamicDigPins), 'PIN')
.appendField(Blockly.Msg.ARD_WRITE_TO)
.setCheck(Blockly.Types.BOOLEAN.checkList);
this.setInputsInline(false);
Expand Down Expand Up @@ -59,8 +71,7 @@ Blockly.Blocks['io_digitalread'] = {
this.setColour(Blockly.Blocks.io.HUE);
this.appendDummyInput()
.appendField(Blockly.Msg.ARD_DIGITALREAD)
.appendField(new Blockly.FieldDropdown(
Blockly.Arduino.Boards.selected.digitalPins), 'PIN');
.appendField(new Blockly.FieldDropdown(dynamicDigPins), 'PIN');
this.setOutput(true, Blockly.Types.BOOLEAN.output);
this.setTooltip(Blockly.Msg.ARD_DIGITALREAD_TIP);
},
Expand Down Expand Up @@ -241,3 +252,75 @@ Blockly.Blocks['io_pulsetimeout'] = {
return Blockly.Types.NUMBER;
}
};

Blockly.Blocks['io_pin_dig'] = {
init: function() {
this.appendDummyInput()
.appendField(Blockly.Msg.ARD_PIN_DIG)
.appendField(new Blockly.FieldDropdown(Blockly.Arduino.Boards.selected.digitalPins), "PIN");
this.setOutput(true);
this.setColour(Blockly.Blocks.io.HUE);
this.setTooltip(Blockly.Msg.ARD_PIN_DIG_TIP);
this.setHelpUrl('https://www.arduino.cc/en/Reference/Board');
},
/** @return {!string} The type of return value for the block, a digital pin. */
getBlockType: function() {
return Blockly.Types.DIGPIN;
},
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'PIN', 'digitalPins');
}
};

Blockly.Blocks['io_pin_an'] = {
init: function() {
this.appendDummyInput()
.appendField(Blockly.Msg.ARD_PIN_AN)
.appendField(new Blockly.FieldDropdown(Blockly.Arduino.Boards.selected.analogPins), "PIN");
this.setOutput(true);
this.setColour(Blockly.Blocks.io.HUE);
this.setTooltip(Blockly.Msg.ARD_PIN_AN_TIP);
this.setHelpUrl('https://www.arduino.cc/en/Reference/Board');
},
/** @return {!string} The type of return value for the block, an analog pin */
getBlockType: function() {
return Blockly.Types.ANAPIN;
},
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'PIN', 'analogPins');
}
};

Blockly.Blocks['io_pin_pwm'] = {
init: function() {
this.appendDummyInput()
.appendField(Blockly.Msg.ARD_PIN_PWM)
.appendField(new Blockly.FieldDropdown(Blockly.Arduino.Boards.selected.pwmPins), "PIN");
this.setOutput(true);
this.setColour(Blockly.Blocks.io.HUE);
this.setTooltip(Blockly.Msg.ARD_PIN_PWM_TIP);
this.setHelpUrl('https://www.arduino.cc/en/Reference/Board');
},
/** @return {!string} The type of return value for the block, a PWM pin. */
getBlockType: function() {
return Blockly.Types.PWMPIN;
},
/**
* Updates the content of the the pin related fields.
* @this Blockly.Block
*/
updateFields: function() {
Blockly.Arduino.Boards.refreshBlockFieldDropdown(
this, 'PIN', 'pwmPins');
}
};
29 changes: 29 additions & 0 deletions blockly/core/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({
compatibleTypes: []
});

/** Pin Type: digital pin
Can be used for digitalRead, digitalWrite
*/
Blockly.Types.DIGPIN = new Blockly.Type({
typeId: 'DIGPIN',
typeName: function() {return Blockly.Msg.ARD_TYPE_DIGPIN;},
compatibleTypes: []
});

/** Pin Type: analog pin
Can be used for analogRead
*/
Blockly.Types.ANAPIN = new Blockly.Type({
typeId: 'ANAPIN',
typeName: function() {return Blockly.Msg.ARD_TYPE_ANAPIN;},
compatibleTypes: []
});

/** Pin Type: PWM pin
Can be used for analogWrite on the digital PWM pins
*/
Blockly.Types.PWMPIN = new Blockly.Type({
typeId: 'PWMPIN',
typeName: function() {return Blockly.Msg.ARD_TYPE_PWMPIN;},
compatibleTypes: []
});

/**
* Some Types have circular dependencies on their compatibilities, so add them
* after declaration.
Expand Down Expand Up @@ -146,6 +173,8 @@ Blockly.Types.getValidTypeArray = function() {
for (var typeKey in Blockly.Types) {
if ((typeKey !== 'UNDEF') && (typeKey !== 'CHILD_BLOCK_MISSING') &&
(typeKey !== 'NULL') && (typeKey !== 'ARRAY') &&
(typeKey !== 'DIGPIN') && (typeKey !== 'ANAPIN') &&
(typeKey !== 'PWMPIN') &&
(typeof Blockly.Types[typeKey] !== 'function') &&
!(Blockly.Types[typeKey] instanceof RegExp)) {
typesArray.push([Blockly.Types[typeKey].typeName(), typeKey]);
Expand Down
64 changes: 61 additions & 3 deletions blockly/generators/arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

goog.provide('Blockly.Arduino');
goog.provide('Blockly.StaticTypingArduino');

goog.require('Blockly.Generator');
goog.require('Blockly.StaticTyping');
Expand All @@ -21,7 +22,55 @@ goog.require('Blockly.StaticTyping');
* @type {!Blockly.Generator}
*/
Blockly.Arduino = new Blockly.Generator('Arduino');
Blockly.Arduino.StaticTyping = new Blockly.StaticTyping();
Blockly.StaticTypingArduino = new Blockly.StaticTyping();

/**
* Find all user-created variables of a given type.
* @param {!Blockly.Block|!Blockly.Workspace} root Root block or workspace.
* @param {!Blockly.Type} type of the required variables.
* @return {!Array.<string>} Array of variable names.
*/
Blockly.StaticTypingArduino.typedVariables = function(root, type) {
var blocks;

if (root.getDescendants) {
// Root is Block.
blocks = root.getDescendants();
} else if (root.getAllBlocks) {
// Root is Workspace.
blocks = root.getAllBlocks();
} else {
throw 'Not Block or Workspace: ' + root;
}

// Iterate through to capture all blocks types and set the function arguments
if (root) {
var varsWithTypes = Blockly.StaticTypingArduino.collectVarsWithTypes(root);
} else {
var variableList = [];
}

var variableHash = Object.create(null);
// Iterate through every block and add each variable to the hash.
for (var x = 0; x < blocks.length; x++) {
if (blocks[x].getVars) {
var blockVariables = blocks[x].getVars();
for (var y = 0; y < blockVariables.length; y++) {
var varName = blockVariables[y];
// Variable name may be null if the block is only half-built.
if (varName && (varsWithTypes[varName] === type)) {
variableHash[varName.toLowerCase()] = varName;
}
}
}
}
// Flatten the hash into a list.
var variableList = [];
for (var name in variableHash) {
variableList.push(variableHash[name]);
}
return variableList;
};

/**
* List of illegal variable names.
Expand Down Expand Up @@ -115,8 +164,8 @@ Blockly.Arduino.init = function(workspace) {
}

// Iterate through to capture all blocks types and set the function arguments
var varsWithTypes = Blockly.Arduino.StaticTyping.collectVarsWithTypes(workspace);
Blockly.Arduino.StaticTyping.setProcedureArgs(workspace, varsWithTypes);
var varsWithTypes = Blockly.StaticTypingArduino.collectVarsWithTypes(workspace);
Blockly.StaticTypingArduino.setProcedureArgs(workspace, varsWithTypes);

// Set variable declarations with their Arduino type in the defines dictionary
for (var varName in varsWithTypes) {
Expand Down Expand Up @@ -391,6 +440,15 @@ Blockly.Arduino.getArduinoType_ = function(typeBlockly) {
// If no block connected default to int, change for easier debugging
//return 'ChildBlockMissing';
return 'int';
case Blockly.Types.DIGPIN.typeId:
// a pin type
return 'int';
case Blockly.Types.ANAPIN.typeId:
// a pin type
return 'int';
case Blockly.Types.PWMPIN.typeId:
// a pin type
return 'int';
default:
return 'Invalid Blockly Type';
}
Expand Down
36 changes: 36 additions & 0 deletions blockly/generators/arduino/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,39 @@ Blockly.Arduino['io_pulsetimeout'] = function(block) {

return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/**
* Function for assigning a variable a specific pin value.
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['io_pin_dig'] = function(block) {
var pin = block.getFieldValue('PIN');

var code = pin;
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/**
* Function for assigning a variable a specific pin value.
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['io_pin_an'] = function(block) {
var pin = block.getFieldValue('PIN');

var code = pin;
return [code, Blockly.Arduino.ORDER_ATOMIC];
};

/**
* Function for assigning a variable a specific pin value.
* @param {!Blockly.Block} block Block to generate the code from.
* @return {string} Completed code.
*/
Blockly.Arduino['io_pin_pwm'] = function(block) {
var pin = block.getFieldValue('PIN');

var code = pin;
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
9 changes: 9 additions & 0 deletions blockly/msg/json/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,17 @@
"ARD_TYPE_NULL": "Geen Type",
"ARD_TYPE_UNDEF": "Ongedefineerd",
"ARD_TYPE_CHILDBLOCKMISSING": "ChildBlockMissing",
"ARD_TYPE_DIGPIN": "Digitale Pin",
"ARD_TYPE_ANAPIN": "Analoge Pin",
"ARD_TYPE_PWMPIN": "PWM Pin",
"ARD_HIGH": "HOOG",
"ARD_LOW": "LAAG",
"ARD_PIN_AN": "analoge pin",
"ARD_PIN_DIG": "digitale pin",
"ARD_PIN_PWM": "PWM pin",
"ARD_PIN_AN_TIP": "Een van de analoge pinnen van de Arduino",
"ARD_PIN_DIG_TIP": "Een van de digitale pinnen van de Arduino",
"ARD_PIN_PWM_TIP": "Een van de Puls Breedte Modulatie (PWM) pinnen van de Arduino",
"ARD_ANALOGREAD": "lees analoge pin#",
"ARD_ANALOGREAD_TIP": "Geeft waarde terug tussen 0 and 1024 zoals gelezen op de analoge PIN",
"ARD_ANALOGWRITE": "schrijf naar analoge pin#",
Expand Down
9 changes: 9 additions & 0 deletions blockly/msg/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,17 @@ Blockly.Msg.ARD_TYPE_ARRAY = 'Array';
Blockly.Msg.ARD_TYPE_NULL = 'Null';
Blockly.Msg.ARD_TYPE_UNDEF = 'Undefined';
Blockly.Msg.ARD_TYPE_CHILDBLOCKMISSING = 'ChildBlockMissing';
Blockly.Msg.ARD_TYPE_DIGPIN = 'Digital Pin';
Blockly.Msg.ARD_TYPE_ANAPIN = 'Analog Pin';
Blockly.Msg.ARD_TYPE_PWMPIN = 'PWM Pin';
Blockly.Msg.ARD_HIGH = 'HIGH';
Blockly.Msg.ARD_LOW = 'LOW';
Blockly.Msg.ARD_PIN_AN = 'analog pin';
Blockly.Msg.ARD_PIN_DIG = 'digital pin';
Blockly.Msg.ARD_PIN_PWM = 'PWM pin';
Blockly.Msg.ARD_PIN_AN_TIP = 'One of the analog pins of the Arduino Board';
Blockly.Msg.ARD_PIN_DIG_TIP = 'One of the digital pins of the Arduino Board';
Blockly.Msg.ARD_PIN_PWM_TIP = 'One of the Pulse Width Modeling (PWM) pins of the Arduino Board';
Blockly.Msg.ARD_ANALOGREAD = 'read analog pin#';
Blockly.Msg.ARD_ANALOGREAD_TIP = 'Return value between 0 and 1024';
Blockly.Msg.ARD_ANALOGWRITE = 'set analog pin#';
Expand Down