Show:

BO.io.Button Class

Creates and interface to a physical button. The Button object dispatches events on state changes such as Pressed, Released and Sustained Press. The Button object also handles debouncing.

The advantage of using the Button class over listening for pin change events on a Pin object, is that the Button class handles debouncing and provides helpful button events: Pressed, Released, Long Press and Sustained Press

PULL_UP vs PULL_DOWN. If the other end of the resistor connected to the button is connected to ground, configuration is PULL_DOWN, if the resistor is connected to power, then the configuration is PULL_UP.

Constructor

BO.io.Button

(
  • board
  • pin
  • buttonMode
  • sustainedPressInterval
)

Defined in src/io/Button.js:18

Parameters:

  • board IOBoard

    A reference to the IOBoard instance

  • pin Pin

    A reference to the Pin the button is connected to.

  • buttonMode Number

    The mode of the button (either Button.PULL_DOWN or Button.PULL_UP if wired with external resistors or Button.INTERNAL_PULL_UP if using the internal pull-up resistors. Default is PULL_DOWN.

  • sustainedPressInterval Number

    The delay time in milliseconds before a sustained press event is fired.

Methods

addEventListener

(
  • type
  • listener
)

Parameters:

  • type String

    The event type

  • listener Function

    The function to be called when the event is fired

dispatchEvent

(
  • type
  • optionalParams
)

Parameters:

  • type Event

    The Event object.

  • optionalParams Object

    Optional parameters passed as an object. return {boolean} True if dispatch is successful, false if not.

hasEventListener

(
  • type
)

Parameters:

  • type String

    The event type return {boolean} True is listener exists for this type, false if not.

onPinChange

() private

Defined in src/io/Button.js:80

pressed

() private

released

() private

removeEventListener

(
  • type
  • listener
)

Parameters:

  • type String

    The event type

  • listener Function

    The function to be called when the event is fired

sustainedPress

() private

Properties

Button.INTERNAL_PULL_UP

Unknown static

Button.PULL_DOWN

Unknown static

Button.PULL_UP

Unknown static

debounceInterval

Number

The debounce time interval in milliseconds.

pinNumber

Number

[read-only] The pin number of the pin the button is attached to.

sustainedPressInterval

Number

The delay time (in milliseconds) the button must be held before a sustained press event is fired.

Events

longPress

BO.io.ButtonEvent.LONG_PRESS

The longPress event is dispatched once when the button has been held for the time duration specified by the sustainedPressInterval property.

Event Payload:

pressed

BO.io.ButtonEvent.PRESS

The pressed event is dispatched when the button is pressed.

Event Payload:

released

BO.io.ButtonEvent.RELEASE

The released event is dispatched when the button is released.

Event Payload:

sustainedPress

BO.io.ButtonEvent.SUSTAINED_PRESS

The sustainedPress event is dispatched continuously at the rate specified by the sustainedPressInterval property while the button is held.

Event Payload: