mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
simplify logic
This commit is contained in:
parent
db8e72791c
commit
c445bbeaf2
2 changed files with 6 additions and 6 deletions
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
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 = !_activeState;
|
_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 = !_activeState;
|
_currentState = false; // Initialize as not pressed
|
||||||
_lastState = _currentState;
|
_lastState = _currentState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,16 +70,16 @@ void Button::update() {
|
||||||
|
|
||||||
bool Button::readButton() {
|
bool Button::readButton() {
|
||||||
if (_isAnalog) {
|
if (_isAnalog) {
|
||||||
return (analogRead(_pin) < _analogThreshold) ? _activeState : !_activeState;
|
return (analogRead(_pin) < _analogThreshold);
|
||||||
} else {
|
} else {
|
||||||
return digitalRead(_pin);
|
return (digitalRead(_pin) == _activeState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Button::handleStateChange() {
|
void Button::handleStateChange() {
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
|
|
||||||
if (_currentState == _activeState) {
|
if (_currentState) {
|
||||||
// Button pressed
|
// Button pressed
|
||||||
_pressTime = now;
|
_pressTime = now;
|
||||||
_state = PRESSED;
|
_state = PRESSED;
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ public:
|
||||||
void onAnyPress(EventCallback callback) { _onAnyPress = callback; }
|
void onAnyPress(EventCallback callback) { _onAnyPress = callback; }
|
||||||
|
|
||||||
// State getters
|
// State getters
|
||||||
bool isPressed() const { return _currentState == _activeState; }
|
bool isPressed() const { return _currentState; }
|
||||||
EventType getLastEvent() const { return _lastEvent; }
|
EventType getLastEvent() const { return _lastEvent; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue