Skip to content

Commit e9e046c

Browse files
authored
Merge pull request #10 from DenisaCG/sensors
Added blocks with access to the sensors
2 parents 486fd02 + 772cd00 commit e9e046c

File tree

4 files changed

+273
-12
lines changed

4 files changed

+273
-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: 271 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,142 @@ 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);
372428
this.setColour(lego_boost_color);
373-
this.setTooltip('Color sensor.');
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+
this.setInputsInline(true);
486+
this.setPreviousStatement(true, null);
487+
this.setNextStatement(true, null);
488+
this.setColour(lego_boost_color);
489+
this.setTooltip('Set the color of the sensor to one of the selected ones.');
490+
this.setHelpUrl('');
491+
}
492+
};
493+
494+
Blockly.Blocks['lego_boost_button_state'] = {
495+
init: function () {
496+
this.appendDummyInput().appendField('Print the button state');
497+
this.setInputsInline(true);
498+
this.setPreviousStatement(true, null);
499+
this.setNextStatement(true, null);
500+
this.setColour(lego_boost_color);
501+
this.setTooltip(
502+
'Output the state of the button. The state is 0 for not pressed and 1 or 2 for pressed.'
503+
);
374504
this.setHelpUrl('');
375505
}
376506
};
@@ -669,9 +799,111 @@ BlocklyPy['lego_boost_set_led_brightness'] = function (block) {
669799
return code;
670800
};
671801

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

677909
// Creating a toolbox containing all the main blocks
@@ -1119,13 +1351,41 @@ const TOOLBOX = {
11191351
{
11201352
kind: 'BLOCK',
11211353
type: 'lego_boost_get_current_led_color'
1354+
},
1355+
{
1356+
kind: 'BLOCK',
1357+
type: 'lego_boost_detect_color_and_distance'
1358+
},
1359+
{
1360+
kind: 'BLOCK',
1361+
type: 'lego_boost_detect_color'
1362+
},
1363+
{
1364+
kind: 'BLOCK',
1365+
type: 'lego_boost_detect_distance'
1366+
},
1367+
{
1368+
kind: 'BLOCK',
1369+
type: 'lego_boost_detect_reflected_distance'
1370+
},
1371+
{
1372+
kind: 'BLOCK',
1373+
type: 'lego_boost_detect_luminosity'
1374+
},
1375+
{
1376+
kind: 'BLOCK',
1377+
type: 'lego_boost_detect_RGB'
1378+
},
1379+
{
1380+
kind: 'BLOCK',
1381+
type: 'lego_boost_set_sensor_color'
1382+
},
1383+
{
1384+
kind: 'BLOCK',
1385+
type: 'lego_boost_button_state'
11221386
}
11231387
// {
11241388
// kind: 'BLOCK',
1125-
// type: 'lego_boost_color_sensor'
1126-
// }
1127-
// {
1128-
// kind: 'BLOCK',
11291389
// type: 'lego_boost_set_led_brightness'
11301390
// }
11311391
]

0 commit comments

Comments
 (0)