AceButton  1.1.0
An adjustable, compact, event-driven (ACE) 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 
68 class ButtonConfig {
69  public:
70  // Various timing constants, in milliseconds.
71  //
72  // Note that the timing constants are stored as uint16_t (2
73  // bytes) instead of unsigned long (4 bytes) which is the type returned by
74  // the millis() system method. It turns out that we can store and perform
75  // all timing calculations using uint16_t without ill effect, as long as the
76  // polling of AceButton::check() happens more frequently than the rollover
77  // time of a uint16_t (i.e. 65.536 seconds) and certain precautions (e.g.
78  // AceButton::checkOrphanedClick()) are taken before a uint16_t rollover
79  // happens. In theory, these additional precautions would be needed even if
80  // an 'unsigned long' is used but almost no one does them because they
81  // assume that their code won't be running continuously for the rollover
82  // time of an 'unsigned long' (i.e. 49.7 days).
83 
85  static const uint16_t kDebounceDelay = 50;
86 
88  static const uint16_t kClickDelay = 200;
89 
91  static const uint16_t kDoubleClickDelay = 400;
92 
94  static const uint16_t kLongPressDelay = 1000;
95 
97  static const uint16_t kRepeatPressDelay = 1000;
98 
100  static const uint16_t kRepeatPressInterval = 200;
101 
102  // Various features controlled by feature flags.
103 
109  typedef uint16_t FeatureFlagType;
110 
112  static const FeatureFlagType kFeatureClick = 0x01;
113 
119  static const FeatureFlagType kFeatureDoubleClick = 0x02;
120 
122  static const FeatureFlagType kFeatureLongPress = 0x04;
123 
125  static const FeatureFlagType kFeatureRepeatPress = 0x08;
126 
128  static const FeatureFlagType kFeatureSuppressAfterClick = 0x10;
129 
135  static const FeatureFlagType kFeatureSuppressAfterDoubleClick = 0x20;
136 
138  static const FeatureFlagType kFeatureSuppressAfterLongPress = 0x40;
139 
141  static const FeatureFlagType kFeatureSuppressAfterRepeatPress = 0x80;
142 
149  static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick = 0x100;
150 
158  static const FeatureFlagType kFeatureSuppressAll =
159  (kFeatureSuppressAfterClick |
160  kFeatureSuppressAfterDoubleClick |
161  kFeatureSuppressAfterLongPress |
162  kFeatureSuppressAfterRepeatPress |
164 
172  typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
173  uint8_t buttonState);
174 
176  ButtonConfig();
177 
178  // These configuration methods are virtual so that they can be overriddden.
179  // Subclasses can override at the class-level by defining a new virtual
180  // function in the subclass, or by defining an instance variable and storing
181  // the parameter with each instance of this class.
182 
184  virtual uint16_t getDebounceDelay() { return kDebounceDelay; }
185 
187  virtual uint16_t getClickDelay() { return kClickDelay; }
188 
193  virtual uint16_t getDoubleClickDelay() {
194  return kDoubleClickDelay;
195  }
196 
198  virtual uint16_t getLongPressDelay() {
199  return kLongPressDelay;
200  }
201 
208  virtual uint16_t getRepeatPressDelay() {
209  return kRepeatPressDelay;
210  }
211 
215  virtual uint16_t getRepeatPressInterval() {
216  return kRepeatPressInterval;
217  }
218 
219  // The getClock() and readButton() are external dependencies that normally
220  // would be injected using separate classes, but in the interest of saving
221  // RAM in an embedded environment, we expose them in this class instead.
222 
228  virtual unsigned long getClock() { return millis(); }
229 
234  virtual unsigned long getClockMicros() { return micros(); }
235 
241  virtual int readButton(uint8_t pin) {
242  return digitalRead(pin);
243  }
244 
245  // These methods return the various feature flags that control the
246  // functionality of the AceButton.
247 
249  bool isFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
250  return mFeatureFlags & features;
251  }
252 
254  void setFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
255  mFeatureFlags |= features;
256  }
257 
259  void clearFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
260  mFeatureFlags &= ~features;
261  }
262 
263  // EventHandler
264 
266  EventHandler getEventHandler() ACE_BUTTON_INLINE {
267  return mEventHandler;
268  }
269 
274  void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE {
275  mEventHandler = eventHandler;
276  }
277 
278  // TimingStats
279 
281  void setTimingStats(TimingStats* timingStats) {
282  mTimingStats = timingStats;
283  }
284 
286  TimingStats* getTimingStats() { return mTimingStats; }
287 
292  static ButtonConfig* getSystemButtonConfig() ACE_BUTTON_INLINE {
293  return &sSystemButtonConfig;
294  }
295 
296  protected:
301  virtual void init() {
302  mFeatureFlags = 0;
303  mTimingStats = nullptr;
304  }
305 
306  private:
307  // Disable copy-constructor and assignment operator
308  ButtonConfig(const ButtonConfig&) = delete;
309  ButtonConfig& operator=(const ButtonConfig&) = delete;
310 
312  EventHandler mEventHandler;
313 
315  FeatureFlagType mFeatureFlags;
316 
318  TimingStats* mTimingStats;
319 
324  static ButtonConfig sSystemButtonConfig;
325 };
326 
327 }
328 #endif
static const uint16_t kRepeatPressInterval
Default value returned by getRepeatPressInterval().
Definition: ButtonConfig.h:100
static const uint16_t kDebounceDelay
Default value returned by getDebounceDelay().
Definition: ButtonConfig.h:85
virtual uint16_t getDoubleClickDelay()
Milliseconds between the first and second click to register as a double-click.
Definition: ButtonConfig.h:193
virtual void init()
Initialize to its pristine state, except for the EventHandler which is unchanged. ...
Definition: ButtonConfig.h:301
virtual uint16_t getRepeatPressDelay()
Milliseconds that a button needs to be Pressed down before the start of the sequence of RepeatPressed...
Definition: ButtonConfig.h:208
static const FeatureFlagType kFeatureClick
Flag to activate the AceButton::kEventClicked event.
Definition: ButtonConfig.h:112
ButtonConfig()
Constructor.
EventHandler getEventHandler() ACE_BUTTON_INLINE
Return the eventHandler.
Definition: ButtonConfig.h:266
TimingStats * getTimingStats()
Get the timing stats.
Definition: ButtonConfig.h:286
void clearFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Disable the given features.
Definition: ButtonConfig.h:259
void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE
Install the event handler.
Definition: ButtonConfig.h:274
void setTimingStats(TimingStats *timingStats)
Set the timing stats object.
Definition: ButtonConfig.h:281
virtual uint16_t getClickDelay()
Milliseconds to wait for a possible click.
Definition: ButtonConfig.h:187
static const FeatureFlagType kFeatureDoubleClick
Flag to activate the AceButton::kEventDoubleClicked event.
Definition: ButtonConfig.h:119
bool isFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Check if the given features are enabled.
Definition: ButtonConfig.h:249
virtual int readButton(uint8_t pin)
Return the HIGH or LOW state of the button.
Definition: ButtonConfig.h:241
static const FeatureFlagType kFeatureRepeatPress
Flag to activate the AceButton::kEventRepeatPressed event.
Definition: ButtonConfig.h:125
uint16_t FeatureFlagType
Type of the feature flag.
Definition: ButtonConfig.h:109
virtual uint16_t getLongPressDelay()
Milliseconds for a long press event.
Definition: ButtonConfig.h:198
static const FeatureFlagType kFeatureSuppressAll
Convenience flag to suppress all suppressions.
Definition: ButtonConfig.h:158
Class that defines the timing parameters and event handler of an AceButton or a group of AceButton in...
Definition: ButtonConfig.h:68
virtual uint16_t getRepeatPressInterval()
Milliseconds between two successive RepeatPressed events.
Definition: ButtonConfig.h:215
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:292
static const FeatureFlagType kFeatureLongPress
Flag to activate the AceButton::kEventLongPress event.
Definition: ButtonConfig.h:122
void(* EventHandler)(AceButton *button, uint8_t eventType, uint8_t buttonState)
The event handler signature.
Definition: ButtonConfig.h:172
static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick
Flag to suppress kEventClicked before a kEventDoubleClicked.
Definition: ButtonConfig.h:149
static const uint16_t kDoubleClickDelay
Default value returned by getDoubleClickDelay().
Definition: ButtonConfig.h:91
static const uint16_t kClickDelay
Default value returned by getClickDelay().
Definition: ButtonConfig.h:88
static const FeatureFlagType kFeatureSuppressAfterLongPress
Flag to suppress kEventReleased after a kEventLongPressed.
Definition: ButtonConfig.h:138
static const uint16_t kRepeatPressDelay
Default value returned by getRepeatPressDelay().
Definition: ButtonConfig.h:97
virtual uint16_t getDebounceDelay()
Milliseconds to wait for debouncing.
Definition: ButtonConfig.h:184
static const uint16_t kLongPressDelay
Default value returned by getLongPressDelay().
Definition: ButtonConfig.h:94
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:254
virtual unsigned long getClockMicros()
Return the microseconds of the internal clock.
Definition: ButtonConfig.h:234
static const FeatureFlagType kFeatureSuppressAfterDoubleClick
Flag to suppress kEventReleased after a kEventDoubleClicked.
Definition: ButtonConfig.h:135
static const FeatureFlagType kFeatureSuppressAfterClick
Flag to suppress kEventReleased after a kEventClicked.
Definition: ButtonConfig.h:128
virtual unsigned long getClock()
Return the milliseconds of the internal clock.
Definition: ButtonConfig.h:228
static const FeatureFlagType kFeatureSuppressAfterRepeatPress
Flag to suppress kEventReleased after a kEventRepeatPressed.
Definition: ButtonConfig.h:141