-
Notifications
You must be signed in to change notification settings - Fork 174
/
ESPUI_blocks.js
279 lines (263 loc) · 14.5 KB
/
ESPUI_blocks.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// This is a block definition for projects like roboblocks
//
// Main Block
Facilino.LANG_COLOUR_HTML = '#BDBDBD';
Facilino.LANG_COLOUR_ESPUI = '#B1B1B1';
Blockly.Blocks['espui'] = {
category: Facilino.locales.getKey('LANG_CATEGORY_HTML'),
subcategory: Facilino.locales.getKey('LANG_SUBCATERGORY_ESPUI'),
helpUrl: Facilino.getHelpUrl('espui'),
tags: ['webinterface'],
examples: ['lol.bly'],
category_colour: Facilino.LANG_COLOUR_HTML,
colour: Facilino.LANG_COLOUR_ESPUI,
init: function() {
var wifiOptions = [['No', false],['Yes', true]];
this.appendDummyInput().appendField('UI Title:').appendField(new Blockly.FieldTextInput(Facilino.locales.getKey('LANG_ESPUI_ESPUI_TITLE')), 'ui_name');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_ESPUI_HOTSPOT')).appendField(new Blockly.FieldDropdown(wifiOptions), 'wifi_option');
this.appendStatementInput('ui_elements').setCheck('ui_element');
this.setColour(Facilino.LANG_COLOUR_ESPUI);
this.setTooltip(Facilino.locales.getKey('LANG_ESPUI_ESPUI_TOOLTIP'));
}
};
Blockly.Arduino['espui'] = function(block) {
var ui_name = block.getFieldValue('ui_name');
var wifi_option = block.getFieldValue('wifi_option');
var ui_elements = Blockly.Arduino.statementToCode(block, 'ui_elements');
Blockly.Arduino.definitions_['define_wifi_h'] = '#include <WiFi.h>';
Blockly.Arduino.definitions_['define_espui_h'] = '#include <ESPUI.h>';
Blockly.Arduino.setups_['setup_espui'] = '\n';
if(wifi_option){
Blockly.Arduino.setups_['setup_espui'] +=
' Serial.begin(115200);\n\n' +
' WiFi.mode(WIFI_AP);\n' +
' WiFi.softAP("' + ui_name + '");\n' +
' Serial.print("IP address: ");\n' +
' Serial.println(WiFi.softAPIP());\n\n';
}
Blockly.Arduino.setups_['setup_espui'] += ui_elements;
Blockly.Arduino.setups_['setup_espui'] += ' ESPUI.begin("' + ui_name + '");\n';
return null;
};
//Elements
Blockly.Blocks['espui_button'] = {
category: Facilino.locales.getKey('LANG_CATEGORY_HTML'),
subcategory: Facilino.locales.getKey('LANG_SUBCATERGORY_ESPUI'),
helpUrl: Facilino.getHelpUrl('espui_button'),
tags: ['webinterface'],
examples: ['lol.bly'],
category_colour: Facilino.LANG_COLOUR_HTML,
colour: Facilino.LANG_COLOUR_ESPUI,
init: function() {
var colour = new Blockly.FieldColour('#000000');
colour.setColours(['#000000','#40e0d0','#50c878','#3498dc','#687894','#e4d422','#eb8921','#e32636']).setColumns(2);
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_BUTTON_BUTTON')).appendField(new Blockly.FieldTextInput(Facilino.locales.getKey('LANG_ESPUI_NAME')), 'ui_name');
//this.appendDummyInput().appendField('UI Color').appendField(new Blockly.FieldDropdown(colorOptions), 'ui_color');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_COLOR')).appendField(colour, 'ui_color');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_TEXT')).appendField(new Blockly.FieldTextInput(Facilino.locales.getKey('LANG_ESPUI_TEXT')), 'button_text');
this.setColour(Facilino.LANG_COLOUR_ESPUI);
this.setPreviousStatement(true, 'ui_element');
this.setNextStatement(true, 'ui_element');
this.setTooltip(Facilino.locales.getKey('LANG_ESPUI_BUTTON_TOOLTIP'));
this.appendStatementInput('on_down').appendField(new Blockly.FieldImage('img/blocks/button_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck(null);
this.appendStatementInput('on_up').appendField(new Blockly.FieldImage('img/blocks/button_released.svg', 24*options.zoom, 24*options.zoom)).setCheck(null);
}
};
Blockly.Arduino['espui_button'] = function(block) {
var ui_name = block.getFieldValue('ui_name');
var color = block.getFieldValue('ui_color');
var colorOptions = {"#000000": "COLOR_NONE", "#40e0d0": "COLOR_TURQUOISE", "#50c878": "COLOR_EMERALD", "#3498dc": "COLOR_PETERRIVER", "#687894": "COLOR_WETASPHALT", "#e4d422": "COLOR_SUNFLOWER", "#eb8921": "COLOR_CARROT", "#e32636": "COLOR_ALIZARIN"};
var ui_color = colorOptions[color];
var button_text = block.getFieldValue('button_text');
var ui_name_clean = ui_name.replace(' ', '_');
var on_down = Blockly.Arduino.statementToCode(block, 'on_down');
var on_up = Blockly.Arduino.statementToCode(block, 'on_up');
Blockly.Arduino.definitions_['define_ui_button_' + ui_name_clean] =
'void button_' + ui_name_clean + '(Control c, int type) {\n' +
' switch(type){\n' +
' case B_DOWN:\n' +
on_down + '\n break;\n' +
' case B_UP:\n' +
on_up + '\n break;\n' +
' }\n' +
'}\n';
var code = ' ESPUI.button("' + ui_name + '", &button_' + ui_name_clean + ', ' + ui_color + ', "' + button_text + '");\n';
return code;
};
Blockly.Blocks['espui_label'] = {
category: Facilino.locales.getKey('LANG_CATEGORY_HTML'),
subcategory: Facilino.locales.getKey('LANG_SUBCATERGORY_ESPUI'),
helpUrl: Facilino.getHelpUrl('espui_label'),
tags: ['webinterface'],
examples: ['lol.bly'],
category_colour: Facilino.LANG_COLOUR_HTML,
colour: Facilino.LANG_COLOUR_ESPUI,
init: function() {
var colour = new Blockly.FieldColour('#000000');
colour.setColours(['#000000','#40e0d0','#50c878','#3498dc','#687894','#e4d422','#eb8921','#e32636']).setColumns(2);
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_LABEL_LABEL')).appendField(new Blockly.FieldTextInput(Facilino.locales.getKey('LANG_ESPUI_NAME')), 'ui_name');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_COLOR')).appendField(colour, 'ui_color');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_TEXT')).appendField(new Blockly.FieldTextInput('value'), 'start_value');
this.setColour(Facilino.LANG_COLOUR_ESPUI);
this.setPreviousStatement(true, 'ui_element');
this.setNextStatement(true, 'ui_element');
this.setTooltip(Facilino.locales.getKey('LANG_ESPUI_LABEL_TOOLTIP'));
}
};
Blockly.Arduino['espui_label'] = function(block) {
var ui_name = block.getFieldValue('ui_name');
var ui_color = block.getFieldValue('ui_color');
var ui_name_clean = ui_name.replace(' ', '_');
var start_value = block.getFieldValue('start_value');
var code = ' ESPUI.label("' + ui_name + '", ' + ui_color + ', "' + start_value + '");\n';
return code;
};
Blockly.Blocks['espui_switcher'] = {
category: Facilino.locales.getKey('LANG_CATEGORY_HTML'),
subcategory: Facilino.locales.getKey('LANG_SUBCATERGORY_ESPUI'),
helpUrl: Facilino.getHelpUrl('espui_switcher'),
tags: ['webinterface'],
examples: ['lol.bly'],
category_colour: Facilino.LANG_COLOUR_HTML,
colour: Facilino.LANG_COLOUR_ESPUI,
init: function() {
var colour = new Blockly.FieldColour('#000000');
colour.setColours(['#000000','#40e0d0','#50c878','#3498dc','#687894','#e4d422','#eb8921','#e32636']).setColumns(2);
var stateOptions = [['Off', 'false'],['On', 'true']];
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_SWITCH_SWITCH')).appendField(new Blockly.FieldTextInput(Facilino.locales.getKey('LANG_ESPUI_NAME')), 'ui_name');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_COLOR')).appendField(colour, 'ui_color');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_STATE')).appendField(new Blockly.FieldDropdown(stateOptions), 'switcher_state');
this.setColour(Facilino.LANG_COLOUR_ESPUI);
this.setPreviousStatement(true, 'ui_element');
this.setNextStatement(true, 'ui_element');
this.setTooltip('A web interface button');
this.appendStatementInput('on_on').appendField(new Blockly.FieldImage('img/blocks/switch_on.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_off').appendField(new Blockly.FieldImage('img/blocks/switch_off.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
}
};
Blockly.Arduino['espui_switcher'] = function(block) {
var ui_name = block.getFieldValue('ui_name');
var color = block.getFieldValue('ui_color');
var colorOptions = {"#000000": "COLOR_NONE", "#40e0d0": "COLOR_TURQUOISE", "#50c878": "COLOR_EMERALD", "#3498dc": "COLOR_PETERRIVER", "#687894": "COLOR_WETASPHALT", "#e4d422": "COLOR_SUNFLOWER", "#eb8921": "COLOR_CARROT", "#e32636": "COLOR_ALIZARIN"};
var ui_color = colorOptions[color];
var switcher_state = block.getFieldValue('switcher_state');
var ui_name_clean = ui_name.replace(' ', '_');
var on_on = Blockly.Arduino.statementToCode(block, 'on_down');
var on_off = Blockly.Arduino.statementToCode(block, 'on_up');
Blockly.Arduino.definitions_['define_ui_switcher_' + ui_name_clean] =
'void switcher_' + ui_name_clean + '(Control c, int type) {\n' +
' switch(type){\n' +
' case S_ACTIVE:\n' +
on_on + '\n break;\n' +
' case S_INACTIVE:\n' +
on_off + '\n break;\n' +
' }\n' +
'}\n';
var code = ' ESPUI.switcher("' + ui_name + '", ' + switcher_state + ', &switcher_' + ui_name_clean + ', ' + ui_color + ');\n';
return code;
};
Blockly.Blocks['espui_pad'] = {
category: Facilino.locales.getKey('LANG_CATEGORY_HTML'),
subcategory: Facilino.locales.getKey('LANG_SUBCATERGORY_ESPUI'),
helpUrl: Facilino.getHelpUrl('espui_pad'),
tags: ['webinterface'],
examples: ['lol.bly'],
category_colour: Facilino.LANG_COLOUR_HTML,
colour: Facilino.LANG_COLOUR_ESPUI,
init: function() {
var colour = new Blockly.FieldColour('#000000');
colour.setColours(['#000000','#40e0d0','#50c878','#3498dc','#687894','#e4d422','#eb8921','#e32636']).setColumns(2);
var centerOptions = [['Yes', 'false'],['No', 'true']];
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_PAD_PAD')).appendField(new Blockly.FieldTextInput(Facilino.locales.getKey('LANG_ESPUI_NAME')), 'ui_name');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_COLOR')).appendField(colour, 'ui_color');
this.appendDummyInput().appendField(Facilino.locales.getKey('LANG_ESPUI_PAD_CENTER')).appendField(new Blockly.FieldDropdown(centerOptions), 'pad_center');
this.setColour(Facilino.LANG_COLOUR_ESPUI);
this.setPreviousStatement(true, 'ui_element');
this.setNextStatement(true, 'ui_element');
this.setTooltip('A web interface button');
this.appendStatementInput('on_down_for').appendField(new Blockly.FieldImage('img/blocks/controller_up_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_up_for').appendField(new Blockly.FieldImage('img/blocks/controller_up_released.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_down_back').appendField(new Blockly.FieldImage('img/blocks/controller_down_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_up_back').appendField(new Blockly.FieldImage('img/blocks/controller_down_released.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_down_left').appendField(new Blockly.FieldImage('img/blocks/controller_right_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_up_left').appendField(new Blockly.FieldImage('img/blocks/controller_right_released.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_down_right').appendField(new Blockly.FieldImage('img/blocks/controller_left_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_up_right').appendField(new Blockly.FieldImage('img/blocks/controller_left_released.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_down_center').appendField(new Blockly.FieldImage('img/blocks/controller_center_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_up_center').appendField(new Blockly.FieldImage('img/blocks/controller_center_released.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.lastOption=this.getFieldValue('pad_center');
},
onchange() {
if (this.lastOption!==this.getFieldValue('pad_center'))
{
if (this.getFieldValue('pad_center')==='false')
{
try{
this.removeInput('on_down_center');
this.removeInput('on_up_center');
this.appendStatementInput('on_down_center').appendField(new Blockly.FieldImage('img/blocks/controller_center_pressed.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
this.appendStatementInput('on_up_center').appendField(new Blockly.FieldImage('img/blocks/controller_center_released.svg', 24*options.zoom, 24*options.zoom)).setCheck('code');
}
catch(e)
{
}
}
else
{
try{
this.removeInput('on_down_center');
this.removeInput('on_up_center');
}
catch(e)
{
}
}
this.lastOption=this.getFieldValue('pad_center');
}
}
};
Blockly.Arduino['espui_pad'] = function(block) {
var ui_name = block.getFieldValue('ui_name');
var color = block.getFieldValue('ui_color');
var colorOptions = {"#000000": "COLOR_NONE", "#40e0d0": "COLOR_TURQUOISE", "#50c878": "COLOR_EMERALD", "#3498dc": "COLOR_PETERRIVER", "#687894": "COLOR_WETASPHALT", "#e4d422": "COLOR_SUNFLOWER", "#eb8921": "COLOR_CARROT", "#e32636": "COLOR_ALIZARIN"};
var ui_color = colorOptions[color];
var pad_center = block.getFieldValue('pad_center');
var ui_name_clean = ui_name.replace(' ', '_');
var on_down_for = Blockly.Arduino.statementToCode(block, 'on_down_for');
var on_up_for = Blockly.Arduino.statementToCode(block, 'on_up_for');
var on_down_back = Blockly.Arduino.statementToCode(block, 'on_down_back');
var on_up_back = Blockly.Arduino.statementToCode(block, 'on_up_back');
var on_down_left = Blockly.Arduino.statementToCode(block, 'on_down_left');
var on_up_left = Blockly.Arduino.statementToCode(block, 'on_up_left');
var on_down_right = Blockly.Arduino.statementToCode(block, 'on_down_right');
var on_up_right = Blockly.Arduino.statementToCode(block, 'on_up_right');
var on_down_center = Blockly.Arduino.statementToCode(block, 'on_down_center');
var on_up_center = Blockly.Arduino.statementToCode(block, 'on_up_center');
Blockly.Arduino.definitions_['define_ui_pad_' + ui_name_clean] =
'void pad_' + ui_name_clean + '(Control c, int type) {\n' +
' switch(type){\n' +
' case P_FOR_DOWN:\n' +
on_down_for + '\n break;\n' +
' case P_FOR_UP:\n' +
on_up_for + '\n break;\n' +
' case P_BACK_DOWN:\n' +
on_down_back + '\n break;\n' +
' case P_BACK_UP:\n' +
on_up_back + '\n break;\n' +
' case P_RIGHT_DOWN:\n' +
on_down_left + '\n break;\n' +
' case P_RIGHT_UP:\n' +
on_up_left + '\n break;\n' +
' case P_LEFT_DOWN:\n' +
on_down_right + '\n break;\n' +
' case P_LEFT_UP:\n' +
on_up_right + '\n break;\n' +
' case P_CENTER_DOWN:\n' +
on_down_center + '\n break;\n' +
' case P_CENTER_UP:\n' +
on_up_center + '\n break;\n' +
' }\n' +
'}\n';
var code = ' ESPUI.pad("' + ui_name + '", ' + pad_center + ', &pad_' + ui_name_clean + ', ' + ui_color + ');\n';
return code;
};