module.exports = require('bindings')('pigpio'); // module.exports.INPUT = 0; // module.exports.OUTPUT = 1; // module.exports.PUD_OFF = 0; // module.exports.PUD_DOWN = 1; // module.exports.PUD_UP = 2; module.exports.PULL_UP = 2; module.exports.PULL_DOWN = 1; 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('', ''); return pi; } module.exports.gpio_close = () => { if (pi >= 0) 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_input = (gpio, mode) => { if (pi >= 0) { let r = module.exports._set_mode(pi, gpio, 0); if (r == 0) return 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_write = (gpio, value) => { if (pi >= 0) return 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.pwm = (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); } } 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); return ser_hand; } module.exports.serial_close = () => { if (ser_hand >= 0) 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_read = count => { if (ser_hand >= 0) return 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); return i2c_hand; } module.exports.i2c_close = () => { if (i2c_hand >= 0) 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_read_byte_data = reg => { if (i2c_hand >= 0) return 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_read_device = count => { if (i2c_hand >= 0) return 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.close_all_handle = () => { module.exports.serial_close(); module.exports.i2c_close(); module.exports.gpio_close(); }