spi.js 7.1 KB

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