Skip to content

Commit eb36148

Browse files
authored
Merge pull request #131 from bjrnptrsn/master
Add attachIdle and setIdleMs
2 parents 1832bfb + 60b8dde commit eb36148

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/OneButton.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void OneButton::setPressMs(const unsigned int ms)
7878
_press_ms = ms;
7979
} // setPressMs
8080

81+
// explicitly set the number of millisec that have to pass by before button idle is detected.
82+
void OneButton::setIdleMs(const unsigned int ms)
83+
{
84+
_idle_ms = ms;
85+
} // setIdleMs
8186

8287
// save function for click event
8388
void OneButton::attachClick(callbackFunction newFunction)
@@ -173,11 +178,19 @@ void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction,
173178
} // attachDuringLongPress
174179

175180

181+
// save function for idle button event
182+
void OneButton::attachIdle(callbackFunction newFunction)
183+
{
184+
_idleFunc = newFunction;
185+
} // attachIdle
186+
187+
176188
void OneButton::reset(void)
177189
{
178190
_state = OneButton::OCS_INIT;
179191
_nClicks = 0;
180-
_startTime = 0;
192+
_startTime = millis();
193+
_idleState = false;
181194
}
182195

183196

@@ -242,6 +255,13 @@ void OneButton::_fsm(bool activeLevel)
242255
// Implementation of the state machine
243256
switch (_state) {
244257
case OneButton::OCS_INIT:
258+
// on idle for idle_ms call idle function
259+
if (!_idleState and (waitTime > _idle_ms))
260+
if (_idleFunc) {
261+
_idleState = true;
262+
_idleFunc();
263+
}
264+
245265
// waiting for level to become active.
246266
if (activeLevel) {
247267
_newState(OneButton::OCS_DOWN);

src/OneButton.h

+16
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class OneButton
7878
*/
7979
void setLongPressIntervalMs(const unsigned int ms) { _long_press_interval_ms = ms; };
8080

81+
/**
82+
* set # millisec after idle is assumed.
83+
*/
84+
void setIdleMs(const unsigned int ms);
85+
8186
// ----- Attach events functions -----
8287

8388
/**
@@ -123,6 +128,12 @@ class OneButton
123128
void attachDuringLongPress(callbackFunction newFunction);
124129
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter);
125130

131+
/**
132+
* Attach an event when the button is in idle position.
133+
* @param newFunction
134+
*/
135+
void attachIdle(callbackFunction newFunction);
136+
126137
// ----- State machine functions -----
127138

128139
/**
@@ -169,6 +180,7 @@ class OneButton
169180
unsigned int _debounce_ms = 50; // number of msecs for debounce times.
170181
unsigned int _click_ms = 400; // number of msecs before a click is detected.
171182
unsigned int _press_ms = 800; // number of msecs before a long button press is detected
183+
unsigned int _idle_ms = 1000; // number of msecs before idle is detected
172184

173185
int _buttonPressed = 0; // this is the level of the input pin when the button is pressed.
174186
// LOW if the button connects the input pin to GND when pressed.
@@ -199,6 +211,8 @@ class OneButton
199211
parameterizedCallbackFunction _paramDuringLongPressFunc = NULL;
200212
void *_duringLongPressFuncParam = NULL;
201213

214+
callbackFunction _idleFunc = NULL;
215+
202216
// These variables that hold information across the upcoming tick calls.
203217
// They are initialized once on program start and are updated every time the
204218
// tick function is called.
@@ -225,6 +239,8 @@ class OneButton
225239

226240
stateMachine_t _state = OCS_INIT;
227241

242+
bool _idleState = false;
243+
228244
int debouncedPinLevel = -1;
229245
int _lastDebouncePinLevel = -1; // used for pin debouncing
230246
unsigned long _lastDebounceTime = 0; // millis()

0 commit comments

Comments
 (0)