time.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Arduino code generator for the Time blocks.
  7. * Arduino built-in function docs: http://arduino.cc/en/Reference/HomePage
  8. */
  9. 'use strict';
  10. goog.provide('Blockly.Python.time');
  11. goog.require('Blockly.Python');
  12. /**
  13. * Code generator for the delay Arduino block.
  14. * Arduino code: loop { delay(X); }
  15. * @param {!Blockly.Block} block Block to generate the code from.
  16. * @return {string} Completed code.
  17. */
  18. Blockly.Python['time_delay'] = function(block) {
  19. var delayTime = Blockly.Python.valueToCode(
  20. block, 'DELAY_TIME_MILI', Blockly.Python.ORDER_ATOMIC) || '0';
  21. var code = 'delay(' + delayTime + ');\n';
  22. return code;
  23. };
  24. /**
  25. * Code generator for the delayMicroseconds block.
  26. * Arduino code: loop { delayMicroseconds(X); }
  27. * @param {!Blockly.Block} block Block to generate the code from.
  28. * @return {string} Completed code.
  29. */
  30. Blockly.Python['time_delaymicros'] = function(block) {
  31. var delayTimeMs = Blockly.Python.valueToCode(
  32. block, 'DELAY_TIME_MICRO', Blockly.Python.ORDER_ATOMIC) || '0';
  33. var code = 'delayMicroseconds(' + delayTimeMs + ');\n';
  34. return code;
  35. };
  36. /**
  37. * Code generator for the elapsed time in milliseconds block.
  38. * Arduino code: loop { millis() }
  39. * @param {!Blockly.Block} block Block to generate the code from.
  40. * @return {array} Completed code with order of operation.
  41. */
  42. Blockly.Python['time_millis'] = function(block) {
  43. var code = 'millis()';
  44. return [code, Blockly.Python.ORDER_ATOMIC];
  45. };
  46. /**
  47. * Code generator for the elapsed time in microseconds block.
  48. * Arduino code: loop { micros() }
  49. * @param {!Blockly.Block} block Block to generate the code from.
  50. * @return {array} Completed code with order of operation.
  51. */
  52. Blockly.Python['time_micros'] = function(block) {
  53. var code = 'micros()';
  54. return [code, Blockly.Python.ORDER_ATOMIC];
  55. };
  56. /**
  57. * Code generator for the wait forever (end of program) block
  58. * Arduino code: loop { while(true); }
  59. * @param {!Blockly.Block} block Block to generate the code from.
  60. * @return {string} Completed code.
  61. */
  62. Blockly.Python['infinite_loop'] = function(block) {
  63. return 'while(true);\n';
  64. };
  65. Blockly.Python['time_chrono_setup'] = function(block) {
  66. Blockly.Python.addInclude("chrono.h", "#include <Chrono.h>\n");
  67. Blockly.Python.addDeclaration("chrono_dec", "Chrono myChrono;\n");
  68. }
  69. Blockly.Python['time_chrono_reset'] = function(block) {
  70. var code = "myChrono.restart();\n";
  71. return code;
  72. }
  73. Blockly.Python['time_chrono_elapsed'] = function(block) {
  74. var code = "myChrono.elapsed()";
  75. return [code, Blockly.Python.ORDER_ATOMIC];
  76. }
  77. Blockly.Python['time_chrono_timeCheck'] = function(block) {
  78. var time = Blockly.Python.valueToCode(this, "TIME", Blockly.Python.ORDER_ATOMIC);
  79. var code = "myChrono.hasPassed(" + time + ")";
  80. return [code, Blockly.Python.ORDER_ATOMIC];
  81. }
  82. Blockly.Python['time_everySecond'] = function(block) {
  83. var time = Blockly.Python.valueToCode(this, "TIME", Blockly.Python.ORDER_ATOMIC);
  84. Blockly.Python.addDeclaration("everySecondState_"+time ,"int everySecondState_"+time+" = 0;")
  85. var stack_statements = Blockly.Python.statementToCode(block, "STACK", Blockly.Python.ORDER_ATOMIC)
  86. if (time >= 2)
  87. var code = "if ((millis() / 1000) % " + time + " == " + (time - 2) + " && everySecondState_"+time+" == 0) {\n"+
  88. " "+stack_statements+"\n"+
  89. " everySecondState_"+time+" = 1;\n"+
  90. "} else if ((millis() / 1000) % " + time + " == " + (time-1) +") {\n"+
  91. " everySecondState_"+time+" = 0;\n"+
  92. "}\n"
  93. else {
  94. var code = "if ((millis() / 100) % 10 == 1 && everySecondState_"+time+" == 0) {\n"+
  95. " "+stack_statements+"\n"+
  96. " everySecondState_"+time+" = 1;\n"+
  97. "} else if ((millis() / 100) % 10 == 2 && everySecondState_"+time+" == 1) {\n"+
  98. " everySecondState_"+time+" = 0;\n"+
  99. "}\n"
  100. }
  101. return code;
  102. }
  103. Blockly.Python['time_everyMilliSecond'] = function(block) {
  104. var time = Blockly.Python.valueToCode(this, "TIME", Blockly.Python.ORDER_ATOMIC);
  105. Blockly.Python.addDeclaration("timePeriodExecutionState_" + time, "int timePeriodExecutionState_" + time + " = 0;")
  106. var stack_statements = Blockly.Python.statementToCode(block, "STACK", Blockly.Python.ORDER_ATOMIC)
  107. var code = "if ((millis()) % " + time + " == " + (time - 1) + " && timePeriodExecutionState_" + time + " == 0) {\n" +
  108. " " + stack_statements + "\n" +
  109. " timePeriodExecutionState_" + time + " = 1;\n" +
  110. "} else if ((millis()) % " + time + " == " + 0 + ") {\n" +
  111. " timePeriodExecutionState_" + time + " = 0;\n" +
  112. "}\n"
  113. return code;
  114. }