diff --git a/index.ts b/index.ts index f3b2755..5ab3818 100644 --- a/index.ts +++ b/index.ts @@ -81,6 +81,7 @@ export class GPIOPort extends EventEmitter { private readonly _pollingInterval: number; private _direction: DirectionMode | OperationError; private _exported: boolean | OperationError; + private _exportRetry: number; private _value: GPIOValue | undefined; private _timeout: ReturnType | undefined; onchange: GPIOChangeEventHandler | undefined; @@ -92,6 +93,7 @@ export class GPIOPort extends EventEmitter { this._pollingInterval = PollingInterval; this._direction = new OperationError("Unknown direction."); this._exported = new OperationError("Unknown export."); + this._exportRetry = 0; this.on("change", (event: GPIOChangeEvent): void => { if (this.onchange !== undefined) this.onchange(event); @@ -153,7 +155,14 @@ export class GPIOPort extends EventEmitter { ); } } catch (error) { - throw new OperationError(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); + } } this._direction = direction; @@ -244,3 +253,9 @@ export async function requestGPIOAccess(): Promise { return new GPIOAccess(ports); } + +function sleep(ms: number) { + return new Promise((resolve) => { + return setTimeout(resolve, ms); + }); +} \ No newline at end of file