2021-10-08 13:30:52 +00:00
|
|
|
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;
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.gpio_open = async () => {
|
|
|
|
if (pi < 0) pi = await module.exports._pigpio_start('', '');
|
2021-10-08 13:30:52 +00:00
|
|
|
return pi;
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.gpio_close = async () => {
|
|
|
|
if (pi >= 0) await module.exports._pigpio_stop(pi);
|
2021-10-08 13:30:52 +00:00
|
|
|
pi = -1;
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.gpio_set_output = async gpio => {
|
|
|
|
if (pi >= 0) return await module.exports._set_mode(pi, gpio, 1);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.gpio_set_input = async (gpio, mode) => {
|
2021-10-08 13:30:52 +00:00
|
|
|
if (pi >= 0) {
|
2021-11-25 14:23:54 +00:00
|
|
|
let r = await module.exports._set_mode(pi, gpio, 0);
|
2021-10-08 13:30:52 +00:00
|
|
|
if (r == 0)
|
2021-11-25 14:23:54 +00:00
|
|
|
return await module.exports._set_pull_up_down(pi, gpio, mode);
|
2021-10-08 13:30:52 +00:00
|
|
|
else
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.gpio_read = async gpio => {
|
|
|
|
if (pi >= 0) return await module.exports._gpio_read(pi, gpio);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.gpio_write = async (gpio, value) => {
|
|
|
|
if (pi >= 0) return await module.exports._gpio_write(pi, gpio, value);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.servo = async (gpio, pulse_width) => {
|
|
|
|
if (pi >= 0) return await module.exports._set_servo_pulsewidth(pi, gpio, pulse_width);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.pwm = async (gpio, pwm_frequency, pwm_duty_cycle) => {
|
2021-10-08 13:30:52 +00:00
|
|
|
if (pi >= 0) {
|
2021-11-25 14:23:54 +00:00
|
|
|
await module.exports._set_PWM_frequency(pi, gpio, pwm_frequency);
|
|
|
|
await module.exports._set_PWM_dutycycle(pi, gpio, pwm_duty_cycle);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
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);
|
2021-10-08 13:30:52 +00:00
|
|
|
return ser_hand;
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.serial_close = async () => {
|
|
|
|
if (ser_hand >= 0) await module.exports._serial_close(pi, ser_hand);
|
2021-10-08 13:30:52 +00:00
|
|
|
ser_hand = -1;
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.serial_write = async data => {
|
|
|
|
if (ser_hand >= 0) return await module.exports._serial_write(pi, ser_hand, Buffer.from(data));
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.serial_read = async count => {
|
2022-01-13 11:04:21 +00:00
|
|
|
if (ser_hand >= 0) return new TextDecoder().decode(await module.exports._serial_read(pi, ser_hand, count));
|
|
|
|
// if (ser_hand >= 0) return await module.exports._serial_read(pi, ser_hand, count);//.toString('utf8');
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
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);
|
2021-10-08 13:30:52 +00:00
|
|
|
return i2c_hand;
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.i2c_close = async () => {
|
|
|
|
if (i2c_hand >= 0) await module.exports._i2c_close(pi, i2c_hand);
|
2021-10-08 13:30:52 +00:00
|
|
|
i2c_hand = -1;
|
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
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);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.i2c_read_byte_data = async reg => {
|
|
|
|
if (i2c_hand >= 0) return await module.exports._i2c_read_byte_data(pi, i2c_hand, reg);
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
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));
|
2021-10-11 14:40:18 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.i2c_read_word_data = async reg => {
|
|
|
|
if (i2c_hand >= 0) return await module.exports._i2c_read_word_data(pi, i2c_hand, reg);
|
2021-10-22 14:33:59 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.i2c_read_device = async count => {
|
2022-01-13 11:04:21 +00:00
|
|
|
if (i2c_hand >= 0) return new TextDecoder().decode(await module.exports._i2c_read_device(pi, i2c_hand, count));
|
|
|
|
// if (i2c_hand >= 0) return await module.exports._i2c_read_device(pi, i2c_hand, count).toString('utf8');
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
2021-11-25 14:23:54 +00:00
|
|
|
module.exports.i2c_write_device = async data => {
|
|
|
|
if (i2c_hand >= 0) return await module.exports._i2c_write_device(pi, i2c_hand, Buffer.from(data));
|
2021-10-08 13:30:52 +00:00
|
|
|
}
|
|
|
|
// 終了処理
|
|
|
|
module.exports.close_all_handle = () => {
|
|
|
|
module.exports.serial_close();
|
|
|
|
module.exports.i2c_close();
|
|
|
|
module.exports.gpio_close();
|
|
|
|
}
|