[update] i2cの新しいブロック

This commit is contained in:
ocogeclub 2021-09-07 21:18:50 +09:00
parent 28bdb59693
commit f511aba1f7
4 changed files with 248 additions and 0 deletions

View File

@ -186,6 +186,20 @@
</value>
</block>
<block type="math_random_float"></block>
<block type="ugj_hextodec">
<value name="hex">
<shadow type="text">
<field name="TEXT">ff</field>
</shadow>
</value>
</block>
<block type="ugj_dectohex">
<value name="dec">
<shadow type="math_number">
<field name="NUM">255</field>
</shadow>
</value>
</block>
</category>
<category name="文字列" css-icon="customIcon fas fa-font" categorystyle="text_category">
<block type="text">
@ -234,6 +248,13 @@
</block>
</value>
</block>
<block type="ugj_charcode">
<value name="char">
<shadow type="text">
<field name="TEXT">a</field>
</shadow>
</value>
</block>
</category>
<category name="リスト" css-icon="customIcon fas fa-list-ol" categorystyle="list_category">
<block type="lists_create_with">
@ -393,6 +414,14 @@
</value>
</block>
<block type="ugj_i2c_close"></block>
<block type="ugj_i2c_write_byte">
<value name="byte_val">
<shadow type="math_number">
<field name="NUM">0</field>
</shadow>
</value>
</block>
<block type="ugj_i2c_read_byte"></block>
<block type="ugj_i2c_write_byte_data">
<value name="reg">
<shadow type="math_number">

View File

