プロセスを整理

This commit is contained in:
Nebel 2019-10-15 20:56:02 +09:00
parent d79f14234a
commit 2e586e9be5
2 changed files with 12 additions and 20 deletions

View file

@ -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);

View file

@ -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) {