Skip to content

Commit

Permalink
Merge branch 'master' into V2.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mathertel authored Dec 2, 2023
2 parents 3691fec + eb36148 commit f438909
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ There is no functional change on them.
| Function | Description |
| ----------------------- | ------------------------------------------------------------------------------ |
| `bool isLongPressed()` | Detect whether or not the button is currently inside a long press. |
| `int getPressedTicks()` | Get the current number of milliseconds that the button has been held down for. |
| `int getPressedMs()` | Get the current number of milliseconds that the button has been held down for. |
| `int pin()` | Get the OneButton pin |
| `int state()` | Get the OneButton state |
| `int debouncedValue()` | Get the OneButton debounced value |
Expand Down
2 changes: 1 addition & 1 deletion examples/BlinkMachine/BlinkMachine.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef enum {
ACTION_FAST // blink LED "FAST"
} MyActions;

#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY) ||defined(ARDUINO_UNOR4_WIFI)
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
#define PIN_INPUT A1
#define PIN_LED 13
Expand Down
102 changes: 102 additions & 0 deletions examples/LongPressEvents/LongPressEvents.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
This is a sample sketch to show how to use the OneButtonLibrary
to detect long press events on a button.
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Setup a test circuit:
* Connect a pushbutton to PIN_INPUT (ButtonPin) and ground.
The sketch shows how to setup the library and bind the functions (LongPressStart, LongPressStop, DuringLongPress) to the events.
In the loop function the button.tick function must be called as often as you like.
The output of the program is:
OneButton Example.
Please press and hold the button for a few seconds.
810 - LongPressStart()
820 - DuringLongPress()
1820 - DuringLongPress()
2820 - DuringLongPress()
3820 - DuringLongPress()
4820 - DuringLongPress()
5820 - DuringLongPress()
6550 - LongPressStop()
*/

// 05.05.2023 created by Ihor Nehrutsa

#include "OneButton.h"

#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input button on pin 2
#define PIN_INPUT 2

#elif defined(ESP8266)
// Example for NodeMCU with input button using FLASH button on D3
#define PIN_INPUT D3

#elif defined(ESP32)
// Example pin assignments for a ESP32 board
// Some boards have a BOOT switch using GPIO 0.
#define PIN_INPUT 0

#endif

// Setup a new OneButton on pin PIN_INPUT
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button(PIN_INPUT, true);

// In case the momentary button puts the input to HIGH when pressed:
// The 2. parameter activeLOW is false when the external wiring sets the button to HIGH when pressed.
// The 3. parameter can be used to disable the PullUp .
// OneButton button(PIN_INPUT, false, false);

// setup code here, to run once:
void setup()
{
Serial.begin(115200);
Serial.println("\nOneButton Example.");
Serial.println("Please press and hold the button for a few seconds.");

// link functions to be called on events.
button.attachLongPressStart(LongPressStart, &button);
button.attachDuringLongPress(DuringLongPress, &button);
button.attachLongPressStop(LongPressStop, &button);

button.setLongPressIntervalMs(1000);
} // setup


// main code here, to run repeatedly:
void loop()
{
// keep watching the push button:
button.tick();

// You can implement other code in here or just wait a while
delay(10);
} // loop


// this function will be called when the button started long pressed.
void LongPressStart(void *oneButton)
{
Serial.print(((OneButton *)oneButton)->getPressedMs());
Serial.println("\t - LongPressStart()");
}

// this function will be called when the button is released.
void LongPressStop(void *oneButton)
{
Serial.print(((OneButton *)oneButton)->getPressedMs());
Serial.println("\t - LongPressStop()\n");
}

// this function will be called when the button is held down.
void DuringLongPress(void *oneButton)
{
Serial.print(((OneButton *)oneButton)->getPressedMs());
Serial.println("\t - DuringLongPress()");
}

// End
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"homepage": "http://www.mathertel.de/Arduino/OneButtonLibrary.aspx",
"frameworks": "arduino",
"platforms": "*"
}
}
32 changes: 27 additions & 5 deletions src/OneButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ void OneButton::setPressMs(const unsigned int ms)
_press_ms = ms;
} // setPressMs

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

// save function for click event
void OneButton::attachClick(callbackFunction newFunction)
Expand Down Expand Up @@ -173,11 +178,19 @@ void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction,
} // attachDuringLongPress


// save function for idle button event
void OneButton::attachIdle(callbackFunction newFunction)
{
_idleFunc = newFunction;
} // attachIdle


void OneButton::reset(void)
{
_state = OneButton::OCS_INIT;
_nClicks = 0;
_startTime = 0;
_startTime = millis();
_idleState = false;
}


