1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /**
- * @license Licensed under the Apache License, Version 2.0 (the "License"):
- * http://www.apache.org/licenses/LICENSE-2.0
- */
- /**
- * @fileoverview Code generator for the Arduino serial blocks.
- * Arduino Serial library docs: https://www.arduino.cc/en/Reference/Serial
- *
- * TODO: There are more functions that can be added:
- * http://arduino.cc/en/Reference/Serial
- */
- 'use strict';
- goog.provide('Blockly.Python.serial');
- goog.require('Blockly.Python');
- /**
- * Code generator of block for writing to the serial com.
- * Arduino code: loop { Serial.print(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['serial_print'] = function(block) {
- var serialId = block.getFieldValue('SERIAL_ID');
- var content = Blockly.Python.valueToCode(
- block, 'CONTENT', Blockly.Python.ORDER_ATOMIC) || '0';
- var checkbox_name = (block.getFieldValue('NEW_LINE') == 'TRUE');
- var serialPins = Blockly.Python.Boards.selected.serialPins[serialId];
- for (var i = 0; i < serialPins.length; i++) {
- Blockly.Python.reservePin(block, serialPins[i][1],
- Blockly.Python.PinTypes.SERIAL, 'SERIAL ' + serialPins[i][0]);
- }
- if (checkbox_name) {
- var code = serialId + '.println(' + content + ')\n';
- } else {
- var code = serialId + '.print(' + content + ')\n';
- }
- return code;
- };
- /**
- * Code generator for block for setting the serial com speed.
- * Arduino code: setup{ Serial.begin(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {array} Completed code.
- */
- Blockly.Python['serial_setup'] = function(block) {
- var serialId = block.getFieldValue('SERIAL_ID');
- var serialSpeed = block.getFieldValue('SPEED');
- var serialSetupCode = serialId + '.begin(' + serialSpeed + ');';
- Blockly.Python.addSetup('serial_' + serialId, serialSetupCode, true);
- var code = '';
- return code;
- };
- /**
- * Code generator of block for writing to the UART com.
- * Arduino code: loop { Serial1.write(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['serial1_write'] = function(block) {
- var serialId = block.getFieldValue('SERIAL_ID');
- var content = Blockly.Python.valueToCode(
- block, 'CONTENT', Blockly.Python.ORDER_ATOMIC) || '0';
- var code = serialId + '.write(' + content + ')\n';
- return code;
- };
- /**
- * Code generator of block for reading from the UART com.
- * Arduino code: loop { Serial1.read(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['serial1_read'] = function(block) {
- var serialId = block.getFieldValue('SERIAL_ID');
- var code = serialId + '.read()\n';
- return code;
- };
- /**
- * Code generator of block for reading from the UART com.
- * Arduino code: loop { Serial1.read(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['serial1_available'] = function(block) {
- var serialId = block.getFieldValue('SERIAL_ID');
- var code = serialId + '.available()\n';
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
|