@ -349,6 +349,53 @@ Value i2cClose(const CallbackInfo& info)
);
}
// デバイスに1バイトを送る
Value i2cWriteByte(const CallbackInfo& info)
{
Env env = info.Env();
if (info.Length() < 2)
{
TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsNumber() || !info[1].IsNumber())
{
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
unsigned int handle = info[0].As<Number>().DoubleValue();
unsigned int byteVal = info[1].As<Number>().DoubleValue();
return Number::New(env,
lgI2cWriteByte(handle, byteVal)
);
}
// デバイスから1バイトを受け取る
Value i2cReadByte(const CallbackInfo& info)
{
Env env = info.Env();
if (info.Length() < 1)
{
TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsNumber())
{
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
unsigned int handle = info[0].As<Number>().DoubleValue();
return Number::New(env,
lgI2cReadByte(handle)
);
}
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタに1バイトを書き込む
Value i2cWriteByteData(const CallbackInfo& info)
{
@ -439,6 +486,8 @@ Init(Env env, Object exports)
exports.Set(String::New(env, "serial_write"), Function::New(env, serialWrite));
exports.Set(String::New(env, "i2c_open"), Function::New(env, i2cOpen));
exports.Set(String::New(env, "i2c_close"), Function::New(env, i2cClose));
exports.Set(String::New(env, "i2c_write_byte"), Function::New(env, i2cWriteByte));
exports.Set(String::New(env, "i2c_read_byte"), Function::New(env, i2cReadByte));
exports.Set(String::New(env, "i2c_write_byte_data"), Function::New(env, i2cWriteByteData));
exports.Set(String::New(env, "i2c_read_byte_data"), Function::New(env, i2cReadByteData));

View File

@ -144,6 +144,103 @@ Blockly.Python['ugj_controls_forEach'] = function (block) {
return code;
};
/** Additional Basic Blocks********************************************************************************* */
var ugjCharcodeDefinition = {
"type": "ugj_charcode",
"message0": "%{BKY_UGJ_CHARCODE_TITLE}",
"args0": [
{
"type": "input_value",
"name": "char",
"check": "String"
}
],
"inputsInline": true,
"output": "Number",
"tooltip": "%{BKY_UGJ_CHARCODE_TOOLTIP}",
"helpUrl": "",
"style": "text_blocks"
};
Blockly.Blocks['ugj_charcode'] = {
init: function () {
this.jsonInit(ugjCharcodeDefinition);
}
};
Blockly.JavaScript['ugj_charcode'] = function(block) {
var value_char = Blockly.JavaScript.valueToCode(block, 'char', Blockly.JavaScript.ORDER_ATOMIC);
var code = `${value_char}.charCodeAt(0)`;
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.Python['ugj_charcode'] = function(block) {
var value_char = Blockly.Python.valueToCode(block, 'char', Blockly.Python.ORDER_ATOMIC);
var code = `ord(${value_char})`;
return [code, Blockly.Python.ORDER_NONE];
};
var ugjHextodecDefinition = {
"type": "ugj_hextodec",
"message0": "%{BKY_UGJ_HEXTODEC_TITLE}",
"args0": [
{
"type": "input_value",
"name": "hex",
"check": "String"
}
],
"inputsInline": true,
"output": "Number",
"tooltip": "%{BKY_UGJ_HEXTODEC_TOOLTIP}",
"helpUrl": "",
"style": "math_blocks"
};
Blockly.Blocks['ugj_hextodec'] = {
init: function () {
this.jsonInit(ugjHextodecDefinition);
}
};
Blockly.JavaScript['ugj_hextodec'] = function (block) {
var value_hex = Blockly.JavaScript.valueToCode(block, 'hex', Blockly.JavaScript.ORDER_ATOMIC);
var code = `parseInt (${value_hex}, 16)`;
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.Python['ugj_hextodec'] = function(block) {
var value_hex = Blockly.Python.valueToCode(block, 'hex', Blockly.Python.ORDER_ATOMIC);
var code = `int(${value_hex}, 16)`;
return [code, Blockly.Python.ORDER_NONE];
};
var ugjDectohexDefinition = {
"type": "ugj_dectohex",
"message0": "%{BKY_UGJ_DECTOHEX_TITLE}",
"args0": [
{
"type": "input_value",
"name": "dec",
"check": "Number"
}
],
"inputsInline": true,
"output": "String",
"tooltip": "%{BKY_UGJ_DECTOHEX_TOOLTIP}",
"helpUrl": "",
"style": "math_blocks"
};
Blockly.Blocks['ugj_dectohex'] = {
init: function () {
this.jsonInit(ugjDectohexDefinition);
}
};
Blockly.JavaScript['ugj_dectohex'] = function(block) {
var value_dec = Blockly.JavaScript.valueToCode(block, 'dec', Blockly.JavaScript.ORDER_ATOMIC);
var code = `'0x' + (${value_dec}).toString(16).toUpperCase()`;
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.Python['ugj_dectohex'] = function(block) {
var value_dec = Blockly.Python.valueToCode(block, 'dec', Blockly.Python.ORDER_ATOMIC);
var code = `'0x' + format(${value_dec}, 'X')`;
return [code, Blockly.Python.ORDER_NONE];
};
/** GPIO *****************************************************************************************************/
/************ */
@ -690,6 +787,68 @@ Blockly.Python['ugj_i2c_close'] = function (block) {
return code;
};
/**************************************** */
/** Sends a single byte to the device. ** */
/**************************************** */
var ugjI2cWriteByteDefinition = {
"type": "ugj_i2c_write_byte",
"message0": "%{BKY_I2C_WRITE_BYTE_TITLE}",
"args0": [
{
"type": "input_value",
"name": "byte_val",
"check": "Number"
}
],
"previousStatement": null,
"nextStatement": null,
"tooltip": "%{BKY_I2C_WRITE_BYTE_TOOLTIP}",
"helpUrl": "",
"style": "gpio_blocks"
};
Blockly.Blocks['ugj_i2c_write_byte'] = {
init: function () {
this.jsonInit(ugjI2cWriteByteDefinition);
}
};
Blockly.JavaScript['ugj_i2c_write_byte'] = function(block) {
var value_byte_val = Blockly.JavaScript.valueToCode(block, 'byte_val', Blockly.JavaScript.ORDER_ATOMIC);
var code = `i2c_write_byte(i2c_hand);\n`;
return code;
};
Blockly.Python['ugj_i2c_write_byte'] = function(block) {
var value_byte_val = Blockly.Python.valueToCode(block, 'byte_val', Blockly.Python.ORDER_ATOMIC);
var code = `i2c_write_byte(i2c_hand);\n`;
return code;
};
/*************************************** */
/** Reads a single byte from the device. */
/*************************************** */
var ugjI2cReadByteDefinition = {
"type": "ugj_i2c_read_byte",
"message0": "%{BKY_I2C_READ_BYTE_TITLE}",
"inputsInline": true,
"output": "Number",
"tooltip": "%{BKY_I2C_READ_BYTE_TOOLTIP}",
"helpUrl": "",
"style": "gpio_blocks"
};
Blockly.Blocks['ugj_i2c_read_byte'] = {
init: function () {
this.jsonInit(ugjI2cReadByteDefinition);
}
};
Blockly.JavaScript['ugj_i2c_read_byte'] = function(block) {
var code = `i2c_read_byte(i2c_hand)`;
return [code, Blockly.JavaScript.ORDER_NONE];
};
Blockly.Python['ugj_i2c_read_byte'] = function(block) {
var code = `i2c_read_byte(i2c_hand)`;
return [code, Blockly.Python.ORDER_NONE];
};
/****************************************************************** */
/** Writes a single byte to the specified register of the device ** */
/****************************************************************** */

View File

@ -105,11 +105,22 @@ Blockly.Msg["SERIAL_READ_TITLE"] = "シリアルポートから %1 文字読み
Blockly.Msg["SERIAL_READ_TOOLTIP"] = "オープン済みシリアルポートから、指定のバイト数だけデータを読み込みます。";
Blockly.Msg["I2C_CLOSE_TITLE"] = "I2C デバイスを閉じる";
Blockly.Msg["I2C_CLOSE_TOOLTIP"] = "I2C接続されたデバイスと通信を切断します。";
Blockly.Msg["I2C_WRITE_BYTE_TITLE"] = "デバイスに1バイトデータ %1 を送信";
Blockly.Msg["I2C_WRITE_BYTE_TOOLTIP"] = "i2cデバイスにバイトデータを送信します。0-0xFFの範囲の数字で入力してください。";
Blockly.Msg["I2C_READ_BYTE_TITLE"] = "I2C デバイスから 1 バイト受け取る";
Blockly.Msg["I2C_READ_BYTE_TOOLTIP"] = "オープン済み I2C デバイスからデータを 1 バイト受け取ります。";
Blockly.Msg["I2C_WRITE_BYTE_DATA_TITLE"] = "レジスタ %1 に %2 を書き込む";
Blockly.Msg["I2C_WRITE_BYTE_DATA_TOOLTIP"] = "デバイスの指定されたレジスタに1バイトを書き込みます。";
Blockly.Msg["I2C_READ_BYTE_DATA_TITLE"] = "レジスタ %1 の値";
Blockly.Msg["I2C_READ_BYTE_DATA_TOOLTIP"] = "デバイスの指定されたレジスタから1バイトを読み込みます。";
Blockly.Msg["UGJ_CHARCODE_TITLE"] = "%1 の文字コード";
Blockly.Msg["UGJ_CHARCODE_TOOLTIP"] = "入力テキストの1文字目の文字コードを返します。";
Blockly.Msg["UGJ_HEXTODEC_TITLE"] = "0x %1";
Blockly.Msg["UGJ_HEXTODEC_TOOLTIP"] = "16進数を10進数に変換します。";
Blockly.Msg["UGJ_DECTOHEX_TITLE"] = "%1 を16進数に変換";
Blockly.Msg["UGJ_DECTOHEX_TOOLTIP"] = "10進数を16進数に変換します。";
Blockly.Msg["UGJ_CANVAS_INIT_TITLE"] = "キャンバスを表示";
Blockly.Msg["UGJ_CANVAS_INIT_TOOLTIP"] = "キャンバスを表示し、使用できるようにします。";