AceButton  1.0.0
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 
97  static const uint8_t kFeatureClick = 0x01;
98 
104  static const uint8_t kFeatureDoubleClick = 0x02;
105 
107  static const uint8_t kFeatureLongPress = 0x04;
108 
110  static const uint8_t kFeatureRepeatPress = 0x08;
111 
113  static const uint8_t kFeatureSuppressAfterClick = 0x10;
114 
120  static const uint8_t kFeatureSuppressAfterDoubleClick = 0x20;
121 
123  static const uint8_t kFeatureSuppressAfterLongPress = 0x40;
124 
126  static const uint8_t kFeatureSuppressAfterRepeatPress = 0x80;
127 
135  static const uint8_t kFeatureSuppressAll =
136  (kFeatureSuppressAfterClick |
137  kFeatureSuppressAfterDoubleClick |
138  kFeatureSuppressAfterLongPress |
140 
148  typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
149  uint8_t buttonState);
150 
152  ButtonConfig();
153 
154  // These configuration methods are virtual so that they can be overriddden.
155  // Subclasses can override at the class-level by defining a new virtual
156  // function in the subclass, or by defining an instance variable and storing
157  // the parameter with each instance of this class.
158 
160  virtual uint16_t getDebounceDelay() { return kDebounceDelay; }
161 
163  virtual uint16_t getClickDelay() { return kClickDelay; }
164 
169  virtual uint16_t getDoubleClickDelay() {
170  return kDoubleClickDelay;
171  }
172 
174  virtual uint16_t getLongPressDelay() {
175  return kLongPressDelay;
176  }
177 
184  virtual uint16_t getRepeatPressDelay() {
185  return kRepeatPressDelay;
186  }
187 
191  virtual uint16_t getRepeatPressInterval() {
192  return kRepeatPressInterval;
193  }
194 
195  // The getClock() and readButton() are external dependencies that normally
196  // would be injected using separate classes, but in the interest of saving
197  // RAM in an embedded environment, we expose them in this class instead.
198 
204  virtual unsigned long getClock() { return millis(); }
205 
211  virtual int readButton(uint8_t pin) {
212  return digitalRead(pin);
213  }
214 
215  // These methods return the various feature flags that control the
216  // functionality of the AceButton.
217 
219  bool isFeature(uint8_t features) ACE_BUTTON_INLINE {
220  return mFeatureFlags & features;
221  }
222 
224  void setFeature(uint8_t features) ACE_BUTTON_INLINE {
225  mFeatureFlags |= features;
226  }
227 
229  void clearFeature(uint8_t features) ACE_BUTTON_INLINE {
230  mFeatureFlags &= ~features;
231  }
232 
234  EventHandler getEventHandler() ACE_BUTTON_INLINE {
235  return mEventHandler;
236  }
237 
242  void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE {
243  mEventHandler = eventHandler;
244  }
245 
250  static ButtonConfig* getSystemButtonConfig() ACE_BUTTON_INLINE {
251  return &sSystemButtonConfig;
252  }
253 
254  protected:
259  virtual void init() { mFeatureFlags = 0; }
260 
261  private:
262  // Disable copy-constructor and assignment operator
263  ButtonConfig(const ButtonConfig&) = delete;
264  ButtonConfig& operator=(const ButtonConfig&) = delete;
265 
267  EventHandler mEventHandler;
268 
270  uint8_t mFeatureFlags;
271 
276  static ButtonConfig sSystemButtonConfig;
277 };
278 
279 }
280 #endif
static const uint8_t kFeatureClick
Flag to activate the AceButton::kEventClicked event.
Definition: ButtonConfig.h:97
static const uint8_t kFeatureDoubleClick
Flag to activate the AceButton::kEventDoubleClicked event.
Definition: ButtonConfig.h:104
static const uint16_t kRepeatPressInterval
Default value returned by getRepeatPressInterval().
Definition: ButtonConfig.h:92
static const uint8_t kFeatureSuppressAll
Convenience flag to suppress all suppressions.
Definition: ButtonConfig.h:135
static const uint16_t kDebounceDelay
Default value returned by getDebounceDelay().
Definition: ButtonConfig.h:77
static const uint8_t kFeatureSuppressAfterRepeatPress
Flag to suppress kEventReleased after a kEventRepeatPressed.
Definition: ButtonConfig.h:126
virtual uint16_t getDoubleClickDelay()
Milliseconds between the first and second click to register as a double-click.
Definition: ButtonConfig.h:169
virtual void init()
Initialize to its pristine state, except for the EventHandler which is unchanged. ...
Definition: ButtonConfig.h:259
virtual uint16_t getRepeatPressDelay()
Milliseconds that a button needs to be Pressed down before the start of the sequence of RepeatPressed...
Definition: ButtonConfig.h:184
EventHandler getEventHandler() ACE_BUTTON_INLINE
Return the eventHandler.
Definition: ButtonConfig.h:234
void setFeature(uint8_t features) ACE_BUTTON_INLINE
Enable the given features.
Definition: ButtonConfig.h:224
void setEventHandler(EventHandler eventHandler) ACE_BUTTON_INLINE
Install the event handler.
Definition: ButtonConfig.h:242
virtual uint16_t getClickDelay()
Milliseconds to wait for a possible click.
Definition: ButtonConfig.h:163
static const uint8_t kFeatureSuppressAfterClick
Flag to suppress kEventReleased after a kEventClicked.
Definition: ButtonConfig.h:113
static const uint8_t kFeatureRepeatPress
Flag to activate the AceButton::kEventRepeatPressed event.
Definition: ButtonConfig.h:110
static const uint8_t kFeatureSuppressAfterLongPress
Flag to suppress kEventReleased after a kEventLongPressed.
Definition: ButtonConfig.h:123
static const uint8_t kFeatureSuppressAfterDoubleClick
Flag to suppress kEventReleased after a kEventDoubleClicked.
Definition: ButtonConfig.h:120
virtual int readButton(uint8_t pin)
Return the HIGH or LOW state of the button.
Definition: ButtonConfig.h:211
virtual uint16_t getLongPressDelay()
Milliseconds for a long press event.
Definition: ButtonConfig.h:174
Definition: AceButton.h:23
Class that defines the timing parameters and event handler of an AceButton or a group of AceButton in...
Definition: ButtonConfig.h:60
void clearFeature(uint8_t features) ACE_BUTTON_INLINE
Disable the given features.
Definition: ButtonConfig.h:229
virtual uint16_t getRepeatPressInterval()
Milliseconds between two successive RepeatPressed events.
Definition: ButtonConfig.h:191
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:250
void(* EventHandler)(AceButton *button, uint8_t eventType, uint8_t buttonState)
The event handler signature.
Definition: ButtonConfig.h:148
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 uint16_t kRepeatPressDelay
Default value returned by getRepeatPressDelay().
Definition: ButtonConfig.h:89
virtual uint16_t getDebounceDelay()
Milliseconds to wait for debouncing.
Definition: ButtonConfig.h:160
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
ButtonConfig()
Constructor.
Definition: ButtonConfig.cpp:24
static const uint8_t kFeatureLongPress
Flag to activate the AceButton::kEventLongPress event.
Definition: ButtonConfig.h:107
bool isFeature(uint8_t features) ACE_BUTTON_INLINE
Check if the given features are enabled.
Definition: ButtonConfig.h:219
virtual unsigned long getClock()
Return the milliseconds of the internal clock.
Definition: ButtonConfig.h:204