Skip to content

Internal logic

Vladimir Zahradnik edited this page Aug 13, 2019 · 2 revisions

Our library uses "pseudo multi-tasking" approach. Instead of using costly functions like delay(), ObjectButton does its job very quickly and instead of waiting, it will remember its last state. Next time it will resume from that state.

To accomplish this, we have implemented an internal state machine. To keep this state machine up-to-date, you need to periodically call ObjectButton#tick() function. Ideally, do it in your Arduino loop() function like this:

ObjectButton button = ObjectButton(...);

void setup() {
}

void loop() {
    button.tick();
}

Our state machine can transition into following states:

  • BUTTON_NOT_PRESSED
  • BUTTON_PRESSED
  • BUTTON_RELEASED
  • BUTTON_DOUBLE_CLICKED

Each time a tick() function is called, internally we'll capture a timestamp using millis(). When a button is pressed, we'll store that value. Next time our state machine is updated, we'll calculate a time difference between current time and time of recorded button press. From this difference we can determine all supported button actions.

Events

When our library detects certain event, it will notify respective listener (if registered) by calling its callback function.

Press event

Press event is detected, when a user presses a button and holds the button for longer than debounceTick interval.

Jitter ellimination

To elliminate accidental events caused by rapid changes on input pin (a jitter), we've introduced debouncing. It's a common practice. If an input pin voltage level is changed for shorter period than debounce interval, most likely the button was not pressed by a user. This interval is user-configurable.

Release event

This event is detected, when a user releases a button after it was pressed. As an advantage, this action is detected immediately. If you require very precise notifications, do not wait for click or double-click events and instead map your code to press and release events.

Long press start

This event is detected, when a user holds a button for longer than time period defined for long press action. This period is user-configurable.

Long press end

This event is detected only after long press was detected and later a button was released.

Click event

This event is detected when user presses a button and subsequently releases this button within period defined for a click action. This period is user-configurable.

Note: Click event is not detected immediately as it occurs. We need to wait until period defined for a click action elapses, because until the period elapses user could in practice still generate a double-click action.

Double-click event

This event is detected when user presses a button and subsequently releases this button twice in a row within a time period shorter than period defined for a click action.

Usage

For specific examples on how to use this library, please look into respective subsections in this wiki.

Clone this wiki locally