From e6fe8e303de35a5d5417de2c25fbfa2f77426fc1 Mon Sep 17 00:00:00 2001 From: ocogeclub Date: Thu, 25 Nov 2021 23:23:54 +0900 Subject: [PATCH] =?UTF-8?q?[update]=20pigpio=20=E3=83=A9=E3=82=A4=E3=83=96?= =?UTF-8?q?=E3=83=A9=E3=83=AA=E3=82=92=20promise=20=E3=83=99=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=81=AB=E5=A4=89=E6=9B=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- local_modules/@ocogeclub/amg8833/AMG8833.js | 26 +- local_modules/@ocogeclub/bme280/BME280.js | 50 +- local_modules/@ocogeclub/paj7620/PAJ7620.js | 52 +- local_modules/@ocogeclub/pigpio/index.js | 88 +-- local_modules/@ocogeclub/pigpio/pigpio.cpp | 813 ++++++++++---------- package-lock.json | 19 +- ugj_blocks.js | 78 +- 7 files changed, 571 insertions(+), 555 deletions(-) diff --git a/local_modules/@ocogeclub/amg8833/AMG8833.js b/local_modules/@ocogeclub/amg8833/AMG8833.js index 46545f3..406cc3f 100644 --- a/local_modules/@ocogeclub/amg8833/AMG8833.js +++ b/local_modules/@ocogeclub/amg8833/AMG8833.js @@ -4,23 +4,23 @@ const pig = require('@ocogeclub/pigpio'); let pi = -1; let i2c_hand = -1; -exports.init = (i2c_bus, i2c_addr) => { - if (pi < 0) pi = pig._pigpio_start('', ''); - if (i2c_hand < 0) pig._i2c_close(pi, i2c_hand); - i2c_hand = pig._i2c_open(pi, i2c_bus, i2c_addr); - pig._i2c_write_byte_data(pi, i2c_hand, 0x00, 0x00); //Normal mode - pig._i2c_write_byte_data(pi, i2c_hand, 0x02, 0x00); //10FPS +exports.init = async (i2c_bus, i2c_addr) => { + if (pi < 0) pi = await pig._pigpio_start('', ''); + if (i2c_hand < 0) await pig._i2c_close(pi, i2c_hand); + i2c_hand = await pig._i2c_open(pi, i2c_bus, i2c_addr); + await pig._i2c_write_byte_data(pi, i2c_hand, 0x00, 0x00); //Normal mode + await pig._i2c_write_byte_data(pi, i2c_hand, 0x02, 0x00); //10FPS } -exports.read_thermistor = () => { - let temp = pig._i2c_read_word_data(pi, i2c_hand, 0x0e); +exports.read_thermistor = async () => { + let temp = await pig._i2c_read_word_data(pi, i2c_hand, 0x0e); return temp * 0.0625; } -exports.read_temp_array = () => { +exports.read_temp_array = async () => { let linedata = []; for (let i = 0; i < 8; i++) { - let data = pig._i2c_read_i2c_block_data(pi, i2c_hand, 0x80 + 0x10 * i, 16); + let data = await pig._i2c_read_i2c_block_data(pi, i2c_hand, 0x80 + 0x10 * i, 16); let oneline = []; for (let j = 0; j < 8; j++) { oneline.push(((data[2 * j + 1] & 0x07) * 256 + data[2 * j]) * 0.25); @@ -30,13 +30,13 @@ exports.read_temp_array = () => { return linedata; } -exports.stop = () => { +exports.stop = async () => { if (i2c_hand >= 0) { - pig._i2c_close(pi, i2c_hand); + await pig._i2c_close(pi, i2c_hand); i2c_hand = -1; } if (pi >= 0) { - pig._pigpio_stop(pi); + await pig._pigpio_stop(pi); pi = -1; } } diff --git a/local_modules/@ocogeclub/bme280/BME280.js b/local_modules/@ocogeclub/bme280/BME280.js index 47a3b2c..0a186a1 100644 --- a/local_modules/@ocogeclub/bme280/BME280.js +++ b/local_modules/@ocogeclub/bme280/BME280.js @@ -41,24 +41,24 @@ this.REGISTER_PRESSURE_DATA = 0xF7; this.REGISTER_TEMP_DATA = 0xFA; this.REGISTER_HUMIDITY_DATA = 0xFD; -exports.init = (options) => { +exports.init = async (options) => { this.pig = require('@ocogeclub/pigpio') // console.log('pig= ' + this.pig) - this.pi = this.pig._pigpio_start('', ''); + this.pi = await this.pig._pigpio_start('', ''); // console.log('pi= ' + this.pi) // this.pi = require('@ocogeclub/lgpio'); this.i2cBusNo = (options && options.hasOwnProperty('i2cBusNo')) ? options.i2cBusNo : 1; this.i2cAddress = (options && options.hasOwnProperty('i2cAddress')) ? options.i2cAddress : this.BME280_DEFAULT_I2C_ADDRESS(); - this.i2cHand = this.pig._i2c_open(this.pi, this.i2cBusNo, this.i2cAddress); + this.i2cHand = await this.pig._i2c_open(this.pi, this.i2cBusNo, this.i2cAddress); // console.log('i2cHand= ' + this.i2cHand) // this.i2cHand = this.pi._i2c_open(this.i2cBusNo, this.i2cAddress); let r; - r = this.pig._i2c_write_byte_data(this.pi, this.i2cHand, this.REGISTER_CHIPID, 0); + r = await this.pig._i2c_write_byte_data(this.pi, this.i2cHand, this.REGISTER_CHIPID, 0); // r = this.pi._i2c_write_byte_data(this.i2cHand, this.REGISTER_CHIPID, 0); if (r < 0) return r; - let chipId = this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_CHIPID); + let chipId = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_CHIPID); // let chipId = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_CHIPID); if (chipId !== this.CHIP_ID_BME280() && chipId !== this.CHIP_ID1_BMP280() && @@ -67,18 +67,18 @@ exports.init = (options) => { return `Unexpected BMx280 chip ID: 0x${chipId.toString(16).toUpperCase()}`; } // console.log(`Found BMx280 chip ID 0x${chipId.toString(16).toUpperCase()} on bus i2c-${this.i2cBusNo}, address 0x${this.i2cAddress.toString(16).toUpperCase()}`); - this.loadCalibration((err) => { + await this.loadCalibration(async (err) => { if (err) { return err; } // Humidity 16x oversampling // - let r = this.pig._i2c_write_byte_data(this.pi, this.i2cHand, this.REGISTER_CONTROL_HUM, 0b00000101); + let r = await this.pig._i2c_write_byte_data(this.pi, 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}`; // Temperture/pressure 16x oversampling, normal mode // - r = this.pig._i2c_write_byte_data(this.pi, this.i2cHand, this.REGISTER_CONTROL, 0b10110111); + r = await this.pig._i2c_write_byte_data(this.pi, 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}`; @@ -90,9 +90,9 @@ exports.init = (options) => { // // Perform a power-on reset procedure. You will need to call init() following a reset() // -exports.reset = () => { +exports.reset = async () => { const POWER_ON_RESET_CMD = 0xB6; - let r = this.pig._i2c_write_byte_data(this.pi, this.i2cHand, this.REGISTER_RESET, POWER_ON_RESET_CMD); + let r = await this.pig._i2c_write_byte_data(this.pi, 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}`; else return 0; @@ -102,24 +102,24 @@ exports.reset = () => { // // Cancels the sensor and releases resources. // -exports.cancel = () => { +exports.cancel = async () => { if (this.i2cHand >= 0) { - this.pig._i2c_close(this.pi, this.i2cHand); + await this.pig._i2c_close(this.pi, this.i2cHand); // this.pi._i2c_close(this.i2cHand); this.i2cHand = null; - this.pig._pigpio_stop(this.pi); + await this.pig._pigpio_stop(this.pi); this.pi = null; } } -exports.readSensorData = () => { +exports.readSensorData = async () => { if (!this.cal) { return 'You must first call bme280.init()'; } // Grab temperature, humidity, and pressure in a single read // - let buffer = this.pig._i2c_read_i2c_block_data(this.pi, this.i2cHand, this.REGISTER_PRESSURE_DATA, 8); + let buffer = await this.pig._i2c_read_i2c_block_data(this.pi, 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) @@ -171,18 +171,18 @@ exports.readSensorData = () => { }; } -exports.loadCalibration = (callback) => { - let buffer = this.pig._i2c_read_i2c_block_data(this.pi, this.i2cHand, this.REGISTER_DIG_T1, 24); +exports.loadCalibration = async (callback) => { + let buffer = await this.pig._i2c_read_i2c_block_data(this.pi, 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.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H1); - let h2 = this.pig._i2c_read_word_data(this.pi, this.i2cHand, this.REGISTER_DIG_H2); - let h3 = this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H3); - let h4 = this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H4); - let h5 = this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H5); - let h5_1 = this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H5 + 1); - let h6 = this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H6); + let h1 = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H1); + let h2 = await this.pig._i2c_read_word_data(this.pi, this.i2cHand, this.REGISTER_DIG_H2); + let h3 = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H3); + let h4 = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H4); + let h5 = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H5); + let h5_1 = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H5 + 1); + let h6 = await this.pig._i2c_read_byte_data(this.pi, this.i2cHand, this.REGISTER_DIG_H6); // 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 h3 = this.pi._i2c_read_byte_data(this.i2cHand, this.REGISTER_DIG_H3); @@ -215,7 +215,7 @@ exports.loadCalibration = (callback) => { }; // console.log('BME280 cal = ' + JSON.stringify(this.cal, null, 2)); - callback(); + await callback(); } } diff --git a/local_modules/@ocogeclub/paj7620/PAJ7620.js b/local_modules/@ocogeclub/paj7620/PAJ7620.js index 9ed92af..5b8dffd 100644 --- a/local_modules/@ocogeclub/paj7620/PAJ7620.js +++ b/local_modules/@ocogeclub/paj7620/PAJ7620.js @@ -317,18 +317,20 @@ let pi = -1; let i2c_hand = -1; //Initialize the sensors exports.init = async (i2c_bus, i2c_addr) => { - if (pi < 0) pi = pig._pigpio_start('', ''); - if (i2c_hand < 0) pig._i2c_close(pi, i2c_hand); - i2c_hand = pig._i2c_open(pi, i2c_bus, i2c_addr); + if (pi < 0) pi = await pig._pigpio_start('', ''); + if (i2c_hand < 0) await pig._i2c_close(pi, i2c_hand); + i2c_hand = await pig._i2c_open(pi, i2c_bus, i2c_addr); + if (debug) + console.log("pi" + pi + "i2c_hand" + i2c_hand); await sleep(.001); - paj7620SelectBank(BANK0); - paj7620SelectBank(BANK0); + await paj7620SelectBank(BANK0); + await paj7620SelectBank(BANK0); - let data0 = paj7620ReadReg(0, 1)[0]; - let data1 = paj7620ReadReg(1, 1)[0]; + let data0 = (await paj7620ReadReg(0, 1))[0]; + let data1 = (await paj7620ReadReg(1, 1))[0]; if (debug) - console.log("data0" + data0 + "data1" + data1); + console.log("data0:" + data0 + ", data1:" + data1); if (data0 != 0x20) //or data1 <> 0x76 console.log("Error with sensor"); //return 0xff @@ -336,27 +338,27 @@ exports.init = async (i2c_bus, i2c_addr) => { console.log("wake-up finish."); for (let i = 0; i < initRegisterArray.length; i += 1) - paj7620WriteReg(initRegisterArray[i][0], initRegisterArray[i][1]); + await paj7620WriteReg(initRegisterArray[i][0], initRegisterArray[i][1]); - paj7620SelectBank(BANK0); + await paj7620SelectBank(BANK0); console.log("Paj7620 initialize register finished."); } // Write a byte to a register on the Gesture sensor -const paj7620WriteReg = (addr, cmd) => - pig._i2c_write_word_data(pi, i2c_hand, addr, cmd); +const paj7620WriteReg = async (addr, cmd) => + await pig._i2c_write_word_data(pi, i2c_hand, addr, cmd); //Select a register bank on the Gesture Sensor -const paj7620SelectBank = bank => { +const paj7620SelectBank = async bank => { if (bank == BANK0) - paj7620WriteReg(PAJ7620_REGITER_BANK_SEL, PAJ7620_BANK0); + await paj7620WriteReg(PAJ7620_REGITER_BANK_SEL, PAJ7620_BANK0); } //Read a block of bytes of length "qty" starting at address "addr" from the Gesture sensor -const paj7620ReadReg = (addr, qty) => { - return pig._i2c_read_i2c_block_data(pi, i2c_hand, addr, qty); +const paj7620ReadReg = async (addr, qty) => { + return await pig._i2c_read_i2c_block_data(pi, i2c_hand, addr, qty); } //Return a vlaue from the gestire sensor which can be used in a program @@ -372,10 +374,10 @@ const paj7620ReadReg = (addr, qty) => { // 9:wave exports.return_gesture = async () => { - let data = paj7620ReadReg(0x43, 1)[0]; + let data = (await paj7620ReadReg(0x43, 1))[0]; if (data == GES_RIGHT_FLAG) { await sleep(GES_ENTRY_TIME); - data = paj7620ReadReg(0x43, 1)[0]; + data = (await paj7620ReadReg(0x43, 1))[0]; if (data == GES_FORWARD_FLAG) { return 1; // await sleep(GES_QUIT_TIME); @@ -390,7 +392,7 @@ exports.return_gesture = async () => { else if (data == GES_LEFT_FLAG) { await sleep(GES_ENTRY_TIME); - data = paj7620ReadReg(0x43, 1)[0]; + data = (await paj7620ReadReg(0x43, 1))[0]; if (data == GES_FORWARD_FLAG) { return 1; // await sleep(GES_QUIT_TIME); @@ -405,7 +407,7 @@ exports.return_gesture = async () => { else if (data == GES_UP_FLAG) { await sleep(GES_ENTRY_TIME); - data = paj7620ReadReg(0x43, 1)[0]; + data = (await paj7620ReadReg(0x43, 1))[0]; if (data == GES_FORWARD_FLAG) { return 1; // await sleep(GES_QUIT_TIME); @@ -420,7 +422,7 @@ exports.return_gesture = async () => { else if (data == GES_DOWN_FLAG) { await sleep(GES_ENTRY_TIME); - data = paj7620ReadReg(0x43, 1)[0]; + data = (await paj7620ReadReg(0x43, 1))[0]; if (data == GES_FORWARD_FLAG) { return 1; // await sleep(GES_QUIT_TIME); @@ -449,20 +451,20 @@ exports.return_gesture = async () => { return 8; else { - let data1 = paj7620ReadReg(0x44, 1)[0]; + let data1 = (await paj7620ReadReg(0x44, 1))[0]; if (data1 == GES_WAVE_FLAG) return 9; } return 0; } -exports.stop = () => { +exports.stop = async () => { if (i2c_hand >= 0) { - pig._i2c_close(pi, i2c_hand); + await pig._i2c_close(pi, i2c_hand); i2c_hand = -1; } if (pi >= 0) { - pig._pigpio_stop(pi); + await pig._pigpio_stop(pi); pi = -1; } } diff --git a/local_modules/@ocogeclub/pigpio/index.js b/local_modules/@ocogeclub/pigpio/index.js index b21642c..b92612b 100644 --- a/local_modules/@ocogeclub/pigpio/index.js +++ b/local_modules/@ocogeclub/pigpio/index.js @@ -13,82 +13,82 @@ module.exports.PULL_NONE = 0; let pi = -1; let i2c_hand = -1; let ser_hand = -1; -module.exports.gpio_open = () => { - if (pi < 0) pi = module.exports._pigpio_start('', ''); +module.exports.gpio_open = async () => { + if (pi < 0) pi = await module.exports._pigpio_start('', ''); return pi; } -module.exports.gpio_close = () => { - if (pi >= 0) module.exports._pigpio_stop(pi); +module.exports.gpio_close = async () => { + if (pi >= 0) await module.exports._pigpio_stop(pi); pi = -1; } -module.exports.gpio_set_output = gpio => { - if (pi >= 0) return module.exports._set_mode(pi, gpio, 1); +module.exports.gpio_set_output = async gpio => { + if (pi >= 0) return await module.exports._set_mode(pi, gpio, 1); } -module.exports.gpio_set_input = (gpio, mode) => { +module.exports.gpio_set_input = async (gpio, mode) => { if (pi >= 0) { - let r = module.exports._set_mode(pi, gpio, 0); + let r = await module.exports._set_mode(pi, gpio, 0); if (r == 0) - return module.exports._set_pull_up_down(pi, gpio, mode); + return await module.exports._set_pull_up_down(pi, gpio, mode); else return r; } } -module.exports.gpio_read = gpio => { - if (pi >= 0) return module.exports._gpio_read(pi, gpio); +module.exports.gpio_read = async gpio => { + if (pi >= 0) return await module.exports._gpio_read(pi, gpio); } -module.exports.gpio_write = (gpio, value) => { - if (pi >= 0) return module.exports._gpio_write(pi, gpio, value); +module.exports.gpio_write = async (gpio, value) => { + if (pi >= 0) return await module.exports._gpio_write(pi, gpio, value); } -module.exports.servo = (gpio, pulse_width) => { - if (pi >= 0) return module.exports._set_servo_pulsewidth(pi, gpio, pulse_width); +module.exports.servo = async (gpio, pulse_width) => { + if (pi >= 0) return await module.exports._set_servo_pulsewidth(pi, gpio, pulse_width); } -module.exports.pwm = (gpio, pwm_frequency, pwm_duty_cycle) => { +module.exports.pwm = async (gpio, pwm_frequency, pwm_duty_cycle) => { if (pi >= 0) { - module.exports._set_PWM_frequency(pi, gpio, pwm_frequency); - module.exports._set_PWM_dutycycle(pi, gpio, pwm_duty_cycle); + await module.exports._set_PWM_frequency(pi, gpio, pwm_frequency); + await module.exports._set_PWM_dutycycle(pi, gpio, pwm_duty_cycle); } } -module.exports.serial_open = (tty, baud) => { - if (ser_hand >= 0) module.exports._serial_close(pi, ser_hand); // 勝手に閉じる - ser_hand = module.exports._serial_open(pi, tty, baud); +module.exports.serial_open = async (tty, baud) => { + if (ser_hand >= 0) await module.exports._serial_close(pi, ser_hand); // 勝手に閉じる + ser_hand = await module.exports._serial_open(pi, tty, baud); return ser_hand; } -module.exports.serial_close = () => { - if (ser_hand >= 0) module.exports._serial_close(pi, ser_hand); +module.exports.serial_close = async () => { + if (ser_hand >= 0) await module.exports._serial_close(pi, ser_hand); ser_hand = -1; } -module.exports.serial_write = data => { - if (ser_hand >= 0) return module.exports._serial_write(pi, ser_hand, Buffer.from(data)); +module.exports.serial_write = async data => { + if (ser_hand >= 0) return await module.exports._serial_write(pi, ser_hand, Buffer.from(data)); } -module.exports.serial_read = count => { - if (ser_hand >= 0) return module.exports._serial_read(pi, ser_hand, count).toString('utf8'); +module.exports.serial_read = async count => { + if (ser_hand >= 0) return await module.exports._serial_read(pi, ser_hand, count).toString('utf8'); } -module.exports.i2c_open = (i2c_bus, i2c_address) => { - if (i2c_hand >= 0) module.exports._i2c_close(pi, i2c_hand); // 勝手に閉じる - i2c_hand = module.exports._i2c_open(pi, i2c_bus, i2c_address); +module.exports.i2c_open = async (i2c_bus, i2c_address) => { + if (i2c_hand >= 0) await module.exports._i2c_close(pi, i2c_hand); // 勝手に閉じる + i2c_hand = await module.exports._i2c_open(pi, i2c_bus, i2c_address); return i2c_hand; } -module.exports.i2c_close = () => { - if (i2c_hand >= 0) module.exports._i2c_close(pi, i2c_hand); +module.exports.i2c_close = async () => { + if (i2c_hand >= 0) await module.exports._i2c_close(pi, 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(pi, i2c_hand, reg, byte_val); +module.exports.i2c_write_byte_data = async (reg, byte_val) => { + if (i2c_hand >= 0) return await module.exports._i2c_write_byte_data(pi, i2c_hand, reg, byte_val); } -module.exports.i2c_read_byte_data = reg => { - if (i2c_hand >= 0) return module.exports._i2c_read_byte_data(pi, i2c_hand, reg); +module.exports.i2c_read_byte_data = async reg => { + if (i2c_hand >= 0) return await module.exports._i2c_read_byte_data(pi, i2c_hand, reg); } -module.exports.i2c_write_i2c_block_data = (reg, data) => { - if (i2c_hand >= 0) return module.exports._i2c_write_i2c_block_data(pi, i2c_hand, reg, Buffer.from(data)); +module.exports.i2c_write_i2c_block_data = async (reg, data) => { + if (i2c_hand >= 0) return await module.exports._i2c_write_i2c_block_data(pi, i2c_hand, reg, Buffer.from(data)); } -module.exports.i2c_read_word_data = reg => { - if (i2c_hand >= 0) return module.exports._i2c_read_word_data(pi, i2c_hand, reg); +module.exports.i2c_read_word_data = async reg => { + if (i2c_hand >= 0) return await module.exports._i2c_read_word_data(pi, i2c_hand, reg); } -module.exports.i2c_read_device = count => { - if (i2c_hand >= 0) return module.exports._i2c_read_device(pi, i2c_hand, count).toString('utf8'); +module.exports.i2c_read_device = async count => { + if (i2c_hand >= 0) return await module.exports._i2c_read_device(pi, i2c_hand, count).toString('utf8'); } -module.exports.i2c_write_device = data => { - if (i2c_hand >= 0) return module.exports._i2c_write_device(pi, i2c_hand, Buffer.from(data)); +module.exports.i2c_write_device = async data => { + if (i2c_hand >= 0) return await module.exports._i2c_write_device(pi, i2c_hand, Buffer.from(data)); } // 終了処理 module.exports.close_all_handle = () => { diff --git a/local_modules/@ocogeclub/pigpio/pigpio.cpp b/local_modules/@ocogeclub/pigpio/pigpio.cpp index bf8375d..e156594 100644 --- a/local_modules/@ocogeclub/pigpio/pigpio.cpp +++ b/local_modules/@ocogeclub/pigpio/pigpio.cpp @@ -9,649 +9,656 @@ using namespace Napi; // pigpio 初期化 -Value _pigpioStart(const CallbackInfo &info) +Promise _pigpioStart(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 2) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 2) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsString() || !info[1].IsString()) + else if (!info[0].IsString() || !info[1].IsString()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - std::string addrStr = info[0].As().Utf8Value(); - std::string portStr = info[1].As().Utf8Value(); - return Number::New(env, pigpio_start(addrStr.c_str(), portStr.c_str())); - // return Number::New(env, pigpio_start(&addrStr[0], &portStr[0])); + else + { + std::string addrStr = info[0].As().Utf8Value(); + std::string portStr = info[1].As().Utf8Value(); + deferred.Resolve(Number::New(env, pigpio_start(addrStr.c_str(), portStr.c_str()))); + } + return deferred.Promise(); } // pigpio 後始末 -Value _pigpioStop(const CallbackInfo &info) +Promise _pigpioStop(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 1) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 1) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "IInvalid argument count").Value()); } - if (!info[0].IsNumber()) + else if (!info[0].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid aargument types").Value()); } - int pi = info[0].As().Int32Value(); - pigpio_stop(pi); - return env.Null(); + else + { + int pi = info[0].As().Int32Value(); + pigpio_stop(pi); + deferred.Resolve(env.Null()); + } + return deferred.Promise(); } // GPIO 端子のモードを設定 -Value _setMode(const CallbackInfo &info) +Promise _setMode(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int gpio = info[1].As().Uint32Value(); - unsigned int mode = info[2].As().Uint32Value(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int gpio = info[1].As().Uint32Value(); + unsigned int mode = info[2].As().Uint32Value(); - return Number::New(env, - set_mode(pi, gpio, mode)); + deferred.Resolve(Number::New(env, set_mode(pi, gpio, mode))); + } + return deferred.Promise(); } -// GPIO 端子のモードを取得 -// Value _GetMode(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 gpio = info[0].As().DoubleValue(); - -// return Number::New(env, -// get_mode(pi, gpio)); -// } - // GPIOの内部プルアップ/ダウン抵抗の設定/クリア -Value _setPullUpDown(const CallbackInfo &info) +Promise _setPullUpDown(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int gpio = info[1].As().Uint32Value(); - unsigned int pud = info[2].As().Uint32Value(); - - return Number::New(env, - set_pull_up_down(pi, gpio, pud)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int gpio = info[1].As().Uint32Value(); + unsigned int pud = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, set_pull_up_down(pi, gpio, pud))); + } + return deferred.Promise(); } // GPIOの電圧を読む -Value _gpioRead(const CallbackInfo &info) +Promise _gpioRead(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 2) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 2) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - - int pi = info[0].As().Int32Value(); - unsigned int gpio = info[1].As().Uint32Value(); - - return Number::New(env, - gpio_read(pi, gpio)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int gpio = info[1].As().Uint32Value(); + deferred.Resolve(Number::New(env, gpio_read(pi, gpio))); + } + return deferred.Promise(); } // GPIO の電圧をセットする -Value _gpioWrite(const CallbackInfo &info) +Promise _gpioWrite(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int gpio = info[1].As().Uint32Value(); - unsigned int value = info[2].As().Uint32Value(); - - return Number::New(env, - gpio_write(pi, gpio, value)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int gpio = info[1].As().Uint32Value(); + unsigned int value = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, gpio_write(pi, gpio, value))); + } + return deferred.Promise(); } // サーボパルス幅をセットする -Value _setServoPulsewidth(const CallbackInfo &info) +Promise _setServoPulsewidth(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int user_gpio = info[1].As().Uint32Value(); - unsigned int pulsewidth = info[2].As().Uint32Value(); - - return Number::New(env, - set_servo_pulsewidth(pi, user_gpio, pulsewidth)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int user_gpio = info[1].As().Uint32Value(); + unsigned int pulsewidth = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, set_servo_pulsewidth(pi, user_gpio, pulsewidth))); + } + return deferred.Promise(); } // PWM周波数を設定する -Value _setPwmFrequency(const CallbackInfo &info) +Promise _setPwmFrequency(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int user_gpio = info[1].As().Uint32Value(); - unsigned int frequency = info[2].As().Uint32Value(); - - return Number::New(env, - set_PWM_frequency(pi, user_gpio, frequency)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int user_gpio = info[1].As().Uint32Value(); + unsigned int frequency = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, set_PWM_frequency(pi, user_gpio, frequency))); + } + return deferred.Promise(); } -// PWMのレンジを設定する -// Value _SetPwmRange(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 user_gpio = info[0].As().DoubleValue(); -// unsigned int range = info[1].As().DoubleValue(); - -// return Number::New(env, -// set_PWM_range(pi, user_gpio, range)); -// } - // PWMのデューティ比を指定して出力を開始する -Value _setPwmDutycycle(const CallbackInfo &info) +Promise _setPwmDutycycle(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int user_gpio = info[1].As().Uint32Value(); - unsigned int dutycycle = info[2].As().Uint32Value(); - - return Number::New(env, - set_PWM_dutycycle(pi, user_gpio, dutycycle)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int user_gpio = info[1].As().Uint32Value(); + unsigned int dutycycle = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, set_PWM_dutycycle(pi, user_gpio, dutycycle))); + } + return deferred.Promise(); } // シリアルポートを開く -Value _serialOpen(const CallbackInfo &info) +Promise _serialOpen(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsString() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsString() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - std::string ser_tty = info[1].As().Utf8Value(); - unsigned int baud = info[2].As().Uint32Value(); - // &ser_tty[0] - return Number::New(env, - serial_open(pi, (char *)ser_tty.c_str(), baud, 0)); + else + { + int pi = info[0].As().Int32Value(); + std::string ser_tty = info[1].As().Utf8Value(); + unsigned int baud = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, serial_open(pi, (char *)ser_tty.c_str(), baud, 0))); + } + return deferred.Promise(); } // シリアルポートを閉じる -Value _serialClose(const CallbackInfo &info) +Promise _serialClose(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 2) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 2) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - - return Number::New(env, - serial_close(pi, handle)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + deferred.Resolve(Number::New(env, serial_close(pi, handle))); + } + return deferred.Promise(); } // シリアルデバイスからデータを読む -Value _serialRead(const CallbackInfo &info) +Promise _serialRead(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int count = info[2].As().Uint32Value(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int count = info[2].As().Uint32Value(); - char buf[count]; - int rxCount = serial_read(pi, handle, buf, count); - auto outBuf = Buffer::Copy(env, buf, rxCount); - - return outBuf; + char buf[count]; + int rxCount = serial_read(pi, handle, buf, count); + auto outBuf = Buffer::Copy(env, buf, rxCount); + deferred.Resolve(outBuf); + } + return deferred.Promise(); } // シリアルデバイスにバイト列を書き込む(data: string) -Value _serialWrite(const CallbackInfo &info) +Promise _serialWrite(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsBuffer()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsBuffer()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - auto buf = info[2].As>(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + auto buf = info[2].As>(); - int count = buf.Length(); - - return Number::New(env, - serial_write(pi, handle, buf.Data(), count)); + int count = buf.Length(); + deferred.Resolve(Number::New(env, serial_write(pi, handle, buf.Data(), count));); + } + return deferred.Promise(); } // I2Cバスアドレスのデバイスのハンドルを返す -Value _i2cOpen(const CallbackInfo &info) +Promise _i2cOpen(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - - int pi = info[0].As().Int32Value(); - unsigned int i2c_bus = info[1].As().Uint32Value(); - unsigned int i2c_addr = info[2].As().Uint32Value(); - - return Number::New(env, - i2c_open(pi, i2c_bus, i2c_addr, 0)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int i2c_bus = info[1].As().Uint32Value(); + unsigned int i2c_addr = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_open(pi, i2c_bus, i2c_addr, 0))); + } + return deferred.Promise(); } // オープン済みI2Cハンドルを閉じる -Value _i2cClose(const CallbackInfo &info) +Promise _i2cClose(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 2) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 2) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - - return Number::New(env, - i2c_close(pi, handle)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_close(pi, handle))); + } + return deferred.Promise(); } // デバイスに1バイトを送る -Value _i2cWriteByte(const CallbackInfo &info) +Promise _i2cWriteByte(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int bVal = info[2].As().Uint32Value(); - - return Number::New(env, - i2c_write_byte(pi, handle, bVal)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int bVal = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_write_byte(pi, handle, bVal))); + } + return deferred.Promise(); } // デバイスから1バイトを受け取る -Value _i2cReadByte(const CallbackInfo &info) +Promise _i2cReadByte(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 2) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 2) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[0].IsNumber()) + else if (!info[0].IsNumber() || !info[0].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - - return Number::New(env, - i2c_read_byte(pi, handle)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_read_byte(pi, handle))); + } + return deferred.Promise(); } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタに1バイトを書き込む -Value _i2cWriteByteData(const CallbackInfo &info) +Promise _i2cWriteByteData(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 4) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 4) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int i2c_reg = info[2].As().Uint32Value(); - unsigned int bVal = info[3].As().Uint32Value(); - - return Number::New(env, - i2c_write_byte_data(pi, handle, i2c_reg, bVal)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int i2c_reg = info[2].As().Uint32Value(); + unsigned int bVal = info[3].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_write_byte_data(pi, handle, i2c_reg, bVal))); + } + return deferred.Promise(); } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタから1バイトを読み込む -Value _i2cReadByteData(const CallbackInfo &info) +Promise _i2cReadByteData(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int i2c_reg = info[2].As().Uint32Value(); - - return Number::New(env, - i2c_read_byte_data(pi, handle, i2c_reg)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int i2c_reg = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_read_byte_data(pi, handle, i2c_reg))); + } + return deferred.Promise(); } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタに最大32バイトのデータを書き込む。 -Value _i2cWriteI2cBlockData(const CallbackInfo &info) +Promise _i2cWriteI2cBlockData(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 4) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 4) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsBuffer()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsBuffer()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int i2c_reg = info[2].As().Uint32Value(); - auto buf = info[3].As>(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int i2c_reg = info[2].As().Uint32Value(); + auto buf = info[3].As>(); - unsigned int count = buf.Length(); - - return Number::New(env, - i2c_write_i2c_block_data(pi, handle, i2c_reg, buf.Data(), count)); + unsigned int count = buf.Length(); + deferred.Resolve(Number::New(env, i2c_write_i2c_block_data(pi, handle, i2c_reg, buf.Data(), count))); + } + return deferred.Promise(); } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタからcountバイトを読み込む。countは1~32。 -Value _i2cReadI2cBlockData(const CallbackInfo &info) +Promise _i2cReadI2cBlockData(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 4) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 4) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int i2cReg = info[2].As().Uint32Value(); - unsigned int count = info[3].As().Uint32Value(); - - char buf[count]; - int rxCount = i2c_read_i2c_block_data(pi, handle, i2cReg, buf, count); - auto outBuf = Buffer::Copy(env, buf, rxCount); - - return outBuf; + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int i2cReg = info[2].As().Uint32Value(); + unsigned int count = info[3].As().Uint32Value(); + char buf[count]; + int rxCount = i2c_read_i2c_block_data(pi, handle, i2cReg, buf, count); + auto outBuf = Buffer::Copy(env, buf, rxCount); + deferred.Resolve(outBuf); + } + return deferred.Promise(); } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタから単一の16ビットワードを読み取る -Value _i2cReadWordData(const CallbackInfo &info) +Promise _i2cReadWordData(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int i2c_reg = info[2].As().Uint32Value(); - - return Number::New(env, - i2c_read_word_data(pi, handle, i2c_reg)); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int i2c_reg = info[2].As().Uint32Value(); + deferred.Resolve(Number::New(env, i2c_read_word_data(pi, handle, i2c_reg))); + } + return deferred.Promise(); } // I2Cハンドルに関連付けられているデバイスの指定されたレジスタに単一の16ビットワードを書き込む -Value _i2cWriteWordData(const CallbackInfo &info) +Promise _i2cWriteWordData(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 4) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 4) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int i2c_reg = info[2].As().Uint32Value(); - unsigned int wVal = info[3].As().Uint32Value(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int i2c_reg = info[2].As().Uint32Value(); + unsigned int wVal = info[3].As().Uint32Value(); - return Number::New(env, - i2c_write_word_data(pi, handle, i2c_reg, wVal)); + deferred.Resolve(Number::New(env, i2c_write_word_data(pi, handle, i2c_reg, wVal))); + } + return deferred.Promise(); } // i2c デバイスからデータを受け取る -Value _i2cReadDevice(const CallbackInfo &info) +Promise _i2cReadDevice(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - unsigned int count = info[2].As().Uint32Value(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + unsigned int count = info[2].As().Uint32Value(); - char buf[count]; - int rxCount = i2c_read_device(pi, handle, buf, count); - auto outBuf = Buffer::Copy(env, buf, rxCount); - - return outBuf; + char buf[count]; + int rxCount = i2c_read_device(pi, handle, buf, count); + auto outBuf = Buffer::Copy(env, buf, rxCount); + deferred.Resolve(outBuf); + } + return deferred.Promise(); } // i2c デバイスにバイト列を送る(data: buffer) -Value _i2cWriteDevice(const CallbackInfo &info) +Promise _i2cWriteDevice(const CallbackInfo &info) { Env env = info.Env(); - if (info.Length() < 3) + auto deferred = Napi::Promise::Deferred::New(env); + if (info.Length() != 3) { - TypeError::New(env, "Wrong number of arguments") - .ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + TypeError::New(env, "Invalid argument count").Value()); } - if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsBuffer()) + else if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsBuffer()) { - TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException(); - return env.Null(); + deferred.Reject( + Napi::TypeError::New(env, "Invalid argument types").Value()); } - int pi = info[0].As().Int32Value(); - unsigned int handle = info[1].As().Uint32Value(); - auto buf = info[2].As>(); + else + { + int pi = info[0].As().Int32Value(); + unsigned int handle = info[1].As().Uint32Value(); + auto buf = info[2].As>(); - unsigned int count = buf.Length(); - - return Number::New(env, - i2c_write_device(pi, handle, buf.Data(), count)); + unsigned int count = buf.Length(); + deferred.Resolve(Number::New(env, i2c_write_device(pi, handle, buf.Data(), count))); + } + return deferred.Promise(); } Object Init(Env env, Object exports) { - // exports.Set(String::New(env, "usleep"), Function::New(env, _Usleep)); exports.Set(String::New(env, "_pigpio_start"), Function::New(env, _pigpioStart)); exports.Set(String::New(env, "_pigpio_stop"), Function::New(env, _pigpioStop)); exports.Set(String::New(env, "_set_mode"), Function::New(env, _setMode)); - // exports.Set(String::New(env, "get_mode"), Function::New(env, _GetMode)); exports.Set(String::New(env, "_set_pull_up_down"), Function::New(env, _setPullUpDown)); 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, "_set_servo_pulsewidth"), Function::New(env, _setServoPulsewidth)); exports.Set(String::New(env, "_set_PWM_frequency"), Function::New(env, _setPwmFrequency)); - // exports.Set(String::New(env, "set_PWM_range"), Function::New(env, _SetPwmRange)); exports.Set(String::New(env, "_set_PWM_dutycycle"), Function::New(env, _setPwmDutycycle)); exports.Set(String::New(env, "_serial_open"), Function::New(env, _serialOpen)); exports.Set(String::New(env, "_serial_close"), Function::New(env, _serialClose)); diff --git a/package-lock.json b/package-lock.json index c58f251..17092ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "ocoge", "version": "0.1.3", "license": "ISC", "dependencies": { @@ -3819,12 +3820,14 @@ "@tensorflow-models/knn-classifier": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@tensorflow-models/knn-classifier/-/knn-classifier-1.2.2.tgz", - "integrity": "sha512-QRnkCf7ErOxSRtvJ6yCwhlLREPcBJGaXRanF46f0iY6ii3Sybjb6Ux0qnNPTrHZChD0izPa3Z4GQEgSAykiHkQ==" + "integrity": "sha512-QRnkCf7ErOxSRtvJ6yCwhlLREPcBJGaXRanF46f0iY6ii3Sybjb6Ux0qnNPTrHZChD0izPa3Z4GQEgSAykiHkQ==", + "requires": {} }, "@tensorflow-models/mobilenet": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@tensorflow-models/mobilenet/-/mobilenet-2.1.0.tgz", - "integrity": "sha512-JjqT9ijHDFA2FEpUGWg7H2lQ0GrMuE2VmiCRBYmUew6b4JKht8LXDjG5HxZh95YH6c/25sZWTpGeHbquloH+hw==" + "integrity": "sha512-JjqT9ijHDFA2FEpUGWg7H2lQ0GrMuE2VmiCRBYmUew6b4JKht8LXDjG5HxZh95YH6c/25sZWTpGeHbquloH+hw==", + "requires": {} }, "@tensorflow/tfjs": { "version": "3.11.0", @@ -3869,7 +3872,8 @@ "@tensorflow/tfjs-converter": { "version": "3.11.0", "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-converter/-/tfjs-converter-3.11.0.tgz", - "integrity": "sha512-rTRIKvBoqL0qdPYpm8UXauZycOiaBHZB2E2v3OoXoHnjvle/Xn/09uZJdrixgGhR+Kahs3Vz27BEEFz6RI5j2w==" + "integrity": "sha512-rTRIKvBoqL0qdPYpm8UXauZycOiaBHZB2E2v3OoXoHnjvle/Xn/09uZJdrixgGhR+Kahs3Vz27BEEFz6RI5j2w==", + "requires": {} }, "@tensorflow/tfjs-core": { "version": "3.11.0", @@ -3897,7 +3901,8 @@ "@tensorflow/tfjs-layers": { "version": "3.11.0", "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-layers/-/tfjs-layers-3.11.0.tgz", - "integrity": "sha512-BtLgLucJZHv5te1K3yjT3iZdHXgMJArrLuOb/oRPOtTp4R2ad5N0V2m5RtuZJ3sI5/ah0h72xtmTWNyTv3/5dw==" + "integrity": "sha512-BtLgLucJZHv5te1K3yjT3iZdHXgMJArrLuOb/oRPOtTp4R2ad5N0V2m5RtuZJ3sI5/ah0h72xtmTWNyTv3/5dw==", + "requires": {} }, "@tensorflow/tfjs-node": { "version": "3.11.0", @@ -6142,7 +6147,8 @@ "@tensorflow/tfjs-converter": { "version": "2.8.6", "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-converter/-/tfjs-converter-2.8.6.tgz", - "integrity": "sha512-Uv4YC66qjVC9UwBxz0IeLZ8KS2CReh63WlGRtHcSwDEYiwsa7cvp9H6lFSSPT7kiJmrK6JtHeJGIVcTuNnSt9w==" + "integrity": "sha512-Uv4YC66qjVC9UwBxz0IeLZ8KS2CReh63WlGRtHcSwDEYiwsa7cvp9H6lFSSPT7kiJmrK6JtHeJGIVcTuNnSt9w==", + "requires": {} }, "@tensorflow/tfjs-core": { "version": "2.8.6", @@ -6168,7 +6174,8 @@ "@tensorflow/tfjs-layers": { "version": "2.8.6", "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-layers/-/tfjs-layers-2.8.6.tgz", - "integrity": "sha512-fdZ0i/R2dIKmy8OB5tBAsm5IbAHfJpI6AlbjxpgoU3aWj1HCdDo+pMji928MkDJhP01ISgFTgw/7PseGNaUflw==" + "integrity": "sha512-fdZ0i/R2dIKmy8OB5tBAsm5IbAHfJpI6AlbjxpgoU3aWj1HCdDo+pMji928MkDJhP01ISgFTgw/7PseGNaUflw==", + "requires": {} }, "@types/webgl2": { "version": "0.0.5", diff --git a/ugj_blocks.js b/ugj_blocks.js index c51f084..d11a4eb 100644 --- a/ugj_blocks.js +++ b/ugj_blocks.js @@ -323,7 +323,7 @@ Blockly.JavaScript['ugj_gpio_open'] = function (block) { Blockly.JavaScript.provideFunction_( 'require_gpio', [`const pi = require('@ocogeclub/` + elutil.gpio_backend + `');`] ); - var code = `console.log('pi='+pi.gpio_open());\n`; // + var code = `await pi.gpio_open();\n`; // return code; }; Blockly.Python['ugj_gpio_open'] = function (block) { @@ -352,7 +352,7 @@ Blockly.Blocks['ugj_gpio_close'] = { } }; Blockly.JavaScript['ugj_gpio_close'] = function (block) { - var code = 'pi.gpio_close();\n'; + var code = 'await pi.gpio_close();\n'; return code; }; Blockly.Python['ugj_gpio_close'] = function (block) { @@ -387,12 +387,12 @@ Blockly.Blocks['ugj_gpio_set_output'] = { }; Blockly.JavaScript['ugj_gpio_set_output'] = function (block) { var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.gpio_set_output(${value_gpio});\n`; + var code = `await pi.gpio_set_output(${value_gpio});\n`; return code; }; Blockly.Python['ugj_gpio_set_output'] = function (block) { var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); - var code = `pi.gpio_set_output(${value_gpio})\n`; + var code = `await pi.gpio_set_output(${value_gpio})\n`; return code; }; @@ -442,13 +442,13 @@ Blockly.Blocks['ugj_gpio_set_input'] = { Blockly.JavaScript['ugj_gpio_set_input'] = function (block) { var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var dropdown_lflag = block.getFieldValue('lflag'); - var code = `pi.gpio_set_input(${value_gpio}, ${dropdown_lflag});\n`; + var code = `await pi.gpio_set_input(${value_gpio}, ${dropdown_lflag});\n`; return code; }; Blockly.Python['ugj_gpio_set_input'] = function (block) { var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); var dropdown_lflag = block.getFieldValue('lflag'); - var code = `pi.gpio_set_input(gpioHand, ${value_gpio}, ${dropdown_lflag})\n`; + var code = `await pi.gpio_set_input(gpioHand, ${value_gpio}, ${dropdown_lflag})\n`; return code; }; @@ -478,12 +478,12 @@ Blockly.Blocks['ugj_gpio_read'] = { }; Blockly.JavaScript['ugj_gpio_read'] = function (block) { var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.gpio_read(${value_gpio})`; + var code = `await pi.gpio_read(${value_gpio})`; return [code, Blockly.JavaScript.ORDER_NONE]; }; Blockly.Python['ugj_gpio_read'] = function (block) { var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); - var code = `pi.gpio_read(gpioHand, ${value_gpio})`; + var code = `await pi.gpio_read(gpioHand, ${value_gpio})`; return [code, Blockly.Python.ORDER_NONE]; }; @@ -529,13 +529,13 @@ Blockly.Blocks['ugj_gpio_write'] = { Blockly.JavaScript['ugj_gpio_write'] = function (block) { var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var dropdown_level = block.getFieldValue('level'); - var code = `pi.gpio_write(${value_gpio}, ${dropdown_level});\n`; + var code = `await pi.gpio_write(${value_gpio}, ${dropdown_level});\n`; return code; }; Blockly.Python['ugj_gpio_write'] = function (block) { var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); var dropdown_level = block.getFieldValue('level'); - var code = `pi.gpio_write(gpioHand, ${value_gpio}, ${dropdown_level})\n`; + var code = `await pi.gpio_write(gpioHand, ${value_gpio}, ${dropdown_level})\n`; return code; }; @@ -572,13 +572,13 @@ Blockly.Blocks['ugj_servo'] = { Blockly.JavaScript['ugj_servo'] = function (block) { var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var value_pulsewidth = Blockly.JavaScript.valueToCode(block, 'pulsewidth', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.servo(${value_gpio}, ${value_pulsewidth});\n`; + var code = `await pi.servo(${value_gpio}, ${value_pulsewidth});\n`; return code; }; Blockly.Python['ugj_servo'] = function (block) { var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); var value_pulsewidth = Blockly.Python.valueToCode(block, 'pulsewidth', Blockly.Python.ORDER_ATOMIC); - var code = `pi.servo(gpioHand, ${value_gpio}, ${value_pulsewidth})\n`; + var code = `await pi.servo(gpioHand, ${value_gpio}, ${value_pulsewidth})\n`; return code; }; @@ -621,14 +621,14 @@ Blockly.JavaScript['ugj_pwm'] = function (block) { var value_gpio = Blockly.JavaScript.valueToCode(block, 'gpio', Blockly.JavaScript.ORDER_ATOMIC); var value_pwm_frequency = Blockly.JavaScript.valueToCode(block, 'pwm_frequency', Blockly.JavaScript.ORDER_ATOMIC); var value_pwm_duty_cycle = Blockly.JavaScript.valueToCode(block, 'pwm_duty_cycle', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.pwm(${value_gpio}, ${value_pwm_frequency}, ${value_pwm_duty_cycle});\n`; + var code = `await pi.pwm(${value_gpio}, ${value_pwm_frequency}, ${value_pwm_duty_cycle});\n`; return code; }; Blockly.Python['ugj_pwm'] = function (block) { var value_gpio = Blockly.Python.valueToCode(block, 'gpio', Blockly.Python.ORDER_ATOMIC); var value_pwm_frequency = Blockly.Python.valueToCode(block, 'pwm_frequency', Blockly.Python.ORDER_ATOMIC); var value_pwm_duty_cycle = Blockly.Python.valueToCode(block, 'pwm_duty_cycle', Blockly.Python.ORDER_ATOMIC); - var code = `pi.pwm(gpioHand, ${value_gpio}, ${value_pwm_frequency}, ${value_pwm_duty_cycle})\n`; + var code = `await pi.pwm(gpioHand, ${value_gpio}, ${value_pwm_frequency}, ${value_pwm_duty_cycle})\n`; return code; }; /********************** */ @@ -680,7 +680,7 @@ Blockly.JavaScript['ugj_serial_open'] = function (block) { Blockly.JavaScript.provideFunction_( 'require_gpio', [`const pi = require('@ocogeclub/` + elutil.gpio_backend + `');`] ); - var code = `pi.serial_open('/dev/serial0', ${dropdown_baud});\n`; + var code = `await pi.serial_open('/dev/serial0', ${dropdown_baud});\n`; return code; }; Blockly.Python['ugj_serial_open'] = function (block) { @@ -746,12 +746,12 @@ Blockly.Blocks['ugj_serial_write'] = { }; Blockly.JavaScript['ugj_serial_write'] = function (block) { var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.serial_write(${value_data});\n`; + var code = `await pi.serial_write(${value_data});\n`; return code; }; Blockly.Python['ugj_serial_write'] = function (block) { var value_data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC); - var code = `pi.serial_write(ser_hand, ${value_data}.encode())\n`; + var code = `await pi.serial_write(ser_hand, ${value_data}.encode())\n`; return code; }; @@ -781,12 +781,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 code = `pi.serial_read(${value_count})`; + var code = `await pi.serial_read(${value_count})`; 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 code = `pi.serial_read(ser_hand, ${value_count}).decode()`; + var code = `await pi.serial_read(ser_hand, ${value_count}).decode()`; return [code, Blockly.Python.ORDER_ATOMIC]; }; @@ -850,11 +850,11 @@ Blockly.Blocks['ugj_i2c_close'] = { } }; Blockly.JavaScript['ugj_i2c_close'] = function (block) { - var code = `pi.i2c_close();\n`; + var code = `await pi.i2c_close();\n`; return code; }; Blockly.Python['ugj_i2c_close'] = function (block) { - var code = `pi.i2c_close(i2c_hand)\n`; + var code = `await pi.i2c_close(i2c_hand)\n`; return code; }; @@ -891,13 +891,13 @@ Blockly.Blocks['ugj_i2c_write_byte_data'] = { Blockly.JavaScript['ugj_i2c_write_byte_data'] = function (block) { 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 code = `pi.i2c_write_byte_data(${value_reg}, ${value_byte_val});\n`; + var code = `await pi.i2c_write_byte_data(${value_reg}, ${value_byte_val});\n`; return code; }; Blockly.Python['ugj_i2c_write_byte_data'] = function (block) { var value_reg = Blockly.Python.valueToCode(block, 'reg', Blockly.Python.ORDER_ATOMIC); var value_byte_val = Blockly.Python.valueToCode(block, 'byte_val', Blockly.Python.ORDER_ATOMIC); - var code = `pi.i2c_write_byte_data(i2c_hand, ${value_reg}, ${value_byte_val})\n`; + var code = `await pi.i2c_write_byte_data(i2c_hand, ${value_reg}, ${value_byte_val})\n`; return code; }; @@ -927,12 +927,12 @@ Blockly.Blocks['ugj_i2c_read_byte_data'] = { }; Blockly.JavaScript['ugj_i2c_read_byte_data'] = function (block) { var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.i2c_read_byte_data(${value_reg})\n`; + var code = `await pi.i2c_read_byte_data(${value_reg})\n`; return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.Python['ugj_i2c_read_byte_data'] = function (block) { var value_reg = Blockly.Python.valueToCode(block, 'reg', Blockly.Python.ORDER_ATOMIC); - var code = `pi.i2c_read_byte_data(i2c_hand, ${value_reg})\n`; + var code = `await pi.i2c_read_byte_data(i2c_hand, ${value_reg})\n`; return [code, Blockly.Python.ORDER_ATOMIC]; }; @@ -966,7 +966,7 @@ Blockly.Blocks['ugj_i2c_write_i2c_block_data'] = { Blockly.JavaScript['ugj_i2c_write_i2c_block_data'] = function (block) { var value_reg = Blockly.JavaScript.valueToCode(block, 'reg', Blockly.JavaScript.ORDER_ATOMIC); var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.i2c_write_i2c_block_data (${value_reg}, ${value_data});`; + var code = `await pi.i2c_write_i2c_block_data (${value_reg}, ${value_data});`; return code; }; Blockly.Python['ugj_i2c_write_i2c_block_data'] = function (block) { @@ -1003,12 +1003,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 code = `pi.i2c_read_device(${value_count})`; + var code = `await pi.i2c_read_device(${value_count})`; 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 code = `pi.i2c_read_device(i2c_hand, ${value_count}).decode()`; + var code = `await pi.i2c_read_device(i2c_hand, ${value_count}).decode()`; return [code, Blockly.Python.ORDER_ATOMIC]; }; /********************************************** */ @@ -1038,12 +1038,12 @@ Blockly.Blocks['ugj_i2c_write_device'] = { }; Blockly.JavaScript['ugj_i2c_write_device'] = function (block) { var value_data = Blockly.JavaScript.valueToCode(block, 'data', Blockly.JavaScript.ORDER_ATOMIC); - var code = `pi.i2c_write_device(${value_data})\n`; + var code = `await pi.i2c_write_device(${value_data})\n`; return code; }; Blockly.Python['ugj_i2c_write_device'] = function (block) { var value_data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC); - var code = `pi.i2c_write_device(i2c_hand, ${value_data}.encode())\n`; + var code = `await pi.i2c_write_device(i2c_hand, ${value_data}.encode())\n`; return code; }; @@ -1101,12 +1101,12 @@ Blockly.JavaScript['ugj_bme280'] = function (block) { ` i2cBusNo: ${elutil.i2c_bus},`, ` i2cAddress: ${value_address}`, `};`, - `bme280.init(options);`, - `let thp = bme280.readSensorData();`, + `await bme280.init(options);`, + `let thp = await bme280.readSensorData();`, `${variable_temp} = Math.round(thp.temperature_C * 10) / 10;`, `${variable_hum} = Math.round(thp.humidity * 10) / 10;`, `${variable_pres} = Math.round(thp.pressure_hPa);`, - `bme280.cancel();`, + `await bme280.cancel();`, `` ].join('\n'); return code; @@ -1161,7 +1161,7 @@ Blockly.JavaScript['ugj_gesture_init'] = function (block) { Blockly.JavaScript.provideFunction_( 'require_paj7620', [`const paj7620 = require('@ocogeclub/paj7620');`] ); - var code = `paj7620.init(${elutil.i2c_bus}, ${value_i2c_addr}); + var code = `await paj7620.init(${elutil.i2c_bus}, ${value_i2c_addr}); `; return code; }; @@ -1218,7 +1218,7 @@ Blockly.Blocks['ugj_gesture_stop'] = { } }; Blockly.JavaScript['ugj_gesture_stop'] = function (block) { - var code = 'paj7620.stop();\n'; + var code = 'await paj7620.stop();\n'; return code; }; Blockly.Python['ugj_gesture_stop'] = function (block) { @@ -1266,7 +1266,7 @@ Blockly.JavaScript['ugj_grideye_init'] = function (block) { Blockly.JavaScript.provideFunction_( 'require_amg8833', [`const _amg8833 = require('@ocogeclub/amg8833');`] ); - var code = `_amg8833.init(${elutil.i2c_bus}, ${dropdown_addr});\n`; + var code = `await _amg8833.init(${elutil.i2c_bus}, ${dropdown_addr});\n`; return code; }; Blockly.Python['ugj_grideye_init'] = function (block) { @@ -1292,7 +1292,7 @@ Blockly.Blocks['ugj_grideye_thermistor'] = { } }; Blockly.JavaScript['ugj_grideye_thermistor'] = function (block) { - var code = `_amg8833.read_thermistor()`; + var code = `await _amg8833.read_thermistor()`; return [code, Blockly.JavaScript.ORDER_NONE]; }; Blockly.Python['ugj_grideye_thermistor'] = function (block) { @@ -1320,7 +1320,7 @@ Blockly.Blocks['ugj_grideye_read'] = { } }; Blockly.JavaScript['ugj_grideye_read'] = function (block) { - var code = '_amg8833.read_temp_array()'; + var code = 'await _amg8833.read_temp_array()'; return [code, Blockly.JavaScript.ORDER_ATOMIC]; }; Blockly.Python['ugj_grideye_read'] = function (block) { @@ -1348,7 +1348,7 @@ Blockly.Blocks['ugj_grideye_stop'] = { } }; Blockly.JavaScript['ugj_grideye_stop'] = function (block) { - var code = '_amg8833.stop();\n'; + var code = 'await _amg8833.stop();\n'; return code; }; Blockly.Python['ugj_grideye_stop'] = function (block) {