From 227bad9e997ac890338bc23fc4a9a7e906c5d6e7 Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Fri, 31 Jan 2020 16:23:50 +0100 Subject: [PATCH] Keep leds sysfs files open Change-type: patch --- lib/gui/app/models/leds.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/gui/app/models/leds.ts b/lib/gui/app/models/leds.ts index 7ad1159d..d018abdb 100644 --- a/lib/gui/app/models/leds.ts +++ b/lib/gui/app/models/leds.ts @@ -21,17 +21,26 @@ import * as settings from './settings'; import { observe } from './store'; class Led { + private handle: fs.FileHandle; private lastValue?: number; constructor(private path: string) {} + private async open() { + if (this.handle === undefined) { + this.handle = await fs.open(this.path, 'w'); + } + } + public async setIntensity(intensity: number) { if (intensity < 0 || intensity > 1) { throw new Error('Led intensity must be between 0 and 1'); } const value = Math.round(intensity * 255); if (value !== this.lastValue) { - await fs.writeFile(this.path, value.toString()); + await this.open(); + await this.handle.write(value.toString(), 0); + // On a regular file we would also need to truncate to the written value length but it looks like it's not the case on sysfs files this.lastValue = value; } }