basic101.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 test 2 blocks.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Python.basic101');
  10. goog.require('Blockly.Python');
  11. Blockly.Python['read_button'] = function(block) {
  12. //var pinNum = block.getFieldValue('PINSNUM');
  13. var pinNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  14. Blockly.Python.addSetup('basic101_setup_button'+pinNum, 'pinMode(' + pinNum + ', INPUT);');
  15. var code = 'digitalRead('+pinNum+')';
  16. return [code, Blockly.Python.ORDER_ATOMIC];
  17. };
  18. Blockly.Python['basic101_dropdown_button']= function(block) {
  19. var code = block.getFieldValue('SELECTPIN');
  20. return [code, Blockly.Python.ORDER_ATOMIC];
  21. };
  22. Blockly.Python['read_knob'] = function(block) {
  23. //var pinNum = block.getFieldValue('PINSNUM');
  24. var pinNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  25. Blockly.Python.addSetup('basic101_setup_knob'+pinNum, 'pinMode(' + pinNum + ', INPUT);');
  26. var code = 'analogRead('+pinNum+')';
  27. return [code, Blockly.Python.ORDER_ATOMIC];
  28. };
  29. Blockly.Python['basic101_dropdown_knob']= function(block) {
  30. var code = block.getFieldValue('SELECTPIN');
  31. return [code, Blockly.Python.ORDER_ATOMIC];
  32. };
  33. Blockly.Python['analog_light'] = function(block) {
  34. //var pinsNum = block.getFieldValue('PINSNUM');
  35. var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  36. var bright = Blockly.Python.valueToCode(block, 'BRIGHTNESS', Blockly.Python.ORDER_ATOMIC);
  37. var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
  38. Blockly.Python.addSetup('basic101_setup_led' + pinsNum, pinSetupCode, false);
  39. var code = 'analogWrite(' + pinsNum + ', ' + bright + ');\n';
  40. return code;
  41. };
  42. Blockly.Python['basic101_dropdown_analog_light']= function(block) {
  43. var code = block.getFieldValue('SELECTPIN');
  44. return [code, Blockly.Python.ORDER_ATOMIC];
  45. };
  46. Blockly.Python['digital_light'] = function(block) {
  47. //var pinsNum = block.getFieldValue('PINSNUM');
  48. var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  49. var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
  50. Blockly.Python.addSetup('basic101_setup_led' + pinsNum, pinSetupCode, false);
  51. var code = 'digitalWrite(' + pinsNum + ', HIGH);\n';
  52. return code;
  53. };
  54. Blockly.Python['basic101_dropdown_digital_light']= function(block) {
  55. var code = block.getFieldValue('SELECTPIN');
  56. return [code, Blockly.Python.ORDER_ATOMIC];
  57. };
  58. Blockly.Python['nolight'] = function(block) {
  59. //var pinsNum = block.getFieldValue('PINSNUM');
  60. var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  61. var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
  62. Blockly.Python.addSetup('basic101_setup_led' + pinsNum, pinSetupCode, false);
  63. var code = 'digitalWrite(' + pinsNum + ', LOW);\n';
  64. return code;
  65. };
  66. Blockly.Python['basic101_dropdown_nolight']= function(block) {
  67. var code = block.getFieldValue('SELECTPIN');
  68. return [code, Blockly.Python.ORDER_ATOMIC];
  69. };
  70. Blockly.Python['tone'] = function(block) {
  71. //var pinsNum = block.getFieldValue('PINSNUM');
  72. var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  73. var freq = Blockly.Python.valueToCode(block, 'FREQUENCY', Blockly.Python.ORDER_ATOMIC);
  74. var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
  75. Blockly.Python.addSetup('basic101_setup_tone' + pinsNum, pinSetupCode, false);
  76. var code = 'tone(' + pinsNum + ',' + freq + ');\n';
  77. return code;
  78. };
  79. Blockly.Python['basic101_dropdown_tone']= function(block) {
  80. var code = block.getFieldValue('SELECTPIN');
  81. return [code, Blockly.Python.ORDER_ATOMIC];
  82. };
  83. Blockly.Python['notone'] = function(block) {
  84. //var pinsNum = block.getFieldValue('PINSNUM');
  85. var pinsNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  86. var pinSetupCode = 'pinMode(' + pinsNum + ', OUTPUT);\n';
  87. Blockly.Python.addSetup('io_' + pinsNum, pinSetupCode, false);
  88. var code = 'noTone(' + pinsNum + ');\n';
  89. return code;
  90. };
  91. Blockly.Python['basic101_dropdown_notone']= function(block) {
  92. var code = block.getFieldValue('SELECTPIN');
  93. return [code, Blockly.Python.ORDER_ATOMIC];
  94. };
  95. Blockly.Python['io_dropdown_digital']= function(block) {
  96. var code = block.getFieldValue('SELECTPIN');
  97. return [code, Blockly.Python.ORDER_ATOMIC];
  98. };
  99. Blockly.Python['io_dropdown_analog']= function(block) {
  100. var code = block.getFieldValue('SELECTPIN');
  101. return [code, Blockly.Python.ORDER_ATOMIC];
  102. };
  103. Blockly.Python['io_dropdown_pwm']= function(block) {
  104. var code = block.getFieldValue('SELECTPIN');
  105. return [code, Blockly.Python.ORDER_ATOMIC];
  106. };
  107. Blockly.Python['counter_button_setup'] = function(block) {
  108. //var pinNum = block.getFieldValue('PINSNUM');
  109. var pinNum = block.getFieldValue('PINSNUM');
  110. Blockly.Python.addSetup('basic101_setup_button'+pinNum, 'pinMode(' + pinNum + ', INPUT);');
  111. Blockly.Python.addDeclaration('basic101_declaration_counter','int counter = 0;\n int triggle = 0;\n');
  112. var code = ' int signal = digitalRead('+pinNum+');\n'+
  113. ' if ((triggle == 0) && (signal==1)) {\n' +
  114. ' counter++;\n'+
  115. ' triggle=1;\n'+
  116. ' }\n'+
  117. ' if ((triggle == 1) && (signal==0)) {\n' +
  118. ' triggle=0;\n'+
  119. ' }\n';
  120. return code;
  121. };
  122. Blockly.Python['read_counter'] = function(block) {
  123. //var pinNum = block.getFieldValue('PINSNUM');
  124. var pinNum = Blockly.Python.valueToCode(block, 'PINSNUM', Blockly.Python.ORDER_ATOMIC);
  125. Blockly.Python.addSetup('basic101_setup_button'+pinNum, 'pinMode(' + pinNum + ', INPUT);');
  126. var code = 'counter';
  127. return [code, Blockly.Python.ORDER_ATOMIC];
  128. };
  129. Blockly.Python['basic101_counter_dropdown_button']= function(block) {
  130. var code = block.getFieldValue('SELECTPIN');
  131. return [code, Blockly.Python.ORDER_ATOMIC];
  132. };