[fix] GPIO や I2C で後片付けをしなかった場合、オコゲを再起動するまでこれらが動作しなくなっていたのを修正。

This commit is contained in:
ocogeclub 2021-10-03 19:04:38 +09:00
parent 4018f0c5b7
commit 141192c70d
8 changed files with 163 additions and 71 deletions

View File

@ -383,11 +383,11 @@
<label text="シリアル" web-line="4.0" web-line-width="200"></label> <label text="シリアル" web-line="4.0" web-line-width="200"></label>
<block type="ugj_serial_open"> <block type="ugj_serial_open">
<field name="baud">9600</field> <field name="baud">9600</field>
<value name="tty"> <!-- <value name="tty">
<shadow type="text"> <shadow type="text">
<field name="TEXT">/dev/serial0</field> <field name="TEXT">/dev/serial0</field>
</shadow> </shadow>
</value> </value> -->
</block> </block>
<block type="ugj_serial_close"></block> <block type="ugj_serial_close"></block>
<block type="ugj_serial_read"> <block type="ugj_serial_read">

View File

@ -99,7 +99,7 @@ Blockly.Msg["TX_PWM_TITLE"] = "PWM : GPIO %1 に、パルス周波数 %2 Hz, デ
Blockly.Msg["TX_PWM_TOOLTIP"] = "パルス周波数をセットして、GPIO端子がPWM出力できるようにします。レンジは100固定です。"; Blockly.Msg["TX_PWM_TOOLTIP"] = "パルス周波数をセットして、GPIO端子がPWM出力できるようにします。レンジは100固定です。";
Blockly.Msg["I2C_OPEN_TITLE"] = "アドレス %1 の I2C デバイスを開く"; Blockly.Msg["I2C_OPEN_TITLE"] = "アドレス %1 の I2C デバイスを開く";
Blockly.Msg["I2C_OPEN_TOOLTIP"] = "I2C接続されたデバイスとの通信を開始します。一度にオープンできるI2Cデバイスはひとつだけです。"; Blockly.Msg["I2C_OPEN_TOOLTIP"] = "I2C接続されたデバイスとの通信を開始します。一度にオープンできるI2Cデバイスはひとつだけです。";
Blockly.Msg["SERIAL_OPEN_TITLE"] = "シリアルポート %1 を速度 %2 bpsで開く"; Blockly.Msg["SERIAL_OPEN_TITLE"] = "シリアルポートを速度 %1 bpsで開く";
Blockly.Msg["SERIAL_OPEN_TOOLTIP"] = "シリアルデバイスとの接続を開きます。"; Blockly.Msg["SERIAL_OPEN_TOOLTIP"] = "シリアルデバイスとの接続を開きます。";
Blockly.Msg["SERIAL_CLOSE_TITLE"] = "シリアルポートを閉じる"; Blockly.Msg["SERIAL_CLOSE_TITLE"] = "シリアルポートを閉じる";
Blockly.Msg["SERIAL_CLOSE_TOOLTIP"] = "シリアルデバイスとの接続を閉じます。"; Blockly.Msg["SERIAL_CLOSE_TOOLTIP"] = "シリアルデバイスとの接続を閉じます。";
@ -362,8 +362,12 @@ const ugj_createCode = (args) => {
// ブロックスクリプト実行 // ブロックスクリプト実行
const ugj_runCode = async () => { const ugj_runCode = async () => {
document.activeElement.blur(); //実行ボタンからフォーカスを外す:エンターキー押下が悪さをするため document.activeElement.blur(); //実行ボタンからフォーカスを外す:エンターキー押下が悪さをするため
let code = ugj_createCode({ 'async': true }); // let code = ugj_createCode({ 'async': true });
await eval(code).catch(e => { alert(e); }); // await eval(code).catch(e => { alert(e); });
let AsyncFunction = Object.getPrototypeOf(async function () { }).constructor
let ocogeFunc = new AsyncFunction(ugj_createCode({}));
await ocogeFunc();
console.log('Code Execution done.');
} }
// コードをダイアログで表示・保存 // コードをダイアログで表示・保存
@ -392,7 +396,7 @@ const ugj_showCode = () => {
btn_export.removeEventListener('click', export_cb); btn_export.removeEventListener('click', export_cb);
} }
const export_cb = () => { const export_cb = () => {
code = ugj_createCode({ 'ext': ext, 'async': true, 'beautify': true }); let code = ugj_createCode({ 'ext': ext, 'async': true, 'beautify': true });
// blackboardWrite()とwindow.alert()、fukidashi()をconsole.log()に書き換え、 // blackboardWrite()とwindow.alert()、fukidashi()をconsole.log()に書き換え、
// document... と ugj_... と elutil... をコメントアウト(ブラウザ関連部分の追放という意味では不完全なので注意) // document... と ugj_... と elutil... をコメントアウト(ブラウザ関連部分の追放という意味では不完全なので注意)
// あと正規表現もいい加減 // あと正規表現もいい加減
@ -616,5 +620,6 @@ window.onbeforeunload = () => {
ugj_saveWorkspace(); ugj_saveWorkspace();
elutil.savePrefsToLS(); elutil.savePrefsToLS();
elutil.killAllChildren(); elutil.killAllChildren();
elutil.cleanupLGPIO();
} }

View File

@ -189,6 +189,11 @@ class elUtil {
this.children = []; this.children = [];
} }
// LGPIO 関連リロードでGPIOをロックしたままハンドルを失うのを防ぐ
cleanupLGPIO() {
let pi = require('@ocogeclub/lgpio').close_all_handle();
}
// 設定(保存ファイルパスと未保存フラグ)をローカルストレージに保存 // 設定(保存ファイルパスと未保存フラグ)をローカルストレージに保存
savePrefsToLS() { savePrefsToLS() {
let wc = '0'; let wc = '0';
@ -268,6 +273,7 @@ class brUtil {
newFile() { ; } newFile() { ; }
setWsChanged() { ; } setWsChanged() { ; }
killAllChildren() { ; } killAllChildren() { ; }
cleanupLGPIO() { ; }
} }
// Electron 動作 / ブラウザ動作自動判別 // Electron 動作 / ブラウザ動作自動判別
@ -285,6 +291,18 @@ if (!is_el) {
var require = module_name => { var require = module_name => {
let block; let block;
switch (module_name) { switch (module_name) {
case '@tensorflow/tfjs-node':
block = 'TensorFlow';
break;
case '@vladmandic/face-api':
block = 'Face-API';
break;
case 'axios':
block = 'URLを取得';
break;
case 'nodemailer':
block = 'メール送信';
break;
case '@ocogeclub/lgpio': case '@ocogeclub/lgpio':
block = 'GPIO'; block = 'GPIO';
break; break;

View File

@ -45,12 +45,12 @@ exports.init = (options) => {
this.i2cBusNo = (options && options.hasOwnProperty('i2cBusNo')) ? options.i2cBusNo : 1; this.i2cBusNo = (options && options.hasOwnProperty('i2cBusNo')) ? options.i2cBusNo : 1;
this.i2cAddress = (options && options.hasOwnProperty('i2cAddress')) ? options.i2cAddress : this.BME280_DEFAULT_I2C_ADDRESS(); this.i2cAddress = (options && options.hasOwnProperty('i2cAddress')) ? options.i2cAddress : this.BME280_DEFAULT_I2C_ADDRESS();
this.i2cHand = this.pi.i2c_open(this.i2cBusNo, this.i2cAddress); this.i2cHand = this.pi._i2c_open(this.i2cBusNo, this.i2cAddress);
let r; let r;
r = this.pi.i2c_write_byte_data(this.i2cHand, this.REGISTER_CHIPID, 0); r = this.pi._i2c_write_byte_data(this.i2cHand, this.REGISTER_CHIPID, 0);
if (r < 0) return r; if (r < 0) return r;
let chipId = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_CHIPID); let chipId = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_CHIPID);
if (chipId !== this.CHIP_ID_BME280() && if (chipId !== this.CHIP_ID_BME280() &&
chipId !== this.CHIP_ID1_BMP280() && chipId !== this.CHIP_ID1_BMP280() &&
chipId !== this.CHIP_ID2_BMP280() && chipId !== this.CHIP_ID2_BMP280() &&
@ -64,11 +64,11 @@ exports.init = (options) => {
} }
// Humidity 16x oversampling // Humidity 16x oversampling
// //
let r = this.pi.i2c_write_byte_data(this.i2cHand, this.REGISTER_CONTROL_HUM, 0b00000101); let r = this.pi._i2c_write_byte_data(this.i2cHand, this.REGISTER_CONTROL_HUM, 0b00000101);
if (r < 0) return `Humidity 16x oversampling error: ${r}`; if (r < 0) return `Humidity 16x oversampling error: ${r}`;
// Temperture/pressure 16x oversampling, normal mode // Temperture/pressure 16x oversampling, normal mode
// //
r = this.pi.i2c_write_byte_data(this.i2cHand, this.REGISTER_CONTROL, 0b10110111); r = this.pi._i2c_write_byte_data(this.i2cHand, this.REGISTER_CONTROL, 0b10110111);
if (r < 0) return `Temperture/pressure 16x oversampling error: ${r}`; if (r < 0) return `Temperture/pressure 16x oversampling error: ${r}`;
return 0; return 0;
@ -81,7 +81,7 @@ exports.init = (options) => {
// //
exports.reset = () => { exports.reset = () => {
const POWER_ON_RESET_CMD = 0xB6; const POWER_ON_RESET_CMD = 0xB6;
let r = this.pi.i2c_write_byte_data(this.i2cHand, this.REGISTER_RESET, POWER_ON_RESET_CMD); let r = this.pi._i2c_write_byte_data(this.i2cHand, this.REGISTER_RESET, POWER_ON_RESET_CMD);
if (r < 0) return `cannot power-on reset: ${r}`; if (r < 0) return `cannot power-on reset: ${r}`;
else return 0; else return 0;
} }
@ -92,7 +92,7 @@ exports.reset = () => {
// //
exports.cancel = () => { exports.cancel = () => {
if (this.i2cHand >= 0) { if (this.i2cHand >= 0) {
this.pi.i2c_close(this.i2cHand); this.pi._i2c_close(this.i2cHand);
this.i2cHand = 0; this.i2cHand = 0;
} }
} }
@ -104,7 +104,7 @@ exports.readSensorData = () => {
// Grab temperature, humidity, and pressure in a single read // Grab temperature, humidity, and pressure in a single read
// //
let 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,16 +156,16 @@ exports.readSensorData = () => {
} }
exports.loadCalibration = (callback) => { exports.loadCalibration = (callback) => {
let 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);
let h2 = this.pi.i2c_read_word_data(this.i2cHand, this.REGISTER_DIG_H2); let h2 = this.pi._i2c_read_word_data(this.i2cHand, this.REGISTER_DIG_H2);
let h3 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H3); let h3 = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H3);
let h4 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H4); let h4 = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H4);
let h5 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H5); let h5 = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H5);
let h5_1 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H5 + 1); let h5_1 = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H5 + 1);
let h6 = this.pi.i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H6); let h6 = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H6);
this.cal = { this.cal = {
dig_T1: this.uint16(buffer[1], buffer[0]), dig_T1: this.uint16(buffer[1], buffer[0]),

View File

@ -6,3 +6,69 @@ module.exports.SET_OPEN_SOURCE = 16;
module.exports.SET_PULL_UP = 32; module.exports.SET_PULL_UP = 32;
module.exports.SET_PULL_DOWN = 64; module.exports.SET_PULL_DOWN = 64;
module.exports.SET_PULL_NONE = 128; module.exports.SET_PULL_NONE = 128;
let gpio_hand = -1;
let ser_hand = -1;
let i2c_hand = -1;
module.exports.gpiochip_open = gpiochip => {
if (gpio_hand < 0) gpio_hand = module.exports._gpiochip_open(gpiochip);
return gpio_hand;
}
module.exports.gpiochip_close = () => {
if (gpio_hand >= 0) module.exports._gpiochip_close(gpio_hand);
gpio_hand = -1;
}
module.exports.gpio_claim_output = gpio => {
if (gpio_hand >= 0) return module.exports._gpio_claim_output(gpio_hand, gpio);
}
module.exports.gpio_claim_input = gpio => {
if (gpio_hand >= 0) return module.exports._gpio_claim_input(gpio_hand, gpio);
}
module.exports.gpio_read = gpio => {
if (gpio_hand >= 0) return module.exports._gpio_read(gpio_hand, gpio);
}
module.exports.gpio_write = (gpio, value) => {
if (gpio_hand >= 0) return module.exports._gpio_write(gpio_hand, gpio, value);
}
module.exports.serial_open = (tty, baud) => {
if (ser_hand >= 0) module.exports._serial_close(ser_hand); // 勝手に閉じる
ser_hand = module.exports._serial_open(tty, baud);
return ser_hand;
}
module.exports.serial_close = () => {
if (ser_hand >= 0) module.exports._serial_close(ser_hand);
ser_hand = -1;
}
module.exports.serial_write = data => {
if (ser_hand >= 0) return module.exports._serial_write(ser_hand, Buffer.from(data));
}
module.exports.serial_read = count => {
if (ser_hand >= 0) return module.exports._serial_read(ser_hand, count).toString('utf8');
}
module.exports.i2c_open = (i2c_bus, i2c_address) => {
if (i2c_hand >= 0) module.exports._i2c_close(i2c_hand); // 勝手に閉じる
i2c_hand = module.exports._i2c_open(i2c_bus, i2c_address);
return i2c_hand;
}
module.exports.i2c_close = () => {
if (i2c_hand >= 0) module.exports._i2c_close(i2c_hand);
i2c_hand = -1;
}
module.exports.i2c_write_byte_data = (reg, byte_val) => {
if (i2c_hand >= 0) return module.exports._i2c_write_byte_data(i2c_hand, reg, byte_val);
}
module.exports.i2c_read_byte_data = reg => {
if (i2c_hand >= 0) return module.exports._i2c_read_byte_data(i2c_hand, reg);
}
module.exports.i2c_read_device = count => {
if (i2c_hand >= 0) return module.exports._i2c_read_device(i2c_hand, count).toString('utf8');
}
module.exports.i2c_write_device = data => {
if (i2c_hand >= 0) return module.exports._i2c_write_device(i2c_hand, Buffer.from(data));
}
// 終了処理
module.exports.close_all_handle = () => {
module.exports.gpiochip_close();
module.exports.serial_close();
module.exports.i2c_close();
}

View File

@ -529,28 +529,28 @@ Value I2cWriteDevice(const CallbackInfo &info)
Object Object
Init(Env env, Object exports) Init(Env env, Object exports)
{ {
exports.Set(String::New(env, "gpiochip_open"), Function::New(env, gpiochipOpen)); exports.Set(String::New(env, "_gpiochip_open"), Function::New(env, gpiochipOpen));
exports.Set(String::New(env, "gpiochip_close"), Function::New(env, gpiochipClose)); exports.Set(String::New(env, "_gpiochip_close"), Function::New(env, gpiochipClose));
exports.Set(String::New(env, "gpio_claim_input"), Function::New(env, gpioClaimInput)); exports.Set(String::New(env, "_gpio_claim_input"), Function::New(env, gpioClaimInput));
exports.Set(String::New(env, "gpio_claim_output"), Function::New(env, gpioClaimOutput)); exports.Set(String::New(env, "_gpio_claim_output"), Function::New(env, gpioClaimOutput));
exports.Set(String::New(env, "gpio_read"), Function::New(env, gpioRead)); exports.Set(String::New(env, "_gpio_read"), Function::New(env, gpioRead));
exports.Set(String::New(env, "gpio_write"), Function::New(env, gpioWrite)); exports.Set(String::New(env, "_gpio_write"), Function::New(env, gpioWrite));
exports.Set(String::New(env, "tx_servo"), Function::New(env, txServo)); exports.Set(String::New(env, "_tx_servo"), Function::New(env, txServo));
exports.Set(String::New(env, "tx_pwm"), Function::New(env, txPwm)); exports.Set(String::New(env, "_tx_pwm"), Function::New(env, txPwm));
exports.Set(String::New(env, "serial_open"), Function::New(env, serialOpen)); exports.Set(String::New(env, "_serial_open"), Function::New(env, serialOpen));
exports.Set(String::New(env, "serial_close"), Function::New(env, serialClose)); exports.Set(String::New(env, "_serial_close"), Function::New(env, serialClose));
exports.Set(String::New(env, "serial_read"), Function::New(env, SerialRead)); exports.Set(String::New(env, "_serial_read"), Function::New(env, SerialRead));
exports.Set(String::New(env, "serial_write"), Function::New(env, serialWrite)); 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_open"), Function::New(env, i2cOpen));
exports.Set(String::New(env, "i2c_close"), Function::New(env, i2cClose)); 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_write_byte"), Function::New(env, i2cWriteByte));
exports.Set(String::New(env, "i2c_read_byte"), Function::New(env, i2cReadByte)); 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_write_byte_data"), Function::New(env, i2cWriteByteData));
exports.Set(String::New(env, "i2c_read_byte_data"), Function::New(env, i2cReadByteData)); exports.Set(String::New(env, "_i2c_read_byte_data"), Function::New(env, i2cReadByteData));
exports.Set(String::New(env, "i2c_read_i2c_block_data"), Function::New(env, i2cReadI2cBlockData)); exports.Set(String::New(env, "_i2c_read_i2c_block_data"), Function::New(env, i2cReadI2cBlockData));
exports.Set(String::New(env, "i2c_read_word_data"), Function::New(env, I2cReadWordData)); exports.Set(String::New(env, "_i2c_read_word_data"), Function::New(env, I2cReadWordData));
exports.Set(String::New(env, "i2c_read_device"), Function::New(env, I2cReadDevice)); exports.Set(String::New(env, "_i2c_read_device"), Function::New(env, I2cReadDevice));
exports.Set(String::New(env, "i2c_write_device"), Function::New(env, I2cWriteDevice)); exports.Set(String::New(env, "_i2c_write_device"), Function::New(env, I2cWriteDevice));
return exports; return exports;
} }

View File

@ -25,9 +25,9 @@
"dependencies": { "dependencies": {
"@ocogeclub/bme280": "file:local_modules/@ocogeclub/bme280", "@ocogeclub/bme280": "file:local_modules/@ocogeclub/bme280",
"@ocogeclub/lgpio": "file:local_modules/@ocogeclub/lgpio", "@ocogeclub/lgpio": "file:local_modules/@ocogeclub/lgpio",
"axios": "^0.21.1",
"nodemailer": "^6.6.0",
"@tensorflow/tfjs-node": "^3.9.0", "@tensorflow/tfjs-node": "^3.9.0",
"@vladmandic/face-api": "^1.5.3" "@vladmandic/face-api": "^1.5.3",
"axios": "^0.21.1",
"nodemailer": "^6.6.0"
} }
} }

