Cart_Reader.ino: Simplify checkButton nesting

Also, fixes a compiler warning on the HW4/5 function, which could reach
the function's end without a "return" statement.
This commit is contained in:
Vincent Pelletier 2022-11-05 05:04:14 +00:00
parent 7b1e74c1fc
commit e334edb4e5

View File

@ -2742,11 +2742,8 @@ int checkButton() {
return 3;
else if (eventButton2 > 2)
return 4;
else
return (checkButton1());
#else
return (checkButton1());
#endif
return (checkButton1());
}
// Read button 1
@ -2913,26 +2910,17 @@ int checkButton() {
// Check if rotary encoder has changed
if (rotaryPos != newPos) {
int rotaryDir = (int)encoder.getDirection();
if (rotaryDir == 1) {
rotaryPos = newPos;
if (rotaryDir == 1) {
return 1;
} else if (rotaryDir == -1) {
rotaryPos = newPos;
return 2;
} else {
return 0;
}
} else if (reading == buttonState) {
return 0;
}
// Check if button has changed
else {
} else if (reading != buttonState) {
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// Debounce button
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
lastButtonState = reading;
} else if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
// Button was pressed down
if (buttonState == 0) {
@ -2941,10 +2929,10 @@ int checkButton() {
// Wait until button was let go again
while ((PING & (1 << PING2)) >> PING2 == 0) {
// Signal long press delay reached
if ((millis() - pushTime) > 2000)
if ((millis() - pushTime) > 2000) {
rgbLed(green_color);
}
lastButtonState = reading;
}
// 2 second long press
if ((millis() - pushTime) > 2000) {
@ -2955,15 +2943,9 @@ int checkButton() {
return 3;
}
}
} else {
lastButtonState = reading;
}
}
return 0;
}
} else {
lastButtonState = reading;
return 0;
}
}
}
// Wait for user to push button