AceButton  1.5
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 
55 class ButtonConfig {
56  public:
57  // Various timing constants, in milliseconds.
58  //
59  // Note that the timing constants are stored as uint16_t (2
60  // bytes) instead of unsigned long (4 bytes) which is the type returned by
61  // the millis() system method. It turns out that we can store and perform
62  // all timing calculations using uint16_t without ill effect, as long as the
63  // polling of AceButton::check() happens more frequently than the rollover
64  // time of a uint16_t (i.e. 65.536 seconds) and certain precautions (e.g.
65  // AceButton::checkOrphanedClick()) are taken before a uint16_t rollover
66  // happens. In theory, these additional precautions would be needed even if
67  // an 'unsigned long' is used but almost no one does them because they
68  // assume that their code won't be running continuously for the rollover
69  // time of an 'unsigned long' (i.e. 49.7 days).
70 
72  static const uint16_t kDebounceDelay = 20;
73 
75  static const uint16_t kClickDelay = 200;
76 
78  static const uint16_t kDoubleClickDelay = 400;
79 
81  static const uint16_t kLongPressDelay = 1000;
82 
84  static const uint16_t kRepeatPressDelay = 1000;
85 
87  static const uint16_t kRepeatPressInterval = 200;
88 
89  // Various features controlled by feature flags.
90 
96  typedef uint16_t FeatureFlagType;
97 
99  static const FeatureFlagType kFeatureClick = 0x01;
100 
106  static const FeatureFlagType kFeatureDoubleClick = 0x02;
107 
109  static const FeatureFlagType kFeatureLongPress = 0x04;
110 
112  static const FeatureFlagType kFeatureRepeatPress = 0x08;
113 
115  static const FeatureFlagType kFeatureSuppressAfterClick = 0x10;
116 
122  static const FeatureFlagType kFeatureSuppressAfterDoubleClick = 0x20;
123 
125  static const FeatureFlagType kFeatureSuppressAfterLongPress = 0x40;
126 
128  static const FeatureFlagType kFeatureSuppressAfterRepeatPress = 0x80;
129 
136  static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick = 0x100;
137 
145  static const FeatureFlagType kFeatureSuppressAll =
146  (kFeatureSuppressAfterClick |
147  kFeatureSuppressAfterDoubleClick |
148  kFeatureSuppressAfterLongPress |
149  kFeatureSuppressAfterRepeatPress |
151 
159  typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
160  uint8_t buttonState);
161 
163  ButtonConfig() = default;
164 
165  #if defined(ESP8266) || defined(ESP32)
166 
178  virtual ~ButtonConfig() = default;
179  #endif
180 
182  uint16_t getDebounceDelay() { return mDebounceDelay; }
183 
185  uint16_t getClickDelay() { return mClickDelay; }
186 
191  uint16_t getDoubleClickDelay() {
192  return mDoubleClickDelay;
193  }
194 
196  uint16_t getLongPressDelay() {
197  return mLongPressDelay;
198  }
199 
206  uint16_t getRepeatPressDelay() {
207  return mRepeatPressDelay;
208  }
209 
214  return mRepeatPressInterval;
215  }
216 
218  void setDebounceDelay(uint16_t debounceDelay) {
219  mDebounceDelay = debounceDelay;
220  }
221 
223  void setClickDelay(uint16_t clickDelay) {
224  mClickDelay = clickDelay;
225  }
226 
228  void setDoubleClickDelay(uint16_t doubleClickDelay) {
229  mDoubleClickDelay = doubleClickDelay;
230  }
231 
233  void setLongPressDelay(uint16_t longPressDelay) {
234  mLongPressDelay = longPressDelay;
235  }
236 
238  void setRepeatPressDelay(uint16_t repeatPressDelay) {
239  mRepeatPressDelay = repeatPressDelay;
240  }
241 
243  void setRepeatPressInterval(uint16_t repeatPressInterval) {
244  mRepeatPressInterval = repeatPressInterval;
245  }
246 
247  // The getClock() and readButton() are external dependencies that normally
248  // would be injected using separate classes, but in the interest of saving
249  // RAM in an embedded environment, we expose them in this class instead.
250 
256  virtual unsigned long getClock() { return millis(); }
257 
263  virtual int readButton(uint8_t pin) {
264  return digitalRead(pin);
265  }
266 
267  // These methods provide access to various feature flags that control the
268  // functionality of the AceButton.
269 
271  bool isFeature(FeatureFlagType features) {
272  return mFeatureFlags & features;
273  }
274 
276  void setFeature(FeatureFlagType features) {
277  mFeatureFlags |= features;
278  }
279 
281  void clearFeature(FeatureFlagType features) {
282  mFeatureFlags &= ~features;
283  }
284 
289  void resetFeatures() {
290  mFeatureFlags = 0;
291  }
292 
293  // EventHandler
294 
297  return mEventHandler;
298  }
299 
304  void setEventHandler(EventHandler eventHandler) {
305  mEventHandler = eventHandler;
306  }
307 
313  return &sSystemButtonConfig;
314  }
315 
316  private:
321  static ButtonConfig sSystemButtonConfig;
322 
323  // Disable copy-constructor and assignment operator
324  ButtonConfig(const ButtonConfig&) = delete;
325  ButtonConfig& operator=(const ButtonConfig&) = delete;
326 
328  EventHandler mEventHandler = nullptr;
329 
331  FeatureFlagType mFeatureFlags = 0;
332 
333  uint16_t mDebounceDelay = kDebounceDelay;
334  uint16_t mClickDelay = kClickDelay;
335  uint16_t mDoubleClickDelay = kDoubleClickDelay;
336  uint16_t mLongPressDelay = kLongPressDelay;
337  uint16_t mRepeatPressDelay = kRepeatPressDelay;
338  uint16_t mRepeatPressInterval = kRepeatPressInterval;
339 };
340 
341 }
342 #endif
uint16_t getRepeatPressInterval()
Milliseconds between two successive RepeatPressed events.
Definition: ButtonConfig.h:213
static const uint16_t kRepeatPressInterval
Default value returned by getRepeatPressInterval().
Definition: ButtonConfig.h:87
static const uint16_t kDebounceDelay
Default value returned by getDebounceDelay().
Definition: ButtonConfig.h:72
void setDoubleClickDelay(uint16_t doubleClickDelay)
Set the doubleClickDelay.
Definition: ButtonConfig.h:228
static const FeatureFlagType kFeatureClick
Flag to activate the AceButton::kEventClicked event.
Definition: ButtonConfig.h:99
uint16_t getDoubleClickDelay()
Milliseconds between the first and second click to register as a double-click.
Definition: ButtonConfig.h:191
void setDebounceDelay(uint16_t debounceDelay)
Set the debounceDelay.
Definition: ButtonConfig.h:218
void setEventHandler(EventHandler eventHandler)
Install the event handler.
Definition: ButtonConfig.h:304
uint16_t getLongPressDelay()
Milliseconds for a long press event.
Definition: ButtonConfig.h:196
static const FeatureFlagType kFeatureDoubleClick
Flag to activate the AceButton::kEventDoubleClicked event.
Definition: ButtonConfig.h:106
void setClickDelay(uint16_t clickDelay)
Set the clickDelay.
Definition: ButtonConfig.h:223
void setRepeatPressDelay(uint16_t repeatPressDelay)
Set the repeatPressDelay.
Definition: ButtonConfig.h:238
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:112
uint16_t FeatureFlagType
Type of the feature flag.
Definition: ButtonConfig.h:96
void clearFeature(FeatureFlagType features)
Disable the given features.
Definition: ButtonConfig.h:281
uint16_t getRepeatPressDelay()
Milliseconds that a button needs to be Pressed down before the start of the sequence of RepeatPressed...
Definition: ButtonConfig.h:206
static const FeatureFlagType kFeatureSuppressAll
Convenience flag to suppress all suppressions.
Definition: ButtonConfig.h:145
Class that defines the timing parameters and event handler of an AceButton or a group of AceButton in...
Definition: ButtonConfig.h:55
uint16_t getClickDelay()
Milliseconds to wait for a possible click.
Definition: ButtonConfig.h:185
static const FeatureFlagType kFeatureLongPress
Flag to activate the AceButton::kEventLongPress event.
Definition: ButtonConfig.h:109
EventHandler getEventHandler()
Return the eventHandler.
Definition: ButtonConfig.h:296
ButtonConfig()=default
Constructor.
void setRepeatPressInterval(uint16_t repeatPressInterval)
Set the repeatPressInterval.
Definition: ButtonConfig.h:243
void(* EventHandler)(AceButton *button, uint8_t eventType, uint8_t buttonState)
The event handler signature.
Definition: ButtonConfig.h:159
static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick
Flag to suppress kEventClicked before a kEventDoubleClicked.
Definition: ButtonConfig.h:136
static const uint16_t kDoubleClickDelay
Default value returned by getDoubleClickDelay().
Definition: ButtonConfig.h:78
void setLongPressDelay(uint16_t longPressDelay)
Set the longPressDelay.
Definition: ButtonConfig.h:233
static const uint16_t kClickDelay
Default value returned by getClickDelay().
Definition: ButtonConfig.h:75
static const FeatureFlagType kFeatureSuppressAfterLongPress
Flag to suppress kEventReleased after a kEventLongPressed.
Definition: ButtonConfig.h:125
static const uint16_t kRepeatPressDelay
Default value returned by getRepeatPressDelay().
Definition: ButtonConfig.h:84
static const uint16_t kLongPressDelay
Default value returned by getLongPressDelay().
Definition: ButtonConfig.h:81
void resetFeatures()
Disable all features.
Definition: ButtonConfig.h:289
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:182
static const FeatureFlagType kFeatureSuppressAfterDoubleClick
Flag to suppress kEventReleased after a kEventDoubleClicked.
Definition: ButtonConfig.h:122
static ButtonConfig * getSystemButtonConfig()
Return a pointer to the singleton instance of the ButtonConfig which is attached to all AceButton ins...
Definition: ButtonConfig.h:312
bool isFeature(FeatureFlagType features)
Check if the given features are enabled.
Definition: ButtonConfig.h:271
static const FeatureFlagType kFeatureSuppressAfterClick
Flag to suppress kEventReleased after a kEventClicked.
Definition: ButtonConfig.h:115
void setFeature(FeatureFlagType features)
Enable the given features.
Definition: ButtonConfig.h:276
virtual unsigned long getClock()
Return the milliseconds of the internal clock.
Definition: ButtonConfig.h:256
static const FeatureFlagType kFeatureSuppressAfterRepeatPress
Flag to suppress kEventReleased after a kEventRepeatPressed.
Definition: ButtonConfig.h:128