From ed52557a3da5d597b22e92eaf1f21c302a828406 Mon Sep 17 00:00:00 2001 From: Jimmy Wennlund Date: Sun, 8 Sep 2024 23:18:57 +0200 Subject: [PATCH] Start scan in update(), and do the work in loop() --- esphome/components/bl0910/bl0910.cpp | 38 +++++++++++++--------------- esphome/components/bl0910/bl0910.h | 4 +++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/esphome/components/bl0910/bl0910.cpp b/esphome/components/bl0910/bl0910.cpp index ce036fd4a8..fb7822ee81 100644 --- a/esphome/components/bl0910/bl0910.cpp +++ b/esphome/components/bl0910/bl0910.cpp @@ -90,30 +90,28 @@ float BL0910::get_powerfactor(uint8_t channel, float freq) { return (360.0f * angle * freq) / 500000.0f; } -void BL0910::loop() {} +void BL0910::update() { this->current_channel_ = 0; } -void BL0910::update() { - static int i = 0; - static float freq = 50.0; - if (i < NUM_CHANNELS) { - if (voltage_sensor_[i]) - voltage_sensor_[i]->publish_state(get_voltage(i)); - if (current_sensor_[i]) - current_sensor_[i]->publish_state(get_current(i)); - if (power_sensor_[i]) - power_sensor_[i]->publish_state(get_power(i)); - if (energy_sensor_[i]) - energy_sensor_[i]->publish_state(get_energy(i)); - if (power_factor_sensor_[i]) - power_factor_sensor_[i]->publish_state(get_powerfactor(i, freq)); - i++; - } else { - freq = get_frequency(); +void BL0910::loop() { + if (this->current_channel_ < NUM_CHANNELS) { + if (voltage_sensor_[this->current_channel_]) + voltage_sensor_[this->current_channel_]->publish_state(get_voltage(this->current_channel_)); + if (current_sensor_[this->current_channel_]) + current_sensor_[this->current_channel_]->publish_state(get_current(this->current_channel_)); + if (power_sensor_[this->current_channel_]) + power_sensor_[this->current_channel_]->publish_state(get_power(this->current_channel_)); + if (energy_sensor_[this->current_channel_]) + energy_sensor_[this->current_channel_]->publish_state(get_energy(this->current_channel_)); + if (power_factor_sensor_[this->current_channel_]) + power_factor_sensor_[this->current_channel_]->publish_state(get_powerfactor(this->current_channel_, this->freq_)); + this->current_channel_++; + } else if (this->current_channel_ == NUM_CHANNELS) { + this->freq_ = get_frequency(); if (frequency_sensor_) - frequency_sensor_->publish_state(freq); + frequency_sensor_->publish_state(this->freq_); if (temperature_sensor_) temperature_sensor_->publish_state(get_temperature()); - i = 0; + this->current_channel_++; } } diff --git a/esphome/components/bl0910/bl0910.h b/esphome/components/bl0910/bl0910.h index 69440656f5..a08babf382 100644 --- a/esphome/components/bl0910/bl0910.h +++ b/esphome/components/bl0910/bl0910.h @@ -62,6 +62,10 @@ class BL0910 : public PollingComponent, float power_reference_[NUM_CHANNELS] = {}; float energy_reference_[NUM_CHANNELS] = {}; + // Current channel being read + uint8_t current_channel_ = 0; + float freq_ = 50.0; + protected: void write_register(uint8_t addr, uint32_t data) { return this->write_register(addr, (data >> 16) & 0xFF, (data >> 8) & 0xFF, data & 0xFF);