module.exports = require('bindings')('lgpio'); module.exports.SET_ACTIVE_LOW = 4; module.exports.SET_OPEN_DRAIN = 8; module.exports.SET_OPEN_SOURCE = 16; module.exports.SET_PULL_UP = 32; module.exports.SET_PULL_DOWN = 64; 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(); }