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