123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /**
- * @license Licensed under the Apache License, Version 2.0 (the "License"):
- * http://www.apache.org/licenses/LICENSE-2.0
- */
- /**
- * @fileoverview Arduino code generator for the Time blocks.
- * Arduino built-in function docs: http://arduino.cc/en/Reference/HomePage
- */
- 'use strict';
- goog.provide('Blockly.Python.time');
- goog.require('Blockly.Python');
- /**
- * Code generator for the delay Arduino block.
- * Arduino code: loop { delay(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['time_delay'] = function(block) {
- var delayTime = Blockly.Python.valueToCode(
- block, 'DELAY_TIME_MILI', Blockly.Python.ORDER_ATOMIC) || '0';
- var code = 'delay(' + delayTime + ');\n';
- return code;
- };
- /**
- * Code generator for the delayMicroseconds block.
- * Arduino code: loop { delayMicroseconds(X); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['time_delaymicros'] = function(block) {
- var delayTimeMs = Blockly.Python.valueToCode(
- block, 'DELAY_TIME_MICRO', Blockly.Python.ORDER_ATOMIC) || '0';
- var code = 'delayMicroseconds(' + delayTimeMs + ');\n';
- return code;
- };
- /**
- * Code generator for the elapsed time in milliseconds block.
- * Arduino code: loop { millis() }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {array} Completed code with order of operation.
- */
- Blockly.Python['time_millis'] = function(block) {
- var code = 'millis()';
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- /**
- * Code generator for the elapsed time in microseconds block.
- * Arduino code: loop { micros() }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {array} Completed code with order of operation.
- */
- Blockly.Python['time_micros'] = function(block) {
- var code = 'micros()';
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
- /**
- * Code generator for the wait forever (end of program) block
- * Arduino code: loop { while(true); }
- * @param {!Blockly.Block} block Block to generate the code from.
- * @return {string} Completed code.
- */
- Blockly.Python['infinite_loop'] = function(block) {
- return 'while(true);\n';
- };
- Blockly.Python['time_chrono_setup'] = function(block) {
- Blockly.Python.addInclude("chrono.h", "#include <Chrono.h>\n");
- Blockly.Python.addDeclaration("chrono_dec", "Chrono myChrono;\n");
- }
- Blockly.Python['time_chrono_reset'] = function(block) {
- var code = "myChrono.restart();\n";
- return code;
- }
- Blockly.Python['time_chrono_elapsed'] = function(block) {
- var code = "myChrono.elapsed()";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['time_chrono_timeCheck'] = function(block) {
- var time = Blockly.Python.valueToCode(this, "TIME", Blockly.Python.ORDER_ATOMIC);
- var code = "myChrono.hasPassed(" + time + ")";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['time_everySecond'] = function(block) {
- var time = Blockly.Python.valueToCode(this, "TIME", Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addDeclaration("everySecondState_"+time ,"int everySecondState_"+time+" = 0;")
- var stack_statements = Blockly.Python.statementToCode(block, "STACK", Blockly.Python.ORDER_ATOMIC)
- if (time >= 2)
- var code = "if ((millis() / 1000) % " + time + " == " + (time - 2) + " && everySecondState_"+time+" == 0) {\n"+
- " "+stack_statements+"\n"+
- " everySecondState_"+time+" = 1;\n"+
- "} else if ((millis() / 1000) % " + time + " == " + (time-1) +") {\n"+
- " everySecondState_"+time+" = 0;\n"+
- "}\n"
- else {
- var code = "if ((millis() / 100) % 10 == 1 && everySecondState_"+time+" == 0) {\n"+
- " "+stack_statements+"\n"+
- " everySecondState_"+time+" = 1;\n"+
- "} else if ((millis() / 100) % 10 == 2 && everySecondState_"+time+" == 1) {\n"+
- " everySecondState_"+time+" = 0;\n"+
- "}\n"
- }
- return code;
- }
- Blockly.Python['time_everyMilliSecond'] = function(block) {
- var time = Blockly.Python.valueToCode(this, "TIME", Blockly.Python.ORDER_ATOMIC);
- Blockly.Python.addDeclaration("timePeriodExecutionState_" + time, "int timePeriodExecutionState_" + time + " = 0;")
- var stack_statements = Blockly.Python.statementToCode(block, "STACK", Blockly.Python.ORDER_ATOMIC)
- var code = "if ((millis()) % " + time + " == " + (time - 1) + " && timePeriodExecutionState_" + time + " == 0) {\n" +
- " " + stack_statements + "\n" +
- " timePeriodExecutionState_" + time + " = 1;\n" +
- "} else if ((millis()) % " + time + " == " + 0 + ") {\n" +
- " timePeriodExecutionState_" + time + " = 0;\n" +
- "}\n"
- return code;
- }
|