opengpio
Version:
A performant c++ based general purpose GPIO controller for linux devices. OpenGPIO is written using libgpiod, line & chip based abstractions.
42 lines (36 loc) • 1.21 kB
text/typescript
import { WatchCallback, Gpio, Edge, CleanupCallback, PinGetter } from '../types';
import lib from '../lib';
import { EventEmitter } from 'events';
export class Watch extends EventEmitter {
private getter: PinGetter = () => false;
private cleanup: CleanupCallback = () => { };
private stopped: boolean = false;
constructor(
gpio: Gpio,
private edge: Edge
) {
super();
const [getter, cleanup] = lib.watch(gpio.chip, gpio.line, (value) => {
if (value && (edge === Edge.Rising || edge === Edge.Both)) {
// Has risen to true
this.emit('event', value);
this.emit('change', value);
this.emit('rise', value);
} else if (!value && (edge === Edge.Falling || edge === Edge.Both)) {
// Has fallen to false
this.emit('event', value);
this.emit('change', value);
this.emit('fall', value);
}
});
this.cleanup = cleanup;
}
get value() {
return this.getter();
}
stop() {
this.stopped = true;
this.removeAllListeners();
this.cleanup();
}
}