LennartHennigs / Button2

Arduino/ESP button library that provides callback functions to track single, double, triple and long clicks. It also takes care of debouncing.
MIT License
475 stars 80 forks source link
arduino arduino-library button c-plus-plus embedded esp32 esp8266 hardware mbed touch

Button2

Arduino/ESP library to simplify working with buttons.

Description

This library allows you to use callback functions to track single, double, triple and long clicks. Alternatively, it provides function to use in your main loop(). The library also takes care of debouncing. Using this lib will reduce and simplify your source code significantly.

It has been tested with Arduino, ESP8266 and ESP32 devices.

To see the latest changes to the library please take a look at the Changelog.

If you find this library helpful please consider giving it a ⭐️ at GitHub and/or buy me a ☕️.

Thank you!

How To Use

This library allows you to define a button and uses callback functions to detect different types of button interactions. If you don't want to use callback there are also functions available for using it in your code's main loop().

Definition

 #include "Button2.h"
  void begin(byte attachTo, byte buttonMode = INPUT_PULLUP, boolean activeLow  = true);

Button Types

   #include "Button2.h"
   #define BUTTON_PIN D3

   Button2 button;

   void setup() {
     button.begin(BUTTON_PIN);
  }

Callback Handler

Longpress Handling

The Loop

   #include "Button2.h"
   #define BUTTON_PIN D3

   Button2 button;

   void handleTap(Button2& b) {
    // check for really long clicks
    if (b.wasPressedFor() > 1000) {
    // do something
    }
   }

   void setup() {
     button.begin(BUTTON_PIN);
     button.setTapHandler(handleTap);
  }

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

Using an timer interrupt instead

Timeouts

  #define BTN_DEBOUNCE_MS 50
  #define BTN_LONGCLICK_MS 200
  #define BTN_DOUBLECLICK_MS 300

Using Button2 in the main loop()

    enum clickType {
      single_click, 
      double_click, 
      triple_click, 
      long_click,
      empty
    };

Status Functions

unsigned int wasPressedFor() const;
byte getNumberOfClicks() const;
byte getType() const;
boolean isPressed() const;
boolean isPressedRaw() const;
bool wasPressed() const;

IDs for Button Instances

Creating A Custom Button State Handler

Examples

Class Definition

The button class offers a few additional functions, please take a look at the Class Definition below.

See below the constructors and member functions the library provides:

Button2();
Button2(byte attachTo, byte buttonMode = INPUT_PULLUP, boolean activeLow = true);

void begin(byte attachTo, byte buttonMode = INPUT_PULLUP, boolean activeLow  = true);

void setDebounceTime(unsigned int ms);
void setLongClickTime(unsigned int ms);
void setDoubleClickTime(unsigned int ms);

unsigned int getDebounceTime();
unsigned int getLongClickTime();
unsigned int getDoubleClickTime();
byte getPin();

void reset();

void setButtonStateFunction(StateCallbackFunction f);

void setChangedHandler(CallbackFunction f);
void setPressedHandler(CallbackFunction f);
void setReleasedHandler(CallbackFunction f);

void setTapHandler(CallbackFunction f);
void setClickHandler(CallbackFunction f);
void setDoubleClickHandler(CallbackFunction f);
void setTripleClickHandler(CallbackFunction f);

void setLongClickHandler(CallbackFunction f);
void setLongClickDetectedHandler(CallbackFunction f);
void setLongClickDetectedRetriggerable(bool retriggerable);
void byte getLongClickCount() const;

unsigned int wasPressedFor() const;
void resetPressedState();
byte resetClickCount();

boolean isPressed() const;
boolean isPressedRaw() const;

bool wasPressed() const;
clickType read(bool keepState = false);
clickType wait(bool keepState = false);
void waitForClick(bool keepState = false);
void waitForDouble(bool keepState = false);
void waitForTriple(bool keepState = false);
void waitForLong(bool keepState = false);

byte getNumberOfClicks() const;
byte getType() const;
String clickToString(clickType type) const;

int getID() const;
void setID(int newID);

bool operator == (Button2 &rhs);

void loop();

Installation

Open the Arduino IDE choose "Sketch > Include Library" and search for "Button2". Or download the ZIP archive (https://github.com/lennarthennigs/Button2/zipball/master), and choose "Sketch > Include Library > Add .ZIP Library..." and select the downloaded file.

License

MIT License

Copyright (c) 2017-2023 Lennart Hennigs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.