From eb109dacc0e456f13e0e0980bce71a24fda80791 Mon Sep 17 00:00:00 2001 From: Alexey Nesterov Date: Fri, 28 Oct 2022 13:49:57 +1000 Subject: [PATCH] Added: rangefinder i2c blocks --- source/blocks/adam_two.js | 101 ++++++++++++++++++++++++++- source/generators/python/adam_two.js | 43 +++++++++++- 2 files changed, 142 insertions(+), 2 deletions(-) diff --git a/source/blocks/adam_two.js b/source/blocks/adam_two.js index b1f646c..8568def 100644 --- a/source/blocks/adam_two.js +++ b/source/blocks/adam_two.js @@ -1201,6 +1201,105 @@ Blockly.defineBlocksWithJsonArray([ "colour": 230, "tooltip": "Инициализация экземпляра класса для работы с дальномерами и колесами", "helpUrl": "" -} +}, + +/** + * common_import_rangefinders_i2c + * + */ +{ + "type": "common_import_rangefinders_i2c", + "lastDummyAlign0": "RIGHT", + "message0": "инициализировать дальномеры %1 открыть шину I2C %2 показывать ошибки %3", + "args0": [ + { + "type": "input_dummy", + "align": "CENTRE" + }, + { + "type": "input_value", + "name": "bus_number", + "check": "Number", + "align": "RIGHT" + }, + { + "type": "field_checkbox", + "name": "verbose_mode", + "checked": false + } + ], + "inputsInline": false, + "output": null, + "colour": 315, + "tooltip": "Инициализирует класс работы с дальномерами и открывает шину I2C", + "helpUrl": "" +}, +/** + * rangefinder_get_distance_i2c + * + */ +{ + "type": "rangefinder_get_distance_i2c", + "message0": "получить расстояние %1 адрес дальномера %2", + "args0": [ + { + "type": "input_dummy" + }, + { + "type": "input_value", + "name": "rangefinder_address" + } + ], + "inputsInline": false, + "output": null, + "colour": 315, + "tooltip": "Получение данных дальномера с указанным адресом ", + "helpUrl": "" +}, + +/** + * rangefinder_i2c_address + * + */ +{ + "type": "rangefinder_i2c_address", + "message0": "переменные I2C дальномеров %1", + "args0": [ + { + "type": "field_dropdown", + "name": "device_addr", + "options": [ + [ + "0x18", + "24" + ], + [ + "0x19", + "25" + ], + [ + "0x1A", + "26" + ], + [ + "0x1B", + "27" + ], + [ + "0x1C", + "28" + ], + [ + "0x1D", + "29" + ] + ] + } + ], + "output": null, + "colour": 315, + "tooltip": "", + "helpUrl": "" +} ]); \ No newline at end of file diff --git a/source/generators/python/adam_two.js b/source/generators/python/adam_two.js index b07d85f..513b5db 100644 --- a/source/generators/python/adam_two.js +++ b/source/generators/python/adam_two.js @@ -5,7 +5,7 @@ 'use strict'; // If any new block imports any library, add that library name here. -Blockly.Python.addReservedWords('math, random, Number, smbus, AdamIMU, MotionAndRangefinders'); +Blockly.Python.addReservedWords('math, random, Number, smbus, AdamIMU, MotionAndRangefinders, ReadSensor'); /** * import_adam_servo_api_with_param @@ -449,4 +449,45 @@ Blockly.Python['import_adam_rangefinders'] = function(block) { Blockly.Python.definitions_['from_motionAndRangefinders_import_MotionAndRangefinders'] = 'from motionAndRangefinders import MotionAndRangefinders'; var code = 'MotionAndRangefinders()'; return [code, Blockly.Python.ORDER_NONE]; +}; + +/** + * common_import_rangefinders_i2c + * + */ +Blockly.Python['common_import_rangefinders_i2c'] = function(block) { + var value_bus_number = Blockly.Python.valueToCode(block, 'bus_number', Blockly.Python.ORDER_ATOMIC); + var checkbox_verbose_mode = block.getFieldValue('verbose_mode') === 'TRUE'; + var value_bus_number = Blockly.Python.valueToCode(block, 'bus_number', Blockly.Python.ORDER_ATOMIC); + Blockly.Python.definitions_['from_rangefinders_i2c.ReadSensor_import_ReadSensor'] = 'from rangefinders_i2c.ReadSensor import ReadSensor'; + + var code0 = 'ReadSensor('+ value_bus_number +')'; + var code1 = 'ReadSensor('+ value_bus_number +', True)'; + + var code = code0 + + if(checkbox_verbose_mode) + code = code1 + + return [code, Blockly.Python.ORDER_NONE]; +}; + +/** + * rangefinder_get_distance_i2c + * + */ +Blockly.Python['rangefinder_get_distance_i2c'] = function(block) { + var value_rangefinder_address = Blockly.Python.valueToCode(block, 'rangefinder_address', Blockly.Python.ORDER_ATOMIC); + var code = '.GetDistance('+ value_rangefinder_address +')'; + return [code, Blockly.Python.ORDER_ATOMIC]; +}; + +/** + * rangefinder_i2c_address + * + */ +Blockly.Python['rangefinder_i2c_address'] = function(block) { + var dropdown_device_addr = block.getFieldValue('device_addr'); + var code = dropdown_device_addr; + return [code, Blockly.Python.ORDER_ATOMIC]; }; \ No newline at end of file