stepper.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Stepper library.
  7. * The Arduino Servo functions syntax can be found in the following URL:
  8. * http://arduino.cc/en/Reference/Stepper
  9. * Note that this block uses the Blockly.FieldInstance instead of
  10. * Blockly.FieldDropdown which generates a unique instance per setup block
  11. * in the workspace.
  12. */
  13. 'use strict';
  14. goog.provide('Blockly.Blocks.stepper');
  15. goog.require('Blockly.Blocks');
  16. goog.require('Blockly.Types');
  17. /** Common HSV hue for all blocks in this category. */
  18. Blockly.Blocks.stepper.HUE = 80;
  19. Blockly.Blocks['stepper_config'] = {
  20. /**
  21. * Block for for the stepper generator configuration including creating
  22. * an object instance and setting up the speed. Info in the setHelpUrl link.
  23. * @this Blockly.Block
  24. */
  25. init: function() {
  26. this.setHelpUrl('http://arduino.cc/en/Reference/StepperConstructor');
  27. this.setColour(Blockly.Blocks.stepper.HUE);
  28. this.appendDummyInput()
  29. .appendField(Blockly.Msg.ARD_STEPPER_SETUP)
  30. .appendField(
  31. new Blockly.FieldInstance('Stepper',
  32. Blockly.Msg.ARD_STEPPER_DEFAULT_NAME,
  33. true, true, false),
  34. 'STEPPER_NAME')
  35. .appendField(Blockly.Msg.ARD_STEPPER_MOTOR);
  36. this.appendDummyInput()
  37. .setAlign(Blockly.ALIGN_RIGHT)
  38. .appendField(Blockly.Msg.ARD_STEPPER_PIN1)
  39. .appendField(new Blockly.FieldDropdown(
  40. Blockly.Python.Boards.selected.digitalPins), 'STEPPER_PIN1')
  41. .appendField(Blockly.Msg.ARD_STEPPER_PIN2)
  42. .appendField(new Blockly.FieldDropdown(
  43. Blockly.Python.Boards.selected.digitalPins), 'STEPPER_PIN2');
  44. this.appendValueInput('STEPPER_STEPS')
  45. .setCheck(Blockly.Types.NUMBER.checkList)
  46. .setAlign(Blockly.ALIGN_RIGHT)
  47. .appendField(Blockly.Msg.ARD_STEPPER_REVOLVS);
  48. this.appendValueInput('STEPPER_SPEED')
  49. .setCheck(Blockly.Types.NUMBER.checkList)
  50. .setAlign(Blockly.ALIGN_RIGHT)
  51. .appendField(Blockly.Msg.ARD_STEPPER_SPEED);
  52. this.setTooltip(Blockly.Msg.ARD_STEPPER_SETUP_TIP);
  53. },
  54. /**
  55. * Updates the content of the the pin related fields.
  56. * @this Blockly.Block
  57. */
  58. updateFields: function() {
  59. Blockly.Boards.refreshBlockFieldDropdown(
  60. this, 'STEPPER_PIN1', 'digitalPins');
  61. Blockly.Boards.refreshBlockFieldDropdown(
  62. this, 'STEPPER_PIN2', 'digitalPins');
  63. }
  64. };
  65. Blockly.Blocks['stepper_step'] = {
  66. /**
  67. * Block for for the stepper 'step()' function.
  68. * @this Blockly.Block
  69. */
  70. init: function() {
  71. this.setHelpUrl('http://arduino.cc/en/Reference/StepperStep');
  72. this.setColour(Blockly.Blocks.stepper.HUE);
  73. this.appendDummyInput()
  74. .appendField(Blockly.Msg.ARD_STEPPER_STEP)
  75. .appendField(
  76. new Blockly.FieldInstance('Stepper',
  77. Blockly.Msg.ARD_STEPPER_DEFAULT_NAME,
  78. false, true, false),
  79. 'STEPPER_NAME');
  80. this.appendValueInput('STEPPER_STEPS')
  81. .setCheck(Blockly.Types.NUMBER.checkList);
  82. this.appendDummyInput()
  83. .appendField(Blockly.Msg.ARD_STEPPER_STEPS);
  84. this.setPreviousStatement(true);
  85. this.setNextStatement(true);
  86. this.setTooltip(Blockly.Msg.ARD_STEPPER_STEP_TIP);
  87. },
  88. /**
  89. * Called whenever anything on the workspace changes.
  90. * It checks/warns if the selected stepper instance has a config block.
  91. * @this Blockly.Block
  92. */
  93. onchange: function() {
  94. if (!this.workspace) return; // Block has been deleted.
  95. var instanceName = this.getFieldValue('STEPPER_NAME')
  96. if (Blockly.Instances.isInstancePresent(instanceName, 'Stepper', this)) {
  97. this.setWarningText(null);
  98. } else {
  99. // Set a warning to select a valid stepper config block
  100. this.setWarningText(
  101. Blockly.Msg.ARD_COMPONENT_WARN1.replace(
  102. '%1', Blockly.Msg.ARD_STEPPER_COMPONENT).replace(
  103. '%2', instanceName));
  104. }
  105. }
  106. };