ocoge/local_modules/rgpio/index.js

70 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

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;
module.exports.PULL_UP = 32;
module.exports.PULL_DOWN = 64;
module.exports.PULL_NONE = 128;
const CHIP_COUNT = 5;
const port = '';
let sbc = -1;
let gpiochip_hand = [];
let ser_hand = -1;
let i2c_hand = -1;
module.exports.gpio_open = async () => {
sbc = await module.exports._rgpiod_start('', port);
if (sbc < 0) return sbc;
for (let i = 0; i < CHIP_COUNT; i++) {
gpiochip_hand.push(await module.exports._gpiochip_open(sbc, i));
}
return gpiochip_hand;
}
module.exports.gpio_close = async () => {
for (let i = 0; i < CHIP_COUNT; i++) {
await module.exports._gpiochip_close(sbc, gpiochip_hand[i]);
}
gpiochip_hand = [];
await module.exports._rgpiod_stop(sbc);
sbc = -1;
}
module.exports.gpio_set_output = async gpio => {
let chip = Math.floor(gpio / 32);
let pin = Math.floor(gpio % 32);
if (gpiochip_hand[chip] >= 0) return await module.exports._gpio_claim_output(sbc, gpiochip_hand[chip], pin);
}
module.exports.gpio_write = async (gpio, value) => {
let chip = Math.floor(gpio / 32);
let pin = Math.floor(gpio % 32);
if (gpiochip_hand[chip] >= 0) return await module.exports._gpio_write(sbc, gpiochip_hand[chip], pin, value);
}
module.exports.i2c_open = async (i2c_bus, i2c_address) => {
if (i2c_hand >= 0) await module.exports._i2c_close(i2c_hand); // 勝手に閉じる
i2c_hand = await module.exports._i2c_open(sbc, i2c_bus, i2c_address, 0);
return i2c_hand;
}
module.exports.i2c_close = async () => {
if (i2c_hand >= 0) await module.exports._i2c_close(sbc, 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(sbc, i2c_hand, reg, byte_val);
}
module.exports.i2c_write_i2c_block_data = async (reg, data) => {
if (i2c_hand >= 0) return await module.exports._i2c_write_i2c_block_data(sbc, 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(sbc, i2c_hand, reg);
}
// 終了処理
module.exports.close_all_handle = async () => {
await module.exports.gpio_close();
// await module.exports.serial_close();
await module.exports.i2c_close();
}