-
Notifications
You must be signed in to change notification settings - Fork 234
/
button.js
43 lines (39 loc) · 893 Bytes
/
button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Digital from "pins/digital";
import Monitor from "pins/digital/monitor";
class Button {
#input;
#pressed;
constructor(options = {}) {
if (options.target)
this.target = options.target;
this.#pressed = options.invert ? 0 : 1;
if (options.onPush) {
this.#input = new Monitor({
mode: options.mode ?? Digital.InputPullUp,
pin: options.pin,
edge: Monitor.Rising | Monitor.Falling,
});
this.#input.onPush = options.onPush;
this.#input.onChanged = () => {
this.#input.onPush.call(this, this.read());
};
}
else {
this.#input = new Digital({
pin: options.pin,
mode: options.mode ?? Digital.InputPullUp,
});
}
}
close() {
this.#input?.close();
this.#input = undefined;
}
read() {
return (this.#input.read() === this.#pressed) ? 1 : 0;
}
get pressed() {
return this.read() ? true : false;
}
}
export default Button;