2022-03-02 11:25:14 +00:00
|
|
|
|
/********* */
|
|
|
|
|
/** 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');
|
2023-05-09 00:05:48 +00:00
|
|
|
|
// Blockly.JavaScript.provideFunction_(
|
2023-05-13 09:42:06 +00:00
|
|
|
|
// 'require_gpio', [`const _pi = require('` + apptool.gpio_lib + `');`]
|
2023-05-09 00:05:48 +00:00
|
|
|
|
// );
|
|
|
|
|
// let modpath = apptool.path.join(apptool.blocks_dir, 'sensors', 'bme280', 'BME280x.js');
|
2023-08-18 02:16:52 +00:00
|
|
|
|
// let modpath = apptool.path.join(apptool.blocks_dir, 'sensors', 'bme280', `BME280x.js`);
|
2022-03-02 11:25:14 +00:00
|
|
|
|
Blockly.JavaScript.provideFunction_(
|
2023-08-18 02:16:52 +00:00
|
|
|
|
'require_bme280', [`const _bme280 = require('@ocoge/bme280');`]
|
|
|
|
|
// 'require_bme280', [`const _bme280 = require('${modpath}');`]
|
2022-03-02 11:25:14 +00:00
|
|
|
|
);
|
|
|
|
|
var code = `const options = {
|
2023-01-01 13:05:12 +00:00
|
|
|
|
i2cBusNo: ${apptool.i2c_bus},
|
2022-03-02 11:25:14 +00:00
|
|
|
|
i2cAddress: ${dropdown_addr}
|
|
|
|
|
};
|
2023-08-18 02:16:52 +00:00
|
|
|
|
await _bme280.init(_rg, options);
|
2022-03-02 11:25:14 +00:00
|
|
|
|
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];
|
2023-01-10 12:46:36 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|