node-web-gpio/index.ts

378 lines
8.6 KiB
TypeScript
Raw Normal View History

import { EventEmitter } from 'events';
import { promises as fs } from 'fs';
import * as path from 'path';
2019-10-13 02:13:19 +09:00
/**
* Interval of file system polling, in milliseconds.
*/
const PollingInterval = 100;
/**
* GPIO
*/
const SysfsGPIOPath = '/sys/class/gpio';
2019-10-15 23:26:29 +09:00
/**
* GPIO
*/
2019-10-18 01:29:58 +09:00
const GPIOPortMapSizeMax = 1024;
/**
* Uint16 Max
*/
2019-10-13 02:13:19 +09:00
const Uint16Max = 65535;
/**
*
* Uint16型変換処理
* @param parseString
* @return Uint16型変換値
*/
2022-01-11 18:07:07 +09:00
function parseUint16(parseString: string) {
const n = Number.parseInt(parseString, 10);
2019-10-13 02:13:19 +09:00
if (0 <= n && n <= Uint16Max) return n;
else throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
}
/** ポート番号 */
2019-10-13 02:13:19 +09:00
type PortNumber = number;
/** ポート名 */
2019-10-13 02:13:19 +09:00
type PortName = string;
/** ピン名 */
2019-10-13 02:13:19 +09:00
type PinName = string;
/** 入出力方向 */
type DirectionMode = 'in' | 'out';
2019-10-13 02:13:19 +09:00
/** GPIO 値 0: LOW / 1: HIGH */
2019-10-13 02:13:19 +09:00
type GPIOValue = 0 | 1;
/**
* GPIO
*/
2019-10-13 02:13:19 +09:00
interface GPIOChangeEvent {
/** 入出力値 */
2019-10-13 02:13:19 +09:00
readonly value: GPIOValue;
/** ポート */
2019-10-13 02:13:19 +09:00
readonly port: GPIOPort;
}
/**
* GPIO
*/
2019-10-13 02:13:19 +09:00
interface GPIOChangeEventHandler {
/** イベント */
2019-10-13 02:13:19 +09:00
(event: GPIOChangeEvent): void;
}
/**
* GPIO
*/
2019-10-13 02:13:19 +09:00
export class GPIOAccess extends EventEmitter {
/** ポート */
2019-10-13 02:13:19 +09:00
private readonly _ports: GPIOPortMap;
/** GPIO チェンジイベントハンドラ */
2019-10-13 02:13:19 +09:00
onchange: GPIOChangeEventHandler | undefined;
/**
* Creates an instance of GPIOAccess.
* @param ports
*/
2019-10-13 02:13:19 +09:00
constructor(ports?: GPIOPortMap) {
super();
this._ports = ports == null ? new GPIOPortMap() : ports;
this._ports.forEach((port) =>
port.on('change', (event) => {
this.emit('change', event);
2019-10-13 02:13:19 +09:00
})
);
this.on('change', (event: GPIOChangeEvent): void => {
2019-10-13 02:13:19 +09:00
if (this.onchange !== undefined) this.onchange(event);
});
}
/**
*
* @return
*/
2019-10-13 02:13:19 +09:00
get ports(): GPIOPortMap {
return this._ports;
}
/**
* Unexport all exported GPIO ports.
*
* @return
2019-10-13 02:13:19 +09:00
*/
async unexportAll(): Promise<void> {
2019-10-13 02:13:19 +09:00
await Promise.all(
[...this.ports.values()].map((port) =>
2019-10-13 02:13:19 +09:00
port.exported ? port.unexport() : undefined
)
);
}
}
2019-10-16 00:27:50 +09:00
/**
* Different from Web GPIO API specification.
*/
2019-10-13 02:13:19 +09:00
export class GPIOPortMap extends Map<PortNumber, GPIOPort> {}
/**
* GPIO
*/
2019-10-13 02:13:19 +09:00
export class GPIOPort extends EventEmitter {
/** ポート番号 */
2019-10-13 02:13:19 +09:00
private readonly _portNumber: PortNumber;
/** ポーリング間隔 */
2019-10-13 02:13:19 +09:00
private readonly _pollingInterval: number;
/** 入出力方向 */
2019-10-13 02:13:19 +09:00
private _direction: DirectionMode | OperationError;
/** エクスポート */
2019-10-13 02:13:19 +09:00
private _exported: boolean | OperationError;
/** エクスポートリトライ回数 */
private _exportRetry: number;
/** 入出力値 */
2019-10-13 02:13:19 +09:00
private _value: GPIOValue | undefined;
/** タイムアウト値 */
2019-10-13 02:13:19 +09:00
private _timeout: ReturnType<typeof setInterval> | undefined;
/** GPIO チェンジイベントハンドラ */
onchange: GPIOChangeEventHandler | undefined;
2019-10-13 02:13:19 +09:00
/**
* Creates an instance of GPIOPort.
* @param portNumber
*/
2019-10-13 02:13:19 +09:00
constructor(portNumber: PortNumber) {
super();
2019-10-15 20:56:02 +09:00
this._portNumber = parseUint16(portNumber.toString());
2019-10-13 02:13:19 +09:00
this._pollingInterval = PollingInterval;
this._direction = new OperationError('Unknown direction.');
this._exported = new OperationError('Unknown export.');
this._exportRetry = 0;
2019-10-13 02:13:19 +09:00
this.on('change', (event: GPIOChangeEvent): void => {
if (this.onchange !== undefined) this.onchange(event);
2019-10-13 02:13:19 +09:00
});
}
/**
*
* @return
*/
2019-10-13 02:13:19 +09:00
get portNumber(): PortNumber {
return this._portNumber;
}
/**
*
* @return
*/
2019-10-13 02:13:19 +09:00
get portName(): PortName {
2019-10-18 01:49:23 +09:00
return `gpio${this.portNumber}`;
2019-10-13 02:13:19 +09:00
}
/**
*
* @return
*/
2019-10-13 02:13:19 +09:00
get pinName(): PinName {
// NOTE: Unknown pinName.
return '';
2019-10-13 02:13:19 +09:00
}
/**
* GPIO getter
* @return GPIO
*/
2019-10-13 02:13:19 +09:00
get direction(): DirectionMode {
if (this._direction instanceof OperationError) throw this._direction;
return this._direction;
}
/**
* GPIO export getter
* @return GPIO
*/
2019-10-13 02:13:19 +09:00
get exported(): boolean {
if (this._exported instanceof OperationError) throw this._exported;
return this._exported;
}
/**
* GPIO
* @param direction GPIO
* @return export
*/
async export(direction: DirectionMode): Promise<void> {
2019-10-13 02:13:19 +09:00
if (!/^(in|out)$/.test(direction)) {
throw new InvalidAccessError(`Must be "in" or "out".`);
}
2019-10-15 23:26:29 +09:00
try {
2019-10-18 01:49:23 +09:00
await fs.access(path.join(SysfsGPIOPath, this.portName));
2019-10-15 23:26:29 +09:00
this._exported = true;
} catch {
this._exported = false;
}
2019-10-13 02:13:19 +09:00
try {
clearInterval(this._timeout as ReturnType<typeof setInterval>);
2019-10-15 23:26:29 +09:00
if (!this.exported) {
await fs.writeFile(
path.join(SysfsGPIOPath, 'export'),
2019-10-15 23:26:29 +09:00
String(this.portNumber)
);
}
2019-10-13 02:13:19 +09:00
await fs.writeFile(
path.join(SysfsGPIOPath, this.portName, 'direction'),
2019-10-13 02:13:19 +09:00
direction
);
if (direction === 'in') {
2019-10-13 02:13:19 +09:00
this._timeout = setInterval(
// eslint-disable-next-line
2019-10-13 02:13:19 +09:00
this.read.bind(this),
this._pollingInterval
);
}
} catch (error: any) {
if (this._exportRetry == 0) {
await sleep(100);
console.warn('May be the first time port access. Retry..');
++this._exportRetry;
await this.export(direction);
} else {
throw new OperationError(error);
}
2019-10-13 02:13:19 +09:00
}
this._direction = direction;
this._exported = true;
}
/**
* Unexport exported GPIO ports.
*
* @return
*/
async unexport(): Promise<void> {
clearInterval(this._timeout as ReturnType<typeof setInterval>);
2019-10-13 02:13:19 +09:00
try {
2019-10-15 23:26:29 +09:00
await fs.writeFile(
path.join(SysfsGPIOPath, 'unexport'),
2019-10-15 23:26:29 +09:00
String(this.portNumber)
);
} catch (error: any) {
2019-10-13 02:13:19 +09:00
throw new OperationError(error);
}
this._exported = false;
}
/**
*
* @return
*/
async read(): Promise<GPIOValue> {
if (!(this.exported && this.direction === 'in')) {
2019-10-15 23:26:29 +09:00
throw new InvalidAccessError(
`The exported must be true and value of direction must be "in".`
);
}
2019-10-13 02:13:19 +09:00
try {
const buffer = await fs.readFile(
path.join(SysfsGPIOPath, this.portName, 'value')
2019-10-13 02:13:19 +09:00
);
const value = parseUint16(buffer.toString()) as GPIOValue;
if (this._value !== value) {
this._value = value;
this.emit('change', { value, port: this });
2019-10-13 02:13:19 +09:00
}
return value;
} catch (error: any) {
2019-10-13 02:13:19 +09:00
throw new OperationError(error);
}
}
/**
*
* @return
*/
async write(value: GPIOValue): Promise<void> {
if (!(this.exported && this.direction === 'out')) {
2019-10-15 23:26:29 +09:00
throw new InvalidAccessError(
`The exported must be true and value of direction must be "out".`
);
}
2019-10-13 02:13:19 +09:00
try {
await fs.writeFile(
path.join(SysfsGPIOPath, this.portName, 'value'),
2019-10-13 02:13:19 +09:00
parseUint16(value.toString()).toString()
);
} catch (error: any) {
2019-10-13 02:13:19 +09:00
throw new OperationError(error);
}
}
}
/**
*
*/
2019-10-18 21:24:18 +09:00
export class InvalidAccessError extends Error {
/**
* Creates an instance of InvalidAccessError.
* @param message
*/
2019-10-18 21:24:18 +09:00
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
/**
*
*/
2019-10-18 21:24:18 +09:00
export class OperationError extends Error {
/**
* Creates an instance of OperationError.
* @param message
*/
2019-10-18 21:24:18 +09:00
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
2019-10-13 02:13:19 +09:00
// Web GPIOの仕様に基づく意図的なasync関数の使用なので、ルールを無効化
// eslint-disable-next-line
2019-10-13 02:13:19 +09:00
export async function requestGPIOAccess(): Promise<GPIOAccess> {
const ports = new GPIOPortMap(
[...Array(GPIOPortMapSizeMax).keys()].map((portNumber) => [
2019-10-13 02:13:19 +09:00
portNumber,
new GPIOPort(portNumber),
2019-10-13 02:13:19 +09:00
])
);
return new GPIOAccess(ports);
}
/**
*
* @param ms
* @return
*/
function sleep(ms: number) {
return new Promise((resolve) => {
return setTimeout(resolve, ms);
});
}