MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / Button

Class Button

modules/drivers/button/button.js:4–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 5

main.jsFile · 0.85
CodeView.jsFile · 0.85
PreferencesView.jsFile · 0.85
main.jsFile · 0.85
combo.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected