From cd50093ae2cf0e031ddcb907d12da5122d1b7fa2 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Fri, 4 Oct 2024 23:28:26 +0900 Subject: [PATCH] use correct offset for GPIO0 https://github.com/raspberrypi/linux/issues/6037 --- index.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 4aa7dae..697b000 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,7 @@ -import { EventEmitter } from 'events'; -import { promises as fs } from 'fs'; -import * as path from 'path'; +import { EventEmitter } from 'node:events'; +import { promises as fs } from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; /** * Interval of file system polling, in milliseconds. @@ -35,6 +36,15 @@ function parseUint16(parseString: string) { else throw new RangeError(`Must be between 0 and ${Uint16Max}.`); } +/** + * GPIO0 オフセット + * @see {@link https://github.com/raspberrypi/linux/issues/6037} + */ +const GpioOffset = + process.platform === 'linux' && 6.6 <= Number(os.release().match(/\d+\.\d+/)) + ? 512 + : 0; + /** ポート番号 */ type PortNumber = number; /** ポート名 */ @@ -151,7 +161,7 @@ export class GPIOPort extends EventEmitter { constructor(portNumber: PortNumber) { super(); - this._portNumber = parseUint16(portNumber.toString()); + this._portNumber = parseUint16(portNumber.toString()) + GpioOffset; this._pollingInterval = PollingInterval; this._direction = new OperationError('Unknown direction.'); this._exported = new OperationError('Unknown export.');