MyButton Library
Loading...
Searching...
No Matches
MyButton.h
1#ifndef _MYBUTTON_H_
2#define _MYBUTTON_H_
3
4/*
5 This library introduces the `MyButton` class, which makes it easy
6 to process push-button input by Arduino/ESP32.
7 https://github.com/iavorvel/MyButton
8*/
9
10#include <Arduino.h>
11
12#ifndef ARDUINO_ISR_ATTR
13#define ARDUINO_ISR_ATTR
14#endif
15
17{
18private:
19 typedef void (*ButtonPressCallback)();
20 bool noISR = true;
21 int pin = -1;
22 static const unsigned long btn_reset = 800UL, btn_debounce = 40UL, btn_longPress = 300UL;
23 volatile unsigned long btn_times[10];
24 volatile unsigned char ti = 0;
25 int prevState = 1;
26 ButtonPressCallback callback[3] = {nullptr, nullptr, nullptr};
27
28public:
29 enum Action
30 {
31 NOPRESS = 0,
32 CLICK,
33 DOUBLE_CLICK,
34 LONG_PRESS
35 };
36
41 void ARDUINO_ISR_ATTR buttonISR();
42
48 MyButton(int _pin);
49
55 bool useInterrupt();
56
63 bool usingInterrupt() const;
64
70 Action check();
71
79 bool on(Action action, ButtonPressCallback cb);
80};
81
82#endif
Definition MyButton.h:17
MyButton(int _pin)
Construct a new My Button object from _pin.
Definition MyButton.cpp:20
bool on(Action action, ButtonPressCallback cb)
Sets a callback/lambda for button gesture (Action)
Definition MyButton.cpp:89
bool useInterrupt()
Attempt to set an interrupt to use with the MyButton object.
Definition MyButton.cpp:28
Action check()
This method must be called as often as possible in the loop() function.
Definition MyButton.cpp:51
bool usingInterrupt() const
Check whether the MyButton object is configured to accept interrupts.
Definition MyButton.cpp:95
void ARDUINO_ISR_ATTR buttonISR()
This function is only internally invoked.
Definition MyButton.cpp:42