Files
Sesame-Device/src/servo.cpp
2023-08-09 15:02:24 +02:00

35 lines
866 B
C++

#include "servo.h"
#include <esp32-hal.h> // For `millis()`
ServoController::ServoController() { }
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();
}
void ServoController::pressButton() {
servo.write(pressedValue);
buttonIsPressed = true;
openingEndTime = millis() + openDuration;
}
void ServoController::releaseButton() {
servo.write(releasedValue);
buttonIsPressed = false;
}
void ServoController::loop(uint32_t millis) {
if (buttonIsPressed && millis > openingEndTime) {
releaseButton();
}
}