mirror of
https://github.com/ocogeclub/ocoge.git
synced 2024-11-21 23:29:48 +00:00
98 lines
3.7 KiB
JavaScript
98 lines
3.7 KiB
JavaScript
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 = async () => {
|
|
if (pi < 0) pi = await module.exports._pigpio_start('', '');
|
|
return pi;
|
|
}
|
|
module.exports.gpio_close = async () => {
|
|
if (pi >= 0) await module.exports._pigpio_stop(pi);
|
|
pi = -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 = async (gpio, mode) => {
|
|
if (pi >= 0) {
|
|
let r = await module.exports._set_mode(pi, gpio, 0);
|
|
if (r == 0)
|
|
return await module.exports._set_pull_up_down(pi, gpio, mode);
|
|
else
|
|
return r;
|
|
}
|
|
}
|
|
module.exports.gpio_read = async gpio => {
|
|
if (pi >= 0) return await module.exports._gpio_read(pi, gpio);
|
|
}
|
|
module.exports.gpio_write = async (gpio, value) => {
|
|
if (pi >= 0) return await module.exports._gpio_write(pi, gpio, value);
|
|
}
|
|
module.exports.servo = async (gpio, pulse_width) => {
|
|
if (pi >= 0) return await module.exports._set_servo_pulsewidth(pi, gpio, pulse_width);
|
|
}
|
|
module.exports.pwm = async (gpio, pwm_frequency, pwm_duty_cycle) => {
|
|
if (pi >= 0) {
|
|
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 = 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 = async () => {
|
|
if (ser_hand >= 0) await module.exports._serial_close(pi, ser_hand);
|
|
ser_hand = -1;
|
|
}
|
|
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 = async count => {
|
|
if (ser_hand >= 0) return await module.exports._serial_read(pi, ser_hand, count).toString('utf8');
|
|
}
|
|
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 = async () => {
|
|
if (i2c_hand >= 0) await module.exports._i2c_close(pi, i2c_hand);
|
|
i2c_hand = -1;
|
|
}
|
|
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 = 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 = 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 = async reg => {
|
|
if (i2c_hand >= 0) return await module.exports._i2c_read_word_data(pi, i2c_hand, reg);
|
|
}
|
|
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 = async data => {
|
|
if (i2c_hand >= 0) return await 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();
|
|
} |