| 2 | import Monitor from "pins/digital/monitor"; |
| 3 | |
| 4 | class 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 | |
| 43 | export default Button; |
no outgoing calls
no test coverage detected