ocoge/local_modules/@ocogeclub/lgpio/index.js

84 lines
3.1 KiB
JavaScript

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;
module.exports.PULL_UP = 32;
module.exports.PULL_DOWN = 64;
module.exports.PULL_NONE = 128;
let gpio_hand = -1;
let ser_hand = -1;
let i2c_hand = -1;
module.exports.gpio_open = () => {
if (gpio_hand < 0) gpio_hand = module.exports._gpiochip_open(0);
return gpio_hand;
}
module.exports.gpio_close = () => {
if (gpio_hand >= 0) module.exports._gpiochip_close(gpio_hand);
gpio_hand = -1;
}
module.exports.gpio_set_output = gpio => {
if (gpio_hand >= 0) return module.exports._gpio_claim_output(gpio_hand, gpio);
}
module.exports.gpio_set_input = (gpio, lflag) => {
if (gpio_hand >= 0) return module.exports._gpio_claim_input(gpio_hand, gpio, lflag);
}
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.servo = (gpio, pulse_width) => {
if (gpio_hand >= 0) return module.exports._tx_servo(gpio_hand, gpio, pulse_width);
}
module.exports.pwm = (gpio, pwm_frequency, pwm_duty_cycle) => {
if (gpio_hand >= 0) return module.exports._tx_pwm(gpio_hand, gpio, pwm_frequency, pwm_duty_cycle);
}
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.gpio_close();
module.exports.serial_close();
module.exports.i2c_close();
}