Skip to content

Commit 566b4c3

Browse files
committed
Added blocks with access to the sensors
1 parent 486fd02 commit 566b4c3

File tree

4 files changed

+282
-12
lines changed

4 files changed

+282
-12
lines changed

examples/LED-demo.jpblockly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"blocks":{"languageVersion":0,"blocks":[{"type":"lego_boost_connect","id":"e]+mHooe7Ytls}.|?e^d","x":107,"y":20,"icons":{"comment":{"text":"Make sure to add the bluetooth address of your own MoveHub! It can be found in the bluetooth settings of your laptop.","pinned":false,"height":80,"width":160}},"inputs":{"BLUETOOTH_ADDRESS":{"block":{"type":"text","id":"b):i%^D-2h,%)L8zw#0Z","fields":{"TEXT":"00:16:53:C3:C2:4F"}}}},"next":{"block":{"type":"lego_boost_sleep","id":"-|5d7;),*QGZpxy.ks,m","inputs":{"SECONDS":{"block":{"type":"math_number","id":".0x2@p`ORJ0It]w|3{AX","fields":{"NUM":1}}}},"next":{"block":{"type":"lego_boost_led_change","id":"R%UrPIdDOR$#42`y~z4)","icons":{"comment":{"text":"Click on the colored box to experiment and choose another color.","pinned":true,"height":80,"width":160}},"inputs":{"COLOR":{"block":{"type":"colour_picker","id":"0S-6tnnJi:UR1UJdge=n","fields":{"COLOUR":"#ff0000"}}}},"next":{"block":{"type":"lego_boost_sleep","id":"(lBR_5x[N#`oKDS8G#1O","inputs":{"SECONDS":{"block":{"type":"math_number","id":"_J-f$z{?J7e+7@t(5`xH","fields":{"NUM":1}}}},"next":{"block":{"type":"lego_boost_led_reset","id":"z|$l$//y$6FrAdZag/n~","next":{"block":{"type":"lego_boost_disconnect","id":"Zjo*_zbFq2(e]f$aHY(*"}}}}}}}}}}}]}}

examples/sensors-demo.jpblockly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"blocks":{"languageVersion":0,"blocks":[{"type":"lego_boost_connect","id":"e]+mHooe7Ytls}.|?e^d","x":102,"y":48,"icons":{"comment":{"text":"Make sure to add the bluetooth address of your own MoveHub! It can be found in the bluetooth settings of your laptop.","pinned":false,"height":80,"width":160}},"inputs":{"BLUETOOTH_ADDRESS":{"block":{"type":"text","id":"b):i%^D-2h,%)L8zw#0Z","fields":{"TEXT":"00:16:53:C3:C2:4F"}}}},"next":{"block":{"type":"lego_boost_detect_color","id":"aY^kL(v4*GPowIO$K[/B","icons":{"comment":{"text":"This type of blocks will use the sensors in order to detect variations of the colour and distance for the selected time period.","pinned":false,"height":80,"width":160}},"inputs":{"TIME":{"block":{"type":"math_number","id":"xpRY*+VA6;QR)qISIt!=","fields":{"NUM":1}}}},"next":{"block":{"type":"lego_boost_disconnect","id":"Zjo*_zbFq2(e]f$aHY(*"}}}}}]}}

examples/sensors.jpblockly

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/lego_boost_python_generators_and_blocks.js

Lines changed: 280 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,146 @@ Blockly.Blocks['lego_boost_set_led_brightness'] = {
365365
}
366366
};
367367

