-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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.target = this; | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"include": [ | ||
"$(MODULES)/pins/digital/monitor/manifest.json", | ||
], | ||
"modules": { | ||
"button": "$(MODULES)/drivers/button/*", | ||
}, | ||
"preload": "button", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Digital from "pins/digital"; | ||
|
||
class LED extends Digital { | ||
#on; | ||
|
||
constructor(options = {}) { | ||
super({ | ||
pin: options.pin, | ||
mode: options.mode ?? Digital.Output, | ||
}); | ||
|
||
this.#on = options.invert ? 0 : 1; | ||
} | ||
read() { | ||
const value = super.read(); | ||
return this.#on ? value : 1 - value; | ||
} | ||
write(value) { | ||
super.write(this.#on ? value : 1 - value); | ||
} | ||
on() { | ||
super.write(this.#on); | ||
} | ||
off() { | ||
super.write(1 - this.#on); | ||
} | ||
} | ||
|
||
export default LED; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"include": [ | ||
"$(MODULES)/pins/digital/manifest.json", | ||
], | ||
"modules": { | ||
"led": "$(MODULES)/drivers/led/*", | ||
}, | ||
"preload": "led", | ||
} |