IPushButton is a C++ library designed to handle push button events such as push, hold, release etc.
It provides a simple interface to manage button states and events with different customization.
- Handles different button states: idle, push, hold, release, delay, and rapid.
- Customizable debounce and repeat delays using the
debounceDelay()andrepeatDelay()methods. - Detects rapid clicks (double, triple, etc.).
- Acceleration feature to increase the button repeat rate (e.g., fast scrolling).
- Supports inverted button states.
- Allows switching between different time measurement methods.
- Easily integrates with existing projects.
To use the IPushButton library in your project, follow these steps:
- Clone the repository:
git clone https://github.com/nalbe/IPushButton.git - Include the header files in your project:
#include <Arduino.h>;#include "IPushButton.h" - Create an instance of the
IPushButtonclass:IPushButton button(pin, mode);(replacepinandmodewith appropriate values) - Call the
update()function in your loop:button.update(); - Handle button events by overriding virtual methods (
onPushFn,onHoldFn, etc.) or usinggetState()to check the current state
Here is an example of how to use the IPushButton class:
#include "IPushButton.h"
// Define a custom push button class inheriting from IPushButton
class MyPushButton : public IPushButton
{
protected:
void onPushFn() override { /* Handle push event */ }
void onHoldFn() override { /* Handle hold event */ }
void onReleaseFn() override { /* Handle release event */ }
void onDelayFn() override { /* Handle delay event */ }
void onIdleFn() override { /* Handle idle event */ }
};
// Create an instance with pin XX and mode YY.
MyPushButton button(D9, INPUT_PULLUP);
// Setup function
void setup()
{
// Set debounce delay to 60 ms (default is 50 ms)
button.debounceDelay(60);
// Set repeat delay to 250 ms (default is 0 ms)
button.repeatDelay(250);
}
// Update the button state in the loop.
void loop()
{
// Update the button state
button.update();
// your code here
}Another way is to handle events directly in the loop:
#include "IPushButton.h"
IPushButton button(D9, INPUT_PULLUP);
void setup()
{
Serial.begin(115200);
button.debounceDelay(60);
button.repeatDelay(250);
}
void loop()
{
button.update();
// Handle button events
if (button.state() & IPushButton::eState::PUSH) {
// Handle push event
Serial.println("push");
}
else if (button.state() & IPushButton::eState::HOLD) {
// Handle hold event
Serial.println("hold");
}
else if (button.state() & IPushButton::eState::RELEASE) {
// Handle release event
Serial.println("release");
}
else if (button.state() & IPushButton::eState::DELAY) {
// Handle delay event
//Serial.println("..");
}
else if (button.state() & IPushButton::eState::IDLE) {
// Handle idle event
//Serial.println(".");
}
if ((button.state() & IPushButton::eState::RAPID) and (button.rapidCount() & 1)) {
// Handle rapid event
Serial.println("dbl_click");
}
// your code here
}// Short push:
push
release
// Hold a while:
push
hold
hold
hold
release
// Double click:
push
release
push
dbl_click
release
// 4 rapid clicks interpreted as 2 double:
push
release
push
dbl_click
release
push
release
push
dbl_click
releaseThis library depends on the Arduino framework. To use this library, ensure that Arduino.h is included in your project before including this library's header file.
- If you are using the Arduino IDE,
Arduino.his automatically included for you, so you don’t need to explicitly include it in your sketch. - If you are using a non-Arduino environment, ensure that the Arduino framework is properly set up and
Arduino.his available.
This feature allows users to choose how timestamps are updated when interacting with a button. There are two modes:
-
Global Timestamp: The timestamp updates only when the button is pressed. The actual time between events may be shorter than the repeat delay, depending on the overall loop delay.
-
Local Timestamp: The timestamp updates continuously on every cycle while the button is pressed. The actual time between events may be longer than the repeat delay, depending on the overall loop delay.
By switching between these methods, user can balance performance and accuracy based on their needs.
IPushButton()IPushButton(pin_t pin, pin_t mode)
bool isEnabled() constbool isDebounceDelay() constbool isRepeatDelay() constbool isInverted() constbool isLocalTime() constbool isAccelerated() consteState state() constpin_t id() constpin_t mode() constmillis_t debounceDelay() constmillis_t repeatDelay() constmillis_t pushTime() constmillis_t releaseTime() constsize_type rapidCount() constsize_type accelerationValue() constsize_type accelerationThreshold() constvoid id(pin_t)void mode(pin_t)void debounceDelay(millis_t)void repeatDelay(millis_t)void useLocalTime(bool)void accelerationValue(size_type)void accelerationThreshold(size_type)void invert(bool)void accelerate(bool)void enable(bool)void disable()void update()void reset()
virtual ~IPushButton()virtual void onPushFn()virtual void onHoldFn()virtual void onReleaseFn()virtual void onDelayFn()virtual void onIdleFn()
This project is licensed under the MIT License - see the LICENSE file for details.