ocoge/blocks/sensors/bme280/index.js
2022-05-24 22:11:10 +09:00

56 lines
2.2 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.

/********* */
/** 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/` + elutil.gpio_backend + `');`]
);
let modpath = elutil.path.join(elutil.blocks_sensors_dir, 'bme280', 'BME280x.js');
Blockly.JavaScript.provideFunction_(
'require_bme280', [`const _bme280 = require('${modpath}');`]
);
var code = `const options = {
i2cBusNo: ${elutil.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];
};