Reverting changes to Button code

This commit is contained in:
hank
2025-06-01 20:15:16 -07:00
parent e15ad108af
commit 40bf7bbb9f
2 changed files with 169 additions and 174 deletions

View File

@@ -1,27 +1,23 @@
#include "Button.h"
Button::Button(uint8_t pin, bool activeState)
: _pin(pin), _activeState(activeState), _isAnalog(false), _analogThreshold(20)
{
: _pin(pin), _activeState(activeState), _isAnalog(false), _analogThreshold(20) {
_currentState = false; // Initialize as not pressed
_lastState = _currentState;
}
Button::Button(uint8_t pin, bool activeState, bool isAnalog, uint16_t analogThreshold)
: _pin(pin), _activeState(activeState), _isAnalog(isAnalog), _analogThreshold(analogThreshold)
{
: _pin(pin), _activeState(activeState), _isAnalog(isAnalog), _analogThreshold(analogThreshold) {
_currentState = false; // Initialize as not pressed
_lastState = _currentState;
}
void Button::begin()
{
void Button::begin() {
_currentState = readButton();
_lastState = _currentState;
}
void Button::update()
{
void Button::update() {
uint32_t now = millis();
// Read button at specified interval
@@ -52,11 +48,9 @@ void Button::update()
// Timeout reached, process the clicks
if (_clickCount == 1) {
triggerEvent(SHORT_PRESS);
}
else if (_clickCount == 2) {
} else if (_clickCount == 2) {
triggerEvent(DOUBLE_PRESS);
}
else if (_clickCount >= 3) {
} else if (_clickCount >= 3) {
triggerEvent(TRIPLE_PRESS);
}
_clickCount = 0;
@@ -71,18 +65,15 @@ void Button::update()
}
}
bool Button::readButton()
{
bool Button::readButton() {
if (_isAnalog) {
return (analogRead(_pin) < _analogThreshold);
}
else {
} else {
return (digitalRead(_pin) == _activeState);
}
}
void Button::handleStateChange()
{
void Button::handleStateChange() {
uint32_t now = millis();
if (_currentState) {
@@ -90,8 +81,7 @@ void Button::handleStateChange()
_pressTime = now;
_state = PRESSED;
triggerEvent(ANY_PRESS);
}
else {
} else {
// Button released
if (_state == PRESSED) {
uint32_t pressDuration = now - _pressTime;
@@ -101,8 +91,7 @@ void Button::handleStateChange()
_clickCount++;
_releaseTime = now;
_state = WAITING_FOR_MULTI_CLICK;
}
else {
} else {
// Long press already handled in update()
_state = IDLE;
_clickCount = 0;
@@ -111,30 +100,24 @@ void Button::handleStateChange()
}
}
void Button::triggerEvent(EventType event)
{
void Button::triggerEvent(EventType event) {
_lastEvent = event;
switch (event) {
case ANY_PRESS:
if (_onAnyPress)
_onAnyPress();
if (_onAnyPress) _onAnyPress();
break;
case SHORT_PRESS:
if (_onShortPress)
_onShortPress();
if (_onShortPress) _onShortPress();
break;
case DOUBLE_PRESS:
if (_onDoublePress)
_onDoublePress();
if (_onDoublePress) _onDoublePress();
break;
case TRIPLE_PRESS:
if (_onTriplePress)
_onTriplePress();
if (_onTriplePress) _onTriplePress();
break;
case LONG_PRESS:
if (_onLongPress)
_onLongPress();
if (_onLongPress) _onLongPress();
break;
default:
break;

View File

@@ -11,7 +11,14 @@
class Button {
public:
enum EventType { NONE, SHORT_PRESS, DOUBLE_PRESS, TRIPLE_PRESS, LONG_PRESS, ANY_PRESS };
enum EventType {
NONE,
SHORT_PRESS,
DOUBLE_PRESS,
TRIPLE_PRESS,
LONG_PRESS,
ANY_PRESS
};
using EventCallback = std::function<void()>;
@@ -33,7 +40,12 @@ public:
EventType getLastEvent() const { return _lastEvent; }
private:
enum State { IDLE, PRESSED, RELEASED, WAITING_FOR_MULTI_CLICK };
enum State {
IDLE,
PRESSED,
RELEASED,
WAITING_FOR_MULTI_CLICK
};
uint8_t _pin;
bool _activeState;