io.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Arduino Digital and Analogue input/output.
  7. * Arduino built in function docs: http://arduino.cc/en/Reference/HomePage
  8. */
  9. 'use strict';
  10. goog.provide('Blockly.Python.IO');
  11. goog.require('Blockly.Python');
  12. /**
  13. * Function for 'set pin' (X) to a state (Y).
  14. * Arduino code: setup { pinMode(X, OUTPUT); }
  15. * loop { digitalWrite(X, Y); }
  16. * @param {!Blockly.Block} block Block to generate the code from.
  17. * @return {string} Completed code.
  18. */
  19. Blockly.Python['io_digitalwrite'] = function(block) {
  20. var pin = Blockly.Python.valueToCode(
  21. block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '1';
  22. var stateOutput = Blockly.Python.valueToCode(
  23. block, 'STATE', Blockly.Python.ORDER_ATOMIC) || 'LOW';
  24. Blockly.Python.reservePin(
  25. block, pin, Blockly.Python.PinTypes.OUTPUT, 'Digital Write');
  26. var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  27. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  28. var code = 'digitalWrite(' + pin + ', ' + stateOutput + ');\n';
  29. return code;
  30. };
  31. /**
  32. * Function for reading a digital pin (X).
  33. * Arduino code: setup { pinMode(X, INPUT); }
  34. * loop { digitalRead(X) }
  35. * @param {!Blockly.Block} block Block to generate the code from.
  36. * @return {array} Completed code with order of operation.
  37. */
  38. Blockly.Python['io_digitalread'] = function(block) {
  39. var pin = Blockly.Python.valueToCode(
  40. block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '1';
  41. Blockly.Python.reservePin(
  42. block, pin, Blockly.Python.PinTypes.INPUT, 'Digital Read');
  43. var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
  44. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  45. var code = 'digitalRead(' + pin + ')';
  46. return [code, Blockly.Python.ORDER_ATOMIC];
  47. };
  48. /**
  49. * Function for setting the state (Y) of a built-in LED (X).
  50. * Arduino code: setup { pinMode(X, OUTPUT); }
  51. * loop { digitalWrite(X, Y); }
  52. * @param {!Blockly.Block} block Block to generate the code from.
  53. * @return {string} Completed code.
  54. */
  55. Blockly.Python['io_builtin_led'] = function(block) {
  56. var pin = block.getFieldValue('BUILT_IN_LED');
  57. var stateOutput = Blockly.Python.valueToCode(
  58. block, 'STATE', Blockly.Python.ORDER_ATOMIC) || 'LOW';
  59. Blockly.Python.reservePin(
  60. block, pin, Blockly.Python.PinTypes.OUTPUT, 'Set LED');
  61. var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  62. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  63. var code = 'digitalWrite(' + pin + ', ' + stateOutput + ');\n';
  64. return code;
  65. };
  66. /**
  67. * Function for setting the state (Y) of an analogue output (X).
  68. * Arduino code: setup { pinMode(X, OUTPUT); }
  69. * loop { analogWrite(X, Y); }
  70. * @param {!Blockly.Block} block Block to generate the code from.
  71. * @return {string} Completed code.
  72. */
  73. Blockly.Python['io_analogwrite'] = function(block) {
  74. var pin = Blockly.Python.valueToCode(
  75. block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '0';
  76. var stateOutput = Blockly.Python.valueToCode(
  77. block, 'ANALOGNUM', Blockly.Python.ORDER_ATOMIC) || '0';
  78. var reg=/A[0-9]/g;
  79. var isAnalogInput = true;
  80. isAnalogInput = reg.test(pin);
  81. // console.log("io.js-input isAnalogInput: "+ isAnalogInput);
  82. var pinI='';
  83. if (!isAnalogInput){
  84. Blockly.Python.addDeclaration('io_analogPinList','const int analogPIN[8]={A0,A1,A2,A3,A4,A5,A6,A7};');
  85. Blockly.Python.addSetup('io_A0','pinMode(A0, OUTPUT);');
  86. Blockly.Python.addSetup('io_A1','pinMode(A1, OUTPUT);');
  87. Blockly.Python.addSetup('io_A2','pinMode(A2, OUTPUT);');
  88. Blockly.Python.addSetup('io_A3','pinMode(A3, OUTPUT);');
  89. Blockly.Python.addSetup('io_A4','pinMode(A4, OUTPUT);');
  90. Blockly.Python.addSetup('io_A5','pinMode(A5, OUTPUT);');
  91. Blockly.Python.addSetup('io_A6','pinMode(A6, OUTPUT);');
  92. Blockly.Python.addSetup('io_A7','pinMode(A7, OUTPUT);');
  93. pinI = "analogPIN["+pin+"]";
  94. } else {
  95. Blockly.Python.reservePin(
  96. block, pin, Blockly.Python.PinTypes.OUTPUT, 'Analogue Write');
  97. var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  98. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  99. }
  100. // Warn if the input value is out of range
  101. if ((stateOutput < 0) || (stateOutput > 255)) {
  102. block.setWarningText('The analogue value set must be between 0 and 255',
  103. 'pwm_value');
  104. } else {
  105. block.setWarningText(null, 'pwm_value');
  106. }
  107. if (isAnalogInput) {
  108. var code = 'analogWrite(' + pin + ', ' + stateOutput + ');\n';
  109. } else {
  110. var code = 'analogWrite(' + pinI + ', ' + stateOutput + ');\n';
  111. }
  112. return code;
  113. };
  114. /**
  115. * Function for reading an analogue pin value (X).
  116. * Arduino code: setup { pinMode(X, INPUT); }
  117. * loop { analogRead(X) }
  118. * @param {!Blockly.Block} block Block to generate the code from.
  119. * @return {array} Completed code with order of operation.
  120. */
  121. Blockly.Python['io_analogread'] = function(block) {
  122. var pin = Blockly.Python.valueToCode(
  123. block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '0';
  124. Blockly.Python.reservePin(
  125. block, pin, Blockly.Python.PinTypes.INPUT, 'Analogue Read');
  126. var pinSetupCode = 'pinMode(' + pin + ', INPUT);';
  127. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  128. var code = 'analogRead(' + pin + ')';
  129. return [code, Blockly.Python.ORDER_ATOMIC];
  130. };
  131. Blockly.Python['io_pwmwrite'] = function(block) {
  132. var pin = Blockly.Python.valueToCode(
  133. block, 'PIN', Blockly.Python.ORDER_ATOMIC) || '0';
  134. var stateOutput = Blockly.Python.valueToCode(
  135. block, 'PWMVALUE', Blockly.Python.ORDER_ATOMIC) || '0';
  136. var reg=/[0-9]/g;
  137. var isVarInput = false;
  138. isVarInput = !reg.test(pin);
  139. // console.log("io.js-input isPwmInput: "+ isVarInput);
  140. Blockly.Python.reservePin(
  141. block, pin, Blockly.Python.PinTypes.OUTPUT, 'PWM Write');
  142. var pinSetupCode = 'pinMode(' + pin + ', OUTPUT);';
  143. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  144. var code = 'analogWrite(' + pin + ', ' + stateOutput + ');\n';
  145. return code;
  146. };
  147. /**
  148. * Value for defining a digital pin state.
  149. * Arduino code: loop { HIGH / LOW }
  150. * @param {!Blockly.Block} block Block to generate the code from.
  151. * @return {array} Completed code with order of operation.
  152. */
  153. Blockly.Python['io_highlow'] = function(block) {
  154. var code = block.getFieldValue('STATE');
  155. return [code, Blockly.Python.ORDER_ATOMIC];
  156. };
  157. Blockly.Python['io_pulsein'] = function(block) {
  158. var pin = block.getFieldValue("PULSEPIN");
  159. var type = Blockly.Python.valueToCode(block, "PULSETYPE", Blockly.Python.ORDER_ATOMIC);
  160. Blockly.Python.reservePin(
  161. block, pin, Blockly.Python.PinTypes.INPUT, 'Pulse Pin');
  162. var pinSetupCode = 'pinMode(' + pin + ', INPUT);\n';
  163. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  164. var code = 'pulseIn(' + pin + ', ' + type + ')';
  165. return [code, Blockly.Python.ORDER_ATOMIC];
  166. };
  167. Blockly.Python['io_pulsetimeout'] = function(block) {
  168. var pin = block.getFieldValue("PULSEPIN");
  169. var type = Blockly.Python.valueToCode(block, "PULSETYPE", Blockly.Python.ORDER_ATOMIC);
  170. var timeout = Blockly.Python.valueToCode(block, "TIMEOUT", Blockly.Python.ORDER_ATOMIC);
  171. Blockly.Python.reservePin(
  172. block, pin, Blockly.Python.PinTypes.INPUT, 'Pulse Pin');
  173. var pinSetupCode = 'pinMode(' + pin + ', INPUT);\n';
  174. Blockly.Python.addSetup('io_' + pin, pinSetupCode, false);
  175. var code = 'pulseIn(' + pin + ', ' + type + ', ' + timeout + ')';
  176. return [code, Blockly.Python.ORDER_ATOMIC];
  177. };
  178. Blockly.Python['io_dropdown_digital']= function(block) {
  179. var code = block.getFieldValue('SELECTPIN');
  180. return [code, Blockly.Python.ORDER_ATOMIC];
  181. };
  182. Blockly.Python['io_dropdown_analog']= function(block) {
  183. var code = block.getFieldValue('SELECTPIN');
  184. return [code, Blockly.Python.ORDER_ATOMIC];
  185. };
  186. Blockly.Python['io_dropdown_pwm']= function(block) {
  187. var code = block.getFieldValue('SELECTPIN');
  188. return [code, Blockly.Python.ORDER_ATOMIC];
  189. };