ocoge/blocks/sensors/bme280/index.js

81 lines
2.6 KiB
JavaScript
Raw Permalink 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.

/********* */
/** BME280 */
/********* */
Blockly.Blocks['ugj_bme280'] = {
init: function () {
this.appendDummyInput()
.appendField("BME280アドレス")
.appendField(new Blockly.FieldDropdown([["0x76", "0x76"], ["0x77", "0x77"]]), "addr")
.appendField(")の計測値を取得");
this.setInputsInline(true);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setStyle('sensor_blocks');
this.setTooltip("環境センサーBME280で、気温摂氏、湿度、気圧hPaを計測します。計測結果は計測値ブロックで参照します。");
this.setHelpUrl("");
}
};
Blockly.JavaScript['ugj_bme280'] = function (block) {
var dropdown_addr = block.getFieldValue('addr');
Blockly.JavaScript.provideFunction_(
'require_gpio', [`const _pi = require('@ocoge.club/` + apptool.gpio_lib + `');`]
);
let modpath = apptool.path.join(apptool.blocks_dir, 'sensors', 'bme280', 'BME280x.js');
Blockly.JavaScript.provideFunction_(
'require_bme280', [`const _bme280 = require('${modpath}');`]
);
var code = `const options = {
i2cBusNo: ${apptool.i2c_bus},
i2cAddress: ${dropdown_addr}
};
await _bme280.init(options);
let _thp = await _bme280.readSensorData();
let _bmedata = [];
_bmedata[0] = Math.round(_thp.temperature_C * 10) / 10;
_bmedata[1] = Math.round(_thp.humidity * 10) / 10;
_bmedata[2] = Math.round(_thp.pressure_hPa);
await _bme280.cancel();
`;
return code;
};
Blockly.Blocks['ugj_bme280_data'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown([["気温", "0"], ["湿度", "1"], ["気圧", "2"]]), "thp");
this.setInputsInline(true);
this.setOutput(true, null);
this.setStyle('sensor_blocks');
this.setTooltip("BME280 の計測値を返します。");
this.setHelpUrl("");
}
};
Blockly.JavaScript['ugj_bme280_data'] = function (block) {
var dropdown_thp = block.getFieldValue('thp');
var code = `_bmedata[${dropdown_thp}]`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
flyout_contents = flyout_contents.concat([
{
"kind": "label",
"text": "温湿度気圧センサー BME280",
"web-line": "4.0",
"web-line-width": "200"
},
{
"kind": "block",
"type": "ugj_bme280",
"fields": {
"addr": "0x76"
}
},
{
"kind": "block",
"type": "ugj_bme280_data",
"fields": {
"thp": "0"
}
}
]);