serial.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Code generator for the Arduino serial blocks.
  7. * Arduino Serial library docs: https://www.arduino.cc/en/Reference/Serial
  8. *
  9. * TODO: There are more functions that can be added:
  10. * http://arduino.cc/en/Reference/Serial
  11. */
  12. 'use strict';
  13. goog.provide('Blockly.Python.serial');
  14. goog.require('Blockly.Python');
  15. /**
  16. * Code generator of block for writing to the serial com.
  17. * Arduino code: loop { Serial.print(X); }
  18. * @param {!Blockly.Block} block Block to generate the code from.
  19. * @return {string} Completed code.
  20. */
  21. Blockly.Python['serial_print'] = function(block) {
  22. var serialId = block.getFieldValue('SERIAL_ID');
  23. var content = Blockly.Python.valueToCode(
  24. block, 'CONTENT', Blockly.Python.ORDER_ATOMIC) || '0';
  25. var checkbox_name = (block.getFieldValue('NEW_LINE') == 'TRUE');
  26. var serialPins = Blockly.Python.Boards.selected.serialPins[serialId];
  27. for (var i = 0; i < serialPins.length; i++) {
  28. Blockly.Python.reservePin(block, serialPins[i][1],
  29. Blockly.Python.PinTypes.SERIAL, 'SERIAL ' + serialPins[i][0]);
  30. }
  31. if (checkbox_name) {
  32. var code = serialId + '.println(' + content + ')\n';
  33. } else {
  34. var code = serialId + '.print(' + content + ')\n';
  35. }
  36. return code;
  37. };
  38. /**
  39. * Code generator for block for setting the serial com speed.
  40. * Arduino code: setup{ Serial.begin(X); }
  41. * @param {!Blockly.Block} block Block to generate the code from.
  42. * @return {array} Completed code.
  43. */
  44. Blockly.Python['serial_setup'] = function(block) {
  45. var serialId = block.getFieldValue('SERIAL_ID');
  46. var serialSpeed = block.getFieldValue('SPEED');
  47. var serialSetupCode = serialId + '.begin(' + serialSpeed + ');';
  48. Blockly.Python.addSetup('serial_' + serialId, serialSetupCode, true);
  49. var code = '';
  50. return code;
  51. };
  52. /**
  53. * Code generator of block for writing to the UART com.
  54. * Arduino code: loop { Serial1.write(X); }
  55. * @param {!Blockly.Block} block Block to generate the code from.
  56. * @return {string} Completed code.
  57. */
  58. Blockly.Python['serial1_write'] = function(block) {
  59. var serialId = block.getFieldValue('SERIAL_ID');
  60. var content = Blockly.Python.valueToCode(
  61. block, 'CONTENT', Blockly.Python.ORDER_ATOMIC) || '0';
  62. var code = serialId + '.write(' + content + ')\n';
  63. return code;
  64. };
  65. /**
  66. * Code generator of block for reading from the UART com.
  67. * Arduino code: loop { Serial1.read(X); }
  68. * @param {!Blockly.Block} block Block to generate the code from.
  69. * @return {string} Completed code.
  70. */
  71. Blockly.Python['serial1_read'] = function(block) {
  72. var serialId = block.getFieldValue('SERIAL_ID');
  73. var code = serialId + '.read()\n';
  74. return code;
  75. };
  76. /**
  77. * Code generator of block for reading from the UART com.
  78. * Arduino code: loop { Serial1.read(X); }
  79. * @param {!Blockly.Block} block Block to generate the code from.
  80. * @return {string} Completed code.
  81. */
  82. Blockly.Python['serial1_available'] = function(block) {
  83. var serialId = block.getFieldValue('SERIAL_ID');
  84. var code = serialId + '.available()\n';
  85. return [code, Blockly.Python.ORDER_ATOMIC];
  86. };