servo.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 Arduino blocks for the Servo library.
  7. * The Arduino Servo functions can be found in
  8. * http://arduino.cc/en/reference/servo
  9. *
  10. * TODO: Add angle selector instead of block input.
  11. */
  12. 'use strict';
  13. goog.provide('Blockly.Blocks.servo');
  14. goog.require('Blockly.Blocks');
  15. goog.require('Blockly.Types');
  16. /** Common HSV hue for all blocks in this category. */
  17. Blockly.Blocks.servo.HUE = 200;
  18. Blockly.Blocks['servo_setup'] = {
  19. /**
  20. * Block for setup a servo .
  21. * @this Blockly.Block
  22. */
  23. init: function() {
  24. this.appendDummyInput()
  25. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_motorDriver.png", 140, 40, "15"));
  26. .appendField(new Blockly.FieldImage("./../blockly/media/main-servo.png", 50, 40, "15"));
  27. this.appendDummyInput()
  28. .appendField(Blockly.Msg.ARD_SERVO_SETUP);
  29. //.appendField('SERVO_SETUP');
  30. this.setColour(Blockly.Blocks.servo.HUE);
  31. this.setTooltip('');
  32. this.setHelpUrl('');
  33. }
  34. };
  35. Blockly.Blocks['servo_write'] = {
  36. /**
  37. * Block for writing an angle value into a servo pin.
  38. * @this Blockly.Block
  39. */
  40. init: function() {
  41. this.setHelpUrl('http://arduino.cc/en/Reference/ServoWrite');
  42. this.setColour(Blockly.Blocks.servo.HUE);
  43. this.appendDummyInput()
  44. .appendField(Blockly.Msg.ARD_SERVO_WRITE)
  45. .appendField(new Blockly.FieldDropdown(
  46. Blockly.Python.Boards.selected.pwmPins), 'SERVO_PIN');
  47. this.setInputsInline(false);
  48. this.appendValueInput('SERVO_ANGLE')
  49. .setCheck(Blockly.Types.NUMBER.checkList)
  50. .appendField(Blockly.Msg.ARD_SERVO_WRITE_TO);
  51. this.appendDummyInput()
  52. .appendField(Blockly.Msg.ARD_SERVO_WRITE_DEG_180);
  53. this.setInputsInline(true);
  54. this.setPreviousStatement(true, null);
  55. this.setNextStatement(true, null);
  56. this.setTooltip(Blockly.Msg.ARD_SERVO_WRITE_TIP);
  57. },
  58. /**
  59. * Updates the content of the the pin related fields.
  60. * @this Blockly.Block
  61. */
  62. updateFields: function() {
  63. Blockly.Python.Boards.refreshBlockFieldDropdown(
  64. this, 'SERVO_PIN', 'pwmPins');
  65. }
  66. };
  67. Blockly.Blocks['servo_read'] = {
  68. /**
  69. * Block for reading an angle value of a servo pin.
  70. * @this Blockly.Block
  71. */
  72. init: function() {
  73. this.setHelpUrl('http://arduino.cc/en/Reference/ServoRead');
  74. this.setColour(Blockly.Blocks.servo.HUE);
  75. this.appendDummyInput()
  76. .appendField(Blockly.Msg.ARD_SERVO_READ)
  77. .appendField(new Blockly.FieldDropdown(
  78. Blockly.Python.Boards.selected.pwmPins), 'SERVO_PIN');
  79. this.setOutput(true, Blockly.Types.NUMBER.output);
  80. this.setTooltip(Blockly.Msg.ARD_SERVO_READ_TIP);
  81. },
  82. /** @return {string} The type of return value for the block, an integer. */
  83. getBlockType: function() {
  84. return Blockly.Types.NUMBER;
  85. },
  86. /**
  87. * Updates the content of the the pin related fields.
  88. * @this Blockly.Block
  89. */
  90. updateFields: function() {
  91. Blockly.Python.Boards.refreshBlockFieldDropdown(
  92. this, 'SERVO_PIN', 'pwmPins');
  93. }
  94. };
  95. Blockly.Blocks['robot_setup'] = {
  96. init: function() {
  97. this.appendDummyInput()
  98. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/cocomod_blockly_bipedalRobot.png", 140, 40, "15"));
  99. .appendField(new Blockly.FieldImage("./../blockly/media/main-biped_robot.png", 50, 40, "15"));
  100. this.appendDummyInput()
  101. .appendField(Blockly.Msg.ROBOT_SETUP);
  102. this.appendDummyInput()
  103. .appendField(Blockly.Msg.ROBOT_SETUP_SETYLPIN)
  104. .appendField(new Blockly.FieldDropdown(
  105. Blockly.Python.Boards.selected.pwmPins), "YL_PIN")
  106. .appendField(Blockly.Msg.ROBOT_SETUP_SETYRPIN)
  107. .appendField(new Blockly.FieldDropdown(
  108. Blockly.Python.Boards.selected.pwmPins), "YR_PIN");
  109. this.appendDummyInput()
  110. .appendField(Blockly.Msg.ROBOT_SETUP_SETRLPIN)
  111. .appendField(new Blockly.FieldDropdown(
  112. Blockly.Python.Boards.selected.pwmPins), "RL_PIN")
  113. .appendField(Blockly.Msg.ROBOT_SETUP_SETRRPIN)
  114. .appendField(new Blockly.FieldDropdown(
  115. Blockly.Python.Boards.selected.pwmPins), "RR_PIN");
  116. this.setNextStatement(false, null);
  117. this.setColour(Blockly.Blocks.servo.HUE);
  118. this.setTooltip('');
  119. this.setHelpUrl('');
  120. }
  121. };
  122. Blockly.Blocks['robot_set_movement'] = {
  123. init: function() {
  124. this.appendValueInput("MOVEMENT")
  125. .setCheck("String")
  126. .appendField(Blockly.Msg.ROBOT_SETMOVE_MOVEMENT);
  127. this.appendValueInput("TEMPO")
  128. .setCheck("Number")
  129. .appendField(Blockly.Msg.ROBOT_SETMOVE_TEMPO);
  130. this.setInputsInline(true);
  131. this.setPreviousStatement(true, null);
  132. this.setNextStatement(true, null);
  133. this.setColour(Blockly.Blocks.servo.HUE);
  134. this.setTooltip('');
  135. this.setHelpUrl('');
  136. }
  137. };
  138. Blockly.Blocks['robot_movement'] = {
  139. init: function() {
  140. this.appendDummyInput()
  141. .appendField(new Blockly.FieldDropdown([
  142. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_GOINGUP, "goingUp"],
  143. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_KICKLEFT, "kickLeft"],
  144. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_KICKRIGHT, "kickRight"],
  145. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_RUN, "run"],
  146. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_WALK, "walk"],
  147. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_BACKYARD, "backyard"],
  148. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_TURNLEFT, "turnLeft"],
  149. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_TURNRIGHT, "turnRight"],
  150. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_MOONWALKLEFT, "moonWalkLeft"],
  151. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_MOONWALKRIGHT, "moonWalkRight"],
  152. [Blockly.Msg.ROBOT_SETMOVE_MOVEMENT_SWING, "swing"]
  153. ]), "MOVEMENT");
  154. this.setOutput(true, null);
  155. this.setColour(Blockly.Blocks.servo.HUE);
  156. this.setTooltip("");
  157. this.setHelpUrl("");
  158. }
  159. };
  160. Blockly.Blocks['robot_tempo'] = {
  161. init: function() {
  162. this.appendDummyInput()
  163. .appendField(new Blockly.FieldNumber(400), "TEMPO");
  164. this.setOutput(true, Blockly.Types.NUMBER.output);
  165. this.setColour(Blockly.Blocks.servo.HUE);
  166. this.setTooltip("");
  167. this.setHelpUrl("");
  168. },
  169. getBlockType: function() {
  170. return Blockly.Types.NUMBER;
  171. }
  172. };