Sesame-Device/include/servo.h

84 lines
2.2 KiB
C
Raw Normal View History

#pragma once
#include <stdint.h>
#include <ESP32Servo.h> // To control the servo
/**
* @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:
/**
* @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
*/
ServoController(int timer, int frequency, int pin);
/**
* @brief Configure the button values
*
* @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
*/
void configure(uint32_t openDuration, int pressedValue, int releasedValue);
/**
* @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;
int timer;
int frequency;
int pin;
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;
};