From a28e3d8486418db572a8c408a3c81d9f4571caee Mon Sep 17 00:00:00 2001 From: Maria-Cristina Ciocci Date: Tue, 12 Apr 2016 23:59:36 +0200 Subject: [PATCH 1/6] 3 types for the pins # Conflicts: # blockly/blocks/arduino/io_extra.js --- blockly/core/types.js | 23 +++++++++++++++++++++++ blockly/msg/messages.js | 3 +++ 2 files changed, 26 insertions(+) diff --git a/blockly/core/types.js b/blockly/core/types.js index eb52a0a568..232355e18c 100644 --- a/blockly/core/types.js +++ b/blockly/core/types.js @@ -93,6 +93,27 @@ Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({ compatibleTypes: [] }); +/** Pin Type: digital pin */ +Blockly.Types.DIGPIN = new Blockly.Type({ + typeId: 'DIGPIN', + typeName: function() {return Blockly.Msg.ARD_TYPE_DIGPIN;}, + compatibleTypes: [] +}); + +/** Pin Type: analog pin */ +Blockly.Types.ANAPIN = new Blockly.Type({ + typeId: 'ANAPIN', + typeName: function() {return Blockly.Msg.ARD_TYPE_ANAPIN;}, + compatibleTypes: [] +}); + +/** Pin Type: PWM pin */ +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. @@ -146,6 +167,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]); diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js index 244587d955..2c5b8f18c1 100644 --- a/blockly/msg/messages.js +++ b/blockly/msg/messages.js @@ -1111,6 +1111,9 @@ 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_ANALOGREAD = 'read analog pin#'; From 2dd51877c7f31960e4cc360e1f73b21d9882ffab Mon Sep 17 00:00:00 2001 From: Maria-Cristina Ciocci Date: Wed, 13 Apr 2016 00:01:19 +0200 Subject: [PATCH 2/6] Dutch translation of pins --- blockly/msg/json/nl.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json index fb69924f19..ec0ea5eba9 100644 --- a/blockly/msg/json/nl.json +++ b/blockly/msg/json/nl.json @@ -366,6 +366,9 @@ "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_ANALOGREAD": "lees analoge pin#", From 2ba991623f9d4d80ad348ec4c3a161520d703b36 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Wed, 13 Apr 2016 12:41:50 +0200 Subject: [PATCH 3/6] test with adding variable to dropdown of digitalread and digitalwrite --- blockly/blocks/arduino/io.js | 19 ++++++++--- blockly/core/types.js | 12 +++++-- blockly/generators/arduino.js | 64 +++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/blockly/blocks/arduino/io.js b/blockly/blocks/arduino/io.js index bd144ba9ef..d000da8a73 100644 --- a/blockly/blocks/arduino/io.js +++ b/blockly/blocks/arduino/io.js @@ -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. @@ -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); @@ -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); }, diff --git a/blockly/core/types.js b/blockly/core/types.js index 232355e18c..642c26c060 100644 --- a/blockly/core/types.js +++ b/blockly/core/types.js @@ -93,21 +93,27 @@ Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({ compatibleTypes: [] }); -/** Pin Type: digital pin */ +/** 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 */ +/** 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 */ +/** 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;}, diff --git a/blockly/generators/arduino.js b/blockly/generators/arduino.js index 12cc5c720d..2bc3f17e60 100644 --- a/blockly/generators/arduino.js +++ b/blockly/generators/arduino.js @@ -11,6 +11,7 @@ 'use strict'; goog.provide('Blockly.Arduino'); +goog.provide('Blockly.StaticTypingArduino'); goog.require('Blockly.Generator'); goog.require('Blockly.StaticTyping'); @@ -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.} 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. @@ -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) { @@ -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.PWNPIN.typeId: + // a pin type + return 'int'; default: return 'Invalid Blockly Type'; } From ef8c5f70bddee97e15e4b97de029737f26957a83 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Wed, 13 Apr 2016 13:00:32 +0200 Subject: [PATCH 4/6] bugfix typo --- blockly/generators/arduino.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockly/generators/arduino.js b/blockly/generators/arduino.js index 2bc3f17e60..4508b8fd2a 100644 --- a/blockly/generators/arduino.js +++ b/blockly/generators/arduino.js @@ -446,7 +446,7 @@ Blockly.Arduino.getArduinoType_ = function(typeBlockly) { case Blockly.Types.ANAPIN.typeId: // a pin type return 'int'; - case Blockly.Types.PWNPIN.typeId: + case Blockly.Types.PWMPIN.typeId: // a pin type return 'int'; default: From fb29246d1f00d2e48c0a7ccc69471fb73beec853 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Wed, 13 Apr 2016 13:00:49 +0200 Subject: [PATCH 5/6] The pin assign blocks --- blockly/blocks/arduino/io.js | 72 ++++++++++++++++++++++++++++++++ blockly/generators/arduino/io.js | 36 ++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/blockly/blocks/arduino/io.js b/blockly/blocks/arduino/io.js index d000da8a73..448729e62b 100644 --- a/blockly/blocks/arduino/io.js +++ b/blockly/blocks/arduino/io.js @@ -252,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'); + } +}; diff --git a/blockly/generators/arduino/io.js b/blockly/generators/arduino/io.js index 0c3bce5ff6..46164e9691 100644 --- a/blockly/generators/arduino/io.js +++ b/blockly/generators/arduino/io.js @@ -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]; +}; \ No newline at end of file From 5a5ecdd8f6215e18ada726eada4adf53af1e1be7 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Wed, 13 Apr 2016 13:06:18 +0200 Subject: [PATCH 6/6] add translatable strings --- blockly/msg/json/nl.json | 6 ++++++ blockly/msg/messages.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json index ec0ea5eba9..8463aa01a8 100644 --- a/blockly/msg/json/nl.json +++ b/blockly/msg/json/nl.json @@ -371,6 +371,12 @@ "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#", diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js index 2c5b8f18c1..3b8cf9a138 100644 --- a/blockly/msg/messages.js +++ b/blockly/msg/messages.js @@ -1116,6 +1116,12 @@ 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#';