'use strict'; let rg = require('bindings')('rgpio'); exports.SET_ACTIVE_LOW = 4; exports.SET_OPEN_DRAIN = 8; exports.SET_OPEN_SOURCE = 16; exports.SET_PULL_UP = 32; exports.SET_PULL_DOWN = 64; exports.SET_PULL_NONE = 128; // Properties let sbc = -1; let chip_hand = []; exports.rgpio_sbc = async (host = 'localhost', port = 8889, show_errors = true) => { if (sbc < 0) { sbc = await rg._rgpiod_start(host, port.toString()); if (sbc < 0) { if (show_errors) console.log(sbc); } } return sbc; } exports.sbc_stop = async () => { await rg._rgpiod_stop(sbc); chip_hand = []; sbc = -1; } exports.gpiochip_open = async gpiochip => { if (!chip_hand[gpiochip] || chip_hand[gpiochip] < 0) { chip_hand[gpiochip] = await rg._gpiochip_open(sbc, gpiochip); } return chip_hand[gpiochip]; // return await rg._gpiochip_open(sbc, gpiochip); } exports.gpio_set_input = async (ugpio, lFlags = 0) => { let chip = Math.trunc(ugpio / 32); let gpio = ugpio % 32; let handle = await this.gpiochip_open(chip); return await rg._gpio_claim_input(sbc, handle, lFlags, gpio); } exports.gpio_set_output = async (ugpio, level = 0, lFlags = 0) => { let chip = Math.trunc(ugpio / 32); let gpio = ugpio % 32; let handle = await this.gpiochip_open(chip); return await rg._gpio_claim_output(sbc, handle, lFlags, gpio, level); } exports.gpio_read = async (ugpio) => { let chip = Math.trunc(ugpio / 32); let gpio = ugpio % 32; let handle = await this.gpiochip_open(chip); return await rg._gpio_read(sbc, handle, gpio); } exports.gpio_write = async (ugpio, level) => { let chip = Math.trunc(ugpio / 32); let gpio = ugpio % 32; let handle = await this.gpiochip_open(chip); return await rg._gpio_write(sbc, handle, gpio, level); } exports.serial_open = async (tty, baud, ser_flags = 0) => { return await rg._serial_open(sbc, tty, baud, ser_flags); } exports.serial_close = async handle => { await rg._serial_close(sbc, handle); } exports.serial_read = async (handle, count = 0, raw = false) => { if (count === 0) { count = await rg._serial_data_available(sbc, handle); if (count === 0) return ''; } if (raw) return await rg._serial_read(sbc, handle, count); else return new TextDecoder().decode(await rg._serial_read(sbc, handle, count)); } exports.serial_write = async (handle, data) => { return await rg._serial_write(sbc, handle, Buffer.from(data), -1); } exports.serial_data_available = async handle => { return await rg._serial_data_available(sbc, handle); } exports.i2c_open = async (i2c_bus, i2c_address, i2c_flags = 0) => { return await rg._i2c_open(sbc, i2c_bus, i2c_address, i2c_flags); } exports.i2c_close = async handle => { await rg._i2c_close(sbc, handle); } exports.i2c_write_byte = async (handle, byte_val) => { return await rg._i2c_write_byte(sbc, handle, byte_val); } exports.i2c_read_byte = async handle => { return await rg._i2c_read_byte(sbc, handle); } exports.i2c_write_byte_data = async (handle, reg, byte_val) => { return await rg._i2c_write_byte_data(sbc, handle, reg, byte_val); } exports.i2c_read_byte_data = async (handle, reg) => { return await rg._i2c_read_byte_data(sbc, handle, reg); } exports.i2c_read_i2c_block_data = async (handle, reg, count = -1) => { return await rg._i2c_read_i2c_block_data(sbc, handle, reg, count); } exports.i2c_write_i2c_block_data = async (handle, reg, data) => { return await rg._i2c_write_i2c_block_data(sbc, handle, reg, Buffer.from(data), -1); } exports.i2c_read_word_data = async (handle, reg) => { return await rg._i2c_read_word_data(sbc, handle, reg); } exports.i2c_write_word_data = async (handle, reg, word_val) => { return await rg._i2c_write_word_data(sbc, handle, reg, word_val); } exports.i2c_read_device = async (handle, count) => { return new TextDecoder().decode(await rg._i2c_read_device(sbc, handle, count)); } exports.i2c_write_device = async (handle, data) => { return await rg._i2c_write_device(sbc, handle, Buffer.from(data), -1); } /***** 同期関数 *****/ exports.rgpio_sbc_sync = (host = 'localhost', port = 8889, show_errors = true) => { if (sbc < 0) { sbc = rg._rgpiod_start_sync(host, port.toString()); if (sbc < 0) { if (show_errors) console.log(sbc); } } return sbc; } exports.sbc_stop_sync = () => { rg._rgpiod_stop_sync(sbc); chip_hand = []; sbc = -1; } exports.i2c_open_sync = (i2c_bus, i2c_address, i2c_flags = 0) => { return rg._i2c_open_sync(sbc, i2c_bus, i2c_address, i2c_flags); } exports.i2c_close_sync = handle => { rg._i2c_close_sync(sbc, handle); } exports.i2c_read_byte_sync = handle => { return rg._i2c_read_byte_sync(sbc, handle); } exports.i2c_write_device_sync = (handle, data, count = -1) => { let buffer = Buffer.from(data); if (count < 0) count = buffer.length; return rg._i2c_write_device_sync(sbc, handle, buffer, count); } // 終了処理 exports.close_all_handle = async () => { await this.sbc_stop(); }