Skip to content

Commit

Permalink
Button and LED for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
phoddie committed Sep 25, 2020
1 parent a461f60 commit e1fe8b4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
44 changes: 44 additions & 0 deletions modules/drivers/button/button.js
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;
9 changes: 9 additions & 0 deletions modules/drivers/button/manifest.json
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",
}
29 changes: 29 additions & 0 deletions modules/drivers/led/led.js
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;
9 changes: 9 additions & 0 deletions modules/drivers/led/manifest.json
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",
}

0 comments on commit e1fe8b4

Please sign in to comment.