blynk.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.blynk');
  10. goog.require('Blockly.Python');
  11. Blockly.Python['blynk_setup'] = function(block) {
  12. var auth = block.getFieldValue('AUTH');
  13. var ssid = block.getFieldValue('SSID');
  14. var pw = block.getFieldValue('PW');
  15. Blockly.Python.addDeclaration('blynk_define','#define BLYNK_PRINT Serial\n');
  16. Blockly.Python.addInclude('blynk_setup','#include <ESP8266WiFi.h>\n'+
  17. '#include <BlynkSimpleEsp8266.h>\n');
  18. var blynk_init = 'char auth[] = "'+ auth + '";\n' +
  19. 'char ssid[] = "' + ssid + '";\n' +
  20. 'char pass[] = "' + pw + '";\n';
  21. Blockly.Python.addDeclaration('Blynk_init', blynk_init);
  22. Blockly.Python.addSetup("Blynk_begin","Blynk.begin(auth,ssid,pass);\n");
  23. var code = 'Blynk.run();\n';
  24. return code;
  25. };
  26. Blockly.Python['blynk_write']= function(block) {
  27. var pin = block.getFieldValue("PIN");
  28. var select = block.getFieldValue("MOD")
  29. var code = 'BLYNK_WRITE('+ pin + ')\n{\n';
  30. // ' int pinValue['+length+']= {0};\n';
  31. // if (select == "zeRGBa") {
  32. // code += ' int blynk_receive_[3]= {0};\n'
  33. // code += ' blynk_receive_[0] = param[0].asInt();\n'+
  34. // ' blynk_receive_[1] = param[1].asInt();\n'+
  35. // ' blynk_receive_[2] = param[2].asInt();\n'
  36. // } else {
  37. // for (let i = 0; i < length; i++) {
  38. // code += ' '+variable+'['+ i +'] = param['+i+'].asInt()';
  39. // }
  40. // }
  41. let statement = Blockly.Python.statementToCode(block, 'STATEMENT');
  42. code += statement +'\n}\n';
  43. Blockly.Python.addFunction("Blynk_Write"+pin, code);
  44. return '';
  45. };
  46. Blockly.Python['blynk_write_getData'] = function(block) {
  47. //var pinNum = block.getFieldValue('PINSNUM');
  48. var code = '';
  49. var select = block.getFieldValue("MOD")
  50. if (select == "zeRGBa" || select == "Joystick") {
  51. let index = block.getFieldValue("SELINDEX");
  52. code = "param["+ index+ "].asInt()";
  53. } else {
  54. code = "param.asInt()";
  55. }
  56. return [code, Blockly.Python.ORDER_ATOMIC];
  57. };
  58. Blockly.Python['blynk_write_getData_joystick'] = function(block) {
  59. //var pinNum = block.getFieldValue('PINSNUM');
  60. var code = '';
  61. var select = block.getFieldValue("MOD")
  62. if (select == "zeRGBa" || select == "Joystick") {
  63. let index = block.getFieldValue("SELINDEX");
  64. code = "param["+ index+ "].asInt()";
  65. } else {
  66. code = "param.asInt()";
  67. }
  68. return [code, Blockly.Python.ORDER_ATOMIC];
  69. };
  70. Blockly.Python['blynk_write_getData_zeRGBa'] = function(block) {
  71. //var pinNum = block.getFieldValue('PINSNUM');
  72. var code = '';
  73. var select = block.getFieldValue("MOD")
  74. if (select == "zeRGBa" || select == "Joystick") {
  75. let index = block.getFieldValue("SELINDEX");
  76. code = "param["+ index+ "].asInt()";
  77. } else {
  78. code = "param.asInt()";
  79. }
  80. return [code, Blockly.Python.ORDER_ATOMIC];
  81. };
  82. Blockly.Python['blynk_write_getData_GraOrAcc'] = function(block) {
  83. //var pinNum = block.getFieldValue('PINSNUM');
  84. let index = block.getFieldValue("SELINDEX");
  85. var code = "param["+ index+ "].asFloat()";
  86. return [code, Blockly.Python.ORDER_ATOMIC];
  87. };
  88. Blockly.Python['blynk_write_getData_GPS'] = function(block) {
  89. //var pinNum = block.getFieldValue('PINSNUM');
  90. let index = block.getFieldValue("SELINDEX");
  91. var code = "param["+ index+ "].asFloat()";
  92. return [code, Blockly.Python.ORDER_ATOMIC];
  93. };
  94. Blockly.Python['blynk_read']= function(block) {
  95. var pin = block.getFieldValue("PIN");
  96. var code = 'BLYNK_READ('+ pin + ')\n{\n';
  97. for (var n = 0; n < this.itemCount_; n++) {
  98. var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  99. Blockly.Python.ORDER_NONE) || '0';
  100. if (block.itemCount_ == 1) {
  101. code += ' Blynk.virtualWrite(' + pin + ',' + item_value + ');\n'
  102. } else {
  103. if (n == 0) code += ' Blynk.virtualWrite(' + pin + ',' +item_value;
  104. else code += ',' + item_value;
  105. if (n == block.itemCount_-1) code += ');\n';
  106. }
  107. }
  108. code += '\n}\n';
  109. Blockly.Python.addFunction("Blynk_Read"+pin, code);
  110. return '';
  111. };
  112. Blockly.Python['blynk_virtualWrite'] = function(block) {
  113. var pin = block.getFieldValue("PIN");
  114. var code = '';
  115. for (var n = 0; n < this.itemCount_; n++) {
  116. var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  117. Blockly.Python.ORDER_NONE) || '0';
  118. if (block.itemCount_ == 1) {
  119. code += 'Blynk.virtualWrite(' + pin + ',' + item_value + ');\n'
  120. } else {
  121. if (n == 0) code += 'Blynk.virtualWrite(' + pin + ',' +item_value;
  122. else code += ',' + item_value;
  123. if (n == block.itemCount_-1) code += ');\n';
  124. }
  125. }
  126. return code;
  127. };
  128. Blockly.Python['blynk_timer'] = function(block) {
  129. var time = block.getFieldValue("TIME");
  130. var statement = Blockly.Python.statementToCode(block, 'DO');
  131. Blockly.Python.addDeclaration("Blynk_timer_dec", "BlynkTimer timer;\n")
  132. Blockly.Python.addFunction("Blynk_timer_func"+time, "void BF_T"+time+ "() {\n"+ statement + "}\n");
  133. Blockly.Python.addSetup("Blynk_timer_setup_"+time, "timer.setInterval("+ time+"L, BF_T"+ time +");\n")
  134. var code = "timer.run();\n"
  135. return code;
  136. };
  137. Blockly.Python['blynk_connect']= function(block) {
  138. var content = Blockly.Python.valueToCode(block, 'STATEMENT', Blockly.Python.ORDER_ATOMIC);
  139. var code = "BLYNK_CONNECT() {\n"+ content + "\n"+ "}\n";
  140. Blockly.Python.addFunction("Blynk_Connect", code);
  141. return '';
  142. };
  143. Blockly.Python['blynk_log'] = function(block) {
  144. var content = Blockly.Python.valueToCode(block, 'STATEMENT', Blockly.Python.ORDER_ATOMIC);
  145. var code = "BLYNK_LOG() {\n"+ content + "\n"+ "}\n";
  146. return code;
  147. };