mirror of
https://github.com/esphome/esphome.git
synced 2026-10-09 21:13:12 +00:00
[core] Use numeric scheduler ids instead of string names (4/5) (#20377)
This commit is contained in:
@@ -5,6 +5,9 @@ namespace esphome::opt3001 {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "opt3001.sensor");
|
||||
|
||||
static constexpr uint32_t RESULT_RETRY_TIMEOUT_ID = 0;
|
||||
static constexpr uint32_t READ_TIMEOUT_ID = 1;
|
||||
|
||||
static const uint8_t OPT3001_REG_RESULT = 0x00;
|
||||
static const uint8_t OPT3001_REG_CONFIGURATION = 0x01;
|
||||
// See datasheet for full description of each bit.
|
||||
@@ -42,7 +45,7 @@ void OPT3001Sensor::read_result_(const std::function<void(float)> &f) {
|
||||
if ((raw_value & OPT3001_CONFIGURATION_CONVERSION_MODE_MASK) != OPT3001_CONFIGURATION_CONVERSION_MODE_SHUTDOWN) {
|
||||
// not ready; wait 10ms and try again
|
||||
ESP_LOGW(TAG, "Data not ready; waiting 10ms");
|
||||
this->set_timeout("opt3001_wait", 10, [this, f]() { read_result_(f); });
|
||||
this->set_timeout(RESULT_RETRY_TIMEOUT_ID, 10, [this, f]() { read_result_(f); });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -70,7 +73,7 @@ void OPT3001Sensor::read_lx_(const std::function<void(float)> &f) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->set_timeout("read", OPT3001_CONVERSION_TIME_800, [this, f]() {
|
||||
this->set_timeout(READ_TIMEOUT_ID, OPT3001_CONVERSION_TIME_800, [this, f]() {
|
||||
if (this->write(&OPT3001_REG_CONFIGURATION, 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGW(TAG, "Starting configuration register read failed");
|
||||
f(NAN);
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace esphome::output {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "output.button");
|
||||
|
||||
static constexpr uint32_t RESET_TIMEOUT_ID = 0;
|
||||
|
||||
void OutputButton::dump_config() {
|
||||
LOG_BUTTON("", "Output Button", this);
|
||||
ESP_LOGCONFIG(TAG, " Duration: %.1fs", this->duration_ / 1e3f);
|
||||
@@ -12,8 +14,8 @@ void OutputButton::dump_config() {
|
||||
void OutputButton::press_action() {
|
||||
this->output_->turn_on();
|
||||
|
||||
// Use a named timeout so that it's automatically cancelled if button is pressed again before it's reset
|
||||
this->set_timeout("reset", this->duration_, [this]() { this->output_->turn_off(); });
|
||||
// One timeout id, so a second press restarts the reset instead of stacking another
|
||||
this->set_timeout(RESET_TIMEOUT_ID, this->duration_, [this]() { this->output_->turn_off(); });
|
||||
}
|
||||
|
||||
} // namespace esphome::output
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace esphome::pid {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "pid.climate");
|
||||
|
||||
static constexpr uint32_t AUTOTUNE_PROGRESS_INTERVAL_ID = 0;
|
||||
|
||||
bool PIDClimate::set_deadband_thresholds(float threshold_low, float threshold_high) {
|
||||
if (threshold_low > threshold_high) {
|
||||
ESP_LOGW(TAG, "Deadband threshold low %.2f must not be greater than high %.2f", threshold_low, threshold_high);
|
||||
@@ -191,7 +193,7 @@ void PIDClimate::start_autotune(std::unique_ptr<PIDAutotuner> &&autotune) {
|
||||
"Until your sensor provides a reading, the autotuner may display \'nan\'",
|
||||
this->get_name().c_str());
|
||||
|
||||
this->set_interval("autotune-progress", 10000, [this]() {
|
||||
this->set_interval(AUTOTUNE_PROGRESS_INTERVAL_ID, 10000, [this]() {
|
||||
if (this->autotuner_ != nullptr && !this->autotuner_->is_finished())
|
||||
this->autotuner_->dump_config();
|
||||
});
|
||||
|
||||
@@ -11,6 +11,8 @@ namespace esphome::remote_transmitter {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "remote_transmitter");
|
||||
|
||||
static constexpr uint32_t COMPLETE_TIMEOUT_ID = 0;
|
||||
|
||||
// Maximum RMT symbol duration (15-bit field)
|
||||
static constexpr uint32_t RMT_SYMBOL_DURATION_MAX = 0x7FFF;
|
||||
|
||||
@@ -206,7 +208,7 @@ void RemoteTransmitterComponent::wait_for_rmt_() {
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 1)
|
||||
void RemoteTransmitterComponent::flush_pending_completion() {
|
||||
// a frame still on the wire is waited out, and its completion reported, before the next one
|
||||
if (this->non_blocking_ && this->cancel_timeout("complete")) {
|
||||
if (this->non_blocking_ && this->cancel_timeout(COMPLETE_TIMEOUT_ID)) {
|
||||
this->wait_for_rmt_();
|
||||
}
|
||||
}
|
||||
@@ -292,7 +294,7 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
||||
this->status_clear_warning();
|
||||
|
||||
if (this->non_blocking_) {
|
||||
this->set_timeout("complete", total_duration / 1000, [this]() { this->wait_for_rmt_(); });
|
||||
this->set_timeout(COMPLETE_TIMEOUT_ID, total_duration / 1000, [this]() { this->wait_for_rmt_(); });
|
||||
} else {
|
||||
this->wait_for_rmt_();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ namespace esphome::scd30 {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "scd30");
|
||||
|
||||
static constexpr uint32_t STATUS_CHECK_INTERVAL_ID = 0;
|
||||
|
||||
static const uint16_t SCD30_CMD_GET_FIRMWARE_VERSION = 0xd100;
|
||||
static const uint16_t SCD30_CMD_START_CONTINUOUS_MEASUREMENTS = 0x0010;
|
||||
static const uint16_t SCD30_CMD_ALTITUDE_COMPENSATION = 0x5102;
|
||||
@@ -107,7 +109,7 @@ void SCD30Component::setup() {
|
||||
}
|
||||
|
||||
// check each 500ms if data is ready, and read it in that case
|
||||
this->set_interval("status-check", 500, [this]() {
|
||||
this->set_interval(STATUS_CHECK_INTERVAL_ID, 500, [this]() {
|
||||
if (this->is_data_ready_())
|
||||
this->update();
|
||||
});
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace esphome::spa06_base {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "spa06");
|
||||
|
||||
static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
|
||||
|
||||
// Sign extension function for <=16 bit types
|
||||
inline int16_t decode16(uint8_t msb, uint8_t lsb, size_t bits, size_t head = 0) {
|
||||
return static_cast<int16_t>(encode_uint16(msb, lsb) << head) >> (16 - bits);
|
||||
@@ -243,7 +245,7 @@ void SPA06Component::update() {
|
||||
}
|
||||
|
||||
// Queue a background task for retrieving the measurement
|
||||
this->set_timeout("measurement", this->conversion_time_, [this]() {
|
||||
this->set_timeout(MEASUREMENT_TIMEOUT_ID, this->conversion_time_, [this]() {
|
||||
float raw_temperature;
|
||||
float temperature = 0.0;
|
||||
float pressure = 0.0;
|
||||
|
||||
@@ -17,7 +17,9 @@ static constexpr uint8_t TRIGGER_ONESHOT_REGISTER = 0x24;
|
||||
static constexpr uint8_t MAX_READ_ATTEMPTS = 5;
|
||||
static constexpr uint8_t READ_RETRY_MS = 5;
|
||||
ESPHOME_LOG_TAG(TAG, "tfluna");
|
||||
static const char *const READ_RETRY_TIMEOUT = "read_retry";
|
||||
|
||||
static constexpr uint32_t SETUP_TIMEOUT_ID = 0;
|
||||
static constexpr uint32_t READ_RETRY_TIMEOUT_ID = 1;
|
||||
|
||||
void TFLuna::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "TF-Luna (i2c):");
|
||||
@@ -121,7 +123,7 @@ void TFLuna::read_data_timeout_() {
|
||||
} else {
|
||||
if (this->attempt_ < MAX_READ_ATTEMPTS) {
|
||||
this->attempt_++;
|
||||
this->set_timeout(READ_RETRY_TIMEOUT, READ_RETRY_MS, [this]() { this->read_data_timeout_(); });
|
||||
this->set_timeout(READ_RETRY_TIMEOUT_ID, READ_RETRY_MS, [this]() { this->read_data_timeout_(); });
|
||||
} else {
|
||||
this->status_set_warning("Hung device, restarting...");
|
||||
this->restart();
|
||||
@@ -130,7 +132,7 @@ void TFLuna::read_data_timeout_() {
|
||||
}
|
||||
|
||||
void TFLuna::update() {
|
||||
this->cancel_timeout(READ_RETRY_TIMEOUT);
|
||||
this->cancel_timeout(READ_RETRY_TIMEOUT_ID);
|
||||
this->attempt_ = 0;
|
||||
if (!this->write_byte(TRIGGER_ONESHOT_REGISTER, 0x01)) {
|
||||
this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
|
||||
@@ -145,7 +147,7 @@ void TFLuna::factory_reset() {
|
||||
return;
|
||||
}
|
||||
this->status_set_warning("Factory reset issued; waiting for device to become ready");
|
||||
this->set_timeout("_setup", 100, [this]() { this->setup(); });
|
||||
this->set_timeout(SETUP_TIMEOUT_ID, 100, [this]() { this->setup(); });
|
||||
}
|
||||
|
||||
void TFLuna::restart() {
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace esphome::tmp102 {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "tmp102");
|
||||
|
||||
static constexpr uint32_t READ_TEMP_TIMEOUT_ID = 0;
|
||||
|
||||
static const uint8_t TMP102_ADDRESS = 0x48;
|
||||
static const uint8_t TMP102_REGISTER_TEMPERATURE = 0x00;
|
||||
static const uint8_t TMP102_REGISTER_CONFIGURATION = 0x01;
|
||||
@@ -29,7 +31,7 @@ void TMP102Component::update() {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
this->set_timeout("read_temp", 50, [this]() {
|
||||
this->set_timeout(READ_TEMP_TIMEOUT_ID, 50, [this]() {
|
||||
int16_t raw_temperature;
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace esphome::tsl2561 {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "tsl2561");
|
||||
|
||||
static constexpr uint32_t READ_TIMEOUT_ID = 0;
|
||||
|
||||
static const uint8_t TSL2561_COMMAND_BIT = 0x80;
|
||||
static const uint8_t TSL2561_WORD_BIT = 0x20;
|
||||
static const uint8_t TSL2561_REGISTER_CONTROL = 0x00;
|
||||
@@ -60,7 +62,7 @@ void TSL2561Sensor::update() {
|
||||
// Make sure the data is there when we will read it.
|
||||
auto timeout = static_cast<uint32_t>(this->get_integration_time_ms_() + 20);
|
||||
|
||||
this->set_timeout("illuminance", timeout, [this]() { this->read_data_(); });
|
||||
this->set_timeout(READ_TIMEOUT_ID, timeout, [this]() { this->read_data_(); });
|
||||
}
|
||||
|
||||
float TSL2561Sensor::calculate_lx_(uint16_t ch0, uint16_t ch1) {
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace esphome::tsl2591 {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "tsl2591.sensor");
|
||||
|
||||
static constexpr uint32_t ADC_WAIT_INTERVAL_ID = 0;
|
||||
|
||||
// Various constants used in TSL2591 register manipulation
|
||||
#define TSL2591_COMMAND_BIT (0xA0) // 1010 0000: bits 7 and 5 for 'command, normal'
|
||||
#define TSL2591_ENABLE_POWERON (0x01) // Flag for ENABLE register, to enable
|
||||
@@ -163,19 +165,17 @@ void TSL2591Component::process_update_() {
|
||||
this->status_clear_warning();
|
||||
}
|
||||
|
||||
#define interval_name "tsl2591_interval_for_update"
|
||||
|
||||
void TSL2591Component::interval_function_for_update_() {
|
||||
if (!this->is_adc_valid()) {
|
||||
uint64_t now = millis();
|
||||
ESP_LOGD(TAG, "Elapsed %3llu ms; still waiting for valid ADC", (now - this->interval_start_));
|
||||
if (now > this->interval_timeout_) {
|
||||
ESP_LOGW(TAG, "Interval timeout for '%s' expired before ADCs became valid", this->name_);
|
||||
this->cancel_interval(interval_name);
|
||||
this->cancel_interval(ADC_WAIT_INTERVAL_ID);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this->cancel_interval(interval_name);
|
||||
this->cancel_interval(ADC_WAIT_INTERVAL_ID);
|
||||
this->process_update_();
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ void TSL2591Component::update() {
|
||||
} else {
|
||||
this->interval_start_ = millis();
|
||||
this->interval_timeout_ = this->interval_start_ + 620;
|
||||
this->set_interval(interval_name, 100, [this] { this->interval_function_for_update_(); });
|
||||
this->set_interval(ADC_WAIT_INTERVAL_ID, 100, [this] { this->interval_function_for_update_(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user