esp32.js
3.1 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
/**
* 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.esp32');
goog.require('Blockly.Arduino');
Blockly.Arduino['arduino_pin_esp32SetPwmOutput'] = function(block) {
var arg0 = block.getFieldValue('PIN') || '0';
var arg1 = Blockly.Arduino.valueToCode(block, 'OUT', Blockly.Arduino.ORDER_UNARY_POSTFIX) || 0;
var arg2 = block.getFieldValue('CH') || '0';
Blockly.Arduino.setups_['esp32SetPwmOutput' + arg0] = 'ledcSetup(' + arg2 + ', 490, 8);';
Blockly.Arduino.setups_['esp32SetPwmOutput2' + arg0] = 'ledcAttachPin(' + arg0 + ', ' + arg2 + ');';
var code = "ledcWrite(" + arg2 + ", " + arg1 + ");\n";
return code;
};
Blockly.Arduino['arduino_pin_esp32SetDACOutput'] = function(block) {
var arg0 = block.getFieldValue('PIN') || '0';
var arg1 = Blockly.Arduino.valueToCode(block, 'OUT', Blockly.Arduino.ORDER_UNARY_POSTFIX) || 0;
var code = "dacWrite(" + arg0 + ", " + arg1 + ");\n";
return code;
};
Blockly.Arduino['arduino_pin_esp32ReadTouchPin'] = function(block) {
var arg0 = block.getFieldValue('PIN') || '0';
var code = "touchRead(" + arg0 + ")";
return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino['arduino_pin_esp32SetServoOutput'] = function(block) {
var arg0 = block.getFieldValue('PIN') || '0';
var arg1 = Blockly.Arduino.valueToCode(block, 'OUT', Blockly.Arduino.ORDER_UNARY_POSTFIX) || 0;
var arg2 = block.getFieldValue('CH') || '0';
Blockly.Arduino.includes_['esp32SetServoOutput'] = '#include <Servo.h>';
Blockly.Arduino.definitions_['esp32SetServoOutput' + arg0] = 'Servo servo_' + arg0 + ';';
Blockly.Arduino.setups_['esp32SetServoOutput' + arg0] = 'servo_' + arg0 + '.attach' + '(' + arg0 + ', ' + arg2 + ');';
var code = 'servo_' + arg0 + '.write' + '(' + arg1 + ');\n';
return code;
};
Blockly.Arduino['arduino_pin_esp32AttachInterrupt'] = function(block) {
var arg0 = block.getFieldValue('PIN') || '0';
var arg1 = block.getFieldValue('MODE') || 'RISING';
var branch = Blockly.Arduino.statementToCode(block, 'SUBSTACK');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
Blockly.Arduino.definitions_['definitions_ISR_' + arg1 + arg0] =
'void IRAM_ATTR ISR_' + arg1 + '_' + arg0 + '() {\n' + branch + '}';
var code = 'attachInterrupt(' + arg0 + ', ISR_' + arg1 + '_' + arg0 + ', ' + arg1 + ');\n';
return code;
};
Blockly.Arduino['arduino_pin_esp32DetachInterrupt'] = function(block) {
var arg0 = block.getFieldValue('PIN') || '0';
var code = 'detachInterrupt(' + arg0 + ');\n';
return code;
};