Adding new Kconfig options and updating LEDManager code for conditional debug LED and external error mirroring

This commit is contained in:
PhosphorosVR
2025-09-07 12:59:18 +02:00
parent 909a2779ac
commit 6f926306b5
8 changed files with 635 additions and 48 deletions

View File

@@ -73,10 +73,13 @@ LEDManager::LEDManager(gpio_num_t pin, gpio_num_t illumninator_led_pin,
void LEDManager::setup()
{
ESP_LOGI(LED_MANAGER_TAG, "Setting up status led.");
#ifdef CONFIG_LED_DEBUG_ENABLE
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);
#else
ESP_LOGI(LED_MANAGER_TAG, "Debug LED disabled via Kconfig (LED_DEBUG_ENABLE=n)");
#endif
#ifdef CONFIG_LED_EXTERNAL_CONTROL
ESP_LOGI(LED_MANAGER_TAG, "Setting up illuminator led.");
@@ -168,6 +171,31 @@ void LEDManager::updateState(const LEDStates_e newState)
if (newState == this->currentState)
return;
// Handle external LED mirroring transitions (store/restore duty)
#if defined(CONFIG_LED_EXTERNAL_CONTROL) && defined(CONFIG_LED_EXTERNAL_AS_DEBUG)
bool wasError = ledStateMap[this->currentState].isError;
bool willBeError = ledStateMap[newState].isError;
if (!wasError && willBeError)
{
// store current duty once
if (!hasStoredExternalDuty)
{
storedExternalDuty = ledc_get_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
hasStoredExternalDuty = true;
}
}
else if (wasError && !willBeError)
{
// restore duty
if (hasStoredExternalDuty)
{
ESP_ERROR_CHECK_WITHOUT_ABORT(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, storedExternalDuty));
ESP_ERROR_CHECK_WITHOUT_ABORT(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
hasStoredExternalDuty = false;
}
}
#endif
this->currentState = newState;
this->currentPatternIndex = 0;
this->finishedPattern = false;
@@ -175,7 +203,20 @@ void LEDManager::updateState(const LEDStates_e newState)
void LEDManager::toggleLED(const bool state) const
{
#ifdef CONFIG_LED_DEBUG_ENABLE
gpio_set_level(blink_led_pin, state);
#endif
#if defined(CONFIG_LED_EXTERNAL_CONTROL) && defined(CONFIG_LED_EXTERNAL_AS_DEBUG)
// Mirror only for error states
if (ledStateMap.contains(this->currentState) && ledStateMap.at(this->currentState).isError)
{
// For pattern ON use 50%, OFF use 0%
uint32_t duty = (state == LED_ON) ? ((50 * 255) / 100) : 0;
ESP_ERROR_CHECK_WITHOUT_ABORT(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty));
ESP_ERROR_CHECK_WITHOUT_ABORT(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
}
#endif
}
void LEDManager::setExternalLEDDutyCycle(uint8_t dutyPercent)

View File

@@ -70,6 +70,11 @@ private:
size_t currentPatternIndex = 0;
size_t timeToDelayFor = 100;
bool finishedPattern = false;
#if defined(CONFIG_LED_EXTERNAL_CONTROL) && defined(CONFIG_LED_EXTERNAL_AS_DEBUG)
mutable bool hasStoredExternalDuty = false;
mutable uint32_t storedExternalDuty = 0; // raw 0-255
#endif
};
void HandleLEDDisplayTask(void *pvParameter);