Naprawa isPressed() - dodanie wykrywania zbocza opadajacego, co rozwiazuje problem wielokrotnego wyzwalania akcji przy przytrzymaniu

This commit is contained in:
Victus
2026-05-11 00:39:26 +02:00
parent 7f44514172
commit 7d997ed543
2 changed files with 22 additions and 5 deletions

View File

@@ -47,6 +47,10 @@ private:
void constrainValue(int &value, int minVal, int maxVal);
void printField(const char *label, int value);
bool lastState1_ = true;
bool lastState2_ = true;
bool lastState3_ = true;
};
#endif

View File

@@ -13,15 +13,28 @@ void Settings::begin() {
pinMode(BTN_DOWN, INPUT_PULLUP);
}
// Zwraca true, jeśli przycisk jest wciśnięty
// Zwraca true, jeśli przycisk został WŁAŚNIE wciśnięty (zbocze opadające)
bool Settings::isPressed(uint8_t btnIndex) {
//ESP_LOGI(TAG_SETTINGS, "BTN check");
bool currentState, isNewlyPressed = false;
switch (btnIndex) {
case 1: return digitalRead(BTN_UP) == LOW;
case 2: return digitalRead(BTN_OK) == LOW;
case 3: return digitalRead(BTN_DOWN) == LOW;
case 1:
currentState = digitalRead(BTN_UP);
isNewlyPressed = (currentState == LOW && lastState1_ == HIGH);
lastState1_ = currentState;
break;
case 2:
currentState = digitalRead(BTN_OK);
isNewlyPressed = (currentState == LOW && lastState2_ == HIGH);
lastState2_ = currentState;
break;
case 3:
currentState = digitalRead(BTN_DOWN);
isNewlyPressed = (currentState == LOW && lastState3_ == HIGH);
lastState3_ = currentState;
break;
default: return false;
}
return isNewlyPressed;
}
// Kombinacja 1 i 3 jednocześnie