VizBlocks
Button.h
Go to the documentation of this file.
1 #ifndef BUTTON_h
2 #define BUTTON_h
3 
4 class Button {
5 
6  private:
7 
8  int _pin;
9  int _id;
10 
11  bool _state;
12 
13  volatile bool _inputFlag = false;
14  bool _changeFlag = false;
15 
16  bool _pressedFlag = false;
17  bool _holdFlag = false;
18 
19  unsigned long _previousTimer;
20 
21  int _clickInterval = 1000;
22  int _holdInterval = 1000;
23  int _repeatInterval = 1000;
24 
25  void (*_cb)(Button*, uint8_t, bool); // Callback function
26 
27  bool _read() {
28  return digitalRead(_pin);
29  }
30 
31  void _setClickInterval(int x) {
32  _clickInterval = x;
33  }
34 
35  void _setHoldInterval(int x) {
36  _holdInterval = x;
37  }
38 
39  void _setRepeatInterval(int x) {
40  _repeatInterval = x;
41  }
42 
43  public:
44 
45  // Public members:
46 
47  static const uint8_t kEventPressed = 0; // Button was pressed
48  static const uint8_t kEventReleased = 1; // Button was released
49  static const uint8_t kEventClicked = 2; // Button was clicked (pressed and released within _clickInterval)
50  static const uint8_t kEventHeld = 3; // Button was held down for longer than _holdInterval
51  static const uint8_t kEventTick = 4; // Event released every _repeatInterval when button held
52 
53  // Public functions:
54 
55  Button(int pin, int id = 99) : _pin(pin), _id(id) {
56  pinMode(_pin, INPUT_PULLUP);
57  _state = _read();
58  _previousTimer = millis();
59  }
60 
61  void initInterrupts(void(*function)()) {
62  attachInterrupt(_pin, function, CHANGE);
63  }
64 
65  void setEventHandler(void(*function)(Button*, uint8_t, bool)) {
66  _cb = function;
67  }
68 
69  bool getState() {
70  return _state;
71  }
72 
73  int getId() {
74  return _id;
75  }
76 
78  return _clickInterval;
79  }
80 
82  return _holdInterval;
83  }
84 
86  return _repeatInterval;
87  }
88 
89  void check() {
90  unsigned long timer = millis();
91  unsigned long deltaTime = timer - _previousTimer;
92  _state = _read();
93 
94  if (_inputFlag == true) {
95  _inputFlag = false;
96 
97  // Button pressed
98  if (_state == LOW
99  && _pressedFlag == false) {
100  _pressedFlag = true;
101  _previousTimer = timer;
102  _cb(this, kEventPressed, _state);
103  return;
104 
105  // Button clicked
106  } else if (_state == HIGH
107  && deltaTime < _clickInterval
108  && _holdFlag == false) {
109  _pressedFlag = false;
110  _previousTimer = timer;
111  _cb(this, kEventClicked, _state);
112  return;
113 
114  // Button released
115  } else if (_state == HIGH) {
116  _pressedFlag = false;
117  _holdFlag = false;
118  _previousTimer = timer;
119  _cb(this, kEventReleased, _state);
120  return;
121  }
122  }
123 
124  // Button held
125  if (_state == LOW
126  && deltaTime > _holdInterval
127  && _holdFlag == false) {
128  _holdFlag = true;
129  _previousTimer = timer;
130  _cb(this, kEventHeld, _state);
131  return;
132 
133  // Button tick
134  } else if (_state == LOW
135  && deltaTime > _repeatInterval
136  && _holdFlag == true) {
137  _previousTimer = timer;
138  _cb(this, kEventTick, _state);
139  return;
140  }
141  }
142 
143  void ICACHE_RAM_ATTR tick() {
144  _inputFlag = true;
145  }
146 
147 };
148 
149 #endif
Button::kEventHeld
static const uint8_t kEventHeld
Definition: Button.h:50
Button::getRepeatInterval
int getRepeatInterval()
Definition: Button.h:85
Button::Button
Button(int pin, int id=99)
Definition: Button.h:55
Button::kEventClicked
static const uint8_t kEventClicked
Definition: Button.h:49
Button::check
void check()
Definition: Button.h:89
Button::getClickInterval
int getClickInterval()
Definition: Button.h:77
Button::setEventHandler
void setEventHandler(void(*function)(Button *, uint8_t, bool))
Definition: Button.h:65
Button::tick
void ICACHE_RAM_ATTR tick()
Definition: Button.h:143
Button
Definition: Button.h:4
Button::kEventPressed
static const uint8_t kEventPressed
Definition: Button.h:47
Button::getState
bool getState()
Definition: Button.h:69
Button::getHoldInterval
int getHoldInterval()
Definition: Button.h:81
Button::kEventReleased
static const uint8_t kEventReleased
Definition: Button.h:48
Button::getId
int getId()
Definition: Button.h:73
Button::kEventTick
static const uint8_t kEventTick
Definition: Button.h:51
Button::initInterrupts
void initInterrupts(void(*function)())
Definition: Button.h:61