2023-04-11 13:25:50 +00:00
|
|
|
module.exports = require('bindings')('rgpio');
|
|
|
|
|
2023-06-25 12:18:33 +00:00
|
|
|
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;
|
2023-04-11 13:25:50 +00:00
|
|
|
|
2023-06-25 12:18:33 +00:00
|
|
|
// Properties
|
2023-04-11 13:25:50 +00:00
|
|
|
let sbc = -1;
|
2023-06-25 12:18:33 +00:00
|
|
|
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);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
return sbc;
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.sbc_stop = async () => {
|
|
|
|
await module.exports._rgpiod_stop(sbc);
|
|
|
|
chip_hand = [];
|
|
|
|
sbc = -1;
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
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));
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.serial_write = async (handle, data) => {
|
|
|
|
return await module.exports._serial_write(sbc, handle, Buffer.from(data), -1);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.serial_data_available = async handle => {
|
|
|
|
await module.exports._serial_data_available(sbc, handle);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
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);
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_close = async handle => {
|
|
|
|
await module.exports._i2c_close(sbc, handle);
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-05-09 00:05:48 +00:00
|
|
|
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_write_byte = async (handle, byte_val) => {
|
|
|
|
return await module.exports._i2c_write_byte(sbc, handle, byte_val);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_read_byte = async handle => {
|
|
|
|
return await module.exports._i2c_read_byte(sbc, handle);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_write_byte_data = async (handle, reg, byte_val) => {
|
|
|
|
return await module.exports._i2c_write_byte_data(sbc, handle, reg, byte_val);
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_read_byte_data = async (handle, reg) => {
|
|
|
|
return await module.exports._i2c_read_byte_data(sbc, handle, reg);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
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);
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_read_word_data = async (handle, reg) => {
|
|
|
|
return await module.exports._i2c_read_word_data(sbc, handle, reg);
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_read_device = async (handle, count) => {
|
|
|
|
return new TextDecoder().decode(await module.exports._i2c_read_device(sbc, handle, count));
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-06-25 12:18:33 +00:00
|
|
|
module.exports.i2c_write_device = async (handle, data) => {
|
|
|
|
return await module.exports._i2c_write_device(sbc, handle, Buffer.from(data), -1);
|
2023-05-09 00:05:48 +00:00
|
|
|
}
|
2023-04-11 13:25:50 +00:00
|
|
|
|
|
|
|
// 終了処理
|
|
|
|
module.exports.close_all_handle = async () => {
|
2023-06-25 12:18:33 +00:00
|
|
|
await module.exports.sbc_stop();
|
|
|
|
// await module.exports.serial_close();
|
|
|
|
// await module.exports.i2c_close();
|
2023-04-11 13:25:50 +00:00
|
|
|
}
|