Module: j5e/button
Description
A module for working with buttons.
A push-button or simply button is a simple switch mechanism to control some aspect of a machine or a process. Buttons are typically made out of hard material, usually plastic or metal. The surface is usually flat or shaped to accommodate the human finger or hand, so as to be easily depressed or pushed. Buttons are most often biased switches, although many un-biased buttons still require a spring to return to their un-pushed state. Terms for the "pushing" of a button include pressing, depressing, mashing, slapping, hitting, and punching.Read more on Wikipedia
Requires
Class: Button
The Button class allows for control of digital buttons
Constructor
new Button(io)
Parameters
(required)
Description:
Pin identifier or IO Options (See instantiation)
Example
// Use a button to control an LED
import Button from "j5e/button";
import LED from "j5e/led";
const button = await new Button(12);
const led = await new LED(13);
button.on("open", function() {
led.off();
});
button.on("close", function() {
led.on();
});
Properties
(read only)
(read only)
(read only)
(read only)
Events
- Button#event:open
- Button#event:close
Methods
Button.configure
Configure a button
button.configure(options);
Returns: The instance on which the method was called
Parameters
(required)
Description:
Device configuration options
Description:
The amount of time a button must be held down before emitting an hold event
Default: 500
Description:
The amount of time in milliseconds to delay button events firing. Cleans up "noisy" state changes
Default: 7
Description:
The type of button, "NO" for normally open, "NC" for normally closed
Default: "NO"
Example
import Button from "j5e/button";
import LED from "j5e/led";
const button = await new Button(14);
button.configure({
debounce: 20
});
button.on("open", function() {
led.off();
});
button.on("close", function() {
led.on();
});
Button.on
Create an event listener
button.on(event, listener);
Parameters
(required)
Description:
The name of the event to listen for
(required)
Description:
A callback to run when the event is fired.
Button.removeListener
Remove an event listener
button.removeListener(event, listener);
Parameters
(required)
Description:
The name of the event that we are removing a listener from
(required)
Description:
The callback that we are removing
Button.emit
Note: This method is not part of the public API, and is subject to change.
Emit an event
button.emit(event);
Parameters
(required)
Description:
The name of the event to emit
Button.once
Create an event listener that will only fire one time.
button.once(event, listener);
Parameters
(required)
Description:
The name of the event to listen for
(required)
Description:
A callback to run when the event is fired.