mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-19 06:23:44 +02:00
Add PoC PWN duty cycle adjustment command for FaceFocus
This commit is contained in:
@@ -48,10 +48,7 @@ ledStateMap_t LEDManager::ledStateMap = {
|
||||
{
|
||||
false,
|
||||
false,
|
||||
{
|
||||
{LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200},
|
||||
{LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}
|
||||
},
|
||||
{{LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -65,32 +62,38 @@ ledStateMap_t LEDManager::ledStateMap = {
|
||||
};
|
||||
|
||||
LEDManager::LEDManager(gpio_num_t pin, gpio_num_t illumninator_led_pin,
|
||||
QueueHandle_t ledStateQueue) : blink_led_pin(pin),
|
||||
illumninator_led_pin(illumninator_led_pin),
|
||||
ledStateQueue(ledStateQueue),
|
||||
currentState(LEDStates_e::LedStateNone) {
|
||||
QueueHandle_t ledStateQueue, std::shared_ptr<ProjectConfig> deviceConfig) : blink_led_pin(pin),
|
||||
illumninator_led_pin(illumninator_led_pin),
|
||||
ledStateQueue(ledStateQueue),
|
||||
currentState(LEDStates_e::LedStateNone),
|
||||
deviceConfig(deviceConfig)
|
||||
{
|
||||
}
|
||||
|
||||
void LEDManager::setup() {
|
||||
ESP_LOGD(LED_MANAGER_TAG, "Setting up status led.");
|
||||
void LEDManager::setup()
|
||||
{
|
||||
ESP_LOGI(LED_MANAGER_TAG, "Setting up status led.");
|
||||
gpio_reset_pin(blink_led_pin);
|
||||
/* Set the GPIO as a push/pull output */
|
||||
gpio_set_direction(blink_led_pin, GPIO_MODE_OUTPUT);
|
||||
this->toggleLED(LED_OFF);
|
||||
|
||||
#ifdef CONFIG_LED_EXTERNAL_CONTROL
|
||||
ESP_LOGD(LED_MANAGER_TAG, "Setting up illuminator led.");
|
||||
ESP_LOGI(LED_MANAGER_TAG, "Setting up illuminator led.");
|
||||
const int freq = CONFIG_LED_EXTERNAL_PWM_FREQ;
|
||||
const auto resolution = LEDC_TIMER_8_BIT;
|
||||
const int dutyCycle = (CONFIG_LED_EXTERNAL_PWM_DUTY_CYCLE * 255) / 100;
|
||||
const auto deviceConfig = this->deviceConfig->getDeviceConfig();
|
||||
|
||||
const uint32_t dutyCycle = (deviceConfig.led_external_pwm_duty_cycle * 255) / 100;
|
||||
|
||||
ESP_LOGI(LED_MANAGER_TAG, "Setting dutyCycle to: %lu ", dutyCycle);
|
||||
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.duty_resolution = resolution,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.freq_hz = freq,
|
||||
.clk_cfg = LEDC_AUTO_CLK
|
||||
};
|
||||
.clk_cfg = LEDC_AUTO_CLK};
|
||||
|
||||
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
||||
|
||||
@@ -101,8 +104,7 @@ void LEDManager::setup() {
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.duty = dutyCycle,
|
||||
.hpoint = 0
|
||||
};
|
||||
.hpoint = 0};
|
||||
|
||||
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
||||
#endif
|
||||
@@ -110,37 +112,46 @@ void LEDManager::setup() {
|
||||
ESP_LOGD(LED_MANAGER_TAG, "Done.");
|
||||
}
|
||||
|
||||
void LEDManager::handleLED() {
|
||||
if (!this->finishedPattern) {
|
||||
void LEDManager::handleLED()
|
||||
{
|
||||
if (!this->finishedPattern)
|
||||
{
|
||||
displayCurrentPattern();
|
||||
return;
|
||||
}
|
||||
|
||||
if (xQueueReceive(this->ledStateQueue, &buffer, 10)) {
|
||||
if (xQueueReceive(this->ledStateQueue, &buffer, 10))
|
||||
{
|
||||
this->updateState(buffer);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// we've finished displaying the pattern, so let's check if it's repeatable and if so - reset it
|
||||
if (ledStateMap[this->currentState].isRepeatable || ledStateMap[this->currentState].isError) {
|
||||
if (ledStateMap[this->currentState].isRepeatable || ledStateMap[this->currentState].isError)
|
||||
{
|
||||
this->currentPatternIndex = 0;
|
||||
this->finishedPattern = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LEDManager::displayCurrentPattern() {
|
||||
void LEDManager::displayCurrentPattern()
|
||||
{
|
||||
auto [state, delayTime] = ledStateMap[this->currentState].patterns[this->currentPatternIndex];
|
||||
this->toggleLED(state);
|
||||
this->timeToDelayFor = delayTime;
|
||||
|
||||
if (this->currentPatternIndex < ledStateMap[this->currentState].patterns.size() - 1)
|
||||
this->currentPatternIndex++;
|
||||
else {
|
||||
else
|
||||
{
|
||||
this->finishedPattern = true;
|
||||
this->toggleLED(LED_OFF);
|
||||
}
|
||||
}
|
||||
|
||||
void LEDManager::updateState(const LEDStates_e newState) {
|
||||
void LEDManager::updateState(const LEDStates_e newState)
|
||||
{
|
||||
// we should change the displayed state
|
||||
// only if we finished displaying the current one - which is handled by the task
|
||||
// if the new state is not the same as the current one
|
||||
@@ -153,21 +164,25 @@ void LEDManager::updateState(const LEDStates_e newState) {
|
||||
if (newState == this->currentState)
|
||||
return;
|
||||
|
||||
if (ledStateMap.contains(newState)) {
|
||||
if (ledStateMap.contains(newState))
|
||||
{
|
||||
this->currentState = newState;
|
||||
this->currentPatternIndex = 0;
|
||||
this->finishedPattern = false;
|
||||
}
|
||||
}
|
||||
|
||||
void LEDManager::toggleLED(const bool state) const {
|
||||
void LEDManager::toggleLED(const bool state) const
|
||||
{
|
||||
gpio_set_level(blink_led_pin, state);
|
||||
}
|
||||
|
||||
void HandleLEDDisplayTask(void *pvParameter) {
|
||||
void HandleLEDDisplayTask(void *pvParameter)
|
||||
{
|
||||
auto *ledManager = static_cast<LEDManager *>(pvParameter);
|
||||
|
||||
while (true) {
|
||||
while (true)
|
||||
{
|
||||
ledManager->handleLED();
|
||||
vTaskDelay(ledManager->getTimeToDelayFor());
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <StateManager.hpp>
|
||||
#include <ProjectConfig.hpp>
|
||||
#include <helpers.hpp>
|
||||
|
||||
// it kinda looks like different boards have these states swapped
|
||||
@@ -41,7 +42,7 @@ typedef std::unordered_map<LEDStates_e, LEDStage>
|
||||
class LEDManager
|
||||
{
|
||||
public:
|
||||
LEDManager(gpio_num_t blink_led_pin, gpio_num_t illumninator_led_pin, QueueHandle_t ledStateQueue);
|
||||
LEDManager(gpio_num_t blink_led_pin, gpio_num_t illumninator_led_pin, QueueHandle_t ledStateQueue, std::shared_ptr<ProjectConfig> deviceConfig);
|
||||
|
||||
void setup();
|
||||
void handleLED();
|
||||
@@ -60,6 +61,7 @@ private:
|
||||
|
||||
LEDStates_e buffer;
|
||||
LEDStates_e currentState;
|
||||
std::shared_ptr<ProjectConfig> deviceConfig;
|
||||
|
||||
size_t currentPatternIndex = 0;
|
||||
size_t timeToDelayFor = 100;
|
||||
|
||||
Reference in New Issue
Block a user