diff --git a/esphome/components/bl0910/bl0910.cpp b/esphome/components/bl0910/bl0910.cpp index d2fba1becd..94ae97b8ca 100644 --- a/esphome/components/bl0910/bl0910.cpp +++ b/esphome/components/bl0910/bl0910.cpp @@ -21,7 +21,7 @@ static uint8_t checksum_calc(const uint8_t *data) { return checksum; } -void BL0910::write_register(uint8_t addr, uint8_t data_h, uint8_t data_m, uint8_t data_l) { +void BL0910::write_register_(uint8_t addr, uint8_t data_h, uint8_t data_m, uint8_t data_l) { uint8_t packet[6] = {0}; packet[0] = BL0910_WRITE_COMMAND; packet[1] = addr; @@ -34,7 +34,7 @@ void BL0910::write_register(uint8_t addr, uint8_t data_h, uint8_t data_m, uint8_ this->disable(); } -int32_t BL0910::read_register(uint8_t addr) { +int32_t BL0910::read_register_(uint8_t addr) { uint8_t packet[6] = {0}; packet[0] = BL0910_READ_COMMAND; @@ -58,34 +58,34 @@ int32_t BL0910::read_register(uint8_t addr) { return encode_uint24(packet[2], packet[3], packet[4]); } -float BL0910::get_voltage(uint8_t channel) { - return ((float) this->read_register(BL0910_REG_RMS[channel])) / this->voltage_reference_[channel]; +float BL0910::get_voltage_(uint8_t channel) { + return ((float) this->read_register_(BL0910_REG_RMS[channel])) / this->voltage_reference_[channel]; } -float BL0910::get_current(uint8_t channel) { - return ((float) this->read_register(BL0910_REG_RMS[channel])) / this->current_reference_[channel]; +float BL0910::get_current_(uint8_t channel) { + return ((float) this->read_register_(BL0910_REG_RMS[channel])) / this->current_reference_[channel]; } -float BL0910::get_power(uint8_t channel) { - return ((float) this->read_register(BL0910_REG_WATT[channel])) / this->power_reference_[channel]; +float BL0910::get_power_(uint8_t channel) { + return ((float) this->read_register_(BL0910_REG_WATT[channel])) / this->power_reference_[channel]; } -float BL0910::get_energy(uint8_t channel) { - return ((float) this->read_register(BL0910_REG_CF_CNT[channel])) / this->energy_reference_[channel]; +float BL0910::get_energy_(uint8_t channel) { + return ((float) this->read_register_(BL0910_REG_CF_CNT[channel])) / this->energy_reference_[channel]; } -float BL0910::get_frequency() { - const float freq = (float) this->read_register(BL0910_REG_PERIOD); +float BL0910::get_frequency_() { + const float freq = (float) this->read_register_(BL0910_REG_PERIOD); return 10000000.0 / freq; } -float BL0910::get_temperature() { - const float temp = (float) this->read_register(BL0910_REG_TPS1); +float BL0910::get_temperature_() { + const float temp = (float) this->read_register_(BL0910_REG_TPS1); return (temp - 64.0) * 12.5 / 59.0 - 40.0; } -float BL0910::get_powerfactor(uint8_t channel, float freq) { - const float angle = (float) this->read_register(BL0910_REG_ANGLE[channel]); +float BL0910::get_powerfactor_(uint8_t channel, float freq) { + const float angle = (float) this->read_register_(BL0910_REG_ANGLE[channel]); return (360.0f * angle * freq) / 500000.0f; } @@ -94,40 +94,40 @@ void BL0910::update() { this->current_channel_ = 0; } void BL0910::loop() { if (this->current_channel_ < NUM_CHANNELS) { if (voltage_sensor_[this->current_channel_]) { - voltage_sensor_[this->current_channel_]->publish_state(this->get_voltage(this->current_channel_)); + voltage_sensor_[this->current_channel_]->publish_state(this->get_voltage_(this->current_channel_)); } if (current_sensor_[this->current_channel_]) { - current_sensor_[this->current_channel_]->publish_state(this->get_current(this->current_channel_)); + current_sensor_[this->current_channel_]->publish_state(this->get_current_(this->current_channel_)); } if (power_sensor_[this->current_channel_]) { - power_sensor_[this->current_channel_]->publish_state(this->get_power(this->current_channel_)); + power_sensor_[this->current_channel_]->publish_state(this->get_power_(this->current_channel_)); } if (energy_sensor_[this->current_channel_]) { - energy_sensor_[this->current_channel_]->publish_state(this->get_energy(this->current_channel_)); + energy_sensor_[this->current_channel_]->publish_state(this->get_energy_(this->current_channel_)); } if (power_factor_sensor_[this->current_channel_]) { power_factor_sensor_[this->current_channel_]->publish_state( - this->get_powerfactor(this->current_channel_, this->freq_)); + this->get_powerfactor_(this->current_channel_, this->freq_)); } this->current_channel_++; } else if (this->current_channel_ == NUM_CHANNELS) { - this->freq_ = this->get_frequency(); + this->freq_ = this->get_frequency_(); if (this->frequency_sensor_) this->frequency_sensor_->publish_state(this->freq_); if (this->temperature_sensor_) - this->temperature_sensor_->publish_state(this->get_temperature()); + this->temperature_sensor_->publish_state(this->get_temperature_()); this->current_channel_++; } } void BL0910::setup() { this->spi_setup(); - this->write_register(BL0910_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A); // Reset to default - this->write_register(BL0910_REG_USR_WRPROT, 0x00, 0x55, 0x55); // Enable User Operation Write - this->write_register(BL0910_REG_MODE, 0x00, 0x00, 0x00); - this->write_register(BL0910_REG_TPS_CTRL, 0x00, 0x07, 0xFF); - this->write_register(BL0910_REG_FAST_RMS_CTRL, 0x40, 0xFF, 0xFF); - this->write_register(BL0910_REG_GAIN1, 0x00, 0x00, 0x00); + this->write_register_(BL0910_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A); // Reset to default + this->write_register_(BL0910_REG_USR_WRPROT, 0x00, 0x55, 0x55); // Enable User Operation Write + this->write_register_(BL0910_REG_MODE, 0x00, 0x00, 0x00); + this->write_register_(BL0910_REG_TPS_CTRL, 0x00, 0x07, 0xFF); + this->write_register_(BL0910_REG_FAST_RMS_CTRL, 0x40, 0xFF, 0xFF); + this->write_register_(BL0910_REG_GAIN1, 0x00, 0x00, 0x00); } void BL0910::dump_config() { // NOLINT(readability-function-cognitive-complexity) diff --git a/esphome/components/bl0910/bl0910.h b/esphome/components/bl0910/bl0910.h index 2217c5c3a6..f402934bd2 100644 --- a/esphome/components/bl0910/bl0910.h +++ b/esphome/components/bl0910/bl0910.h @@ -66,18 +66,18 @@ class BL0910 : public PollingComponent, uint8_t current_channel_ = 0; float freq_ = 50.0; - void write_register(uint8_t addr, uint32_t data) { - return this->write_register(addr, (data >> 16) & 0xFF, (data >> 8) & 0xFF, data & 0xFF); + void write_register_(uint8_t addr, uint32_t data) { + return this->write_register_(addr, (data >> 16) & 0xFF, (data >> 8) & 0xFF, data & 0xFF); } - void write_register(uint8_t addr, uint8_t data_h, uint8_t data_m, uint8_t data_l); - int32_t read_register(uint8_t addr); - float get_voltage(uint8_t channel); - float get_frequency(); - float get_current(uint8_t channel); - float get_power(uint8_t channel); - float get_energy(uint8_t channel); - float get_temperature(); - float get_powerfactor(uint8_t channel, float freq); + void write_register_(uint8_t addr, uint8_t data_h, uint8_t data_m, uint8_t data_l); + int32_t read_register_(uint8_t addr); + float get_voltage_(uint8_t channel); + float get_frequency_(); + float get_current_(uint8_t channel); + float get_power_(uint8_t channel); + float get_energy_(uint8_t channel); + float get_temperature_(); + float get_powerfactor_(uint8_t channel, float freq); }; } // namespace bl0910 } // namespace esphome