ocoge/blocks/sensors/pico_slave/index.js

66 lines
2.2 KiB
JavaScript
Raw Normal View History

Blockly.Blocks['oc_i2cslave'] = {
init: function () {
this.appendValueInput("i2c_addr")
.setCheck("Number")
.appendField("PICOスレーブI2Cアドレス");
this.appendDummyInput()
.appendField(new Blockly.FieldVariable("データ"), "data")
.appendField("を受け取ったら");
this.appendStatementInput("do")
.setCheck(null);
this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setTooltip("Raspberry Pi Pico及びその互換機を I2Cスレーブ化します。受信データは1バイトデータです。i2c_slave.py が必要です。");
this.setHelpUrl("");
this.setStyle('gpio_blocks');
}
};
Blockly.Python['oc_i2cslave'] = function (block) {
var value_i2c_addr = Blockly.Python.valueToCode(block, 'i2c_addr', Blockly.Python.ORDER_ATOMIC);
var variable_data = Blockly.Python.nameDB_.getName(block.getFieldValue('data'), Blockly.Names.NameType.VARIABLE);
var statements_do = Blockly.Python.statementToCode(block, 'do');
Blockly.Python.provideFunction_(
'import_i2cslave', ['from i2cSlave import i2c_slave']
);
var code = `_i2cs = i2c_slave(0,sda=0,scl=1,slaveAddress=${value_i2c_addr})
while True:
${variable_data} = int(_i2cs.get())
${statements_do}
`;
return code;
};
flyout_contents = flyout_contents.concat([
{
"kind": "label",
"text": "RPi Pico I2Cスレーブ化",
"web-line": "4.0",
"web-line-width": "200"
}, {
"kind": "block",
"type": "oc_i2cslave",
"fields": {
"data": {
"name": "データ"
}
},
"inputs": {
"i2c_addr": {
"shadow": {
"type": "ugj_hextodec",
"inputs": {
"hex": {
"shadow": {
"type": "text",
"fields": {
"TEXT": "41"
}
}
}
}
}
}
}
}
])