[update] lgpioモジュールの一部関数の戻り値の返し方を変更

This commit is contained in:
ocogeclub 2021-09-22 21:35:08 +09:00
parent a16f58b2a9
commit 5caa639a26
4 changed files with 20 additions and 70 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@ build/
push.sh push.sh
n n
*.bak *.bak
*.old *.old
lg/

View File

@ -104,7 +104,7 @@ class BME280 {
// Grab temperature, humidity, and pressure in a single read // 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`; if (!buffer) return `couldn't grab data`;
// Temperature (temperature first since we need t_fine for pressure and humidity) // Temperature (temperature first since we need t_fine for pressure and humidity)
// //
@ -156,7 +156,7 @@ class BME280 {
} }
loadCalibration(callback) { 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)); // for (let i = 0; i < 24; i++) console.log(parseInt(buffer[i], 16));
if (buffer) { if (buffer) {
let h1 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H1); let h1 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H1);

View File

@ -257,18 +257,11 @@ Value SerialRead(const CallbackInfo &info)
int handle = info[0].As<Number>().Int32Value(); int handle = info[0].As<Number>().Int32Value();
int count = info[1].As<Number>().Int32Value(); int count = info[1].As<Number>().Int32Value();
// char rxBuf[count + 1]; char rxBuf[count];
// int32_t resultCount = lgSerialRead(handle, rxBuf, count); int rxCount = lgSerialRead(handle, rxBuf, count);
// Buffer<char> resultBuf = Buffer<char>::Copy(env, rxBuf, count); auto outBuf = Buffer<char>::Copy(env, rxBuf, rxCount);
auto rxBuf = Buffer<char>::New(env, count);
int rxCount = lgSerialRead(handle, rxBuf.Data(), count);
Array returnArr = Array::New(env); return outBuf;
int i = 0;
returnArr[i] = Number::New(env, rxCount);
returnArr[++i] = rxBuf;
return returnArr;
} }
// シリアルデバイスにバイト列を送る(data: buffer) // シリアルデバイスにバイト列を送る(data: buffer)
@ -452,15 +445,11 @@ Value i2cReadI2cBlockData(const CallbackInfo &info)
int i2cReg = info[1].As<Number>().Int32Value(); int i2cReg = info[1].As<Number>().Int32Value();
int count = info[2].As<Number>().Int32Value(); int count = info[2].As<Number>().Int32Value();
auto rxBuf = Buffer<char>::New(env, count); char rxBuf[count];
int rxCount = lgI2cReadI2CBlockData(handle, i2cReg, rxBuf.Data(), count); int rxCount = lgI2cReadI2CBlockData(handle, i2cReg, rxBuf, count);
auto outBuf = Buffer<char>::Copy(env, rxBuf, rxCount);
Array returnArr = Array::New(env); return outBuf;
int i = 0;
returnArr[i] = Number::New(env, rxCount);
returnArr[++i] = rxBuf;
return returnArr;
} }
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタから単一の16ビットワードを読み取る // I2Cハンドルに関連付けられているデバイスの指定されたレジスタから単一の16ビットワードを読み取る
@ -505,15 +494,11 @@ Value I2cReadDevice(const CallbackInfo &info)
int handle = info[0].As<Number>().Int32Value(); int handle = info[0].As<Number>().Int32Value();
int count = info[1].As<Number>().Int32Value(); int count = info[1].As<Number>().Int32Value();
auto rxBuf = Buffer<char>::New(env, count); char rxBuf[count];
int rxCount = lgI2cReadDevice(handle, rxBuf.Data(), count); int rxCount = lgI2cReadDevice(handle, rxBuf, count);
auto outBuf = Buffer<char>::Copy(env, rxBuf, rxCount);
auto returnArr = Array::New(env); return outBuf;
int i = 0;
returnArr[i] = Number::New(env, rxCount);
returnArr[++i] = rxBuf;
return returnArr;
} }
// i2c デバイスにバイト列を送る(data: buffer) // i2c デバイスにバイト列を送る(data: buffer)

View File

@ -767,30 +767,12 @@ Blockly.Blocks['ugj_serial_read'] = {
}; };
Blockly.JavaScript['ugj_serial_read'] = function (block) { Blockly.JavaScript['ugj_serial_read'] = function (block) {
var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC); var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC);
var functionName = Blockly.JavaScript.provideFunction_( var code = `pi.serial_read(ser_hand, ${value_count}).toString('utf8')`;
'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)`;
return [code, Blockly.JavaScript.ORDER_ATOMIC]; return [code, Blockly.JavaScript.ORDER_ATOMIC];
}; };
Blockly.Python['ugj_serial_read'] = function (block) { Blockly.Python['ugj_serial_read'] = function (block) {
var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC); var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
var functionName = Blockly.Python.provideFunction_( var code = `pi.serial_read(ser_hand, ${value_count}).decode()`;
'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)`;
return [code, Blockly.Python.ORDER_ATOMIC]; return [code, Blockly.Python.ORDER_ATOMIC];
}; };
@ -966,30 +948,12 @@ Blockly.Blocks['ugj_i2c_read_device'] = {
}; };
Blockly.JavaScript['ugj_i2c_read_device'] = function (block) { Blockly.JavaScript['ugj_i2c_read_device'] = function (block) {
var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC); var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC);
var functionName = Blockly.JavaScript.provideFunction_( var code = `pi.i2c_read_device(i2c_hand, ${value_count}).toString('utf8')`;
'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)`;
return [code, Blockly.JavaScript.ORDER_ATOMIC]; return [code, Blockly.JavaScript.ORDER_ATOMIC];
}; };
Blockly.Python['ugj_i2c_read_device'] = function (block) { Blockly.Python['ugj_i2c_read_device'] = function (block) {
var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC); var value_count = Blockly.Python.valueToCode(block, 'count', Blockly.Python.ORDER_ATOMIC);
var functionName = Blockly.Python.provideFunction_( var code = `pi.i2c_read_device(i2c_hand, ${value_count}).decode()`;
'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)`;
return [code, Blockly.Python.ORDER_ATOMIC]; return [code, Blockly.Python.ORDER_ATOMIC];
}; };
/********************************************** */ /********************************************** */