Skip to content

Commit 05242ac

Browse files
authored
Merge pull request #141 from mattallen37/master
Add support for an immediate "press" event callback and minor changes in debounce behavior.
2 parents 54c81d0 + 2251f1a commit 05242ac

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ Here's a full list of events handled by this library:
150150
151151
| Attach Function | Description |
152152
| ----------------------- | ------------------------------------------------------------- |
153-
| `attachClick` | Fires as soon as a single click is detected. |
153+
| `attachPress` | Fires as soon as a press is detected. |
154+
| `attachClick` | Fires as soon as a single click press and release is detected.|
154155
| `attachDoubleClick` | Fires as soon as a double click is detected. |
155156
| `attachMultiClick` | Fires as soon as multiple clicks have been detected. |
156157
| `attachLongPressStart` | Fires as soon as the button is held down for 800 milliseconds.|
@@ -175,12 +176,16 @@ This is because a single click callback must not to be triggered in case of a do
175176
| `setPressMs(int)` | `800 msec` | Duration to hold a button to trigger a long press. |
176177
177178
You may change these default values but be aware that when you specify too short times
178-
it is hard to click twice or you will create a press instead of a click.
179+
it is hard to click twice or you will create a long press instead of a click.
179180
180181
The former functions `setDebounceTicks`, `setClickTicks` and `setPressTicks` are marked deprecated.
181182
The term `Ticks` in these functions where confusing. Replace them with the ...Ms function calls.
182183
There is no functional change on them.
183184
185+
Set debounce ms to a negative value to only debounce on release. `setDebounceMs(-25);` will immediately
186+
update to a pressed state, and will debounce for 25ms going into the released state. This will expidite
187+
the `attachPress` callback function to run instantly.
188+
184189
185190
### Additional Functions
186191

src/OneButton.cpp

+31-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ OneButton::OneButton(const int pin, const bool activeLow, const bool pullupActiv
5959

6060

6161
// explicitly set the number of millisec that have to pass by before a click is assumed stable.
62-
void OneButton::setDebounceMs(const unsigned int ms)
62+
void OneButton::setDebounceMs(const int ms)
6363
{
6464
_debounce_ms = ms;
6565
} // setDebounceMs
@@ -84,6 +84,20 @@ void OneButton::setIdleMs(const unsigned int ms)
8484
_idle_ms = ms;
8585
} // setIdleMs
8686

