@@ -301,6 +301,80 @@ Blockly.Blocks['lego_boost_sleep'] = {
301
301
this . setHelpUrl ( '' ) ;
302
302
}
303
303
} ;
304
+
305
+ Blockly . Blocks [ 'lego_boost_led_change' ] = {
306
+ init : function ( ) {
307
+ this . appendValueInput ( 'COLOR' ) . appendField ( 'Change LED color to' ) ;
308
+ this . setInputsInline ( true ) ;
309
+ this . setPreviousStatement ( true , null ) ;
310
+ this . setNextStatement ( true , null ) ;
311
+ this . setColour ( lego_boost_color ) ;
312
+ this . setTooltip ( 'Change LED color.' ) ;
313
+ this . setHelpUrl ( '' ) ;
314
+ }
315
+ } ;
316
+
317
+ Blockly . Blocks [ 'lego_boost_led_off' ] = {
318
+ init : function ( ) {
319
+ this . appendDummyInput ( ) . appendField ( 'Turn LED off' ) ;
320
+ this . setInputsInline ( true ) ;
321
+ this . setPreviousStatement ( true , null ) ;
322
+ this . setNextStatement ( true , null ) ;
323
+ this . setColour ( lego_boost_color ) ;
324
+ this . setTooltip ( 'Turn LED off.' ) ;
325
+ this . setHelpUrl ( '' ) ;
326
+ }
327
+ } ;
328
+
329
+ Blockly . Blocks [ 'lego_boost_led_reset' ] = {
330
+ init : function ( ) {
331
+ this . appendDummyInput ( ) . appendField ( 'Reset LED' ) ;
332
+ this . setInputsInline ( true ) ;
333
+ this . setPreviousStatement ( true , null ) ;
334
+ this . setNextStatement ( true , null ) ;
335
+ this . setColour ( lego_boost_color ) ;
336
+ this . setTooltip ( 'Reset LED.' ) ;
337
+ this . setHelpUrl ( '' ) ;
338
+ }
339
+ } ;
340
+
341
+ Blockly . Blocks [ 'lego_boost_get_current_led_color' ] = {
342
+ init : function ( ) {
343
+ this . appendDummyInput ( ) . appendField ( 'Get current LED color' ) ;
344
+ this . setOutput ( true , null ) ;
345
+ this . setColour ( lego_boost_color ) ;
346
+ this . setTooltip ( 'Get current LED color.' ) ;
347
+ this . setHelpUrl ( '' ) ;
348
+ }
349
+ } ;
350
+
351
+ Blockly . Blocks [ 'lego_boost_set_led_brightness' ] = {
352
+ init : function ( ) {
353
+ this . appendValueInput ( 'BRIGHTNESS' )
354
+ . setCheck ( 'Number' )
355
+ . appendField ( 'Set LED brightness to' ) ;
356
+ this . appendDummyInput ( ) . appendField ( '%' ) ;
357
+ this . setInputsInline ( true ) ;
358
+ this . setPreviousStatement ( true , null ) ;
359
+ this . setNextStatement ( true , null ) ;
360
+ this . setColour ( lego_boost_color ) ;
361
+ this . setTooltip (
362
+ 'Set LED brightness procentage. 0% is off, 100% is full brightness.'
363
+ ) ;
364
+ this . setHelpUrl ( '' ) ;
365
+ }
366
+ } ;
367
+
368
+ Blockly . Blocks [ 'lego_boost_color_sensor' ] = {
369
+ init : function ( ) {
370
+ this . appendDummyInput ( ) . appendField ( 'Color sensor' ) ;
371
+ this . setOutput ( true , null ) ;
372
+ this . setColour ( lego_boost_color ) ;
373
+ this . setTooltip ( 'Color sensor.' ) ;
374
+ this . setHelpUrl ( '' ) ;
375
+ }
376
+ } ;
377
+
304
378
/*
305
379
* Generators
306
380
*/
@@ -545,6 +619,61 @@ BlocklyPy['lego_boost_sleep'] = function (block) {
545
619
return code ;
546
620
} ;
547
621
622
+ Blockly . Blocks [ 'lego_boost_led_change' ] . toplevel_init = `
623
+ def hex_to_rgb(value):
624
+ value = value.lstrip('#')
625
+ return (tuple(int(value[i:i+2], 16) for i in (0, 2, 4)))
626
+
627
+ ` ;
628
+
629
+ BlocklyPy [ 'lego_boost_led_change' ] = function ( block ) {
630
+ var value_color = BlocklyPy . valueToCode (
631
+ block ,
632
+ 'COLOR' ,
633
+ BlocklyPy . ORDER_ATOMIC
634
+ ) ;
635
+
636
+ var code =
637
+ 'color = hex_to_rgb(' + value_color + ')\nhub.led.set_color(color)\n' ;
638
+ return code ;
639
+ } ;
640
+
641
+ BlocklyPy [ 'lego_boost_led_off' ] = function ( block ) {
642
+ var code = 'hub.led.set_color((0, 0, 0))\n' ;
643
+ return code ;
644
+ } ;
645
+
646
+ BlocklyPy [ 'lego_boost_led_reset' ] = function ( block ) {
647
+ var code =
648
+ "if hub != 'None':\n hub.led.set_color((0, 0, 255))\nelse:\n hub.led.set_color(0, 0, 0)\n" ;
649
+ return code ;
650
+ } ;
651
+
652
+ Blockly . Blocks [ 'lego_boost_get_current_led_color' ] . toplevel_init = `
653
+ from pylgbst.peripherals import LEDRGB
654
+ ` ;
655
+
656
+ BlocklyPy [ 'lego_boost_get_current_led_color' ] = function ( block ) {
657
+ var code = 'hub.led.get_sensor_data(LEDRGB.MODE_RGB)\n' ;
658
+ return [ code , BlocklyPy . ORDER_NONE ] ;
659
+ } ;
660
+
661
+ BlocklyPy [ 'lego_boost_set_led_brightness' ] = function ( block ) {
662
+ var value_brightness = BlocklyPy . valueToCode (
663
+ block ,
664
+ 'BRIGHTNESS' ,
665
+ BlocklyPy . ORDER_ATOMIC
666
+ ) ;
667
+
668
+ var code = 'hub.port_LED.set_brightness(' + value_brightness + ')\n' ;
669
+ return code ;
670
+ } ;
671
+
672
+ BlocklyPy [ 'lego_boost_color_sensor' ] = function ( block ) {
673
+ var code = 'hub.vision_sensor.color\n' ;
674
+ return [ code , BlocklyPy . ORDER_NONE ] ;
675
+ } ;
676
+
548
677
// Creating a toolbox containing all the main blocks
549
678
// and adding the lego boost catgory.
550
679
const TOOLBOX = {
@@ -974,7 +1103,31 @@ const TOOLBOX = {
974
1103
{
975
1104
kind : 'BLOCK' ,
976
1105
type : 'lego_boost_sleep'
1106
+ } ,
1107
+ {
1108
+ kind : 'BLOCK' ,
1109
+ type : 'lego_boost_led_change'
1110
+ } ,
1111
+ {
1112
+ kind : 'BLOCK' ,
1113
+ type : 'lego_boost_led_off'
1114
+ } ,
1115
+ {
1116
+ kind : 'BLOCK' ,
1117
+ type : 'lego_boost_led_reset'
1118
+ } ,
1119
+ {
1120
+ kind : 'BLOCK' ,
1121
+ type : 'lego_boost_get_current_led_color'
977
1122
}
1123
+ // {
1124
+ // kind: 'BLOCK',
1125
+ // type: 'lego_boost_color_sensor'
1126
+ // }
1127
+ // {
1128
+ // kind: 'BLOCK',
1129
+ // type: 'lego_boost_set_led_brightness'
1130
+ // }
978
1131
]
979
1132
}
980
1133
]
0 commit comments