mirror of
https://github.com/chirimen-oh/node-web-gpio.git
synced 2025-01-18 16:08:00 +00:00
parent
137f9363d5
commit
4e25298fb8
1 changed files with 16 additions and 1 deletions
15
index.ts
15
index.ts
|
@ -81,6 +81,7 @@ export class GPIOPort extends EventEmitter {
|
||||||
private readonly _pollingInterval: number;
|
private readonly _pollingInterval: number;
|
||||||
private _direction: DirectionMode | OperationError;
|
private _direction: DirectionMode | OperationError;
|
||||||
private _exported: boolean | OperationError;
|
private _exported: boolean | OperationError;
|
||||||
|
private _exportRetry: number;
|
||||||
private _value: GPIOValue | undefined;
|
private _value: GPIOValue | undefined;
|
||||||
private _timeout: ReturnType<typeof setInterval> | undefined;
|
private _timeout: ReturnType<typeof setInterval> | undefined;
|
||||||
onchange: GPIOChangeEventHandler | undefined;
|
onchange: GPIOChangeEventHandler | undefined;
|
||||||
|
@ -92,6 +93,7 @@ export class GPIOPort extends EventEmitter {
|
||||||
this._pollingInterval = PollingInterval;
|
this._pollingInterval = PollingInterval;
|
||||||
this._direction = new OperationError("Unknown direction.");
|
this._direction = new OperationError("Unknown direction.");
|
||||||
this._exported = new OperationError("Unknown export.");
|
this._exported = new OperationError("Unknown export.");
|
||||||
|
this._exportRetry = 0;
|
||||||
|
|
||||||
this.on("change", (event: GPIOChangeEvent): void => {
|
this.on("change", (event: GPIOChangeEvent): void => {
|
||||||
if (this.onchange !== undefined) this.onchange(event);
|
if (this.onchange !== undefined) this.onchange(event);
|
||||||
|
@ -153,8 +155,15 @@ export class GPIOPort extends EventEmitter {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
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);
|
throw new OperationError(error);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this._direction = direction;
|
this._direction = direction;
|
||||||
this._exported = true;
|
this._exported = true;
|
||||||
|
@ -244,3 +253,9 @@ export async function requestGPIOAccess(): Promise<GPIOAccess> {
|
||||||
|
|
||||||
return new GPIOAccess(ports);
|
return new GPIOAccess(ports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sleep(ms: number) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
return setTimeout(resolve, ms);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue