From 7ea0d2200b87e28f21b16df92f88e3be88236b4e Mon Sep 17 00:00:00 2001 From: Fisch Date: Tue, 23 May 2023 21:11:14 +0200 Subject: [PATCH] fix startbutton longpress --- controller_teensy/src/main.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/controller_teensy/src/main.cpp b/controller_teensy/src/main.cpp index d3297f4..002dc80 100644 --- a/controller_teensy/src/main.cpp +++ b/controller_teensy/src/main.cpp @@ -577,16 +577,44 @@ void leds() { } 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 (digitalRead(PIN_START) && !button_start_state) { //start engine button pressed and was not pressed before button_start_state=true; //pressed button_start_lastchange=loopmillis; //save time for debouncing }else if (!digitalRead(PIN_START) && button_start_state) { //released an was pressed before button_start_state=false; // not pressed + button_start_wait_release_flag=false; 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 ( (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 @@ -598,6 +626,7 @@ void readButtons() { writeLogComment(loopmillis, "Disarmed by button"); } } + */ }