ocoge/local_modules/rgpio/index.js

156 lines
5.0 KiB
JavaScript
Raw Normal View History

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