-
Notifications
You must be signed in to change notification settings - Fork 19
/
CustomNodeBlocks.js
555 lines (507 loc) · 18.5 KB
/
CustomNodeBlocks.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Block colors
var block_color_io=20;
var block_color_timer = 60;
var block_color_leds = 87;
Blockly.Lua['pin_mode'] = function (block) {
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var mode = block.getFieldValue('MODE');
var code = "gpio.mode(" + pin + ", " + mode + ")\n";
return code;
};
Blockly.Blocks['pin_mode'] = {
init: function () {
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown([["input", "gpio.INPUT"], ["output", "gpio.OUTPUT"]]), "MODE");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(block_color_io);
this.setTooltip('Set the mode - input/output for a pin.');
this.setHelpUrl('http://www.example.com/');
}
};
Blockly.Lua['pin_write'] = function (block) {
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var level = Blockly.Lua.valueToCode(block, 'level',
Blockly.Lua.ORDER_ATOMIC) || 'False';
var code = "gpio.write(" + pin + ", " + level + " and 1 or 0)\n";
return code;
};
Blockly.Blocks['pin_write'] = {
init: function () {
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.appendValueInput("level")
.setCheck("Boolean")
.appendField("write");
this.setInputsInline(true);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(block_color_io);
this.setTooltip('Write to a pin');
this.setHelpUrl('http://www.example.com/');
}
};
Blockly.Lua['pin_read'] = function (block) {
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var code = "gpio.read(" + pin + ")";
return [code, Blockly.Lua.ORDER_FUNCTION_CALL];
};
Blockly.Blocks['pin_read'] = {
init: function () {
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.appendDummyInput()
.appendField("read");
this.setInputsInline(true);
this.setOutput(true);
this.setColour(block_color_io);
this.setTooltip('');
this.setHelpUrl('http://www.example.com/');
}
};
Blockly.Lua['analog_read'] = function (block) {
// Analog pin is fixed on the esp.
var code = "adc.read(0)";
return [code, Blockly.Lua.ORDER_FUNCTION_CALL];
};
Blockly.Blocks['analog_read'] = {
init: function () {
this.appendDummyInput()
.appendField('Read analog');
this.setOutput(true);
this.setColour(block_color_io);
}
};
Blockly.Lua['tmr_alarm'] = function (block) {
// Repeat string
var timer = Blockly.Lua.valueToCode(block, 'timer',
Blockly.Lua.ORDER_ATOMIC) || 0;
var interval = Blockly.Lua.valueToCode(block, 'interval',
Blockly.Lua.ORDER_ATOMIC) || 100;
var repeat = block.getFieldValue('REPEAT');
var branch = Blockly.Lua.statementToCode(block, 'DO') || '';
var code = 'tmr.alarm(' + timer + ',' + interval + ', ' + repeat + ', function() \n' + branch + '\nend )\n';
return code;
};
Blockly.Blocks['tmr_alarm'] = {
init: function() {
this.appendValueInput("timer")
.setCheck("Number")
.appendField("timer");
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown([["every", "1"], ["after", "0"]]), "REPEAT");
this.appendValueInput("interval")
.setCheck("Number");
this.appendStatementInput("DO")
.appendField("millis do");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(block_color_timer);
this.setTooltip('');
}
};
Blockly.Lua['tmr_stop'] = function (block) {
var timer = Blockly.Lua.valueToCode(block, 'timer',
Blockly.Lua.ORDER_ATOMIC) || 0;
var code = 'tmr.stop(' + timer + ')\n';
return code;
};
Blockly.Blocks['tmr_stop'] = {
init: function () {
this.appendValueInput("timer")
.appendField("stop timer");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setTooltip('');
this.setColour(block_color_timer);
}
};
Blockly.Lua['text_rep'] = function (block) {
// Repeat string
var text = Blockly.Lua.valueToCode(block, 'TEXT',
Blockly.Lua.ORDER_ATOMIC) || '\'\'';
var count = Blockly.Lua.valueToCode(block, 'COUNT',
Blockly.Lua.ORDER_ATOMIC) || '0';
var code = 'string.rep(' + text + ', ' + count + ')';
return [code, Blockly.Lua.ORDER_FUNCTION_CALL];
};
Blockly.Blocks['text_rep'] = {
init: function () {
this.appendValueInput("TEXT")
.setCheck(["String", "Colour"])
.appendField("repeat text");
this.appendValueInput("COUNT")
.setCheck("Number")
.appendField("for");
this.appendDummyInput()
.appendField("times");
this.setInputsInline(true);
this.setOutput(true, "String");
this.setColour(160);
this.setTooltip('');
}
};
Blockly.Lua['ws2812_writergb'] = function (block) {
// Ws2812 write rgb list
// Decode a list(table) of RGB not a string.
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var data = Blockly.Lua.valueToCode(block, 'data',
Blockly.Lua.ORDER_ATOMIC) || '\'\'';
var code = 'ws2812.writergb(' + pin + ", " + data + ')\n';
return code;
};
Blockly.Blocks['ws2812_writergb'] ={
init: function () {
this.appendDummyInput()
.appendField("ws2812 output");
this.appendValueInput("data")
.setCheck("String")
.appendField("colour list");
this.appendDummyInput()
.appendField("on ");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setInputsInline(false);
this.setColour(block_color_leds);
this.setTooltip('');
}
};
Blockly.Lua['dht_temp'] = function(block) {
// Read DHT temperature
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
// To get one value, we have to construct in brackets, then deref.
var code= '({dht.read(' + pin + ')})[2]';
return [code, Blockly.Lua.ORDER_FUNCTION_CALL];
};
Blockly.Blocks['dht_temp'] = {
init: function() {
this.appendDummyInput()
.appendField("Read dht temperature on");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.setOutput(true);
this.setColour(block_color_io);
this.setTooltip("Read the temperature from a dht sensor");
}
};
Blockly.Lua['dht_humidity'] = function(block) {
// Read DHT temperature
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
// To get one value, we have to construct in brackets, then deref.
var code= '({dht.read(' + pin + ')})[3]';
return [code, Blockly.Lua.ORDER_FUNCTION_CALL];
};
Blockly.Blocks['dht_humidity'] = {
init: function() {
this.appendDummyInput()
.appendField("Get dht humidity on");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.setOutput(true);
this.setColour(block_color_io);
this.setTooltip("Read the humidity from a dht sensor");
}
};
/* Redefine colours for use with rgb devices like the ws2812. */
/* Keep colours as chars */
var repack_colour_=function(colour) {
var r = parseInt(colour.substring(2, 3), 16);
var g = parseInt(colour.substring(4, 5), 16);
var b = parseInt(colour.substring(6, 7), 16);
return r + "," + g + "," + b;
};
Blockly.Lua['colour_picker'] = function(block) {
// Colour picker.
var code = 'string.char(' + repack_colour_(block.getFieldValue('COLOUR')) + ')';
return [code, Blockly.Lua.ORDER_ATOMIC];
};
Blockly.Lua['colour_random'] = function(block) {
// Generate a random colour.
var functionName = Blockly.Lua.provideFunction_(
'colour_random',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '()',
' local r= math.random(0, 2^5 - 1)',
' local g= math.random(0, 2^5 - 1)',
' local b= math.random(0, 2^5 - 1)',
' return string.char(r, g, b)',
'end']);
var code = functionName + '()';
return [code, Blockly.Lua.ORDER_HIGH];
};
Blockly.Lua['colour_rgb'] = function(block) {
// Compose a colour from RGB components.
var r = Blockly.Lua.valueToCode(block, 'RED',
Blockly.Lua.ORDER_NONE) || 0;
var g = Blockly.Lua.valueToCode(block, 'GREEN',
Blockly.Lua.ORDER_NONE) || 0;
var b = Blockly.Lua.valueToCode(block, 'BLUE',
Blockly.Lua.ORDER_NONE) || 0;
var code = 'string.char(' + r + ', ' + g + ', ' + b + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
Blockly.Lua['colour_blend'] = function(block) {
// Blend two colours together.
var functionName = Blockly.Lua.provideFunction_(
'colour_blend',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(colour1, colour2, ratio)',
' local r1 = string.byte(colour1, 0)',
' local r2 = string.byte(colour2, 0)',
' local g1 = string.byte(colour1, 1)',
' local g2 = string.byte(colour2, 1)',
' local b1 = string.byte(colour1, 2)',
' local b2 = string.byte(colour2, 2)',
' local ratio = math.min(1, math.max(0, ratio))',
' local r = math.floor(r1 * (1 - ratio) + r2 * ratio + .5)',
' local g = math.floor(g1 * (1 - ratio) + g2 * ratio + .5)',
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
' return string.char(r, g, b)',
'end']);
var colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
Blockly.Lua.ORDER_NONE) || 'string.char(0, 0, 0)';
var colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
Blockly.Lua.ORDER_NONE) || 'string.char(0, 0, 0)';
var ratio = Blockly.Lua.valueToCode(block, 'RATIO',
Blockly.Lua.ORDER_NONE) || 0;
var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
/* Fixes (to be pushed back into lua/lists.js - update_libs.sh needs to accommodate this properly) */
Blockly.Lua['lists_split'] = function(block) {
var mode = block.getFieldValue('MODE');
var input = Blockly.Lua.valueToCode(block, 'INPUT');
if(mode == 'JOIN') {
var code = "table.concat(" + input + ")";
return [code, Blockly.Lua.ORDER_HIGH];
} else {
//
throw "Woops - not yet implemented";
}
};
Blockly.Blocks['lists_combine'] = {
init: function() {
this.appendDummyInput()
.appendField("combine");
this.appendValueInput("first")
.setCheck("Array");
this.appendValueInput("second")
.setCheck("Array");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(65);
}
};
/* Combine two lists into one list */
Blockly.Lua['lists_combine'] = function(block) {
var first = Blockly.Lua.valueToCode(block, 'first');
var second = Blockly.Lua.valueToCode(block, 'second');
var functionName = Blockly.Lua.provideFunction_(
'lists_combine',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(first, second)',
' local new_list = {}',
' for item=1,#first do',
' table.insert(new_list, first[item])',
' end',
' for item=1, #second do',
' table.insert(new_list, second[item])',
' end',
' return new_list',
'end']);
var code = functionName + '(' + first + ',\n ' + second + ')';
return [code, Blockly.Lua.ORDER_HIGH];
};
Blockly.Blocks['lights_led_grid'] ={
init: function () {
this.appendDummyInput()
.appendField("led grid:");
var grid_width = 8; // TODO: Configurable
var grid_height = 8;
for(var row=0; row<grid_height; row++) {
var line = this.appendDummyInput().appendField(row + " ");
for(var column=0; column<grid_width; column++) {
// TODO: Option, variables or colours.
line = line.appendField(new Blockly.FieldColour('#000000'), 'd' + row + '_' + column );
}
}
this.setOutput(true);
this.setColour(block_color_leds);
this.setTooltip('Make a grid of colours for lights.');
}
};
Blockly.Lua['lights_led_grid'] = function(block) {
var code = 'string.char(';
var grid_width = 8; // TODO: Configurable
var grid_height = 8;
var next_comma = "\n ";
for(var row=0; row<grid_height; row++) {
var prefix="d" + row + "_";
for(var column=0; column<grid_width; column++) {
code += next_comma + repack_colour_(block.getFieldValue(prefix + column));
next_comma = ", ";
}
next_comma = ",\n ";
}
code += ')';
return [code, Blockly.Lua.ORDER_ATOMIC]
};
Blockly.Blocks['pwm_setup'] ={
init: function () {
this.appendDummyInput()
.appendField("Setup PWM:");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.appendValueInput("frequency")
.setCheck("Number")
.appendField("frequency");
this.setColour(block_color_io);
this.setTooltip("Setup a pin for PWM");
this.setPreviousStatement(true);
this.setNextStatement(true);
}
};
Blockly.Lua['pwm_setup'] = function(block) {
// Get the pin
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var frequency = Blockly.Lua.valueToCode(block, 'frequency',
Blockly.Lua.ORDER_ATOMIC) || 0;
var code= 'pwm.setup(' + pin + ', ' + frequency + ', 0)\n';
return code;
};
Blockly.Blocks['pwm_start'] ={
init: function () {
this.appendDummyInput()
.appendField("Start PWM:");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.appendValueInput("amount")
.setCheck("Number")
.appendField("amount");
this.setColour(block_color_io);
this.setTooltip("Start PWM output at a ratio - 0-1023");
this.setPreviousStatement(true);
this.setNextStatement(true);
}
};
Blockly.Lua['pwm_start'] = function(block) {
// Get the pin
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var ratio = Blockly.Lua.valueToCode(block, 'amount',
Blockly.Lua.ORDER_ATOMIC) || 0;
var code= 'pwm.setduty(' + pin + ', ' + ratio + '); pwm.start(' + pin + ')\n';
return code;
};
Blockly.Blocks['pwm_stop'] ={
init: function () {
this.appendDummyInput()
.appendField("Stop PWM:");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.setColour(block_color_io);
this.setTooltip("Stop PWM");
this.setPreviousStatement(true);
this.setNextStatement(true);
}
};
Blockly.Lua['pwm_stop'] = function(block) {
// Get the pin
var pin = Blockly.Lua.valueToCode(block, 'pin',
Blockly.Lua.ORDER_ATOMIC) || 0;
var code= 'pwm.stop(' + pin + ')\n';
return code;
};
Blockly.Blocks['Servo_setup'] = {
init: function () {
this.appendDummyInput()
.appendField("Setup Servo:");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.setColour(block_color_io);
this.setTooltip("Setup a Pin for use on a servo motor");
this.setPreviousStatement(true);
this.setNextStatement(true);
}};
Blockly.Lua['Servo_setup'] = function(block) {
var pin = Blockly.Lua.valueToCode(block, 'pin', Blockly.Lua.ORDER_ATOMIC) || 0;
var functionName = Blockly.Lua.provideFunction_(
'servo_setup',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(pin)',
' pwm.setup(pin, 50, 0)',
'end']);
return functionName + '(' + pin + ')\n';
};
Blockly.Blocks['Servo_set_position'] = {
init: function () {
this.appendDummyInput()
.appendField("Set Servo Position:");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.appendValueInput("angle")
.setCheck("Number")
.appendField("angle (-90 to 90)");
this.setColour(block_color_io);
this.setTooltip("Set the angle of a servo motor");
this.setPreviousStatement(true);
this.setNextStatement(true);
}};
Blockly.Lua['Servo_set_position'] = function(block) {
var pin = Blockly.Lua.valueToCode(block, 'pin', Blockly.Lua.ORDER_ATOMIC) || 0;
var angle = Blockly.Lua.valueToCode(block, 'angle', Blockly.Lua.ORDER_ATOMIC) || 0;
// Currently need 115 degrees to get 90. It's centred - so some scaling needed. - call it 1.3
var functionName = Blockly.Lua.provideFunction_(
'servo_set_position',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(pin, angle)',
' range_scale = 0.5 * 1.7',
' range_start = (1023 * (1.5 - range_scale))/20.0',
' range_end = (1023 * (1.5 + range_scale))/20.0',
' ratio = range_start + ((range_end - range_start) * (angle+90))/180',
// ' print(range_start, range_end, angle, ratio)',
' pwm.setduty(pin, ratio)',
' pwm.start(pin)',
'end']);
return functionName + '(' + pin + ',' + angle + ')\n';
};
Blockly.Blocks['Servo_detach'] = {
init: function () {
this.appendDummyInput()
.appendField("Detach (stop) Servo:");
this.appendValueInput("pin")
.setCheck("Number")
.appendField("pin");
this.setColour(block_color_io);
this.setTooltip("Detach/Stop a servo motor");
this.setPreviousStatement(true);
this.setNextStatement(true);
}};
Blockly.Lua['Servo_detach'] = function(block) {
var pin = Blockly.Lua.valueToCode(block, 'pin', Blockly.Lua.ORDER_ATOMIC) || 0;
var functionName = Blockly.Lua.provideFunction_(
'Servo_detach',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(pin)',
' pwm.stop(pin)',
'end']);
return functionName + '(' + pin + ')\n';
};