From f511aba1f7bc86f78e63d2cbe2b27d11886d2474 Mon Sep 17 00:00:00 2001 From: ocogeclub Date: Tue, 7 Sep 2021 21:18:50 +0900 Subject: [PATCH] =?UTF-8?q?[update]=20i2c=E3=81=AE=E6=96=B0=E3=81=97?= =?UTF-8?q?=E3=81=84=E3=83=96=E3=83=AD=E3=83=83=E3=82=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 29 ++++ local_modules/ocoge_lgpio/ocoge_lgpio.cpp | 49 +++++++ scripts/ugj_blocks.js | 159 ++++++++++++++++++++++ scripts/ugj_script.js | 11 ++ 4 files changed, 248 insertions(+) diff --git a/index.html b/index.html index f9397c8..41e4768 100644 --- a/index.html +++ b/index.html @@ -186,6 +186,20 @@ + + + + ff + + + + + + + 255 + + + @@ -234,6 +248,13 @@ + + + + a + + + @@ -393,6 +414,14 @@ + + + + 0 + + + + diff --git a/local_modules/ocoge_lgpio/ocoge_lgpio.cpp b/local_modules/ocoge_lgpio/ocoge_lgpio.cpp index 3c81e31..7b8227e 100644 --- a/local_modules/ocoge_lgpio/ocoge_lgpio.cpp +++ b/local_modules/ocoge_lgpio/ocoge_lgpio.cpp @@ -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().DoubleValue(); + unsigned int byteVal = info[1].As().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().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)); diff --git a/scripts/ugj_blocks.js b/scripts/ugj_blocks.js index 804533e..72f629c 100644 --- a/scripts/ugj_blocks.js +++ b/scripts/ugj_blocks.js @@ -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 ** */ /****************************************************************** */ diff --git a/scripts/ugj_script.js b/scripts/ugj_script.js index 591f157..891bf2a 100644 --- a/scripts/ugj_script.js +++ b/scripts/ugj_script.js @@ -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デバイスに1バイトデータを送信します。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"] = "キャンバスを表示し、使用できるようにします。";