2022-04-07 17:45:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ESP32Servo.h> // To control the servo
|
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
struct ServoConfiguration {
|
2023-08-09 13:38:12 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-08-09 13:38:12 +02:00
|
|
|
};
|
|
|
|
|
2022-04-07 17:45:23 +02:00
|
|
|
/**
|
|
|
|
* @brief A controller for the button control servo
|
|
|
|
*
|
|
|
|
* The controller simply configures the servo for operation,
|
|
|
|
* and then sets the desired servo value for the 'pressed' and 'released' states.
|
|
|
|
* The controller requires periodic updating through the `loop()` function
|
|
|
|
* in order to release the button after the specified time.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class ServoController {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2023-08-09 13:38:12 +02:00
|
|
|
* @brief Construct a new servo controller
|
2022-04-07 17:45:23 +02:00
|
|
|
*
|
2023-08-09 13:38:12 +02:00
|
|
|
* @param The configuration for the servo
|
2022-04-07 17:45:23 +02:00
|
|
|
*/
|
2024-04-22 12:58:18 +02:00
|
|
|
ServoController(ServoConfiguration configuration);
|
2022-04-07 17:45:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Update the servo state periodically
|
|
|
|
*
|
2024-04-20 09:57:15 +02:00
|
|
|
* This function will be periodically called to update the servo state,
|
2022-04-07 17:45:23 +02:00
|
|
|
* specifically to release the button after the opening time has elapsed.
|
|
|
|
*
|
|
|
|
* There is no required interval to call this function, but the accuracy of
|
|
|
|
* the opening interval is dependent on the calling frequency.
|
|
|
|
*/
|
2023-08-09 15:02:24 +02:00
|
|
|
void loop(uint32_t millis);
|
2022-04-07 17:45:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Push the door opener button down by moving the servo arm.
|
|
|
|
*/
|
|
|
|
void pressButton();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Indicator that the door button is pushed
|
|
|
|
bool buttonIsPressed = false;
|
|
|
|
|
2024-04-20 09:57:15 +02:00
|
|
|
// Indicate that the button should be pressed
|
|
|
|
bool shouldPressButton = false;
|
|
|
|
|
2022-04-07 17:45:23 +02:00
|
|
|
uint32_t openDuration = 0;
|
|
|
|
|
|
|
|
int pressedValue = 0;
|
|
|
|
|
|
|
|
int releasedValue = 0;
|
|
|
|
|
|
|
|
// The time (in ms since start) when the door opening should end
|
|
|
|
uint32_t openingEndTime = 0;
|
|
|
|
|
|
|
|
// Servo controller
|
|
|
|
Servo servo;
|
|
|
|
|
|
|
|
// PWM Module needed for the servo
|
|
|
|
ESP32PWM pwm;
|
|
|
|
|
2024-04-20 09:57:15 +02:00
|
|
|
// The task on core 1 that resets the servo
|
|
|
|
TaskHandle_t servoResetTask;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the door opener button by moving the servo arm.
|
|
|
|
*/
|
|
|
|
void releaseButton();
|
|
|
|
|
2022-04-07 17:45:23 +02:00
|
|
|
};
|