From b99245085eb8688e384b647ad8d50cd9d4c82808 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 9 Aug 2023 13:38:12 +0200 Subject: [PATCH] Create servo configuration --- include/servo.h | 55 ++++++++++++++++++++++++++++++++++--------------- src/main.cpp | 12 +++++++++-- src/servo.cpp | 19 +++++++---------- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/include/servo.h b/include/servo.h index 5859068..c8ad6be 100644 --- a/include/servo.h +++ b/include/servo.h @@ -3,6 +3,39 @@ #include #include // To control the servo +typedef struct ServoConfiguration { + /** + * @brief The timer to use for the servo control + * number 0-3 indicating which timer to allocate in this library + */ + int pwmTimer; + + /** + * @brief The servo frequency (depending on the model used) + */ + int pwmFrequency; + + /** + * @brief The pin where the servo PWM line is connected + */ + int pin; + + /** + * @brief The duration (in ms) for which the button should remain pressed + */ + uint32_t openDuration; + + /** + * @brief The servo value (in µs) that specifies the 'pressed' state + */ + int pressedValue; + + /** + * @brief The servo value (in µs) that specifies the 'released' state + */ + int releasedValue; +}; + /** * @brief A controller for the button control servo * @@ -17,22 +50,16 @@ class ServoController { public: /** - * @brief Construct a new Servo Controller object - * - * @param timer The timer to use for the servo control - * @param frequency The servo frequency (depending on the model used) - * @param pin The pin where the servo PWM line is connected + * @brief Construct a new servo controller */ - ServoController(int timer, int frequency, int pin); + ServoController(); /** - * @brief Configure the button values + * @brief Configure the servo * - * @param openDuration The duration (in ms) for which the button should remain pressed - * @param pressedValue The servo value (in µs) that specifies the 'pressed' state - * @param releasedValue The servo value (in µs) that specifies the 'released' state + * @param The configuration for the servo */ - void configure(uint32_t openDuration, int pressedValue, int releasedValue); + void configure(ServoConfiguration configuration); /** * @brief Update the servo state periodically @@ -60,12 +87,6 @@ private: // Indicator that the door button is pushed bool buttonIsPressed = false; - int timer; - - int frequency; - - int pin; - uint32_t openDuration = 0; int pressedValue = 0; diff --git a/src/main.cpp b/src/main.cpp index 29bd9b5..3d7dafc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ TimeCheck timeCheck{}; ServerConnection server(serverUrl, serverPort, serverPath); -ServoController servo(pwmTimer, servoFrequency, servoPin); +ServoController servo{}; AsyncWebServer local(localPort); @@ -40,7 +40,15 @@ void setup() { Serial.setDebugOutput(true); Serial.println("[INFO] Device started"); - servo.configure(lockOpeningDuration, servoPressedState, servoReleasedState); + ServoConfiguration servoConfig; + servoConfig.pwmTimer = pwmTimer; + servoConfig.pwmFrequency = servoFrequency; + servoConfig.pin = servoPin; + servoConfig.openDuration = lockOpeningDuration; + servoConfig.pressedValue = servoPressedState; + servoConfig.releasedValue = servoReleasedState; + + servo.configure(servoConfig); Serial.println("[INFO] Servo configured"); controller.configure(); diff --git a/src/servo.cpp b/src/servo.cpp index 5d7cc20..5809244 100644 --- a/src/servo.cpp +++ b/src/servo.cpp @@ -2,18 +2,15 @@ #include // For `millis()` -ServoController::ServoController(int timer, int frequency, int pin) - : timer(timer), frequency(frequency), pin(pin) { - -} +ServoController::ServoController() { } -void ServoController::configure(uint32_t openDuration, int pressedValue, int releasedValue) { - this->openDuration = openDuration; - this->pressedValue = pressedValue; - this->releasedValue = releasedValue; - ESP32PWM::allocateTimer(timer); - servo.setPeriodHertz(frequency); - servo.attach(pin); +void ServoController::configure(ServoConfiguration configuration) { + openDuration = configuration.openDuration; + pressedValue = configuration.pressedValue; + releasedValue = configuration.releasedValue; + ESP32PWM::allocateTimer(configuration.pwmTimer); + servo.setPeriodHertz(configuration.pwmFrequency); + servo.attach(configuration.pin); releaseButton(); }