Function to get portName

This commit is contained in:
Nebel 2019-10-18 01:49:23 +09:00
parent bab227cb47
commit a227fb07eb
2 changed files with 10 additions and 12 deletions

View file

@ -63,8 +63,7 @@ class GPIOPort extends events_1.EventEmitter {
return this._portNumber; return this._portNumber;
} }
get portName() { get portName() {
// NOTE: Unknown portName. return `gpio${this.portNumber}`;
return "";
} }
get pinName() { get pinName() {
// NOTE: Unknown pinName. // NOTE: Unknown pinName.
@ -85,7 +84,7 @@ class GPIOPort extends events_1.EventEmitter {
throw new InvalidAccessError(`Must be "in" or "out".`); throw new InvalidAccessError(`Must be "in" or "out".`);
} }
try { try {
await fs_1.promises.access(path.join(SysfsGPIOPath, `gpio${this.portNumber}`)); await fs_1.promises.access(path.join(SysfsGPIOPath, this.portName));
this._exported = true; this._exported = true;
} }
catch { catch {
@ -96,7 +95,7 @@ class GPIOPort extends events_1.EventEmitter {
if (!this.exported) { if (!this.exported) {
await fs_1.promises.writeFile(path.join(SysfsGPIOPath, "export"), String(this.portNumber)); await fs_1.promises.writeFile(path.join(SysfsGPIOPath, "export"), String(this.portNumber));
} }
await fs_1.promises.writeFile(path.join(SysfsGPIOPath, `gpio${this.portNumber}`, "direction"), direction); await fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName, "direction"), direction);
if (direction === "in") { if (direction === "in") {
this._timeout = setInterval(this.read.bind(this), this._pollingInterval); this._timeout = setInterval(this.read.bind(this), this._pollingInterval);
} }
@ -122,7 +121,7 @@ class GPIOPort extends events_1.EventEmitter {
throw new InvalidAccessError(`The exported must be true and value of direction must be "in".`); throw new InvalidAccessError(`The exported must be true and value of direction must be "in".`);
} }
try { try {
const buffer = await fs_1.promises.readFile(path.join(SysfsGPIOPath, `gpio${this.portNumber}`, "value")); const buffer = await fs_1.promises.readFile(path.join(SysfsGPIOPath, this.portName, "value"));
const value = parseUint16(buffer.toString()); const value = parseUint16(buffer.toString());
if (this._value !== value) { if (this._value !== value) {
this._value = value; this._value = value;
@ -139,7 +138,7 @@ class GPIOPort extends events_1.EventEmitter {
throw new InvalidAccessError(`The exported must be true and value of direction must be "out".`); throw new InvalidAccessError(`The exported must be true and value of direction must be "out".`);
} }
try { try {
await fs_1.promises.writeFile(path.join(SysfsGPIOPath, `gpio${this.portNumber}`, "value"), parseUint16(value.toString()).toString()); await fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName, "value"), parseUint16(value.toString()).toString());
} }
catch (error) { catch (error) {
throw new OperationError(error); throw new OperationError(error);

View file

@ -111,8 +111,7 @@ export class GPIOPort extends EventEmitter {
} }
get portName(): PortName { get portName(): PortName {
// NOTE: Unknown portName. return `gpio${this.portNumber}`;
return "";
} }
get pinName(): PinName { get pinName(): PinName {
@ -136,7 +135,7 @@ export class GPIOPort extends EventEmitter {
} }
try { try {
await fs.access(path.join(SysfsGPIOPath, `gpio${this.portNumber}`)); await fs.access(path.join(SysfsGPIOPath, this.portName));
this._exported = true; this._exported = true;
} catch { } catch {
this._exported = false; this._exported = false;
@ -151,7 +150,7 @@ export class GPIOPort extends EventEmitter {
); );
} }
await fs.writeFile( await fs.writeFile(
path.join(SysfsGPIOPath, `gpio${this.portNumber}`, "direction"), path.join(SysfsGPIOPath, this.portName, "direction"),
direction direction
); );
if (direction === "in") { if (direction === "in") {
@ -192,7 +191,7 @@ export class GPIOPort extends EventEmitter {
try { try {
const buffer = await fs.readFile( const buffer = await fs.readFile(
path.join(SysfsGPIOPath, `gpio${this.portNumber}`, "value") path.join(SysfsGPIOPath, this.portName, "value")
); );
const value = parseUint16(buffer.toString()) as GPIOValue; const value = parseUint16(buffer.toString()) as GPIOValue;
@ -217,7 +216,7 @@ export class GPIOPort extends EventEmitter {
try { try {
await fs.writeFile( await fs.writeFile(
path.join(SysfsGPIOPath, `gpio${this.portNumber}`, "value"), path.join(SysfsGPIOPath, this.portName, "value"),
parseUint16(value.toString()).toString() parseUint16(value.toString()).toString()
); );
} catch (error) { } catch (error) {