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,57 +2910,42 @@ 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();
rotaryPos = newPos;
if (rotaryDir == 1) { if (rotaryDir == 1) {
rotaryPos = newPos;
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) { buttonState = reading;
if (reading != buttonState) { // Button was pressed down
buttonState = reading; if (buttonState == 0) {
// Button was pressed down setColor_RGB(0, 0, 0);
if (buttonState == 0) { unsigned long pushTime = millis();
setColor_RGB(0, 0, 0); // Wait until button was let go again
unsigned long pushTime = millis(); while ((PING & (1 << PING2)) >> PING2 == 0) {
// Wait until button was let go again // Signal long press delay reached
while ((PING & (1 << PING2)) >> PING2 == 0) {
// Signal long press delay reached
if ((millis() - pushTime) > 2000)
rgbLed(green_color);
}
lastButtonState = reading;
// 2 second long press
if ((millis() - pushTime) > 2000) { if ((millis() - pushTime) > 2000) {
return 4; rgbLed(green_color);
}
// normal press
else {
return 3;
} }
} }
} else {
lastButtonState = reading; // 2 second long press
return 0; if ((millis() - pushTime) > 2000) {
return 4;
}
// normal press
else {
return 3;
}
} }
} else {
lastButtonState = reading;
return 0;
} }
} }
return 0;
} }
// Wait for user to push button // Wait for user to push button