View File

@ -309,7 +309,9 @@ Blockly.JavaScript['ugj_gpiochip_open'] = function (block) {
Blockly.JavaScript.provideFunction_( Blockly.JavaScript.provideFunction_(
'require_oclg', [`const pi = require('@ocogeclub/lgpio');`] 'require_oclg', [`const pi = require('@ocogeclub/lgpio');`]
); );
var code = `var lgHand = pi.gpiochip_open(0);\n`; // // var code = `pi.gpio_chip_open(0);\nconsole.log(pi.get_hand());`; //
// var code = `var lgHand = pi.gpiochip_open(0);\n`; //
var code = `pi.gpiochip_open(0);\n`; //
return code; return code;
}; };
Blockly.Python['ugj_gpiochip_open'] = function (block) { Blockly.Python['ugj_gpiochip_open'] = function (block) {
@ -338,7 +340,8 @@ Blockly.Blocks['ugj_gpiochip_close'] = {
} }
}; };
Blockly.JavaScript['ugj_gpiochip_close'] = function (block) { Blockly.JavaScript['ugj_gpiochip_close'] = function (block) {
var code = 'pi.gpiochip_close(lgHand);\n'; var code = 'pi.gpiochip_close();\n';
// var code = 'pi.gpiochip_close(lgHand);\n';
return code; return code;
}; };
Blockly.Python['ugj_gpiochip_close'] = function (block) { Blockly.Python['ugj_gpiochip_close'] = function (block) {
@ -373,12 +376,12 @@ Blockly.Blocks['ugj_gpio_claim_output'] = {
}; };
Blockly.JavaScript['ugj_gpio_claim_output'] = function (block) { Blockly.JavaScript['ugj_gpio_claim_output'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var code = `pi.gpio_claim_output(lgHand, ${value_gpio});\n`; var code = `pi.gpio_claim_output(${value_gpio});\n`;
return code; return code;
}; };
Blockly.Python['ugj_gpio_claim_output'] = function (block) { Blockly.Python['ugj_gpio_claim_output'] = function (block) {
var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC);
var code = `pi.gpio_claim_output(lgHand, ${value_gpio})\n`; var code = `pi.gpio_claim_output(${value_gpio})\n`;
return code; return code;
}; };
@ -428,7 +431,7 @@ Blockly.Blocks['ugj_gpio_claim_input'] = {
Blockly.JavaScript['ugj_gpio_claim_input'] = function (block) { Blockly.JavaScript['ugj_gpio_claim_input'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var dropdown_lflag = block.getFieldValue('lflag'); var dropdown_lflag = block.getFieldValue('lflag');
var code = `pi.gpio_claim_input(lgHand, ${value_gpio}, ${dropdown_lflag});\n`; var code = `pi.gpio_claim_input(${value_gpio}, ${dropdown_lflag});\n`;
return code; return code;
}; };
Blockly.Python['ugj_gpio_claim_input'] = function (block) { Blockly.Python['ugj_gpio_claim_input'] = function (block) {
@ -515,7 +518,7 @@ Blockly.Blocks['ugj_gpio_write'] = {
Blockly.JavaScript['ugj_gpio_write'] = function (block) { Blockly.JavaScript['ugj_gpio_write'] = function (block) {
var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC);
var dropdown_level = block.getFieldValue('level'); var dropdown_level = block.getFieldValue('level');
var code = `pi.gpio_write(lgHand, ${value_gpio}, ${dropdown_level});\n`; var code = `pi.gpio_write(${value_gpio}, ${dropdown_level});\n`;
return code; return code;
}; };
Blockly.Python['ugj_gpio_write'] = function (block) { Blockly.Python['ugj_gpio_write'] = function (block) {
@ -624,11 +627,11 @@ var ugjSerialOpenDefinition = {
"type": "ugj_serial_open", "type": "ugj_serial_open",
"message0": "%{BKY_SERIAL_OPEN_TITLE}", "message0": "%{BKY_SERIAL_OPEN_TITLE}",
"args0": [ "args0": [
{ // {
"type": "input_value", // "type": "input_value",
"name": "tty", // "name": "tty",
"check": "String" // "check": "String"
}, // },
{ {
"type": "field_dropdown", "type": "field_dropdown",
"name": "baud", "name": "baud",
@ -661,12 +664,12 @@ Blockly.Blocks['ugj_serial_open'] = {
} }
}; };
Blockly.JavaScript['ugj_serial_open'] = function (block) { Blockly.JavaScript['ugj_serial_open'] = function (block) {
var value_tty = Blockly.JavaScript.valueToCode(block, 'tty', Blockly.JavaScript.ORDER_ATOMIC); // var value_tty = Blockly.JavaScript.valueToCode(block, 'tty', Blockly.JavaScript.ORDER_ATOMIC);
var dropdown_baud = block.getFieldValue('baud'); var dropdown_baud = block.getFieldValue('baud');
Blockly.JavaScript.provideFunction_( Blockly.JavaScript.provideFunction_(
'require_oclg', [`const pi = require('@ocogeclub/lgpio');`] 'require_oclg', [`const pi = require('@ocogeclub/lgpio');`]
); );
var code = `let ser_hand = pi.serial_open(${value_tty}, ${dropdown_baud}, 0);\n`; var code = `pi.serial_open('/dev/serial0', ${dropdown_baud});\n`;
return code; return code;
}; };
Blockly.Python['ugj_serial_open'] = function (block) { Blockly.Python['ugj_serial_open'] = function (block) {
@ -697,7 +700,7 @@ Blockly.Blocks['ugj_serial_close'] = {
} }
}; };
Blockly.JavaScript['ugj_serial_close'] = function (block) { Blockly.JavaScript['ugj_serial_close'] = function (block) {
var code = 'pi.serial_close(ser_hand);\n'; var code = 'pi.serial_close();\n';
return code; return code;
}; };
Blockly.Python['ugj_serial_close'] = function (block) { Blockly.Python['ugj_serial_close'] = function (block) {
@ -732,7 +735,7 @@ Blockly.Blocks['ugj_serial_write'] = {
}; };
Blockly.JavaScript['ugj_serial_write'] = function (block) { Blockly.JavaScript['ugj_serial_write'] = function (block) {
var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC); var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC);
var code = `pi.serial_write(ser_hand, Buffer.from(${value_data}));\n`; var code = `pi.serial_write(${value_data});\n`;
return code; return code;
}; };
Blockly.Python['ugj_serial_write'] = function (block) { Blockly.Python['ugj_serial_write'] = function (block) {
@ -767,7 +770,7 @@ 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 code = `pi.serial_read(ser_hand, ${value_count}).toString('utf8')`; var code = `pi.serial_read(${value_count})`;
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) {
@ -806,7 +809,7 @@ Blockly.JavaScript['ugj_i2c_open'] = function (block) {
Blockly.JavaScript.provideFunction_( Blockly.JavaScript.provideFunction_(
'require_oclg', [`const pi = require('@ocogeclub/lgpio');`] 'require_oclg', [`const pi = require('@ocogeclub/lgpio');`]
); );
var code = `let i2c_hand = pi.i2c_open(1, ${value_i2c_address});\n`; var code = `pi.i2c_open(1, ${value_i2c_address});\n`;
return code; return code;
}; };
Blockly.Python['ugj_i2c_open'] = function (block) { Blockly.Python['ugj_i2c_open'] = function (block) {
@ -836,7 +839,7 @@ Blockly.Blocks['ugj_i2c_close'] = {
} }
}; };
Blockly.JavaScript['ugj_i2c_close'] = function (block) { Blockly.JavaScript['ugj_i2c_close'] = function (block) {
var code = `pi.i2c_close(i2c_hand);\n`; var code = `pi.i2c_close();\n`;
return code; return code;
}; };
Blockly.Python['ugj_i2c_close'] = function (block) { Blockly.Python['ugj_i2c_close'] = function (block) {
@ -877,7 +880,7 @@ Blockly.Blocks['ugj_i2c_write_byte_data'] = {
Blockly.JavaScript['ugj_i2c_write_byte_data'] = function (block) { Blockly.JavaScript['ugj_i2c_write_byte_data'] = function (block) {
var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC); var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC);
var value_byte_val = Blockly.JavaScript.valueToCode(block, 'byte_val', Blockly.JavaScript.ORDER_ATOMIC); var value_byte_val = Blockly.JavaScript.valueToCode(block, 'byte_val', Blockly.JavaScript.ORDER_ATOMIC);
var code = `pi.i2c_write_byte_data(i2c_hand, ${value_reg}, ${value_byte_val});\n`; var code = `pi.i2c_write_byte_data(${value_reg}, ${value_byte_val});\n`;
return code; return code;
}; };
Blockly.Python['ugj_i2c_write_byte_data'] = function (block) { Blockly.Python['ugj_i2c_write_byte_data'] = function (block) {
@ -913,7 +916,7 @@ Blockly.Blocks['ugj_i2c_read_byte_data'] = {
}; };
Blockly.JavaScript['ugj_i2c_read_byte_data'] = function (block) { Blockly.JavaScript['ugj_i2c_read_byte_data'] = function (block) {
var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC); var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC);
var code = `pi.i2c_read_byte_data(i2c_hand, ${value_reg})\n`; var code = `pi.i2c_read_byte_data(${value_reg})\n`;
return [code, Blockly.JavaScript.ORDER_ATOMIC]; return [code, Blockly.JavaScript.ORDER_ATOMIC];
}; };
Blockly.Python['ugj_i2c_read_byte_data'] = function (block) { Blockly.Python['ugj_i2c_read_byte_data'] = function (block) {
@ -948,7 +951,7 @@ 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 code = `pi.i2c_read_device(i2c_hand, ${value_count}).toString('utf8')`; var code = `pi.i2c_read_device(${value_count})`;
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) {
@ -983,7 +986,7 @@ Blockly.Blocks['ugj_i2c_write_device'] = {
}; };
Blockly.JavaScript['ugj_i2c_write_device'] = function (block) { Blockly.JavaScript['ugj_i2c_write_device'] = function (block) {
var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC); var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC);
var code = `pi.i2c_write_device(i2c_hand, Buffer.from(${value_data}))\n`; var code = `pi.i2c_write_device(${value_data})\n`;
return code; return code;
}; };
Blockly.Python['ugj_i2c_write_device'] = function (block) { Blockly.Python['ugj_i2c_write_device'] = function (block) {