From 5caa639a26f65af7644db71bd2265009946c30c6 Mon Sep 17 00:00:00 2001 From: ocogeclub Date: Wed, 22 Sep 2021 21:35:08 +0900 Subject: [PATCH] =?UTF-8?q?[update]=20lgpio=E3=83=A2=E3=82=B8=E3=83=A5?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E3=81=AE=E4=B8=80=E9=83=A8=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=81=AE=E6=88=BB=E3=82=8A=E5=80=A4=E3=81=AE=E8=BF=94=E3=81=97?= =?UTF-8?q?=E6=96=B9=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- local_modules/@ocogeclub/bme280/BME280.js | 4 +-- local_modules/@ocogeclub/lgpio/lgpio.cpp | 39 +++++++------------- ugj_blocks.js | 44 +++-------------------- 4 files changed, 20 insertions(+), 70 deletions(-) diff --git a/.gitignore b/.gitignore index 979c41b..723ecfc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ build/ push.sh n *.bak -*.old \ No newline at end of file +*.old +lg/ \ No newline at end of file diff --git a/local_modules/@ocogeclub/bme280/BME280.js b/local_modules/@ocogeclub/bme280/BME280.js index a2cdfdd..e69f369 100644 --- a/local_modules/@ocogeclub/bme280/BME280.js +++ b/local_modules/@ocogeclub/bme280/BME280.js @@ -104,7 +104,7 @@ class BME280 { // Grab temperature, humidity, and pressure in a single read // - let [count, buffer] = this.pi.i2c_read_i2c_block_data(this.i2cHand, this.REGISTER_PRESSURE_DATA, 8); + let buffer = this.pi.i2c_read_i2c_block_data(this.i2cHand, this.REGISTER_PRESSURE_DATA, 8); if (!buffer) return `couldn't grab data`; // Temperature (temperature first since we need t_fine for pressure and humidity) // @@ -156,7 +156,7 @@ class BME280 { } loadCalibration(callback) { - let [count, buffer] = this.pi.i2c_read_i2c_block_data(this.i2cHand, this.REGISTER_DIG_T1, 24); + let buffer = this.pi.i2c_read_i2c_block_data(this.i2cHand, this.REGISTER_DIG_T1, 24); // for (let i = 0; i < 24; i++) console.log(parseInt(buffer[i], 16)); if (buffer) { let h1 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H1); diff --git a/local_modules/@ocogeclub/lgpio/lgpio.cpp b/local_modules/@ocogeclub/lgpio/lgpio.cpp index 6636c88..aa3a73e 100644 --- a/local_modules/@ocogeclub/lgpio/lgpio.cpp +++ b/local_modules/@ocogeclub/lgpio/lgpio.cpp @@ -257,18 +257,11 @@ Value SerialRead(const CallbackInfo &info) int handle = info[0].As().Int32Value(); int count = info[1].As().Int32Value(); - // char rxBuf[count + 1]; - // int32_t resultCount = lgSerialRead(handle, rxBuf, count); - // Buffer resultBuf = Buffer::Copy(env, rxBuf, count); - auto rxBuf = Buffer::New(env, count); - int rxCount = lgSerialRead(handle, rxBuf.Data(), count); + char rxBuf[count]; + int rxCount = lgSerialRead(handle, rxBuf, count); + auto outBuf = Buffer::Copy(env, rxBuf, rxCount); - Array returnArr = Array::New(env); - int i = 0; - returnArr[i] = Number::New(env, rxCount); - returnArr[++i] = rxBuf; - - return returnArr; + return outBuf; } // シリアルデバイスにバイト列を送る(data: buffer) @@ -452,15 +445,11 @@ Value i2cReadI2cBlockData(const CallbackInfo &info) int i2cReg = info[1].As().Int32Value(); int count = info[2].As().Int32Value(); - auto rxBuf = Buffer::New(env, count); - int rxCount = lgI2cReadI2CBlockData(handle, i2cReg, rxBuf.Data(), count); + char rxBuf[count]; + int rxCount = lgI2cReadI2CBlockData(handle, i2cReg, rxBuf, count); + auto outBuf = Buffer::Copy(env, rxBuf, rxCount); - Array returnArr = Array::New(env); - int i = 0; - returnArr[i] = Number::New(env, rxCount); - returnArr[++i] = rxBuf; - - return returnArr; + return outBuf; } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタから単一の16ビットワードを読み取る @@ -505,15 +494,11 @@ Value I2cReadDevice(const CallbackInfo &info) int handle = info[0].As().Int32Value(); int count = info[1].As().Int32Value(); - auto rxBuf = Buffer::New(env, count); - int rxCount = lgI2cReadDevice(handle, rxBuf.Data(), count); + char rxBuf[count]; + int rxCount = lgI2cReadDevice(handle, rxBuf, count); + auto outBuf = Buffer::Copy(env, rxBuf, rxCount); - auto returnArr = Array::New(env); - int i = 0; - returnArr[i] = Number::New(env, rxCount); - returnArr[++i] = rxBuf; - - return returnArr; + return outBuf; } // i2c デバイスにバイト列を送る(data: buffer) diff --git a/ugj_blocks.js b/ugj_blocks.js index a3f13a1..5a88945 100644 --- a/ugj_blocks.js +++ b/ugj_blocks.js @@ -767,30 +767,12 @@ Blockly.Blocks['ugj_serial_read'] = { }; Blockly.JavaScript['ugj_serial_read'] = function (block) { var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC); - var functionName = Blockly.JavaScript.provideFunction_( - 'ugj_serial_read', - [ - 'const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = ser_hand => {', - `[count, data] = pi.serial_read(ser_hand, ${value_count});`, - `return data.toString('utf8', 0, count);`, - '}' - ] - ); - var code = `${functionName}(ser_hand)`; + var code = `pi.serial_read(ser_hand, ${value_count}).toString('utf8')`; return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.Python['ugj_serial_read'] = function (block) { var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC); - var functionName = Blockly.Python.provideFunction_( - 'ugj_serial_read', - [ - 'def ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(ser_hand) :', - ` (count, data) = pi.serial_read(ser_hand, ${value_count})`, - ` return data[0:count].decode()`, - '' - ] - ); - var code = `${functionName}(ser_hand)`; + var code = `pi.serial_read(ser_hand, ${value_count}).decode()`; return [code, Blockly.Python.ORDER_ATOMIC]; }; @@ -966,30 +948,12 @@ Blockly.Blocks['ugj_i2c_read_device'] = { }; Blockly.JavaScript['ugj_i2c_read_device'] = function (block) { var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC); - var functionName = Blockly.JavaScript.provideFunction_( - 'ugj_i2c_read_device', - [ - 'const ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = i2c_hand => {', - `[count, data] = pi.i2c_read_device(i2c_hand, ${value_count});`, - `return data.toString('utf8', 0, count);`, - '}' - ] - ); - var code = `${functionName}(i2c_hand)`; + var code = `pi.i2c_read_device(i2c_hand, ${value_count}).toString('utf8')`; return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.Python['ugj_i2c_read_device'] = function (block) { var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC); - var functionName = Blockly.Python.provideFunction_( - 'ugj_i2c_read_device', - [ - 'def ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(i2c_hand) :', - ` (count, data) = pi.i2c_read_device(i2c_hand, ${value_count})`, - ` return data[0:count].decode()`, - '' - ] - ); - var code = `${functionName}(i2c_hand)`; + var code = `pi.i2c_read_device(i2c_hand, ${value_count}).decode()`; return [code, Blockly.Python.ORDER_ATOMIC]; }; /********************************************** */