368-
Blockly.Blocks['lego_boost_color_sensor'] = {
368+
Blockly.Blocks['lego_boost_detect_color_and_distance'] = {
369369
init: function () {
370-
this.appendDummyInput().appendField('Color sensor');
371-
this.setOutput(true, null);
370+
this.appendValueInput('TIME')
371+
.setCheck('Number')
372+
.appendField('Detect the color and distance for');
373+
this.appendDummyInput().appendField('seconds');
374+
this.setInputsInline(true);
375+
this.setPreviousStatement(true, null);
376+
this.setNextStatement(true, null);
377+
this.setColour(lego_boost_color);
378+
this.setTooltip(
379+
'Use the color and distance sensor for detection, for a certain period of time.'
380+
);
381+
this.setHelpUrl('');
382+
}
383+
};
384+
385+
Blockly.Blocks['lego_boost_detect_color'] = {
386+
init: function () {
387+
this.appendValueInput('TIME')
388+
.setCheck('Number')
389+
.appendField('Detect the color for');
390+
this.appendDummyInput().appendField('seconds');
391+
this.setInputsInline(true);
392+
this.setPreviousStatement(true, null);
393+
this.setNextStatement(true, null);
394+
this.setColour(lego_boost_color);
395+
this.setTooltip(
396+
'Use the color sensor for detection, for a certain period of time.'
397+
);
398+
this.setHelpUrl('');
399+
}
400+
};
401+
402+
Blockly.Blocks['lego_boost_detect_distance'] = {
403+
init: function () {
404+
this.appendValueInput('TIME')
405+
.setCheck('Number')
406+
.appendField('Detect the distance for');
407+
this.appendDummyInput().appendField('seconds');
408+
this.setInputsInline(true);
409+
this.setPreviousStatement(true, null);
410+
this.setNextStatement(true, null);
411+
this.setColour(lego_boost_color);
412+
this.setTooltip(
413+
'Use the distance sensor for detection, for a certain period of time. Detect the distance until the object placed in front of the robot. The robot can detect a distance from 0 to 10 cm. The number is an integer.'
414+
);
415+
this.setHelpUrl('');
416+
}
417+
};
418+
419+
Blockly.Blocks['lego_boost_detect_reflected_distance'] = {
420+
init: function () {
421+
this.appendValueInput('TIME')
422+
.setCheck('Number')
423+
.appendField('Detect the reflected distance for');
424+
this.appendDummyInput().appendField('seconds');
425+
this.setInputsInline(true);
426+
this.setPreviousStatement(true, null);
427+
this.setNextStatement(true, null);
428+
this.setColour(lego_boost_color);
429+
this.setTooltip(
430+
'Use the distance sensor for detection, for a certain period of time. Detect the reflected distance until the object placed in front of the robot. The result is a float number from 0 to 1.'
431+
);
432+
this.setHelpUrl('');
433+
}
434+
};
435+
436+
Blockly.Blocks['lego_boost_detect_luminosity'] = {
437+
init: function () {
438+
this.appendValueInput('TIME')
439+
.setCheck('Number')
440+
.appendField('Detect the RGB value for');
441+
this.appendDummyInput().appendField('seconds');
442+
this.setInputsInline(true);
443+
this.setPreviousStatement(true, null);
444+
this.setNextStatement(true, null);
445+
this.setColour(lego_boost_color);
446+
this.setTooltip(
447+
'Use the distance sensor for detection, for a certain period of time. Detect the ambient RGB value of the object placed in front of the robot. '
448+
);
449+
this.setHelpUrl('');
450+
}
451+
};
452+
453+
Blockly.Blocks['lego_boost_detect_RGB'] = {
454+
init: function () {
455+
this.appendValueInput('TIME')
456+
.setCheck('Number')
457+
.appendField('Detect the ambient light value for');
458+
this.appendDummyInput().appendField('seconds');
459+
this.setInputsInline(true);
460+
this.setPreviousStatement(true, null);
461+
this.setNextStatement(true, null);
462+
this.setColour(lego_boost_color);
463+
this.setTooltip(
464+
'Use the distance sensor for detection, for a certain period of time. Detect the ambient light value of the enviroment in front of the robot. The result is a float number from 0 to 1.'
465+
);
466+
this.setHelpUrl('');
467+
}
468+
};
469+
470+
Blockly.Blocks['lego_boost_set_sensor_color'] = {
471+
init: function () {
472+
this.appendDummyInput()
473+
.appendField('Set sensor light to color')
474+
.appendField(
475+
new Blockly.FieldDropdown([
476+
['Red', 'COLOR_RED'],
477+
['Blue', 'COLOR_BLUE'],
478+
['Cyan', 'COLOR_CYAN'],
479+
['Yellow', 'COLOR_YELLOW'],
480+
['White', 'COLOR_WHITE'],
481+
['Black', 'COLOR_BLACK']
482+
]),
483+
'COLOR'
484+
)
485+
;
486+
this.setInputsInline(true);
487+
this.setPreviousStatement(true, null);
488+
this.setNextStatement(true, null);
489+
this.setColour(lego_boost_color);
490+
this.setTooltip(
491+
'Set the color of the sensor to one of the selected ones.'
492+
);
493+
this.setHelpUrl('');
494+
}
495+
};
496+
497+
498+
Blockly.Blocks['lego_boost_button_state'] = {
499+
init: function () {
500+
this.appendDummyInput().appendField('Print the button state');
501+
this.setInputsInline(true);
502+
this.setPreviousStatement(true, null);
503+
this.setNextStatement(true, null);
372504
this.setColour(lego_boost_color);
373-
this.setTooltip('Color sensor.');
505+
this.setTooltip(
506+
'Output the state of the button. The state is 0 for not pressed and 1 or 2 for pressed.'
507+
);
374508
this.setHelpUrl('');
375509
}
376510
};
@@ -669,9 +803,116 @@ BlocklyPy['lego_boost_set_led_brightness'] = function (block) {
669803
return code;
670804
};
671805

672-
BlocklyPy['lego_boost_color_sensor'] = function (block) {
673-
var code = 'hub.vision_sensor.color\n';
674-
return [code, BlocklyPy.ORDER_NONE];
806+
Blockly.Blocks['lego_boost_detect_color_and_distance'].toplevel_init = `
807+
from pylgbst.hub import VisionSensor
808+
809+
`;
810+
811+
BlocklyPy['lego_boost_detect_color_and_distance'] = function (block) {
812+
var value_time = BlocklyPy.valueToCode(
813+
block,
814+
'TIME',
815+
BlocklyPy.ORDER_ATOMIC
816+
);
817+
818+
var code = 'def callback(color, distance):\n print("Color: %s / Distance: %s" % (color, distance))\nhub.vision_sensor.subscribe(callback, mode=VisionSensor.COLOR_DISTANCE_FLOAT)\ntime.sleep(' + value_time +')# play with sensor while it waits\nhub.vision_sensor.unsubscribe(callback)\n';
819+
return code;
820+
};
821+
822+
Blockly.Blocks['lego_boost_detect_color'].toplevel_init = `
823+
from pylgbst.hub import VisionSensor
824+
825+
`;
826+
827+
BlocklyPy['lego_boost_detect_color'] = function (block) {
828+
var value_time = BlocklyPy.valueToCode(
829+
block,
830+
'TIME',
831+
BlocklyPy.ORDER_ATOMIC
832+
);
833+
834+
var code = 'def callback(color):\n print("Color: %s" % (color))\nhub.vision_sensor.subscribe(callback, mode=VisionSensor.COLOR_INDEX)\ntime.sleep(' + value_time +')# play with sensor while it waits\nhub.vision_sensor.unsubscribe(callback)\n';
835+
return code;
836+
};
837+
838+
Blockly.Blocks['lego_boost_detect_distance'].toplevel_init = `
839+
from pylgbst.hub import VisionSensor
840+
841+
`;
842+
843+
BlocklyPy['lego_boost_detect_distance'] = function (block) {
844+
var value_time = BlocklyPy.valueToCode(
845+
block,
846+
'TIME',
847+
BlocklyPy.ORDER_ATOMIC
848+
);
849+
850+
var code = 'def callback(distance):\n print("Distance: %s" % (distance))\nhub.vision_sensor.subscribe(callback, mode=VisionSensor.DISTANCE_INCHES)\ntime.sleep(' + value_time +')# play with sensor while it waits\nhub.vision_sensor.unsubscribe(callback)\n';
851+
return code;
852+
};
853+
854+
Blockly.Blocks['lego_boost_detect_reflected_distance'].toplevel_init = `
855+
from pylgbst.hub import VisionSensor
856+
857+
`;
858+
859+
BlocklyPy['lego_boost_detect_reflected_distance'] = function (block) {
860+
var value_time = BlocklyPy.valueToCode(
861+
block,
862+
'TIME',
863+
BlocklyPy.ORDER_ATOMIC
864+
);
865+
866+
var code = 'def callback(reflected):\n print("Reflected distance: %s" % (reflected))\nhub.vision_sensor.subscribe(callback, mode=VisionSensor.DISTANCE_REFLECTED)\ntime.sleep(' + value_time +')# play with sensor while it waits\nhub.vision_sensor.unsubscribe(callback)\n';
867+
return code;
868+
};
869+
870+
Blockly.Blocks['lego_boost_detect_luminosity'].toplevel_init = `
871+
from pylgbst.hub import VisionSensor
872+
873+
`;
874+
875+
BlocklyPy['lego_boost_detect_luminosity'] = function (block) {
876+
var value_time = BlocklyPy.valueToCode(
877+
block,
878+
'TIME',
879+
BlocklyPy.ORDER_ATOMIC
880+
);
881+
882+
var code = 'def callback(luminosity):\n print("Ambient light: %s" % (luminosity))\nhub.vision_sensor.subscribe(callback, mode=VisionSensor.AMBIENT_LIGHT)\ntime.sleep(' + value_time +')# play with sensor while it waits\nhub.vision_sensor.unsubscribe(callback)\n';
883+
return code;
884+
};
885+
886+
Blockly.Blocks['lego_boost_detect_RGB'].toplevel_init = `
887+
from pylgbst.hub import VisionSensor
888+
889+
`;
890+
891+
BlocklyPy['lego_boost_detect_RGB'] = function (block) {
892+
var value_time = BlocklyPy.valueToCode(
893+
block,
894+
'TIME',
895+
BlocklyPy.ORDER_ATOMIC
896+
);
897+
898+
var code = 'def callback(red, green, blue):\n print("Color RGB: %s" % (red, green, blue))\nhub.vision_sensor.subscribe(callback, mode=VisionSensor.COLOR_RGB)\ntime.sleep(' + value_time +')# play with sensor while it waits\nhub.vision_sensor.unsubscribe(callback)\n';
899+
return code;
900+
};
901+
902+
Blockly.Blocks['lego_boost_set_sensor_color'].toplevel_init = `
903+
from pylgbst.hub import VisionSensor, COLOR_BLUE, COLOR_CYAN, COLOR_YELLOW, COLOR_RED, COLOR_WHITE, COLOR_BLACK
904+
905+
`;
906+
907+
BlocklyPy['lego_boost_set_sensor_color'] = function (block) {
908+
var dropdown_color_value = block.getFieldValue('COLOR');
909+
var code = 'hub.vision_sensor.set_color(' + dropdown_color_value + ')\n';
910+
return code;
911+
};
912+
913+
BlocklyPy['lego_boost_button_state'] = function (block) {
914+
var code = 'def callback(is_pressed):\n print("Btn pressed: %s" % is_pressed)\nhub.button.subscribe(callback)\ntime.sleep(1)\n';
915+
return code;
675916
};
676917

677918
// Creating a toolbox containing all the main blocks
@@ -1119,13 +1360,41 @@ const TOOLBOX = {
11191360
{
11201361
kind: 'BLOCK',
11211362
type: 'lego_boost_get_current_led_color'
1363+
},
1364+
{
1365+
kind: 'BLOCK',
1366+
type: 'lego_boost_detect_color_and_distance'
1367+
},
1368+
{
1369+
kind: 'BLOCK',
1370+
type: 'lego_boost_detect_color'
1371+
},
1372+
{
1373+
kind: 'BLOCK',
1374+
type: 'lego_boost_detect_distance'
1375+
},
1376+
{
1377+
kind: 'BLOCK',
1378+
type: 'lego_boost_detect_reflected_distance'
1379+
},
1380+
{
1381+
kind: 'BLOCK',
1382+
type: 'lego_boost_detect_luminosity'
1383+
},
1384+
{
1385+
kind: 'BLOCK',
1386+
type: 'lego_boost_detect_RGB'
1387+
},
1388+
{
1389+
kind: 'BLOCK',
1390+
type: 'lego_boost_set_sensor_color'
1391+
},
1392+
{
1393+
kind: 'BLOCK',
1394+
type: 'lego_boost_button_state'
11221395
}
11231396
// {
11241397
// kind: 'BLOCK',
1125-
// type: 'lego_boost_color_sensor'
1126-
// }
1127-
// {
1128-
// kind: 'BLOCK',
11291398
// type: 'lego_boost_set_led_brightness'
11301399
// }
11311400
]

0 commit comments

Comments
 (0)