ocoge/blocks/sensors/dht11/index.js
2022-03-03 21:32:00 +09:00

41 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Blockly.Blocks['ugj_dht11'] = {
init: function () {
this.appendValueInput("pin")
.setCheck("Number")
.appendField("GPIO");
this.appendDummyInput()
.appendField("の DHT11 センサーの値を取得");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setTooltip("");
this.setHelpUrl("気温・湿度センサーDHT11で、気温摂氏、湿度を計測します。計測値は計測値ブロックで参照します。");
this.setStyle('sensor_blocks');
}
};
Blockly.JavaScript['ugj_dht11'] = function (block) {
var value_pin = Blockly.JavaScript.valueToCode(block, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
let pypath = elutil.path.join(elutil.blocks_sensors_dir, 'dht11', 'dht11.py');
var code = `let _th = require('child_process').spawnSync('python3', ['${pypath}', '${value_pin}'], {timeout: 5000}).stdout.toString();
let _dhtdata = JSON.parse(_th);`;
// let _dht11data[0] =
// console.log('t=' + obj.temperature);
// console.log('h=' + obj.humidity);
return code;
};
Blockly.Blocks['ugj_dht11_data'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown([["気温", "temperature"], ["湿度", "humidity"]]), "th");
this.setInputsInline(true);
this.setOutput(true, null);
this.setStyle('sensor_blocks');
this.setTooltip("DHT11 の計測値を返します。");
this.setHelpUrl("");
}
};
Blockly.JavaScript['ugj_dht11_data'] = function (block) {
var dropdown_th = block.getFieldValue('th');
var code = `_dhtdata.${dropdown_th}`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};