marcinbor85 / SmartButton

Asynchronous SmartButton library for handling various button events
MIT License
15 stars 9 forks source link

SmartButton Library

SmartButton Library is used for handling various button events. It supports button press, release, multiple click and button hold events. Library is implemented in C++ with build-in Arduino port, but it is easy to port to different architecture.

Features

Changelog

2020.08.12 - 0.3.0

2020.08.10 - 0.2.0

Example

Single and double click example:

#include <Arduino.h>
#include <SmartButton.h>

constexpr int BUTTON_PIN = 2;
constexpr int LED_PIN = 13;

using namespace smartbutton;

SmartButton button(BUTTON_PIN, SmartButton::InputType::NORMAL_HIGH);

void setup()
{
    pinMode(LED_PIN, OUTPUT);           // Digital output for led
    pinMode(BUTTON_PIN, INPUT_PULLUP);  // Digital input with pull-up resistors (normal high)

    // Initialize and register smart button
    button.begin([] (SmartButton *button, SmartButton::Event event, int clickCounter)
    {
        if (event == SmartButton::Event::CLICK) {   // Click event handler
            if (clickCounter == 1) {
                digitalWrite(LED_PIN, LOW);         // Single click will turn led off
            } else if (clickCounter == 2) {
                digitalWrite(LED_PIN, HIGH);        // Double click will turn led on
            }
        }
    });
}

void loop()
{
    SmartButton::service();   // Asynchronous service routine, should be called periodically
}