fix startbutton longpress

This commit is contained in:
interfisch 2023-05-23 21:11:14 +02:00
parent f7e4f3fb65
commit 7ea0d2200b
1 changed files with 29 additions and 0 deletions

View File

@ -577,16 +577,44 @@ void leds() {
} }
void readButtons() { void readButtons() {
bool button_start_longpress_flag=false;
bool button_start_shortpress_flag=false;
static bool button_start_wait_release_flag=false;
bool last_button_start_state=button_start_state;
if (loopmillis > button_start_lastchange+DEBOUNCE_TIME) { //wait some time after last change if (loopmillis > button_start_lastchange+DEBOUNCE_TIME) { //wait some time after last change
if (digitalRead(PIN_START) && !button_start_state) { //start engine button pressed and was not pressed before if (digitalRead(PIN_START) && !button_start_state) { //start engine button pressed and was not pressed before
button_start_state=true; //pressed button_start_state=true; //pressed
button_start_lastchange=loopmillis; //save time for debouncing button_start_lastchange=loopmillis; //save time for debouncing
}else if (!digitalRead(PIN_START) && button_start_state) { //released an was pressed before }else if (!digitalRead(PIN_START) && button_start_state) { //released an was pressed before
button_start_state=false; // not pressed button_start_state=false; // not pressed
button_start_wait_release_flag=false;
button_start_lastchange=loopmillis; //save time for debouncing button_start_lastchange=loopmillis; //save time for debouncing
} }
} }
if (!button_start_wait_release_flag) { //action not prohibited currently
if (button_start_state) { //button is pressed
if ( (loopmillis> button_start_lastchange + LONG_PRESS_ARMING_TIME)) { //pressed long
button_start_longpress_flag=true;
button_start_wait_release_flag=true; //do not trigger again until button released
}
}else if(!button_start_state && last_button_start_state) { //just released
button_start_shortpress_flag=true;
}
}
if (button_start_shortpress_flag) {
armed=false; //disarm
writeLogComment(loopmillis, "Disarmed by button");
}
if (button_start_longpress_flag) {
armed=true; //arm if button pressed long enough
writeLogComment(loopmillis, "Armed by button");
}
/* TODO: if works, remove this
if (button_start_state) { //pressed if (button_start_state) { //pressed
if ( (loopmillis> button_start_lastchange + LONG_PRESS_ARMING_TIME)) { //pressed long if ( (loopmillis> button_start_lastchange + LONG_PRESS_ARMING_TIME)) { //pressed long
if (throttle_pos<=0 && brake_pos<=0 && controllers_connected && !armed) { //brake or thottle not pressed, controllers connected if (throttle_pos<=0 && brake_pos<=0 && controllers_connected && !armed) { //brake or thottle not pressed, controllers connected
@ -598,6 +626,7 @@ void readButtons() {
writeLogComment(loopmillis, "Disarmed by button"); writeLogComment(loopmillis, "Disarmed by button");
} }
} }
*/
} }