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

View File

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