AceButton  1.0.6
An Adjustable Compact Event-driven (ACE) button library for Arduino.
ButtonConfig.h
1 /*
2  Copyright 2018 Brian T. Park
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef BUTTON_CONFIG_H
18 #define BUTTON_CONFIG_H
19 
20 #include <Arduino.h>
21 
22 // TODO: Verify if this is actually needed. The program size seems to be
23 // identical with or without it on the Arduino IDE (which uses gcc).
24 #define ACE_BUTTON_INLINE __attribute__((always_inline))
25 
26 namespace ace_button {
27 
28 // forward declare the AceButton
29 class AceButton;
30 
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 = 50;
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 
112 
114  static const FeatureFlagType kFeatureLongPress = 0x04;
115 
118 
121 
128 
131 
134 
142 
156 
164  typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
165  uint8_t buttonState);
166 
168  ButtonConfig();
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  virtual uint16_t getDebounceDelay() { return kDebounceDelay; }
177 
179  virtual uint16_t getClickDelay() { return kClickDelay; }
180 
185  virtual uint16_t getDoubleClickDelay() {
186  return kDoubleClickDelay;
187  }
188 
190  virtual uint16_t getLongPressDelay() {
191  return kLongPressDelay;
192  }
193 
200  virtual uint16_t getRepeatPressDelay() {
201  return kRepeatPressDelay;
202  }
203 
207  virtual uint16_t getRepeatPressInterval() {
208  return kRepeatPressInterval;
209  }
210 
211  // The getClock() and readButton() are external dependencies that normally
212  // would be injected using separate classes, but in the interest of saving
213  // RAM in an embedded environment, we expose them in this class instead.
214 
220  virtual unsigned long getClock() { return millis(); }
221 
227  virtual int readButton(uint8_t pin) {
228  return digitalRead(pin);
229  }
230 
231  // These methods return the various feature flags that control the
232  // functionality of the AceButton.
233 
235  bool isFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
236  return mFeatureFlags & features;
237  }
238 
240  void setFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
241  mFeatureFlags |= features;
242  }
243 
245  void clearFeature(FeatureFlagType features) ACE_BUTTON_INLINE {
246  mFeatureFlags &= ~features;
247  }
248 
250  EventHandler getEventHandler() ACE_BUTTON_INLINE {
251  return mEventHandler;
252  }
253 
258  void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE {
259  mEventHandler = eventHandler;
260  }
261 
266  static ButtonConfig* getSystemButtonConfig() ACE_BUTTON_INLINE {
267  return &sSystemButtonConfig;
268  }
269 
270  protected:
275  virtual void init() { mFeatureFlags = 0; }
276 
277  private:
278  // Disable copy-constructor and assignment operator
279  ButtonConfig(const ButtonConfig&) = delete;
280  ButtonConfig& operator=(const ButtonConfig&) = delete;
281 
283  EventHandler mEventHandler;
284 
286  FeatureFlagType mFeatureFlags;
287 
292  static ButtonConfig sSystemButtonConfig;
293 };
294 
295 }
296 #endif
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
virtual uint16_t getDoubleClickDelay()
Milliseconds between the first and second click to register as a double-click.
Definition: ButtonConfig.h:185
virtual void init()
Initialize to its pristine state, except for the EventHandler which is unchanged. ...
Definition: ButtonConfig.h:275
virtual 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 kFeatureClick
Flag to activate the AceButton::kEventClicked event.
Definition: ButtonConfig.h:104
ButtonConfig()
Constructor.
EventHandler getEventHandler() ACE_BUTTON_INLINE
Return the eventHandler.
Definition: ButtonConfig.h:250
void clearFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Disable the given features.
Definition: ButtonConfig.h:245
void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE
Install the event handler.
Definition: ButtonConfig.h:258
virtual uint16_t getClickDelay()
Milliseconds to wait for a possible click.
Definition: ButtonConfig.h:179
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:235
virtual int readButton(uint8_t pin)
Return the HIGH or LOW state of the button.
Definition: ButtonConfig.h:227
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
virtual uint16_t getLongPressDelay()
Milliseconds for a long press event.
Definition: ButtonConfig.h:190
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
virtual uint16_t getRepeatPressInterval()
Milliseconds between two successive RepeatPressed events.
Definition: ButtonConfig.h:207
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:266
static const FeatureFlagType kFeatureLongPress
Flag to activate the AceButton::kEventLongPress event.
Definition: ButtonConfig.h:114
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
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
virtual uint16_t getDebounceDelay()
Milliseconds to wait for debouncing.
Definition: ButtonConfig.h:176
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:42
void setFeature(FeatureFlagType features) ACE_BUTTON_INLINE
Enable the given features.
Definition: ButtonConfig.h:240
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:220
static const FeatureFlagType kFeatureSuppressAfterRepeatPress
Flag to suppress kEventReleased after a kEventRepeatPressed.
Definition: ButtonConfig.h:133