ocoge/blocks/sensors/dht11/index.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

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 = apptool.path.join(ugj_const.blocks_dir, 'sensors', 'dht11', 'dht11.py');
2022-03-03 12:32:00 +00:00
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];
};
flyout_contents = flyout_contents.concat([
{
"kind": "label",
"text": "温湿度センサー DHT11",
"web-line": "4.0",
"web-line-width": "200"
},
{
"kind": "block",
"type": "ugj_dht11",
"inputs": {
"pin": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": "8"
}
}
}
}
},
{
"kind": "block",
"type": "ugj_dht11_data",
"fields": {
"th": "temperature"
}
}
]);