1 /** 2 * @license Licensed under the Apache License, Version 2.0 (the "License"): 3 * http://www.apache.org/licenses/LICENSE-2.0 4 */ 5 6/** 7 * @fileoverview Blocks for Arduino SPI library. 8 * The Arduino SPI functions syntax can be found in: 9 * http://arduino.cc/en/Reference/SPI 10 */ 11'use strict'; 12 13goog.provide('Blockly.Blocks.spi'); 14 15goog.require('Blockly.Blocks'); 16goog.require('Blockly.Types'); 17 18 19/** Common HSV hue for all blocks in this category. */ 20Blockly.Blocks.spi.HUE = 170; 21 22Blockly.Blocks['spi_setup'] = { 23 /** 24 * Block for the spi configuration. Info in the setHelpUrl link. 25 * @this Blockly.Block 26 */ 27 init: function() { 28 this.setHelpUrl('http://arduino.cc/en/Reference/SPI'); 29 this.setColour(Blockly.Blocks.spi.HUE); 30 this.appendDummyInput() 31 .appendField(Blockly.Msg.ARD_SPI_SETUP) 32 .appendField(new Blockly.FieldDropdown( 33 Blockly.Arduino.Boards.selected.spi), 'SPI_ID') 34 .appendField(Blockly.Msg.ARD_SPI_SETUP_CONF); 35 this.appendDummyInput() 36 .appendField(Blockly.Msg.ARD_SPI_SETUP_SHIFT) 37 .appendField( 38 new Blockly.FieldDropdown( 39 [[Blockly.Msg.ARD_SPI_SETUP_MSBFIRST, 'MSBFIRST'], 40 [Blockly.Msg.ARD_SPI_SETUP_LSBFIRST, 'LSBFIRST']]), 41 'SPI_SHIFT_ORDER'); 42 this.appendDummyInput() 43 .appendField(Blockly.Msg.ARD_SPI_SETUP_DIVIDE) 44 .appendField( 45 new Blockly.FieldDropdown( 46 Blockly.Arduino.Boards.selected.spiClockDivide), 47 'SPI_CLOCK_DIVIDE'); 48 this.appendDummyInput() 49 .appendField(Blockly.Msg.ARD_SPI_SETUP_MODE) 50 .appendField( 51 new Blockly.FieldDropdown( 52 [[Blockly.Msg.ARD_SPI_SETUP_MODE0, 'SPI_MODE0'], 53 [Blockly.Msg.ARD_SPI_SETUP_MODE1, 'SPI_MODE1'], 54 [Blockly.Msg.ARD_SPI_SETUP_MODE2, 'SPI_MODE2'], 55 [Blockly.Msg.ARD_SPI_SETUP_MODE3, 'SPI_MODE3']]), 56 'SPI_MODE'); 57 this.setTooltip(Blockly.Msg.ARD_SPI_SETUP_TIP); 58 }, 59 /** 60 * Returns the selected SPI instance. 61 * @return {!string} SPI instance name. 62 * @this Blockly.Block 63 */ 64 getSpiSetupInstance: function() { 65 return this.getFieldValue('SPI_ID'); 66 }, 67 /** 68 * Updates the content of the the board SPI related fields. 69 * @this Blockly.Block 70 */ 71 updateFields: function() { 72 Blockly.Arduino.Boards.refreshBlockFieldDropdown( 73 this, 'SPI_ID', 'spi'); 74 Blockly.Arduino.Boards.refreshBlockFieldDropdown( 75 this, 'SPI_CLOCK_DIVIDE', 'spiClockDivide'); 76 } 77}; 78 79Blockly.Blocks['spi_transfer'] = { 80 /** 81 * Block for for the spi transfer. Info in the setHelpUrl link. 82 * @this Blockly.Block 83 */ 84 init: function() { 85 // Drop down list to contain all digital pins plus an option for 'none' 86 var slaveNone = [[Blockly.Msg.ARD_SPI_TRANS_NONE, 'none']]; 87 var digitalPinsExtended = slaveNone.concat( 88 Blockly.Arduino.Boards.selected.digitalPins); 89 90 this.setHelpUrl('http://arduino.cc/en/Reference/SPITransfer'); 91 this.setColour(Blockly.Blocks.spi.HUE); 92 this.appendDummyInput() 93 .appendField(new Blockly.FieldDropdown( 94 Blockly.Arduino.Boards.selected.spi), 'SPI_ID'); 95 this.appendValueInput('SPI_DATA') 96 .setCheck(Blockly.Types.NUMBER.checkList) 97 .appendField(Blockly.Msg.ARD_SPI_TRANS_VAL); 98 this.appendDummyInput() 99 .appendField(Blockly.Msg.ARD_SPI_TRANS_SLAVE) 100 .appendField( 101 new Blockly.FieldDropdown(digitalPinsExtended), 'SPI_SS'); 102 this.setInputsInline(true); 103 this.setPreviousStatement(true, null); 104 this.setNextStatement(true, null); 105 this.setTooltip(Blockly.Msg.ARD_SPI_TRANS_TIP); 106 }, 107 /** 108 * Called whenever anything on the workspace changes. 109 * It checks the instances of stepper_config and attaches a warning to this 110 * block if not valid data is found. 111 * @this Blockly.Block 112 */ 113 onchange: function() { 114 if (!this.workspace) { return; } // Block has been deleted. 115 116 // Get the Serial instance from this block 117 var thisInstanceName = this.getFieldValue('SPI_ID'); 118 119 // Iterate through top level blocks to find a setup instance for the SPI id 120 var blocks = Blockly.mainWorkspace.getTopBlocks(); 121 var setupInstancePresent = false;
122 for (var x = 0, length_ = blocks.length; x < length_; x++) { 123 var func = blocks[x].getSpiSetupInstance; 124 if (func) { 125 var setupBlockInstanceName = func.call(blocks[x]); 126 if (thisInstanceName == setupBlockInstanceName) { 127 setupInstancePresent = true; 128 } 129 } 130 } 131 132 if (!setupInstancePresent) { 133 this.setWarningText(Blockly.Msg.ARD_SPI_TRANS_WARN1.replace('%1', thisInstanceName), 134 'spi_setup'); 135 } else { 136 this.setWarningText(null, 'spi_setup'); 137 } 138 }, 139 /** 140 * Retrieves the type of the selected variable, Arduino code returns a byte, 141 * for now set it to integer. 142 * @return {!string} Blockly type. 143 */ 144 getBlockType: function() { 145 return Blockly.Types.NUMBER; 146 }, 147 /** 148 * Updates the content of the board SPI related fields. 149 * @this Blockly.Block 150 */ 151 updateFields: function() { 152 // Special case, otherwise Blockly.Arduino.Boards.refreshBlockFieldDropdown 153 var field = this.getField('SPI_SS'); 154 var fieldValue = field.getValue(); 155 var slaveNone = [[Blockly.Msg.ARD_SPI_TRANS_NONE, 'none']]; 156 field.menuGenerator_ = 157 slaveNone.concat(Blockly.Arduino.Boards.selected['digitalPins']); 158 159 var currentValuePresent = false; 160 for (var i = 0, length_ = field.menuGenerator_.length; i < length_; i++) { 161 if (fieldValue == field.menuGenerator_[i][1]) { 162 currentValuePresent = true; 163 } 164 } 165 // If the old value is not present any more, add a warning to the block. 166 if (!currentValuePresent) { 167 this.setWarningText(Blockly.Msg.ARD_SPI_TRANS_WARN2.replace('%1', fieldValue), 168 'bPin'); 169 } else { 170 this.setWarningText(null, 'bPin'); 171 } 172 } 173}; 174 175Blockly.Blocks['spi_transfer_return'] = { 176 /** 177 * Block for for the spi transfer with a return value. 178 * @this Blockly.Block 179 */ 180 init: function() { 181 // Drop down list to contain all digital pins plus an option for 'none' 182 var slaveNone = [[Blockly.Msg.ARD_SPI_TRANS_NONE, 'none']]; 183 var digitalPinsExtended = slaveNone.concat( 184 Blockly.Arduino.Boards.selected.digitalPins); 185 186 this.setHelpUrl('http://arduino.cc/en/Reference/SPITransfer'); 187 this.setColour(Blockly.Blocks.spi.HUE); 188 this.appendDummyInput() 189 .appendField(new Blockly.FieldDropdown( 190 Blockly.Arduino.Boards.selected.spi), 'SPI_ID'); 191 this.appendValueInput('SPI_DATA') 192 .appendField(Blockly.Msg.ARD_SPI_TRANS_VAL); 193 this.appendDummyInput() 194 .appendField(Blockly.Msg.ARD_SPI_TRANS_SLAVE) 195 .appendField( 196 new Blockly.FieldDropdown(digitalPinsExtended), 'SPI_SS'); 197 this.setInputsInline(true); 198 this.setOutput(true); 199 this.setTooltip(Blockly.Msg.ARD_SPI_TRANSRETURN_TIP); 200 }, 201 /** Same as spi_transfer block */ 202 onchange: Blockly.Blocks['spi_transfer'].onchange, 203 /** Same as spi_transfer block */ 204 getBlockType: Blockly.Blocks['spi_transfer'].getBlockType, 205 /** Same as spi_transfer block */ 206 updateFields: Blockly.Blocks['spi_transfer'].updateFields 207};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.