-
-
Notifications
You must be signed in to change notification settings - Fork 5
Internal logic
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.
When our library detects certain event, it will notify respective listener (if registered) by calling its callback function.
Press event is detected, when a user presses a button and holds the button for longer than debounceTick
interval.
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.
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.
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.
This event is detected only after long press was detected and later a button was released.
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.
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.
For specific examples on how to use this library, please look into respective subsections in this wiki.