87+
// save function for click event
88+
void OneButton::attachPress(callbackFunction newFunction)
89+
{
90+
_pressFunc = newFunction;
91+
} // attachPress
92+
93+
94+
// save function for parameterized click event
95+
void OneButton::attachPress(parameterizedCallbackFunction newFunction, void *parameter)
96+
{
97+
_paramPressFunc = newFunction;
98+
_pressFuncParam = parameter;
99+
} // attachPress
100+
87101
// save function for click event
88102
void OneButton::attachClick(callbackFunction newFunction)
89103
{
@@ -204,28 +218,33 @@ int OneButton::getNumberClicks(void)
204218
/**
205219
* @brief Debounce input pin level for use in SpesialInput.
206220
*/
207-
int OneButton::debounce(const int value) {
221+
bool OneButton::debounce(const bool value) {
208222
now = millis(); // current (relative) time in msecs.
209-
if (_lastDebouncePinLevel == value) {
210-
if (now - _lastDebounceTime >= _debounce_ms)
211-
debouncedPinLevel = value;
223+
224+
// Don't debounce going into active state, if _debounce_ms is negative
225+
if(value && _debounce_ms < 0)
226+
debouncedLevel = value;
227+
228+
if (_lastDebounceLevel == value) {
229+
if (now - _lastDebounceTime >= abs(_debounce_ms))
230+
debouncedLevel = value;
212231
} else {
213232
_lastDebounceTime = now;
214-
_lastDebouncePinLevel = value;
233+
_lastDebounceLevel = value;
215234
}
216-
return debouncedPinLevel;
235+
return debouncedLevel;
217236
};
218237

219238

220239
/**
221240
* @brief Check input of the configured pin,
222-
* debounce input pin level and then
241+
* debounce button state and then
223242
* advance the finite state machine (FSM).
224243
*/
225244
void OneButton::tick(void)
226245
{
227246
if (_pin >= 0) {
228-
_fsm(debounce(digitalRead(_pin)) == _buttonPressed);
247+
_fsm(debounce(digitalRead(_pin) == _buttonPressed));
229248
}
230249
} // tick()
231250

@@ -267,6 +286,9 @@ void OneButton::_fsm(bool activeLevel)
267286
_newState(OneButton::OCS_DOWN);
268287
_startTime = now; // remember starting time
269288
_nClicks = 0;
289+
290+
if (_pressFunc) _pressFunc();
291+
if (_paramPressFunc) _paramPressFunc(_pressFuncParam);
270292
} // if
271293
break;
272294

src/OneButton.h

+17-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class OneButton
5656
*/
5757
[[deprecated("Use setDebounceMs() instead.")]]
5858
void setDebounceTicks(const unsigned int ms) { setDebounceMs(ms); }; // deprecated
59-
void setDebounceMs(const unsigned int ms);
59+
void setDebounceMs(const int ms);
6060

6161
/**
6262
* set # millisec after single click is assumed.
@@ -85,6 +85,13 @@ class OneButton
8585

8686
// ----- Attach events functions -----
8787

88+
/**
89+
* Attach an event to be called immediately when a depress is detected.
90+
* @param newFunction This function will be called when the event has been detected.
91+
*/
92+
void attachPress(callbackFunction newFunction);
93+
void attachPress(parameterizedCallbackFunction newFunction, void *parameter);
94+
8895
/**
8996
* Attach an event to be called when a single click is detected.
9097
* @param newFunction This function will be called when the event has been detected.
@@ -177,7 +184,7 @@ class OneButton
177184

178185
private:
179186
int _pin = -1; // hardware pin number.
180-
unsigned int _debounce_ms = 50; // number of msecs for debounce times.
187+
int _debounce_ms = 50; // number of msecs for debounce times.
181188
unsigned int _click_ms = 400; // number of msecs before a click is detected.
182189
unsigned int _press_ms = 800; // number of msecs before a long button press is detected
183190
unsigned int _idle_ms = 1000; // number of msecs before idle is detected
@@ -187,6 +194,10 @@ class OneButton
187194
// HIGH if the button connects the input pin to VCC when pressed.
188195

189196
// These variables will hold functions acting as event source.
197+
callbackFunction _pressFunc = NULL;
198+
parameterizedCallbackFunction _paramPressFunc = NULL;
199+
void *_pressFuncParam = NULL;
200+
190201
callbackFunction _clickFunc = NULL;
191202
parameterizedCallbackFunction _paramClickFunc = NULL;
192203
void *_clickFuncParam = NULL;
@@ -241,8 +252,8 @@ class OneButton
241252

242253
bool _idleState = false;
243254

244-
int debouncedPinLevel = -1;
245-
int _lastDebouncePinLevel = -1; // used for pin debouncing
255+
bool debouncedLevel = false;
256+
bool _lastDebounceLevel = false; // used for pin debouncing
246257
unsigned long _lastDebounceTime = 0; // millis()
247258
unsigned long now = 0; // millis()
248259

@@ -256,8 +267,8 @@ class OneButton
256267
public:
257268
int pin() const { return _pin; };
258269
stateMachine_t state() const { return _state; };
259-
int debounce(const int value);
260-
int debouncedValue() const { return debouncedPinLevel; };
270+
bool debounce(const bool value);
271+
int debouncedValue() const { return debouncedLevel; };
261272

262273
/**
263274
* @brief Use this function in the DuringLongPress and LongPressStop events to get the time since the button was pressed.

0 commit comments

Comments
 (0)