AceButton  1.3
An adjustable, compact, event-driven (ACE) button library for Arduino.
AceButton.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_BUTTON_ACE_BUTTON_H
26 #define ACE_BUTTON_ACE_BUTTON_H
27 
28 #include <Arduino.h>
29 #include "ButtonConfig.h"
30 
31 namespace ace_button {
32 
50 class AceButton {
51  public:
52  // The supported event types.
53 
55  static const uint8_t kEventPressed = 0;
56 
58  static const uint8_t kEventReleased = 1;
59 
64  static const uint8_t kEventClicked = 2;
65 
70  static const uint8_t kEventDoubleClicked = 3;
71 
76  static const uint8_t kEventLongPressed = 4;
77 
84  static const uint8_t kEventRepeatPressed = 5;
85 
90  static const uint8_t kButtonStateUnknown = 2;
91 
124  explicit AceButton(uint8_t pin = 0, uint8_t defaultReleasedState = HIGH,
125  uint8_t id = 0);
126 
132  explicit AceButton(ButtonConfig* buttonConfig);
133 
139  void init(uint8_t pin = 0, uint8_t defaultReleasedState = HIGH,
140  uint8_t id = 0);
141 
143  ButtonConfig* getButtonConfig() ACE_BUTTON_INLINE {
144  return mButtonConfig;
145  }
146 
152  void setButtonConfig(ButtonConfig* buttonConfig) ACE_BUTTON_INLINE {
153  mButtonConfig = buttonConfig;
154  }
155 
166  ACE_BUTTON_INLINE {
167  mButtonConfig->setEventHandler(eventHandler);
168  }
169 
171  uint8_t getPin() ACE_BUTTON_INLINE { return mPin; }
172 
174  uint8_t getId() ACE_BUTTON_INLINE { return mId; }
175 
177  uint8_t getDefaultReleasedState();
178 
192  uint8_t getLastButtonState() ACE_BUTTON_INLINE {
193  return mLastButtonState;
194  }
195 
203  void check();
204 
220  bool isReleased(uint8_t buttonState) ACE_BUTTON_INLINE {
221  return buttonState == getDefaultReleasedState();
222  }
223 
224  // Some of these private methods may be useful to the calling client but I
225  // don't want to release them to the public because I want to keep the API as
226  // small as possible for easier long term maintenance. (Once a method is
227  // released to the public, it must be supported forever to ensure backwards
228  // compatibility with older client code.)
229 
230  private:
231  // Disable copy-constructor and assignment operator
232  AceButton(const AceButton&) = delete;
233  AceButton& operator=(const AceButton&) = delete;
234 
236  void setPin(uint8_t pin) ACE_BUTTON_INLINE { mPin = pin; }
237 
245  void setDefaultReleasedState(uint8_t state);
246 
248  void setId(uint8_t id) ACE_BUTTON_INLINE { mId = id; }
249 
250  // Various bit masks to store a boolean flag in the 'mFlags' field.
251  // We use bit masks to save static RAM. If we had used a 'bool' type, each
252  // of these would consume one byte.
253  static const uint8_t kFlagDefaultReleasedState = 0x01;
254  static const uint8_t kFlagDebouncing = 0x02;
255  static const uint8_t kFlagPressed = 0x04;
256  static const uint8_t kFlagClicked = 0x08;
257  static const uint8_t kFlagDoubleClicked = 0x10;
258  static const uint8_t kFlagLongPressed = 0x20;
259  static const uint8_t kFlagRepeatPressed = 0x40;
260  static const uint8_t kFlagClickPostponed = 0x80;
261 
262  // Methods for accessing the button's internal states.
263  // I don't expect these to be useful to the outside world.
264 
265  // If this is set, then mLastDebounceTime is valid.
266  bool isDebouncing() ACE_BUTTON_INLINE {
267  return mFlags & kFlagDebouncing;
268  }
269 
270  void setDebouncing() ACE_BUTTON_INLINE {
271  mFlags |= kFlagDebouncing;
272  }
273 
274  void clearDebouncing() ACE_BUTTON_INLINE {
275  mFlags &= ~kFlagDebouncing;
276  }
277 
278  // If this is set, then mLastPressTime is valid.
279  bool isPressed() ACE_BUTTON_INLINE {
280  return mFlags & kFlagPressed;
281  }
282 
283  void setPressed() ACE_BUTTON_INLINE {
284  mFlags |= kFlagPressed;
285  }
286 
287  void clearPressed() ACE_BUTTON_INLINE {
288  mFlags &= ~kFlagPressed;
289  }
290 
291  // If this is set, then mLastClickTime is valid.
292  bool isClicked() ACE_BUTTON_INLINE {
293  return mFlags & kFlagClicked;
294  }
295 
296  void setClicked() ACE_BUTTON_INLINE {
297  mFlags |= kFlagClicked;
298  }
299 
300  void clearClicked() ACE_BUTTON_INLINE {
301  mFlags &= ~kFlagClicked;
302  }
303 
304  // A double click was detected. No need to store the last double-clicked
305  // time because we don't support a triple-click event (yet).
306  bool isDoubleClicked() ACE_BUTTON_INLINE {
307  return mFlags & kFlagDoubleClicked;
308  }
309 
310  void setDoubleClicked() ACE_BUTTON_INLINE {
311  mFlags |= kFlagDoubleClicked;
312  }
313 
314  void clearDoubleClicked() ACE_BUTTON_INLINE {
315  mFlags &= ~kFlagDoubleClicked;
316  }
317 
318  // If this is set, then mLastPressTime can be treated as the start
319  // of a long press.
320  bool isLongPressed() ACE_BUTTON_INLINE {
321  return mFlags & kFlagLongPressed;
322  }
323 
324  void setLongPressed() ACE_BUTTON_INLINE {
325  mFlags |= kFlagLongPressed;
326  }
327 
328  void clearLongPressed() ACE_BUTTON_INLINE {
329  mFlags &= ~kFlagLongPressed;
330  }
331 
332  // If this is set, then mLastRepeatPressTime is valid.
333  bool isRepeatPressed() ACE_BUTTON_INLINE {
334  return mFlags & kFlagRepeatPressed;
335  }
336 
337  void setRepeatPressed() ACE_BUTTON_INLINE {
338  mFlags |= kFlagRepeatPressed;
339  }
340 
341  void clearRepeatPressed() ACE_BUTTON_INLINE {
342  mFlags &= ~kFlagRepeatPressed;
343  }
344 
345  bool isClickPostponed() ACE_BUTTON_INLINE {
346  return mFlags & kFlagClickPostponed;
347  }
348 
349  void setClickPostponed() ACE_BUTTON_INLINE {
350  mFlags |= kFlagClickPostponed;
351  }
352 
353  void clearClickPostponed() ACE_BUTTON_INLINE {
354  mFlags &= ~kFlagClickPostponed;
355  }
356 
362  bool checkDebounced(uint16_t now, uint8_t buttonState);
363 
370  bool checkInitialized(uint16_t buttonState);
371 
373  void checkEvent(uint16_t now, uint8_t buttonState);
374 
376  void checkLongPress(uint16_t now, uint8_t buttonState);
377 
379  void checkRepeatPress(uint16_t now, uint8_t buttonState);
380 
382  void checkChanged(uint16_t now, uint8_t buttonState);
383 
388  void checkReleased(uint16_t now, uint8_t buttonState);
389 
391  void checkPressed(uint16_t now, uint8_t buttonState);
392 
394  void checkClicked(uint16_t now);
395 
400  void checkDoubleClicked(uint16_t now);
401 
410  void checkOrphanedClick(uint16_t now);
411 
416  void checkPostponedClick(uint16_t now);
417 
464  void handleEvent(uint8_t eventType);
465 
466  uint8_t mPin; // button pin number
467  uint8_t mId; // identifier, e.g. an index into an array
468 
469  // Internal states of the button debouncing and event handling.
470  // NOTE: We don't keep track of the lastDoubleClickTime, because we
471  // don't support a TripleClicked event. That may change in the future.
472  uint16_t mLastDebounceTime; // ms
473  uint16_t mLastClickTime; // ms
474  uint16_t mLastPressTime; // ms
475  uint16_t mLastRepeatPressTime; // ms
476 
478  uint8_t mFlags;
479 
484  uint8_t mLastButtonState;
485 
487  ButtonConfig* mButtonConfig;
488 };
489 
490 }
491 #endif
void setEventHandler(ButtonConfig::EventHandler eventHandler) ACE_BUTTON_INLINE
Convenience method to set the event handler.
Definition: AceButton.h:165
void setButtonConfig(ButtonConfig *buttonConfig) ACE_BUTTON_INLINE
Set the ButtonConfig associated with this Button.
Definition: AceButton.h:152
uint8_t getDefaultReleasedState()
Get the initial released state of the button, HIGH or LOW.
Definition: AceButton.cpp:67
bool isReleased(uint8_t buttonState) ACE_BUTTON_INLINE
Returns true if the given buttonState represents a &#39;Released&#39; state for the button.
Definition: AceButton.h:220
uint8_t getLastButtonState() ACE_BUTTON_INLINE
Return the button state that was last valid.
Definition: AceButton.h:192
static const uint8_t kEventRepeatPressed
Button was held down and auto generated multiple presses.
Definition: AceButton.h:84
void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE
Install the event handler.
Definition: ButtonConfig.h:296
static const uint8_t kButtonStateUnknown
Button state is unknown.
Definition: AceButton.h:90
ButtonConfig * getButtonConfig() ACE_BUTTON_INLINE
Get the ButtonConfig associated with this Button.
Definition: AceButton.h:143
void init(uint8_t pin=0, uint8_t defaultReleasedState=HIGH, uint8_t id=0)
Reset the button to the initial constructed state.
Definition: AceButton.cpp:49
Class that defines the timing parameters and event handler of an AceButton or a group of AceButton in...
Definition: ButtonConfig.h:60
static const uint8_t kEventDoubleClicked
Button was double-clicked.
Definition: AceButton.h:70
static const uint8_t kEventLongPressed
Button was held down for longer than ButtonConfig::getLongPressDelay()).
Definition: AceButton.h:76
uint8_t getId() ACE_BUTTON_INLINE
Get the custom identifier of the button.
Definition: AceButton.h:174
void(* EventHandler)(AceButton *button, uint8_t eventType, uint8_t buttonState)
The event handler signature.
Definition: ButtonConfig.h:164
uint8_t getPin() ACE_BUTTON_INLINE
Get the button&#39;s pin number.
Definition: AceButton.h:171
void check()
Check state of button and trigger event processing.
Definition: AceButton.cpp:73
An Adjustable Compact Event-driven (ACE) Button library that debounces and dispatches button events t...
Definition: AceButton.h:50
AceButton(uint8_t pin=0, uint8_t defaultReleasedState=HIGH, uint8_t id=0)
Constructor defines parameters of the button that changes from button to button.
Definition: AceButton.cpp:39
static const uint8_t kEventReleased
Button was released.
Definition: AceButton.h:58
static const uint8_t kEventPressed
Button was pressed.
Definition: AceButton.h:55
static const uint8_t kEventClicked
Button was clicked (Pressed and Released within ButtonConfig::getClickDelay()).
Definition: AceButton.h:64