More work on some feedback

This commit is contained in:
Jimmy Wennlund 2024-09-08 23:04:25 +02:00
parent 8952ba13f8
commit 70ec4de7d3
2 changed files with 22 additions and 22 deletions

View file

@ -56,36 +56,36 @@ int32_t BL0910::read_register(uint8_t addr) {
return 0xFF000000; return 0xFF000000;
} }
return (((int8_t) packet[2]) << 16) | (packet[3] << 8) | packet[4]; return encode_uint24(packet[2], packet[3], packet[4]);
} }
float BL0910::getVoltage(uint8_t channel) { float BL0910::get_voltage(uint8_t channel) {
return ((float) read_register(BL0910_REG_RMS[channel])) / this->voltage_reference[channel]; return ((float) read_register(BL0910_REG_RMS[channel])) / this->voltage_reference[channel];
} }
float BL0910::getCurrent(uint8_t channel) { float BL0910::get_current(uint8_t channel) {
return ((float) read_register(BL0910_REG_RMS[channel])) / this->current_reference[channel]; return ((float) read_register(BL0910_REG_RMS[channel])) / this->current_reference[channel];
} }
float BL0910::getPower(uint8_t channel) { float BL0910::get_power(uint8_t channel) {
return ((float) read_register(BL0910_REG_WATT[channel])) / this->power_reference[channel]; return ((float) read_register(BL0910_REG_WATT[channel])) / this->power_reference[channel];
} }
float BL0910::getEnergy(uint8_t channel) { float BL0910::get_energy(uint8_t channel) {
return ((float) read_register(BL0910_REG_CF_CNT[channel])) / this->energy_reference[channel]; return ((float) read_register(BL0910_REG_CF_CNT[channel])) / this->energy_reference[channel];
} }
float BL0910::getFreq(void) { float BL0910::get_frequency(void) {
const float freq = (float) read_register(BL0910_REG_PERIOD); const float freq = (float) read_register(BL0910_REG_PERIOD);
return 10000000.0 / freq; return 10000000.0 / freq;
} }
float BL0910::getTemperature(void) { float BL0910::get_temperature(void) {
const float temp = (float) read_register(BL0910_REG_TPS1); const float temp = (float) read_register(BL0910_REG_TPS1);
return (temp - 64.0) * 12.5 / 59.0 - 40.0; return (temp - 64.0) * 12.5 / 59.0 - 40.0;
} }
float BL0910::getPowerFactor(uint8_t channel, float freq) { float BL0910::get_powerfactor(uint8_t channel, float freq) {
const float angle = (float) read_register(BL0910_REG_ANGLE[channel]); const float angle = (float) read_register(BL0910_REG_ANGLE[channel]);
return (360.0f * angle * freq) / 500000.0f; return (360.0f * angle * freq) / 500000.0f;
} }
@ -97,22 +97,22 @@ void BL0910::update() {
static float freq = 50.0; static float freq = 50.0;
if (i < NUM_CHANNELS) { if (i < NUM_CHANNELS) {
if (voltage_sensor[i]) if (voltage_sensor[i])
voltage_sensor[i]->publish_state(getVoltage(i)); voltage_sensor[i]->publish_state(get_voltage(i));
if (current_sensor[i]) if (current_sensor[i])
current_sensor[i]->publish_state(getCurrent(i)); current_sensor[i]->publish_state(get_current(i));
if (power_sensor[i]) if (power_sensor[i])
power_sensor[i]->publish_state(getPower(i)); power_sensor[i]->publish_state(get_power(i));
if (energy_sensor[i]) if (energy_sensor[i])
energy_sensor[i]->publish_state(getEnergy(i)); energy_sensor[i]->publish_state(get_energy(i));
if (power_factor_sensor[i]) if (power_factor_sensor[i])
power_factor_sensor[i]->publish_state(getPowerFactor(i, freq)); power_factor_sensor[i]->publish_state(get_powerfactor(i, freq));
i++; i++;
} else { } else {
freq = getFreq(); freq = get_frequency();
if (frequency_sensor) if (frequency_sensor)
frequency_sensor->publish_state(freq); frequency_sensor->publish_state(freq);
if (temperature_sensor) if (temperature_sensor)
temperature_sensor->publish_state(getTemperature()); temperature_sensor->publish_state(get_temperature());
i = 0; i = 0;
} }
} }

View file

@ -66,13 +66,13 @@ class BL0910 : public PollingComponent,
} }
void write_register(uint8_t addr, uint8_t data_h, uint8_t data_m, uint8_t data_l); 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); int32_t read_register(uint8_t addr);
float getVoltage(uint8_t channel); float get_voltage(uint8_t channel);
float getFreq(void); float get_frequency(void);
float getCurrent(uint8_t channel); float get_current(uint8_t channel);
float getPower(uint8_t channel); float get_power(uint8_t channel);
float getEnergy(uint8_t channel); float get_energy(uint8_t channel);
float getTemperature(void); float get_temperature(void);
float getPowerFactor(uint8_t channel, float freq); float get_powerfactor(uint8_t channel, float freq);
}; };
} // namespace bl0910 } // namespace bl0910
} // namespace esphome } // namespace esphome