ocoge/local_modules/rgpio/index.js

150 lines
5.6 KiB
JavaScript

module.exports = require('bindings')('rgpio');
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;
// Properties
let sbc = -1;
let chip_hand = [];
module.exports.rgpio_sbc = async (host = 'localhost', port = 8889, show_errors = true) => {
if (sbc < 0) {
sbc = await module.exports._rgpiod_start(host, port.toString());
if (sbc < 0) {
if (show_errors) console.log(sbc);
}
}
return sbc;
}
module.exports.sbc_stop = async () => {
await module.exports._rgpiod_stop(sbc);
chip_hand = [];
sbc = -1;
}
module.exports.gpiochip_open = async gpiochip => {
if (!chip_hand[gpiochip] || chip_hand[gpiochip] < 0) {
chip_hand[gpiochip] = await module.exports._gpiochip_open(sbc, gpiochip);
}
return chip_hand[gpiochip];
// return await module.exports._gpiochip_open(sbc, gpiochip);
}
module.exports.gpio_set_input = async (ugpio, lFlags = 0) => {
let chip = Math.trunc(ugpio / 32);
let gpio = ugpio % 32;
let handle = await module.exports.gpiochip_open(chip);
return await module.exports._gpio_claim_input(sbc, handle, lFlags, gpio);
}
module.exports.gpio_set_output = async (ugpio, level = 0, lFlags = 0) => {
let chip = Math.trunc(ugpio / 32);
let gpio = ugpio % 32;
let handle = await module.exports.gpiochip_open(chip);
return await module.exports._gpio_claim_output(sbc, handle, lFlags, gpio, level);
}
module.exports.gpio_read = async (ugpio) => {
let chip = Math.trunc(ugpio / 32);
let gpio = ugpio % 32;
let handle = await module.exports.gpiochip_open(chip);
return await module.exports._gpio_read(sbc, handle, gpio);
}
module.exports.gpio_write = async (ugpio, level) => {
let chip = Math.trunc(ugpio / 32);
let gpio = ugpio % 32;
let handle = await module.exports.gpiochip_open(chip);
return await module.exports._gpio_write(sbc, handle, gpio, level);
}
module.exports.serial_open = async (tty, baud, ser_flags = 0) => {
return await module.exports._serial_open(sbc, tty, baud, ser_flags);
}
module.exports.serial_close = async handle => {
await module.exports._serial_close(sbc, handle);
}
module.exports.serial_read = async (handle, count = 0) => {
if (count === 0) {
count = await module.exports._serial_data_available(sbc, handle);
if (count === 0) return '';
}
return new TextDecoder().decode(await module.exports._serial_read(sbc, handle, count));
}
module.exports.serial_write = async (handle, data) => {
return await module.exports._serial_write(sbc, handle, Buffer.from(data), -1);
}
module.exports.serial_data_available = async handle => {
await module.exports._serial_data_available(sbc, handle);
}
module.exports.i2c_open = async (i2c_bus, i2c_address, i2c_flags = 0) => {
return await module.exports._i2c_open(sbc, i2c_bus, i2c_address, i2c_flags);
}
module.exports.i2c_close = async handle => {
await module.exports._i2c_close(sbc, handle);
}
module.exports.i2c_write_byte = async (handle, byte_val) => {
return await module.exports._i2c_write_byte(sbc, handle, byte_val);
}
module.exports.i2c_read_byte = async handle => {
return await module.exports._i2c_read_byte(sbc, handle);
}
module.exports.i2c_write_byte_data = async (handle, reg, byte_val) => {
return await module.exports._i2c_write_byte_data(sbc, handle, reg, byte_val);
}
module.exports.i2c_read_byte_data = async (handle, reg) => {
return await module.exports._i2c_read_byte_data(sbc, handle, reg);
}
module.exports.i2c_read_i2c_block_data = async (handle, reg, count = -1) => {
return await module.exports._i2c_read_i2c_block_data(sbc, handle, reg, count);
}
module.exports.i2c_write_i2c_block_data = async (handle, reg, data) => {
return await module.exports._i2c_write_i2c_block_data(sbc, handle, reg, Buffer.from(data), -1);
}
module.exports.i2c_read_word_data = async (handle, reg) => {
return await module.exports._i2c_read_word_data(sbc, handle, reg);
}
module.exports.i2c_write_word_data = async (handle, reg, word_val) => {
return await module.exports._i2c_write_word_data(sbc, handle, reg, word_val);
}
module.exports.i2c_read_device = async (handle, count) => {
return new TextDecoder().decode(await module.exports._i2c_read_device(sbc, handle, count));
}
module.exports.i2c_write_device = async (handle, data) => {
return await module.exports._i2c_write_device(sbc, handle, Buffer.from(data), -1);
}
/***** 同期関数 *****/
module.exports.rgpio_sbc_sync = (host = 'localhost', port = 8889, show_errors = true) => {
if (sbc < 0) {
sbc = module.exports._rgpiod_start_sync(host, port.toString());
if (sbc < 0) {
if (show_errors) console.log(sbc);
}
}
return sbc;
}
module.exports.sbc_stop_sync = () => {
module.exports._rgpiod_stop_sync(sbc);
chip_hand = [];
sbc = -1;
}
module.exports.i2c_open_sync = (i2c_bus, i2c_address, i2c_flags = 0) => {
return module.exports._i2c_open_sync(sbc, i2c_bus, i2c_address, i2c_flags);
}
module.exports.i2c_close_sync = handle => {
module.exports._i2c_close_sync(sbc, handle);
}
module.exports.i2c_read_byte_sync = handle => {
return module.exports._i2c_read_byte_sync(sbc, handle);
}
module.exports.i2c_write_device_sync = (handle, data, count = -1) => {
let buffer = Buffer.from(data);
if (count < 0) count = buffer.length;
return module.exports._i2c_write_device_sync(sbc, handle, buffer, count);
}
// 終了処理
module.exports.close_all_handle = async () => {
await module.exports.sbc_stop();
}