control.js
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Visual Blocks Language
*
* Copyright 2020 openblock.cc.
* https://github.com/openblockcc/openblock-blocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
goog.provide('Blockly.Arduino.control');
goog.require('Blockly.Arduino');
Blockly.Arduino['control_wait'] = function(block) {
var arg0 = Blockly.Arduino.valueToCode(block, 'DURATION',
Blockly.Arduino.ORDER_UNARY_POSTFIX);
var code = "delay(" + arg0 + " * 1000" + ");\n";
return code;
};
Blockly.Arduino['control_repeat'] = function(block) {
var repeats = Blockly.Arduino.valueToCode(block, 'TIMES',
Blockly.Arduino.ORDER_UNARY_POSTFIX);
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code = "for (int index = 0; index < " + repeats + "; index++) {\n";
code += branch;
code += "}\n";
return code;
};
Blockly.Arduino['control_forever'] = function(block) {
if (Blockly.Arduino.firstLoop && !block.getSurroundParent()
&& block.getRootBlock().type === 'event_whenarduinobegin') {
Blockly.Arduino.firstLoop = false;
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code = "}\n\n";
code += "void loop() {\n";
code += branch;
code += Blockly.Arduino.INDENT + "repeat();\n";
return code;
} else {
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code = "while (1) {\n";
code += branch;
code += Blockly.Arduino.INDENT + "repeat();\n}\n";
return code;
}
};
Blockly.Arduino['control_if'] = function(block) {
var argument = Blockly.Arduino.valueToCode(block, 'CONDITION',
Blockly.Arduino.ORDER_NONE) || 'false';
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code = "if (" + argument + ") {\n";
code += branch;
code += "}\n";
return code;
};
Blockly.Arduino['control_if_else'] = function(block) {
var argument = Blockly.Arduino.valueToCode(block, 'CONDITION',
Blockly.Arduino.ORDER_NONE) || 'false';
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var branch2 = Blockly.Arduino.statementToCode(block, 'SUBSTACK2');
branch2 = Blockly.Arduino.addLoopTrap(branch2, block.id);
var code = "if (" + argument + ") {\n";
code += branch;
code += "}\nelse{\n";
code += branch2;
code += "}\n";
return code;
};
Blockly.Arduino['control_wait_until'] = function(block) {
var argument = Blockly.Arduino.valueToCode(block, 'CONDITION',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || 'false';
var code = "while (!" + argument + ") {\n";
code += Blockly.Arduino.INDENT + "repeat();\n}\n";
return code;
};
Blockly.Arduino['control_repeat_until'] = function(block) {
var argument = Blockly.Arduino.valueToCode(block, 'CONDITION',
Blockly.Arduino.ORDER_UNARY_POSTFIX) || 'false';
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code = "while (!" + argument + ") {\n";
code += branch;
code += Blockly.Arduino.INDENT + "repeat();\n}\n";
return code;
};