Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering on any input state change #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/EasyButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void EasyButton::begin()
}
_time = millis();
_last_state = _current_state;
_last_read_state = _current_state;
_changed = false;
_last_change = _time;
}
Expand All @@ -32,6 +33,11 @@ bool EasyButton::read()
pinVal = !pinVal;
}

if (_filtering && pinVal != _last_read_state) {
_last_change = read_started_ms;
_last_read_state = pinVal;
}

if (read_started_ms - _last_change < _db_time)
{
// Debounce time has not ellapsed.
Expand Down Expand Up @@ -119,4 +125,4 @@ void EasyButton::update()
{
_checkPressedTime();
}
}
}
6 changes: 4 additions & 2 deletions src/EasyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EasyButton : public EasyButtonBase
friend class EasyButtonTouch;

public:
EasyButton(uint8_t pin, uint32_t debounce_time = 35, bool pullup_enable = true, bool active_low = true) : EasyButtonBase(active_low), _pin(pin), _db_time(debounce_time), _pu_enabled(pullup_enable), _read_type(EASYBUTTON_READ_TYPE_POLL)
EasyButton(uint8_t pin, uint32_t debounce_time = 35, bool pullup_enable = true, bool active_low = true, bool filtering = false) : EasyButtonBase(active_low), _pin(pin), _db_time(debounce_time), _pu_enabled(pullup_enable), _read_type(EASYBUTTON_READ_TYPE_POLL), _filtering(filtering)
{
}
~EasyButton() {}
Expand All @@ -38,8 +38,10 @@ class EasyButton : public EasyButtonBase
uint32_t _db_time; // Debounce time (ms).
bool _pu_enabled; // Internal pullup resistor enabled.
uint8_t _read_type; // Read type. Poll or Interrupt.
bool _last_read_state;
bool _filtering; // whether to filter switch activation as well as debouncing

virtual bool _readPin(); // Abstracts the pin value reading.
};

#endif
#endif