AceButton  1.3.3
An adjustable, compact, event-driven button library for Arduino.
ButtonConfig.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_BUTTON_CONFIG_H
26 #define ACE_BUTTON_BUTTON_CONFIG_H
27 
28 #include <Arduino.h>
29 
30 // TODO: Verify if this is actually needed. The program size seems to be
31 // identical with or without it on the Arduino IDE (which uses gcc).
32 #define ACE_BUTTON_INLINE __attribute__((always_inline))
33 
34 namespace ace_button {
35 
36 class AceButton;
37 class TimingStats;
38 
60 class ButtonConfig {
61  public:
62  // Various timing constants, in milliseconds.
63  //
64  // Note that the timing constants are stored as uint16_t (2
65  // bytes) instead of unsigned long (4 bytes) which is the type returned by
66  // the millis() system method. It turns out that we can store and perform
67  // all timing calculations using uint16_t without ill effect, as long as the
68  // polling of AceButton::check() happens more frequently than the rollover
69  // time of a uint16_t (i.e. 65.536 seconds) and certain precautions (e.g.
70  // AceButton::checkOrphanedClick()) are taken before a uint16_t rollover
71  // happens. In theory, these additional precautions would be needed even if
72  // an 'unsigned long' is used but almost no one does them because they
73  // assume that their code won't be running continuously for the rollover
74  // time of an 'unsigned long' (i.e. 49.7 days).
75 
77  static const uint16_t kDebounceDelay = 20;
78 
80  static const uint16_t kClickDelay = 200;
81 
83  static const uint16_t kDoubleClickDelay = 400;
84 
86  static const uint16_t kLongPressDelay = 1000;
87 
89  static const uint16_t kRepeatPressDelay = 1000;
90 
92  static const uint16_t kRepeatPressInterval = 200;
93 
94  // Various features controlled by feature flags.
95 
101  typedef uint16_t FeatureFlagType;
102 
104  static const FeatureFlagType kFeatureClick = 0x01;
105 
111  static const FeatureFlagType kFeatureDoubleClick = 0x02;
112 
114  static const FeatureFlagType kFeatureLongPress = 0x04;
115 
117  static const FeatureFlagType kFeatureRepeatPress = 0x08;
118 
120  static const FeatureFlagType kFeatureSuppressAfterClick = 0x10;
121 
127  static const FeatureFlagType kFeatureSuppressAfterDoubleClick = 0x20;
128 
130  static const FeatureFlagType kFeatureSuppressAfterLongPress = 0x40;
131 
133  static const FeatureFlagType kFeatureSuppressAfterRepeatPress = 0x80;
134 
141  static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick = 0x100;
142 
150  static const FeatureFlagType kFeatureSuppressAll =
151  (kFeatureSuppressAfterClick |
152  kFeatureSuppressAfterDoubleClick |
153  kFeatureSuppressAfterLongPress |
154  kFeatureSuppressAfterRepeatPress |
156 
164  typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
165  uint8_t buttonState);
166 
169 
170  // These configuration methods are virtual so that they can be overriddden.
171  // Subclasses can override at the class-level by defining a new virtual
172  // function in the subclass, or by defining an instance variable and storing
173  // the parameter with each instance of this class.
174 
176  uint16_t getDebounceDelay() { return mDebounceDelay; }
177 
179  uint16_t getClickDelay() { return mClickDelay; }
180 
185  uint16_t getDoubleClickDelay() {
186  return mDoubleClickDelay;
187  }
188 
190  uint16_t getLongPressDelay() {
191  return mLongPressDelay;
192  }
193 
200  uint16_t getRepeatPressDelay() {
201  return mRepeatPressDelay;
202  }
203 
208  return mRepeatPressInterval;
209  }
210 
212  void setDebounceDelay(uint16_t debounceDelay) {
213  mDebounceDelay = debounceDelay;
214  }
215 
217  void setClickDelay(uint16_t clickDelay) {
218  mClickDelay = clickDelay;
219  }
220 
222  void setDoubleClickDelay(uint16_t doubleClickDelay) {
223  mDoubleClickDelay = doubleClickDelay;
224  }
225 
227  void setLongPressDelay(uint16_t longPressDelay) {
228  mLongPressDelay = longPressDelay;
229  }
230 
232  void setRepeatPressDelay(uint16_t repeatPressDelay) {
233  mRepeatPressDelay = repeatPressDelay;
234  }
235 
237  void setRepeatPressInterval(uint16_t repeatPressInterval) {
238  mRepeatPressInterval = repeatPressInterval;
239  }
240 
241  // The getClock() and readButton() are external dependencies that normally
242  // would be injected using separate classes, but in the interest of saving
243  // RAM in an embedded environment, we expose them in this class instead.
244 
250  virtual unsigned long getClock() { return millis(); }
251 
256  virtual unsigned long getClockMicros() { return micros(); }
257 
263  virtual int readButton(uint8_t pin) {
264  return digitalRead(pin);
265  }
266 
267  // These methods return the various feature flags that control the
268  // functionality of the AceButton.
269 
271  bool isFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
272  return mFeatureFlags & features;
273  }
274 
276  void setFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
277  mFeatureFlags |= features;
278  }
279 
281  void clearFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
282  mFeatureFlags &= ~features;
283  }
284 
285  // EventHandler
286 
288  EventHandler getEventHandler() ACE_BUTTON_INLINE {
289  return mEventHandler;
290  }
291 
296  void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE {
297  mEventHandler = eventHandler;
298  }
299 
300  // TimingStats
301 
303  void setTimingStats(TimingStats* timingStats) {
304  mTimingStats = timingStats;
305  }
306 
308  TimingStats* getTimingStats() { return mTimingStats; }
309 
314  static ButtonConfig* getSystemButtonConfig() ACE_BUTTON_INLINE {
315  return &sSystemButtonConfig;
316  }
317 
318  protected:
323  virtual void init() {
324  mFeatureFlags = 0;
325  mTimingStats = nullptr;
326  }
327 
328  private:
333  static ButtonConfig sSystemButtonConfig;
334 
335  // Disable copy-constructor and assignment operator
336  ButtonConfig(const ButtonConfig&) = delete;
337  ButtonConfig& operator=(const ButtonConfig&) = delete;
338 
340  EventHandler mEventHandler = nullptr;
341 
343  FeatureFlagType mFeatureFlags = 0;
344 
346  TimingStats* mTimingStats = nullptr;
347 
348  uint16_t mDebounceDelay = kDebounceDelay;
349  uint16_t mClickDelay = kClickDelay;
350  uint16_t mDoubleClickDelay = kDoubleClickDelay;
351  uint16_t mLongPressDelay = kLongPressDelay;
352  uint16_t mRepeatPressDelay = kRepeatPressDelay;
353  uint16_t mRepeatPressInterval = kRepeatPressInterval;
354 };
355 
356 }
357 #endif
uint16_t getRepeatPressInterval()
Milliseconds between two successive RepeatPressed events.
Definition: ButtonConfig.h:207
static const uint16_t kRepeatPressInterval
Default value returned by getRepeatPressInterval().
Definition: ButtonConfig.h:92
static const uint16_t kDebounceDelay
Default value returned by getDebounceDelay().
Definition: ButtonConfig.h:77
void setDoubleClickDelay(uint16_t doubleClickDelay)
Set the doubleClickDelay.
Definition: ButtonConfig.h:222
virtual void init()
Initialize to its pristine state, except for the EventHandler which is unchanged. ...
Definition: ButtonConfig.h:323
static const FeatureFlagType kFeatureClick
Flag to activate the AceButton::kEventClicked event.
Definition: ButtonConfig.h:104
ButtonConfig()
Constructor.
Definition: ButtonConfig.h:168
EventHandler getEventHandler() ACE_BUTTON_INLINE
Return the eventHandler.
Definition: ButtonConfig.h:288
uint16_t getDoubleClickDelay()
Milliseconds between the first and second click to register as a double-click.
Definition: ButtonConfig.h:185
TimingStats * getTimingStats()
Get the timing stats.
Definition: ButtonConfig.h:308
void clearFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Disable the given features.
Definition: ButtonConfig.h:281
void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE
Install the event handler.
Definition: ButtonConfig.h:296
void setTimingStats(TimingStats *timingStats)
Set the timing stats object.
Definition: ButtonConfig.h:303
void setDebounceDelay(uint16_t debounceDelay)
Set the debounceDelay.
Definition: ButtonConfig.h:212
uint16_t getLongPressDelay()
Milliseconds for a long press event.
Definition: ButtonConfig.h:190
static const FeatureFlagType kFeatureDoubleClick
Flag to activate the AceButton::kEventDoubleClicked event.
Definition: ButtonConfig.h:111
bool isFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Check if the given features are enabled.
Definition: ButtonConfig.h:271
void setClickDelay(uint16_t clickDelay)
Set the clickDelay.
Definition: ButtonConfig.h:217
void setRepeatPressDelay(uint16_t repeatPressDelay)
Set the repeatPressDelay.
Definition: ButtonConfig.h:232
virtual int readButton(uint8_t pin)
Return the HIGH or LOW state of the button.
Definition: ButtonConfig.h:263
static const FeatureFlagType kFeatureRepeatPress
Flag to activate the AceButton::kEventRepeatPressed event.
Definition: ButtonConfig.h:117
uint16_t FeatureFlagType
Type of the feature flag.
Definition: ButtonConfig.h:101
uint16_t getRepeatPressDelay()
Milliseconds that a button needs to be Pressed down before the start of the sequence of RepeatPressed...
Definition: ButtonConfig.h:200
static const FeatureFlagType kFeatureSuppressAll
Convenience flag to suppress all suppressions.
Definition: ButtonConfig.h:150
Class that defines the timing parameters and event handler of an AceButton or a group of AceButton in...
Definition: ButtonConfig.h:60
uint16_t getClickDelay()
Milliseconds to wait for a possible click.
Definition: ButtonConfig.h:179
static ButtonConfig * getSystemButtonConfig() ACE_BUTTON_INLINE
Return a pointer to the singleton instance of the ButtonConfig which is attached to all AceButton ins...
Definition: ButtonConfig.h:314
static const FeatureFlagType kFeatureLongPress
Flag to activate the AceButton::kEventLongPress event.
Definition: ButtonConfig.h:114
void setRepeatPressInterval(uint16_t repeatPressInterval)
Set the repeatPressInterval.
Definition: ButtonConfig.h:237
void(* EventHandler)(AceButton *button, uint8_t eventType, uint8_t buttonState)
The event handler signature.
Definition: ButtonConfig.h:164
static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick
Flag to suppress kEventClicked before a kEventDoubleClicked.
Definition: ButtonConfig.h:141
static const uint16_t kDoubleClickDelay
Default value returned by getDoubleClickDelay().
Definition: ButtonConfig.h:83
void setLongPressDelay(uint16_t longPressDelay)
Set the longPressDelay.
Definition: ButtonConfig.h:227
static const uint16_t kClickDelay
Default value returned by getClickDelay().
Definition: ButtonConfig.h:80
static const FeatureFlagType kFeatureSuppressAfterLongPress
Flag to suppress kEventReleased after a kEventLongPressed.
Definition: ButtonConfig.h:130
static const uint16_t kRepeatPressDelay
Default value returned by getRepeatPressDelay().
Definition: ButtonConfig.h:89
static const uint16_t kLongPressDelay
Default value returned by getLongPressDelay().
Definition: ButtonConfig.h:86
An Adjustable Compact Event-driven (ACE) Button library that debounces and dispatches button events t...
Definition: AceButton.h:50
void setFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Enable the given features.
Definition: ButtonConfig.h:276
uint16_t getDebounceDelay()
Milliseconds to wait for debouncing.
Definition: ButtonConfig.h:176
virtual unsigned long getClockMicros()
Return the microseconds of the internal clock.
Definition: ButtonConfig.h:256
static const FeatureFlagType kFeatureSuppressAfterDoubleClick
Flag to suppress kEventReleased after a kEventDoubleClicked.
Definition: ButtonConfig.h:127
static const FeatureFlagType kFeatureSuppressAfterClick
Flag to suppress kEventReleased after a kEventClicked.
Definition: ButtonConfig.h:120
virtual unsigned long getClock()
Return the milliseconds of the internal clock.
Definition: ButtonConfig.h:250
static const FeatureFlagType kFeatureSuppressAfterRepeatPress
Flag to suppress kEventReleased after a kEventRepeatPressed.
Definition: ButtonConfig.h:133