stepper.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 code generator for the Stepper library blocks.
  7. * The Arduino Stepper library docs: http://arduino.cc/en/Reference/Stepper
  8. */
  9. 'use strict';
  10. goog.provide('Blockly.Python.stepper');
  11. goog.require('Blockly.Python');
  12. /**
  13. * Code generator for the stepper generator configuration. Nothing is added
  14. * to the 'loop()' function. Sets the pins (X and Y), steps per revolution (Z),
  15. * speed(A) and instance name (B).
  16. * Arduino code: #include <Stepper.h>
  17. * Stepper B(Z, X, Y);
  18. * setup() { B.setSpeed(A); }
  19. * @param {!Blockly.Block} block Block to generate the code from.
  20. * @return {string} Empty string as no code goes into 'loop()'.
  21. */
  22. Blockly.Python['stepper_config'] = function(block) {
  23. var pin1 = block.getFieldValue('STEPPER_PIN1');
  24. var pin2 = block.getFieldValue('STEPPER_PIN2');
  25. var pinType = Blockly.Python.PinTypes.STEPPER;
  26. var stepperName = block.getFieldValue('STEPPER_NAME');
  27. var stepperSteps = Blockly.Python.valueToCode(block, 'STEPPER_STEPS',
  28. Blockly.Python.ORDER_ATOMIC) || '360';
  29. var stepperSpeed = Blockly.Python.valueToCode(block, 'STEPPER_SPEED',
  30. Blockly.Python.ORDER_ATOMIC) || '90';
  31. //stepper is a variable containing the used pins
  32. Blockly.Python.addVariable(stepperName,
  33. 'int ' + stepperName + '[2] = {' + pin1 + ', ' + pin2 + '};', true);
  34. stepperName = 'stepper_' + stepperName
  35. Blockly.Python.reservePin(block, pin1, pinType, 'Stepper');
  36. Blockly.Python.reservePin(block, pin2, pinType, 'Stepper');
  37. Blockly.Python.addInclude('stepper', '#include <Stepper.h>');
  38. var globalCode = 'Stepper ' + stepperName + '(' + stepperSteps + ', ' +
  39. pin1 + ', ' + pin2 + ');';
  40. Blockly.Python.addDeclaration(stepperName, globalCode);
  41. var setupCode = stepperName + '.setSpeed(' + stepperSpeed + ');';
  42. Blockly.Python.addSetup(stepperName, setupCode, true);
  43. return '';
  44. };
  45. /**
  46. * Code generator for moving the stepper instance (X) a number of steps (Y).
  47. * Library info in the setHelpUrl link.
  48. * This block requires the stepper_config block to be present.
  49. * Arduino code: loop { X.steps(Y) }
  50. * @param {!Blockly.Block} block Block to generate the code from.
  51. * @return {array} Completed code with order of operation.
  52. */
  53. Blockly.Python['stepper_step'] = function(block) {
  54. var stepperInstanceName = 'stepper_' + block.getFieldValue('STEPPER_NAME');
  55. var stepperSteps = Blockly.Python.valueToCode(block, 'STEPPER_STEPS',
  56. Blockly.Python.ORDER_ATOMIC) || '0';
  57. var code = stepperInstanceName + '.step(' + stepperSteps + ')\n';
  58. return code;
  59. };