mirror of
https://github.com/ocogeclub/ocoge.git
synced 2024-11-21 23:29:48 +00:00
107 lines
4.1 KiB
JavaScript
107 lines
4.1 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;
|
|
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._gpiod_start('', '');
|
|
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 () => {
|
|
if (sbc >= 0) {
|
|
for (let i = 0; i < CHIP_COUNT; i++) {
|
|
if (gpiochip_hand[i] >= 0)
|
|
await module.exports._gpiochip_close(sbc, gpiochip_hand[i]);
|
|
}
|
|
gpiochip_hand = [];
|
|
await module.exports._gpiod_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.serial_open = async (tty, baud) => {
|
|
if (ser_hand >= 0) await module.exports._serial_close(sbc, ser_hand); // 勝手に閉じる
|
|
ser_hand = await module.exports._serial_open(sbc, tty, baud);
|
|
return ser_hand;
|
|
}
|
|
module.exports.serial_close = async () => {
|
|
if (ser_hand >= 0) await module.exports._serial_close(sbc, ser_hand);
|
|
ser_hand = -1;
|
|
}
|
|
module.exports.serial_write = async data => {
|
|
if (ser_hand >= 0) return await module.exports._serial_write(sbc, ser_hand, Buffer.from(data));
|
|
}
|
|
module.exports.serial_read = async count => {
|
|
if (ser_hand >= 0) return new TextDecoder().decode(await module.exports._serial_read(sbc, ser_hand, count));
|
|
// if (ser_hand >= 0) return await module.exports._serial_read(pi, ser_hand, count);//.toString('utf8');
|
|
}
|
|
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);
|
|
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 = async (byte_val) => {
|
|
if (i2c_hand >= 0) return await module.exports._i2c_write_byte(sbc, i2c_hand, byte_val);
|
|
}
|
|
module.exports.i2c_read_byte = async () => {
|
|
if (i2c_hand >= 0) return await module.exports._i2c_read_byte(sbc, i2c_hand);
|
|
}
|
|
|
|
|
|
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_read_byte_data = async reg => {
|
|
if (i2c_hand >= 0) return await module.exports._i2c_read_byte_data(sbc, i2c_hand, reg);
|
|
}
|
|
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.i2c_read_device = async count => {
|
|
if (i2c_hand >= 0) return new TextDecoder().decode(await module.exports._i2c_read_device(sbc, i2c_hand, count));
|
|
}
|
|
module.exports.i2c_write_device = async data => {
|
|
if (i2c_hand >= 0) return await module.exports._i2c_write_device(sbc, i2c_hand, Buffer.from(data));
|
|
}
|
|
|
|
// 終了処理
|
|
module.exports.close_all_handle = async () => {
|
|
await module.exports.gpio_close();
|
|
await module.exports.serial_close();
|
|
await module.exports.i2c_close();
|
|
} |