Sesame-Device/include/servo.h

105 lines
2.4 KiB
C
Raw Normal View History

#pragma once
#include <stdint.h>
#include <ESP32Servo.h> // To control the servo
2023-08-09 13:38:12 +02:00
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
*
* 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
*/
2023-08-09 13:38:12 +02:00
ServoController();
/**
2023-08-09 13:38:12 +02:00
* @brief Configure the servo
*
2023-08-09 13:38:12 +02:00
* @param The configuration for the servo
*/
2023-08-09 13:38:12 +02:00
void configure(ServoConfiguration configuration);
/**
* @brief Update the servo state periodically
*
* This function should 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
* the opening interval is dependent on the calling frequency.
*/
void loop();
/**
* Push the door opener button down by moving the servo arm.
*/
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;
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;
};