diff --git a/main/button.cpp b/main/button.cpp index 927dac3..7b6f5cd 100644 --- a/main/button.cpp +++ b/main/button.cpp @@ -44,11 +44,13 @@ void buttonCommands::action (uint8_t count){ break; case 1: - ESP_LOGW(TAG, "RESTART"); - buzzer->beep(1,1000,1); - vTaskDelay(1000 / portTICK_PERIOD_MS); - esp_restart(); + //ESP_LOGW(TAG, "RESTART"); + //buzzer->beep(1,1000,1); + //vTaskDelay(1000 / portTICK_PERIOD_MS); + //esp_restart(); + ESP_LOGW(TAG, "cmd %d: sending button event to control task", count); + control->sendButtonEvent(count); break; case 2: diff --git a/main/control.cpp b/main/control.cpp index ff77931..0497a9b 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -61,6 +61,7 @@ void controlledArmchair::startHandleLoop() { mode = controlMode_t::IDLE; break; + case controlMode_t::IDLE: //copy preset commands for idling both motors commands = cmds_bothMotorsIdle; @@ -69,7 +70,9 @@ void controlledArmchair::startHandleLoop() { vTaskDelay(200 / portTICK_PERIOD_MS); break; + case controlMode_t::JOYSTICK: + vTaskDelay(20 / portTICK_PERIOD_MS); //get current joystick data with getData method of evaluatedJoystick stickData = joystick_l->getData(); //additionaly scale coordinates (more detail in slower area) @@ -80,19 +83,39 @@ void controlledArmchair::startHandleLoop() { motorRight->setTarget(commands.right.state, commands.right.duty); motorLeft->setTarget(commands.left.state, commands.left.duty); //TODO make motorctl.setTarget also accept motorcommand struct directly - vTaskDelay(20 / portTICK_PERIOD_MS); + + //--- button event --- + if (buttonEvent){ + joystick_l->defineCenter(); + buzzer->beep(2, 200, 100); + } break; + case controlMode_t::MASSAGE: - //generate motor commands + vTaskDelay(20 / portTICK_PERIOD_MS); + //--- read joystick --- + //only update joystick data when input not frozen + if (!freezeInput){ + stickData = joystick_l->getData(); + } + + //--- generate motor commands --- //pass joystick data from getData method of evaluatedJoystick to generateCommandsShaking function - commands = joystick_generateCommandsShaking(joystick_l->getData()); + commands = joystick_generateCommandsShaking(stickData); //apply motor commands motorRight->setTarget(commands.right.state, commands.right.duty); motorLeft->setTarget(commands.left.state, commands.left.duty); - vTaskDelay(20 / portTICK_PERIOD_MS); + + //--- button event --- + if (buttonEvent){ + //toggle freeze of input (lock joystick at current values) + freezeInput = !freezeInput; + buzzer->beep(2, 200, 100); + } break; + case controlMode_t::HTTP: //--- get joystick data from queue --- //Note this function waits several seconds (httpconfig.timeoutMs) for data to arrive, otherwise Center data or NULL is returned @@ -111,9 +134,17 @@ void controlledArmchair::startHandleLoop() { motorLeft->setTarget(commands.left.state, commands.left.duty); break; + // //TODO: add other modes here } + //--- reset button event --- (only one run of handle loop) + //TODO: what if variable gets set durin above code? -> mutex around entire handle loop + if (buttonEvent == true){ + ESP_LOGI(TAG, "resetting button event"); + buttonEvent = false; + } + //----------------------- //------ slow loop ------ @@ -142,10 +173,24 @@ void controlledArmchair::resetTimeout(){ +//------------------------------------ +//--------- sendButtonEvent ---------- +//------------------------------------ +void controlledArmchair::sendButtonEvent(uint8_t count){ + //TODO mutex - if not replaced with queue + ESP_LOGI(TAG, "setting button event"); + buttonEvent = true; + buttonCount = count; +} + + + //------------------------------------ //---------- handleTimeout ----------- //------------------------------------ -float inactivityTolerance = 10; //percentage the duty can vary since last timeout check and still counts as incative +//percentage the duty can vary since last timeout check and still counts as incative +//TODO: add this to config +float inactivityTolerance = 10; //local function that checks whether two values differ more than a given tolerance bool validateActivity(float dutyOld, float dutyNow, float tolerance){ diff --git a/main/control.hpp b/main/control.hpp index 52dc4b1..67a62b3 100644 --- a/main/control.hpp +++ b/main/control.hpp @@ -58,6 +58,10 @@ class controlledArmchair { //function that restarts timer which initiates the automatic timeout (switch to IDLE) after certain time of inactivity void resetTimeout(); + //function for sending a button event (e.g. from button task at event) to control task + //TODO: use queue instead? + void sendButtonEvent(uint8_t count); + private: //--- functions --- @@ -83,6 +87,13 @@ class controlledArmchair { //variables for http mode uint32_t http_timestamp_lastData = 0; + //variables for MASSAGE mode + bool freezeInput = false; + + //variable to store button event + bool buttonEvent = false; + uint8_t buttonCount = 0; + //definition of mode enum controlMode_t mode = controlMode_t::IDLE;