Expand Down Expand Up @@ -242,6 +255,13 @@ void OneButton::_fsm(bool activeLevel)
// Implementation of the state machine
switch (_state) {
case OneButton::OCS_INIT:
// on idle for idle_ms call idle function
if (!_idleState and (waitTime > _idle_ms))
if (_idleFunc) {
_idleState = true;
_idleFunc();
}

// waiting for level to become active.
if (activeLevel) {
_newState(OneButton::OCS_DOWN);
Expand All @@ -257,7 +277,7 @@ void OneButton::_fsm(bool activeLevel)
_newState(OneButton::OCS_UP);
_startTime = now; // remember starting time

} else if ((activeLevel) && (waitTime > _press_ms)) {
} else if (waitTime > _press_ms) {
if (_longPressStartFunc) _longPressStartFunc();
if (_paramLongPressStartFunc) _paramLongPressStartFunc(_longPressStartFuncParam);
_newState(OneButton::OCS_PRESS);
Expand Down Expand Up @@ -308,12 +328,14 @@ void OneButton::_fsm(bool activeLevel)

if (!activeLevel) {
_newState(OneButton::OCS_PRESSEND);
_startTime = now;

} else {
// still the button is pressed
if (_duringLongPressFunc) _duringLongPressFunc();
if (_paramDuringLongPressFunc) _paramDuringLongPressFunc(_duringLongPressFuncParam);
if ((now - _lastDuringLongPressTime) >= _long_press_interval_ms) {
if (_duringLongPressFunc) _duringLongPressFunc();
if (_paramDuringLongPressFunc) _paramDuringLongPressFunc(_duringLongPressFuncParam);
_lastDuringLongPressTime = now;
}
} // if
break;

Expand Down
36 changes: 34 additions & 2 deletions src/OneButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ class OneButton
void setPressTicks(const unsigned int ms) { setPressMs(ms); }; // deprecated
void setPressMs(const unsigned int ms);

/**
* set interval in msecs between calls of the DuringLongPress event.
* 0 ms is the fastest events calls.
*/
void setLongPressIntervalMs(const unsigned int ms) { _long_press_interval_ms = ms; };

/**
* set # millisec after idle is assumed.
*/
void setIdleMs(const unsigned int ms);

// ----- Attach events functions -----

/**
Expand Down Expand Up @@ -111,11 +122,18 @@ class OneButton

/**
* Attach an event to fire periodically while the button is held down.
* The period of calls is set by setLongPressIntervalMs(ms).
* @param newFunction
*/
void attachDuringLongPress(callbackFunction newFunction);
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter);

/**
* Attach an event when the button is in idle position.
* @param newFunction
*/
void attachIdle(callbackFunction newFunction);

// ----- State machine functions -----

/**
Expand All @@ -130,7 +148,7 @@ class OneButton
* level is given by the parameter.
* Run the finite state machine (FSM) using the given level.
*/
void tick(bool level);
void tick(bool activeLevel);


/**
Expand Down Expand Up @@ -162,6 +180,7 @@ class OneButton
unsigned int _debounce_ms = 50; // number of msecs for debounce times.
unsigned int _click_ms = 400; // number of msecs before a click is detected.
unsigned int _press_ms = 800; // number of msecs before a long button press is detected
unsigned int _idle_ms = 1000; // number of msecs before idle is detected

int _buttonPressed = 0; // this is the level of the input pin when the button is pressed.
// LOW if the button connects the input pin to GND when pressed.
Expand Down Expand Up @@ -192,6 +211,8 @@ class OneButton
parameterizedCallbackFunction _paramDuringLongPressFunc = NULL;
void *_duringLongPressFuncParam = NULL;

callbackFunction _idleFunc = NULL;

// These variables that hold information across the upcoming tick calls.
// They are initialized once on program start and are updated every time the
// tick function is called.
Expand All @@ -218,20 +239,31 @@ class OneButton

stateMachine_t _state = OCS_INIT;

bool _idleState = false;

int debouncedPinLevel = -1;
int _lastDebouncePinLevel = -1; // used for pin debouncing
unsigned long _lastDebounceTime = 0; // millis()
unsigned long now = 0; // millis()

unsigned long _startTime = 0; // start of current input change to checking debouncing
unsigned long _startTime = 0; // start time of current activeLevel change
int _nClicks = 0; // count the number of clicks with this variable
int _maxClicks = 1; // max number (1, 2, multi=3) of clicks of interest by registration of event functions.

unsigned int _long_press_interval_ms = 0; // interval in msecs between calls of the DuringLongPress event
unsigned long _lastDuringLongPressTime = 0; // used to produce the DuringLongPress interval

public:
int pin() const { return _pin; };
stateMachine_t state() const { return _state; };
int debounce(const int value);
int debouncedValue() const { return debouncedPinLevel; };

/**
* @brief Use this function in the DuringLongPress and LongPressStop events to get the time since the button was pressed.
* @return milliseconds from the start of the button press.
*/
unsigned long getPressedMs() { return(millis() - _startTime); };
};

#endif

0 comments on commit f438909

Please sign in to comment.