91 lines
2.8 KiB
C++
91 lines
2.8 KiB
C++
#include "ads131m08_sensor.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace ads131m08 {
|
|
|
|
static const char *const TAG = "adsm131m08.sensor";
|
|
|
|
void Channel::loop()
|
|
{
|
|
if(this->first_reading_) {
|
|
if(this->parent_ != nullptr) {
|
|
this->parent_->set_channel_gain(this->channel_, this->gain_);
|
|
this->parent_->set_channel_input_selection(this->channel_, this->input_);
|
|
this->parent_->set_channel_phase_calibration(this->channel_, this->normalised_phase_calibration());
|
|
this->parent_->set_channel_offset_calibration(this->channel_, this->normalised_offset_calibration());
|
|
this->parent_->set_channel_gain_calibration(this->channel_, this->normalised_gain_calibration());
|
|
this->parent_->set_dcblock_filter_disable(this->channel_, true);
|
|
this->parent_->set_channel_enable(this->channel_, true);
|
|
this->first_reading_ = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
float Channel::sample()
|
|
{
|
|
return (this->parent_ == nullptr) ? NAN : this->parent_->get_sampled_value(this->channel_);
|
|
}
|
|
|
|
// converts gain of 0.0 - 2.0 to the corresponding values that ADS131M08 expects
|
|
uint32_t Channel::normalised_gain_calibration() const
|
|
{
|
|
uint32_t result = (uint32_t)lroundf(gain_cal_ * 8388608.0f);
|
|
return result & 0xFFFFFF;
|
|
}
|
|
|
|
uint32_t Channel::normalised_offset_calibration() const
|
|
{
|
|
uint32_t result = (uint32_t)offset_cal_;
|
|
return result & 0xFFFFFF;
|
|
}
|
|
|
|
int16_t Channel::normalised_phase_calibration() const
|
|
{
|
|
int16_t result = (int16_t)phase_cal_;
|
|
return result;
|
|
}
|
|
|
|
void Channel::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "Channel %" PRIu8 ":", this->channel_);
|
|
ESP_LOGCONFIG(TAG, " Gain: %" PRIu8, 1 << this->gain_);
|
|
ESP_LOGCONFIG(TAG, " Gain calibration: %f", this->gain_cal_);
|
|
ESP_LOGCONFIG(TAG, " Offset calibration: %" PRIu32, this->offset_cal_);
|
|
ESP_LOGCONFIG(TAG, " Phase calibration: %" PRId32, this->phase_cal_);
|
|
ESP_LOGCONFIG(TAG, " Input select: %s", (this->input_ == ICM_AIN0P_AIN0N) ? "normal" \
|
|
: (this->input_ == ICM_INPUT_SHORTED) ? "shorted" \
|
|
: (this->input_ == ICM_POSITIVE_DC_TEST_SIGNAL) ? "positive_dc" \
|
|
: (this->input_ == ICM_NEGATIVE_DC_TEST_SIGNAL) ? "negative_dc" \
|
|
: "invalid");
|
|
}
|
|
|
|
|
|
void RMS_Sensor::loop()
|
|
{
|
|
if(!this->set_calc_rms_) {
|
|
if(this->parent_ != nullptr) {
|
|
if(this->calc_rms_) {
|
|
this->set_calc_rms_ = this->parent_->set_calc_rms(true);
|
|
}
|
|
else {
|
|
this->set_calc_rms_ = true;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
float value = this->parent_->get_average(this->calc_rms_);
|
|
publish_state(value);
|
|
}
|
|
}
|
|
|
|
void RMS_Sensor::dump_config()
|
|
{
|
|
LOG_SENSOR(" ", "Sensor", this);
|
|
ESP_LOGCONFIG(TAG, " Calculate rms: %s", this->calc_rms_ ? "YES" : "NO");
|
|
}
|
|
|
|
} // namespace ads131m08_acdc
|
|
|
|
} // namespace esphome
|