From 7b1b985a15e3ca3f124f27fa7dfae55854912921 Mon Sep 17 00:00:00 2001
From: jonny_ji7 <jonny@wwad.de>
Date: Thu, 11 Aug 2022 15:15:55 +0200
Subject: [PATCH] Add longPressed case to button class, rework cmds

- slightly decrease msFadeDecel by 100ms

- different approach in button.cpp for case last button press was long
  e.g. reworked commands:
   - 1x long press = restart
   - 1x short press = center stick, freeze
   - 1x short 1x long = run new auto commands
   - 2x short = toggle idle

- change default minOnMs / minOffMs for evaulated switch class -> fix
  button presses sometimes not recognized
---
 components/gpio/gpio_evaluateSwitch.hpp |   4 +-
 main/button.cpp                         | 132 +++++++++++++-----------
 main/button.hpp                         |   2 +-
 main/config.cpp                         |   2 +-
 4 files changed, 74 insertions(+), 66 deletions(-)

diff --git a/components/gpio/gpio_evaluateSwitch.hpp b/components/gpio/gpio_evaluateSwitch.hpp
index 23caa14..c4a27e4 100644
--- a/components/gpio/gpio_evaluateSwitch.hpp
+++ b/components/gpio/gpio_evaluateSwitch.hpp
@@ -22,8 +22,8 @@ extern "C"
 class gpio_evaluatedSwitch {
     public:
         //--- input ---
-        uint32_t minOnMs = 50;
-        uint32_t minOffMs = 50;
+        uint32_t minOnMs = 30;
+        uint32_t minOffMs = 30;
         gpio_evaluatedSwitch( //constructor minimal (default parameters pullup=true, inverted=false)
                 gpio_num_t gpio_num_declare
                 );
diff --git a/main/button.cpp b/main/button.cpp
index 145ac7b..1762e6e 100644
--- a/main/button.cpp
+++ b/main/button.cpp
@@ -36,7 +36,7 @@ buttonCommands::buttonCommands(gpio_evaluatedSwitch * button_f, evaluatedJoystic
 //--------- action -----------
 //----------------------------
 //function that runs commands depending on a count value
-void buttonCommands::action (uint8_t count){
+void buttonCommands::action (uint8_t count, bool lastPressLong){
     //--- variable declarations ---
     bool decelEnabled; //for different beeping when toggling
     commandSimple_t cmds[8]; //array for commands for automatedArmchair
@@ -52,69 +52,77 @@ void buttonCommands::action (uint8_t count){
             buzzer->beep(3, 400, 100);
             break;
 
-        case 0: //special case when last button press is longer than timeout (>1s)
-            ESP_LOGW(TAG, "RESTART");
-            buzzer->beep(1,1000,1);
-            vTaskDelay(1000 / portTICK_PERIOD_MS);
-            esp_restart();
-
         case 1:
-            ////////ESP_LOGW(TAG, "cmd %d: sending button event to control task", count);
-            //////////-> define joystick center or toggle freeze input (executed in control task)
-            ////////control->sendButtonEvent(count); //TODO: always send button event to control task (not just at count=1) -> control.cpp has to be changed
-            //====== TESTING: send cmd to auto mode =====
-            //define commands
-            cmds[0] =
-            {
-                .motorCmds = {
-                    .left = {motorstate_t::REV, 90},
-                    .right = {motorstate_t::REV, 90}
-                },
-                .msDuration = 1200,
-                .fadeDecel = 800,
-                .fadeAccel = 1300,
-                .instruction = auto_instruction_t::NONE
-            };
-            cmds[1] =
-            {
-                .motorCmds = {
-                    .left = {motorstate_t::FWD, 70},
-                    .right = {motorstate_t::FWD, 70}
-                },
-                .msDuration = 70,
-                .fadeDecel = 0,
-                .fadeAccel = 300,
-                .instruction = auto_instruction_t::NONE
-            };
-            cmds[2] =
-            {
-                .motorCmds = {
-                    .left = {motorstate_t::IDLE, 0},
-                    .right = {motorstate_t::IDLE, 0}
-                },
-                .msDuration = 10,
-                .fadeDecel = 800,
-                .fadeAccel = 1300,
-                .instruction = auto_instruction_t::SWITCH_JOYSTICK_MODE
-            };
+            //restart contoller when 1x long pressed
+            if (lastPressLong){
+                ESP_LOGW(TAG, "RESTART");
+                buzzer->beep(1,1000,1);
+                vTaskDelay(1000 / portTICK_PERIOD_MS);
+                esp_restart();
+                return;
+            } 
 
-            //send commands to automatedArmchair command queue
-            armchair.addCommands(cmds, 3);
-
-            //change mode to AUTO
-            control->changeMode(controlMode_t::AUTO);
+            ESP_LOGW(TAG, "cmd %d: sending button event to control task", count);
+            //-> define joystick center or toggle freeze input (executed in control task)
+            control->sendButtonEvent(count); //TODO: always send button event to control task (not just at count=1) -> control.cpp has to be changed
             break;
+        case 2:
+            //run automatic commands to lift leg support when pressed 1x short 1x long
+            if (lastPressLong){
+                //define commands
+                cmds[0] =
+                {
+                    .motorCmds = {
+                        .left = {motorstate_t::REV, 90},
+                        .right = {motorstate_t::REV, 90}
+                    },
+                    .msDuration = 1200,
+                    .fadeDecel = 800,
+                    .fadeAccel = 1300,
+                    .instruction = auto_instruction_t::NONE
+                };
+                cmds[1] =
+                {
+                    .motorCmds = {
+                        .left = {motorstate_t::FWD, 70},
+                        .right = {motorstate_t::FWD, 70}
+                    },
+                    .msDuration = 70,
+                    .fadeDecel = 0,
+                    .fadeAccel = 300,
+                    .instruction = auto_instruction_t::NONE
+                };
+                cmds[2] =
+                {
+                    .motorCmds = {
+                        .left = {motorstate_t::IDLE, 0},
+                        .right = {motorstate_t::IDLE, 0}
+                    },
+                    .msDuration = 10,
+                    .fadeDecel = 800,
+                    .fadeAccel = 1300,
+                    .instruction = auto_instruction_t::SWITCH_JOYSTICK_MODE
+                };
+
+                //send commands to automatedArmchair command queue
+                armchair.addCommands(cmds, 3);
+
+                //change mode to AUTO
+                control->changeMode(controlMode_t::AUTO);
+                return;
+            }
+
+            //toggle idle when 2x pressed
+            ESP_LOGW(TAG, "cmd %d: toggle IDLE", count);
+            control->toggleIdle(); //toggle between idle and previous/default mode
+            break;
+
 
         case 3:
             ESP_LOGW(TAG, "cmd %d: switch to JOYSTICK", count);
             control->changeMode(controlMode_t::JOYSTICK); //switch to JOYSTICK mode
             break;
 
-        case 2:
-            ESP_LOGW(TAG, "cmd %d: toggle IDLE", count);
-            control->toggleIdle(); //toggle between idle and previous/default mode
-            break;
-
         case 4:
             ESP_LOGW(TAG, "cmd %d: toggle between HTTP and JOYSTICK", count);
             control->toggleModes(controlMode_t::HTTP, controlMode_t::JOYSTICK); //toggle between HTTP and JOYSTICK mode
@@ -166,7 +174,7 @@ void buttonCommands::startHandleLoop() {
             case inputState_t::IDLE: //wait for initial button press
                 if (button->risingEdge) {
                     count = 1;
-                    buzzer->beep(1, 60, 0);
+                    buzzer->beep(1, 65, 0);
                     timestamp_lastAction = esp_log_timestamp();
                     state = inputState_t::WAIT_FOR_INPUT;
                     ESP_LOGI(TAG, "first button press detected -> waiting for further events");
@@ -177,7 +185,7 @@ void buttonCommands::startHandleLoop() {
                 //button pressed again
                 if (button->risingEdge){
                     count++;
-                    buzzer->beep(1, 60, 0);
+                    buzzer->beep(1, 65, 0);
                     timestamp_lastAction = esp_log_timestamp();
                     ESP_LOGI(TAG, "another press detected -> count=%d -> waiting for further events", count);
                 }
@@ -189,14 +197,14 @@ void buttonCommands::startHandleLoop() {
                     ESP_LOGI(TAG, "timeout - running action function for count=%d", count);
                     //--- run action function ---
                     //check if still pressed
+                    bool lastPressLong = false;
                     if (button->state == true){
                         //run special case when last press was longer than timeout
-                        action(0); 
-                    } else {
-                        //run action function with current count of button presses
-                        action(count);
+                        lastPressLong = true;
+                    }
+                        //run action function with current count of button presses
+                        action(count, lastPressLong);
                     }
-                }
                 break;
         }
     }
diff --git a/main/button.hpp b/main/button.hpp
index a62cd1a..09de729 100644
--- a/main/button.hpp
+++ b/main/button.hpp
@@ -33,7 +33,7 @@ class buttonCommands {
 
     private:
         //--- functions ---
-        void action(uint8_t count);
+        void action(uint8_t count, bool lastPressLong);
 
         //--- objects ---
         gpio_evaluatedSwitch* button;
diff --git a/main/config.cpp b/main/config.cpp
index aecf079..3214948 100644
--- a/main/config.cpp
+++ b/main/config.cpp
@@ -30,7 +30,7 @@ single100a_config_t configDriverRight = {
 //--- configure motor contol ---
 motorctl_config_t configMotorControl = {
     .msFadeAccel = 1300, //acceleration of the motor (ms it takes from 0% to 100%)
-    .msFadeDecel = 800, //deceleration of the motor (ms it takes from 100% to 0%)
+    .msFadeDecel = 700, //deceleration of the motor (ms it takes from 100% to 0%)
     .currentMax = 10
 };