[core] Fix status_momentary API misuse and optimize parameter type (#12216)

This commit is contained in:
J. Nick Koston
2025-12-01 08:34:07 -06:00
committed by GitHub
parent 664881bc13
commit 065c1bfc6a
7 changed files with 27 additions and 14 deletions

View File

@@ -33,7 +33,7 @@ class DemoAlarmControlPanel : public AlarmControlPanel, public Component {
case ACP_STATE_ARMED_AWAY:
if (this->get_requires_code_to_arm() && call.get_code().has_value()) {
if (call.get_code().value() != "1234") {
this->status_momentary_error("Invalid code", 5000);
this->status_momentary_error("invalid_code", 5000);
return;
}
}
@@ -42,7 +42,7 @@ class DemoAlarmControlPanel : public AlarmControlPanel, public Component {
case ACP_STATE_DISARMED:
if (this->get_requires_code() && call.get_code().has_value()) {
if (call.get_code().value() != "1234") {
this->status_momentary_error("Invalid code", 5000);
this->status_momentary_error("invalid_code", 5000);
return;
}
}

View File

@@ -225,7 +225,7 @@ bool ES8388::set_dac_output(DacOutputLine line) {
optional<DacOutputLine> ES8388::get_dac_power() {
uint8_t dac_power;
if (!this->read_byte(ES8388_DACPOWER, &dac_power)) {
this->status_momentary_warning("Failed to read ES8388_DACPOWER");
this->status_momentary_warning("dacpower_read");
return {};
}
switch (dac_power) {
@@ -268,7 +268,7 @@ bool ES8388::set_adc_input_mic(AdcInputMicLine line) {
optional<AdcInputMicLine> ES8388::get_mic_input() {
uint8_t mic_input;
if (!this->read_byte(ES8388_ADCCONTROL2, &mic_input)) {
this->status_momentary_warning("Failed to read ES8388_ADCCONTROL2");
this->status_momentary_warning("adccontrol2_read");
return {};
}
switch (mic_input) {

View File

@@ -402,7 +402,7 @@ error:
this->backend_->abort();
}
this->status_momentary_error("onerror", 5000);
this->status_momentary_error("err", 5000);
#ifdef USE_OTA_STATE_CALLBACK
this->state_callback_.call(ota::OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
#endif

View File

@@ -298,8 +298,7 @@ void MicroWakeWord::loop() {
// uses floating point operations.
if (!FrontendPopulateState(&this->frontend_config_, &this->frontend_state_,
this->microphone_source_->get_audio_stream_info().get_sample_rate())) {
this->status_momentary_error(
"Failed to allocate buffers for spectrogram feature processor, attempting again in 1 second", 1000);
this->status_momentary_error("frontend_alloc", 1000);
return;
}
@@ -308,7 +307,7 @@ void MicroWakeWord::loop() {
if (this->inference_task_handle_ == nullptr) {
FrontendFreeStateContents(&this->frontend_state_); // Deallocate frontend state
this->status_momentary_error("Task failed to start, attempting again in 1 second", 1000);
this->status_momentary_error("task_start", 1000);
}
}
break;

View File

@@ -167,7 +167,7 @@ bool SoundLevelComponent::start_() {
this->audio_buffer_ = audio::AudioSourceTransferBuffer::create(
this->microphone_source_->get_audio_stream_info().ms_to_bytes(AUDIO_BUFFER_DURATION_MS));
if (this->audio_buffer_ == nullptr) {
this->status_momentary_error("Failed to allocate transfer buffer", 15000);
this->status_momentary_error("transfer_buffer", 15000);
return false;
}
@@ -176,7 +176,7 @@ bool SoundLevelComponent::start_() {
std::shared_ptr<RingBuffer> temp_ring_buffer =
RingBuffer::create(this->microphone_source_->get_audio_stream_info().ms_to_bytes(RING_BUFFER_DURATION_MS));
if (temp_ring_buffer.use_count() == 0) {
this->status_momentary_error("Failed to allocate ring buffer", 15000);
this->status_momentary_error("ring_buffer", 15000);
this->stop_();
return false;
} else {

View File

@@ -369,11 +369,11 @@ void Component::status_clear_error() {
this->component_state_ &= ~STATUS_LED_ERROR;
ESP_LOGE(TAG, "%s cleared Error flag", LOG_STR_ARG(this->get_component_log_str()));
}
void Component::status_momentary_warning(const std::string &name, uint32_t length) {
void Component::status_momentary_warning(const char *name, uint32_t length) {
this->status_set_warning();
this->set_timeout(name, length, [this]() { this->status_clear_warning(); });
}
void Component::status_momentary_error(const std::string &name, uint32_t length) {
void Component::status_momentary_error(const char *name, uint32_t length) {
this->status_set_error();
this->set_timeout(name, length, [this]() { this->status_clear_error(); });
}

View File

@@ -241,9 +241,23 @@ class Component {
void status_clear_error();
void status_momentary_warning(const std::string &name, uint32_t length = 5000);
/** Set warning status flag and automatically clear it after a timeout.
*
* @param name Identifier for the timeout (used to cancel/replace existing timeouts with the same name).
* Must be a static string literal (stored in flash/rodata), not a temporary or dynamic string.
* This is NOT a message to display - use status_set_warning() with a message if logging is needed.
* @param length Duration in milliseconds before the warning is automatically cleared.
*/
void status_momentary_warning(const char *name, uint32_t length = 5000);
void status_momentary_error(const std::string &name, uint32_t length = 5000);
/** Set error status flag and automatically clear it after a timeout.
*
* @param name Identifier for the timeout (used to cancel/replace existing timeouts with the same name).
* Must be a static string literal (stored in flash/rodata), not a temporary or dynamic string.
* This is NOT a message to display - use status_set_error() with a message if logging is needed.
* @param length Duration in milliseconds before the error is automatically cleared.
*/
void status_momentary_error(const char *name, uint32_t length = 5000);
bool has_overridden_loop() const;