Keep leds sysfs files open

Change-type: patch
This commit is contained in:
Alexis Svinartchouk 2020-01-31 16:23:50 +01:00 committed by Alexis Svinartchouk
parent cb8168de41
commit 227bad9e99

View File

@ -21,17 +21,26 @@ import * as settings from './settings';
import { observe } from './store'; import { observe } from './store';
class Led { class Led {
private handle: fs.FileHandle;
private lastValue?: number; private lastValue?: number;
constructor(private path: string) {} constructor(private path: string) {}
private async open() {
if (this.handle === undefined) {
this.handle = await fs.open(this.path, 'w');
}
}
public async setIntensity(intensity: number) { public async setIntensity(intensity: number) {
if (intensity < 0 || intensity > 1) { if (intensity < 0 || intensity > 1) {
throw new Error('Led intensity must be between 0 and 1'); throw new Error('Led intensity must be between 0 and 1');
} }
const value = Math.round(intensity * 255); const value = Math.round(intensity * 255);
if (value !== this.lastValue) { 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; this.lastValue = value;
} }
} }