diff --git a/include/servo.h b/include/servo.h index 435dc8d..89741e1 100644 --- a/include/servo.h +++ b/include/servo.h @@ -65,7 +65,7 @@ public: /** * @brief Update the servo state periodically * - * This function should be periodically called to update the servo state, + * This function will be periodically called to update the servo state, * specifically to release the button after the opening time has elapsed. * * There is no required interval to call this function, but the accuracy of @@ -78,16 +78,14 @@ public: */ void pressButton(); - /** - * Release the door opener button by moving the servo arm. - */ - void releaseButton(); - private: // Indicator that the door button is pushed bool buttonIsPressed = false; + // Indicate that the button should be pressed + bool shouldPressButton = false; + uint32_t openDuration = 0; int pressedValue = 0; @@ -103,4 +101,12 @@ private: // PWM Module needed for the servo ESP32PWM pwm; + // The task on core 1 that resets the servo + TaskHandle_t servoResetTask; + + /** + * Release the door opener button by moving the servo arm. + */ + void releaseButton(); + }; \ No newline at end of file diff --git a/src/controller.cpp b/src/controller.cpp index afe3f26..8a82684 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -64,7 +64,6 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura void SesameController::loop(uint32_t millis) { currentTime = millis; server.loop(millis); - servo.loop(millis); } // MARK: Local diff --git a/src/servo.cpp b/src/servo.cpp index 51d9f84..224b7d5 100644 --- a/src/servo.cpp +++ b/src/servo.cpp @@ -4,7 +4,28 @@ ServoController::ServoController() { } +void performReset(void * pvParameters) { + ServoController* servo = (ServoController *) pvParameters; + for(;;){ + servo->loop(millis()); + delay(50); + } +} + void ServoController::configure(ServoConfiguration configuration) { + + // Create a task that runs on a different core, + // So that it's always executed + xTaskCreatePinnedToCore( + performReset, /* Task function. */ + "Servo", /* name of task. */ + 1000, /* Stack size of task */ + this, /* parameter of the task */ + 1, /* priority of the task */ + &servoResetTask, + 1); /* pin task to core 1 */ + + openDuration = configuration.openDuration; pressedValue = configuration.pressedValue; releasedValue = configuration.releasedValue; @@ -15,9 +36,7 @@ void ServoController::configure(ServoConfiguration configuration) { } void ServoController::pressButton() { - servo.write(pressedValue); - buttonIsPressed = true; - openingEndTime = millis() + openDuration; + shouldPressButton = true; } void ServoController::releaseButton() { @@ -26,7 +45,12 @@ void ServoController::releaseButton() { } void ServoController::loop(uint32_t millis) { - if (buttonIsPressed && millis > openingEndTime) { + if (shouldPressButton) { + servo.write(pressedValue); + openingEndTime = millis + openDuration; + buttonIsPressed = true; + shouldPressButton = false; + } else if (buttonIsPressed && millis > openingEndTime) { releaseButton(); } }