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

Name
Type
Description
io
(required)
number, string, or object

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

Name
Type
Description
isClosed
(read only)
boolean
True if the button is being pressed
isOpen
(read only)
boolean
True if the button is not being pressed
downValue
(read only)
number
Get the raw downValue (depends on type and io input mode)
upValue
(read only)
number
Get the raw upValue (depends on type and io input mode)
holdtime
number
The length of time a button must be held before firing a hold event (in ms)

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

Name
Type
Description
options
(required)
object

Description:
Device configuration options

options.holdtime
number

Description:
The amount of time a button must be held down before emitting an hold event
Default: 500

options.debounce
number

Description:
The amount of time in milliseconds to delay button events firing. Cleans up "noisy" state changes
Default: 7

options.type
string

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

Name
Type
Description
event
(required)
string

Description:
The name of the event to listen for

listener
(required)
function

Description:
A callback to run when the event is fired.


Button.removeListener

Remove an event listener

button.removeListener(event, listener);

Parameters

Name
Type
Description
event
(required)
string

Description:
The name of the event that we are removing a listener from

listener
(required)
function

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

Name
Type
Description
event
(required)
string

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

Name
Type
Description
event
(required)
string

Description:
The name of the event to listen for

listener
(required)
function

Description:
A callback to run when the event is fired.