AceButton  1.4.1
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 namespace ace_button {
31 
32 class AceButton;
33 class TimingStats;
34 
56 class ButtonConfig {
57  public:
58  // Various timing constants, in milliseconds.
59  //
60  // Note that the timing constants are stored as uint16_t (2
61  // bytes) instead of unsigned long (4 bytes) which is the type returned by
62  // the millis() system method. It turns out that we can store and perform
63  // all timing calculations using uint16_t without ill effect, as long as the
64  // polling of AceButton::check() happens more frequently than the rollover
65  // time of a uint16_t (i.e. 65.536 seconds) and certain precautions (e.g.
66  // AceButton::checkOrphanedClick()) are taken before a uint16_t rollover
67  // happens. In theory, these additional precautions would be needed even if
68  // an 'unsigned long' is used but almost no one does them because they
69  // assume that their code won't be running continuously for the rollover
70  // time of an 'unsigned long' (i.e. 49.7 days).
71 
73  static const uint16_t kDebounceDelay = 20;
74 
76  static const uint16_t kClickDelay = 200;
77 
79  static const uint16_t kDoubleClickDelay = 400;
80 
82  static const uint16_t kLongPressDelay = 1000;
83 
85  static const uint16_t kRepeatPressDelay = 1000;
86 
88  static const uint16_t kRepeatPressInterval = 200;
89 
90  // Various features controlled by feature flags.
91 
97  typedef uint16_t FeatureFlagType;
98 
100  static const FeatureFlagType kFeatureClick = 0x01;
101 
107  static const FeatureFlagType kFeatureDoubleClick = 0x02;
108 
110  static const FeatureFlagType kFeatureLongPress = 0x04;
111 
113  static const FeatureFlagType kFeatureRepeatPress = 0x08;
114 
116  static const FeatureFlagType kFeatureSuppressAfterClick = 0x10;
117 
123  static const FeatureFlagType kFeatureSuppressAfterDoubleClick = 0x20;
124 
126  static const FeatureFlagType kFeatureSuppressAfterLongPress = 0x40;
127 
129  static const FeatureFlagType kFeatureSuppressAfterRepeatPress = 0x80;
130 
137  static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick = 0x100;
138 
146  static const FeatureFlagType kFeatureSuppressAll =
147  (kFeatureSuppressAfterClick |
148  kFeatureSuppressAfterDoubleClick |
149  kFeatureSuppressAfterLongPress |
150  kFeatureSuppressAfterRepeatPress |
152 
160  typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
161  uint8_t buttonState);
162 
165 
166  // These configuration methods are virtual so that they can be overriddden.
167  // Subclasses can override at the class-level by defining a new virtual
168  // function in the subclass, or by defining an instance variable and storing
169  // the parameter with each instance of this class.
170 
172  uint16_t getDebounceDelay() { return mDebounceDelay; }
173 
175  uint16_t getClickDelay() { return mClickDelay; }
176 
181  uint16_t getDoubleClickDelay() {
182  return mDoubleClickDelay;
183  }
184 
186  uint16_t getLongPressDelay() {
187  return mLongPressDelay;
188  }
189 
196  uint16_t getRepeatPressDelay() {
197  return mRepeatPressDelay;
198  }
199 
204  return mRepeatPressInterval;
205  }
206 
208  void setDebounceDelay(uint16_t debounceDelay) {
209  mDebounceDelay = debounceDelay;
210  }
211 
213  void setClickDelay(uint16_t clickDelay) {
214  mClickDelay = clickDelay;
215  }
216 
218  void setDoubleClickDelay(uint16_t doubleClickDelay) {
219  mDoubleClickDelay = doubleClickDelay;
220  }
221 
223  void setLongPressDelay(uint16_t longPressDelay) {
224  mLongPressDelay = longPressDelay;
225  }
226 
228  void setRepeatPressDelay(uint16_t repeatPressDelay) {
229  mRepeatPressDelay = repeatPressDelay;
230  }
231 
233  void setRepeatPressInterval(uint16_t repeatPressInterval) {
234  mRepeatPressInterval = repeatPressInterval;
235  }
236 
237  // The getClock() and readButton() are external dependencies that normally
238  // would be injected using separate classes, but in the interest of saving
239  // RAM in an embedded environment, we expose them in this class instead.
240 
246  virtual unsigned long getClock() { return millis(); }
247 
252  virtual unsigned long getClockMicros() { return micros(); }
253 
259  virtual int readButton(uint8_t pin) {
260  return digitalRead(pin);
261  }
262 
263  // These methods return the various feature flags that control the
264  // functionality of the AceButton.
265 
267  bool isFeature(FeatureFlagType features) {
268  return mFeatureFlags & features;
269  }
270 
272  void setFeature(FeatureFlagType features) {
273  mFeatureFlags |= features;
274  }
275 
277  void clearFeature(FeatureFlagType features) {
278  mFeatureFlags &= ~features;
279  }
280 
281  // EventHandler
282 
285  return mEventHandler;
286  }
287 
292  void setEventHandler(EventHandler eventHandler) {
293  mEventHandler = eventHandler;
294  }
295 
296  // TimingStats
297 
299  void setTimingStats(TimingStats* timingStats) {
300  mTimingStats = timingStats;
301  }
302 
304  TimingStats* getTimingStats() { return mTimingStats; }
305 
311  return &sSystemButtonConfig;
312  }
313 
314  protected:
319  virtual void init() {
320  mFeatureFlags = 0;
321  mTimingStats = nullptr;
322  }
323 
324  private:
329  static ButtonConfig sSystemButtonConfig;
330 
331  // Disable copy-constructor and assignment operator
332  ButtonConfig(const ButtonConfig&) = delete;
333  ButtonConfig& operator=(const ButtonConfig&) = delete;
334 
336  EventHandler mEventHandler = nullptr;
337 
339  TimingStats* mTimingStats = nullptr;
340 
342  FeatureFlagType mFeatureFlags = 0;
343 
344  uint16_t mDebounceDelay = kDebounceDelay;
345  uint16_t mClickDelay = kClickDelay;
346  uint16_t mDoubleClickDelay = kDoubleClickDelay;
347  uint16_t mLongPressDelay = kLongPressDelay;
348  uint16_t mRepeatPressDelay = kRepeatPressDelay;
349  uint16_t mRepeatPressInterval = kRepeatPressInterval;
350 };
351 
352 }
353 #endif
uint16_t getRepeatPressInterval()
Milliseconds between two successive RepeatPressed events.
Definition: ButtonConfig.h:203
static const uint16_t kRepeatPressInterval
Default value returned by getRepeatPressInterval().
Definition: ButtonConfig.h:88
static const uint16_t kDebounceDelay
Default value returned by getDebounceDelay().
Definition: ButtonConfig.h:73
void setDoubleClickDelay(uint16_t doubleClickDelay)
Set the doubleClickDelay.
Definition: ButtonConfig.h:218
virtual void init()
Initialize to its pristine state, except for the EventHandler which is unchanged. ...
Definition: ButtonConfig.h:319
static const FeatureFlagType kFeatureClick
Flag to activate the AceButton::kEventClicked event.
Definition: ButtonConfig.h:100
ButtonConfig()
Constructor.
Definition: ButtonConfig.h:164
uint16_t getDoubleClickDelay()
Milliseconds between the first and second click to register as a double-click.
Definition: ButtonConfig.h:181
TimingStats * getTimingStats()
Get the timing stats.
Definition: ButtonConfig.h:304
void setTimingStats(TimingStats *timingStats)
Set the timing stats object.
Definition: ButtonConfig.h:299
void setDebounceDelay(uint16_t debounceDelay)
Set the debounceDelay.
Definition: ButtonConfig.h:208
void setEventHandler(EventHandler eventHandler)
Install the event handler.
Definition: ButtonConfig.h:292
uint16_t getLongPressDelay()
Milliseconds for a long press event.
Definition: ButtonConfig.h:186
static const FeatureFlagType kFeatureDoubleClick
Flag to activate the AceButton::kEventDoubleClicked event.
Definition: ButtonConfig.h:107
void setClickDelay(uint16_t clickDelay)
Set the clickDelay.
Definition: ButtonConfig.h:213
void setRepeatPressDelay(uint16_t repeatPressDelay)
Set the repeatPressDelay.
Definition: ButtonConfig.h:228
virtual int readButton(uint8_t pin)
Return the HIGH or LOW state of the button.
Definition: ButtonConfig.h:259
static const FeatureFlagType kFeatureRepeatPress
Flag to activate the AceButton::kEventRepeatPressed event.
Definition: ButtonConfig.h:113
uint16_t FeatureFlagType
Type of the feature flag.
Definition: ButtonConfig.h:97
void clearFeature(FeatureFlagType features)
Disable the given features.
Definition: ButtonConfig.h:277
uint16_t getRepeatPressDelay()
Milliseconds that a button needs to be Pressed down before the start of the sequence of RepeatPressed...
Definition: ButtonConfig.h:196
static const FeatureFlagType kFeatureSuppressAll
Convenience flag to suppress all suppressions.
Definition: ButtonConfig.h:146
Class that defines the timing parameters and event handler of an AceButton or a group of AceButton in...
Definition: ButtonConfig.h:56
uint16_t getClickDelay()
Milliseconds to wait for a possible click.
Definition: ButtonConfig.h:175
static const FeatureFlagType kFeatureLongPress
Flag to activate the AceButton::kEventLongPress event.
Definition: ButtonConfig.h:110
EventHandler getEventHandler()
Return the eventHandler.
Definition: ButtonConfig.h:284
void setRepeatPressInterval(uint16_t repeatPressInterval)
Set the repeatPressInterval.
Definition: ButtonConfig.h:233
void(* EventHandler)(AceButton *button, uint8_t eventType, uint8_t buttonState)
The event handler signature.
Definition: ButtonConfig.h:160
static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick
Flag to suppress kEventClicked before a kEventDoubleClicked.
Definition: ButtonConfig.h:137
static const uint16_t kDoubleClickDelay
Default value returned by getDoubleClickDelay().
Definition: ButtonConfig.h:79
void setLongPressDelay(uint16_t longPressDelay)
Set the longPressDelay.
Definition: ButtonConfig.h:223
static const uint16_t kClickDelay
Default value returned by getClickDelay().
Definition: ButtonConfig.h:76
static const FeatureFlagType kFeatureSuppressAfterLongPress
Flag to suppress kEventReleased after a kEventLongPressed.
Definition: ButtonConfig.h:126
static const uint16_t kRepeatPressDelay
Default value returned by getRepeatPressDelay().
Definition: ButtonConfig.h:85
static const uint16_t kLongPressDelay
Default value returned by getLongPressDelay().
Definition: ButtonConfig.h:82
An Adjustable Compact Event-driven (ACE) Button library that debounces and dispatches button events t...
Definition: AceButton.h:50
uint16_t getDebounceDelay()
Milliseconds to wait for debouncing.
Definition: ButtonConfig.h:172
virtual unsigned long getClockMicros()
Return the microseconds of the internal clock.
Definition: ButtonConfig.h:252
static const FeatureFlagType kFeatureSuppressAfterDoubleClick
Flag to suppress kEventReleased after a kEventDoubleClicked.
Definition: ButtonConfig.h:123
static ButtonConfig * getSystemButtonConfig()
Return a pointer to the singleton instance of the ButtonConfig which is attached to all AceButton ins...
Definition: ButtonConfig.h:310
bool isFeature(FeatureFlagType features)
Check if the given features are enabled.
Definition: ButtonConfig.h:267
static const FeatureFlagType kFeatureSuppressAfterClick
Flag to suppress kEventReleased after a kEventClicked.
Definition: ButtonConfig.h:116
void setFeature(FeatureFlagType features)
Enable the given features.
Definition: ButtonConfig.h:272
virtual unsigned long getClock()
Return the milliseconds of the internal clock.
Definition: ButtonConfig.h:246
static const FeatureFlagType kFeatureSuppressAfterRepeatPress
Flag to suppress kEventReleased after a kEventRepeatPressed.
Definition: ButtonConfig.h:129