From 2e586e9be56d02a09b9dade27baa93723a2b3413 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Tue, 15 Oct 2019 20:56:02 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=BB=E3=82=B9=E3=82=92?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 12 ++++++------ index.ts | 20 ++++++-------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index f1025dc..964254a 100644 --- a/index.js +++ b/index.js @@ -44,7 +44,7 @@ exports.GPIOPortMap = GPIOPortMap; class GPIOPort extends events_1.EventEmitter { constructor(portNumber) { super(); - this._portNumber = portNumber; + this._portNumber = parseUint16(portNumber.toString()); this._pollingInterval = PollingInterval; this._direction = new OperationError("Unknown direction."); this._exported = new OperationError("Unknown export."); @@ -80,8 +80,8 @@ class GPIOPort extends events_1.EventEmitter { } try { clearInterval(this._timeout); - await fs_1.promises.writeFile(`/sys/class/gpio/export`, parseUint16(this.portNumber.toString()).toString()); - await fs_1.promises.writeFile(`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/direction`, direction); + await fs_1.promises.writeFile(`/sys/class/gpio/export`, String(this.portNumber)); + await fs_1.promises.writeFile(`/sys/class/gpio/gpio${this.portNumber}/direction`, direction); if (direction === "in") { this._timeout = setInterval(this.read.bind(this), this._pollingInterval); } @@ -95,7 +95,7 @@ class GPIOPort extends events_1.EventEmitter { async unexport() { clearInterval(this._timeout); try { - await fs_1.promises.writeFile(`/sys/class/gpio/unexport`, parseUint16(this.portNumber.toString()).toString()); + await fs_1.promises.writeFile(`/sys/class/gpio/unexport`, String(this.portNumber)); } catch (error) { throw new OperationError(error); @@ -104,7 +104,7 @@ class GPIOPort extends events_1.EventEmitter { } async read() { try { - const buffer = await fs_1.promises.readFile(`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value`); + const buffer = await fs_1.promises.readFile(`/sys/class/gpio/gpio${this.portNumber}/value`); const value = parseUint16(buffer.toString()); if (this._value !== value) { this._value = value; @@ -118,7 +118,7 @@ class GPIOPort extends events_1.EventEmitter { } async write(value) { try { - await fs_1.promises.writeFile(`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value`, parseUint16(value.toString()).toString()); + await fs_1.promises.writeFile(`/sys/class/gpio/gpio${this.portNumber}/value`, parseUint16(value.toString()).toString()); } catch (error) { throw new OperationError(error); diff --git a/index.ts b/index.ts index 8d0e10d..3ddf1dc 100644 --- a/index.ts +++ b/index.ts @@ -88,7 +88,7 @@ export class GPIOPort extends EventEmitter { constructor(portNumber: PortNumber) { super(); - this._portNumber = portNumber; + this._portNumber = parseUint16(portNumber.toString()); this._pollingInterval = PollingInterval; this._direction = new OperationError("Unknown direction."); this._exported = new OperationError("Unknown export."); @@ -129,14 +129,9 @@ export class GPIOPort extends EventEmitter { try { clearInterval(this._timeout as any); + await fs.writeFile(`/sys/class/gpio/export`, String(this.portNumber)); await fs.writeFile( - `/sys/class/gpio/export`, - parseUint16(this.portNumber.toString()).toString() - ); - await fs.writeFile( - `/sys/class/gpio/gpio${parseUint16( - this.portNumber.toString() - )}/direction`, + `/sys/class/gpio/gpio${this.portNumber}/direction`, direction ); if (direction === "in") { @@ -157,10 +152,7 @@ export class GPIOPort extends EventEmitter { clearInterval(this._timeout as any); try { - await fs.writeFile( - `/sys/class/gpio/unexport`, - parseUint16(this.portNumber.toString()).toString() - ); + await fs.writeFile(`/sys/class/gpio/unexport`, String(this.portNumber)); } catch (error) { throw new OperationError(error); } @@ -171,7 +163,7 @@ export class GPIOPort extends EventEmitter { async read() { try { const buffer = await fs.readFile( - `/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value` + `/sys/class/gpio/gpio${this.portNumber}/value` ); const value = parseUint16(buffer.toString()) as GPIOValue; @@ -190,7 +182,7 @@ export class GPIOPort extends EventEmitter { async write(value: GPIOValue) { try { await fs.writeFile( - `/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value`, + `/sys/class/gpio/gpio${this.portNumber}/value`, parseUint16(value.toString()).toString() ); } catch (error) {