components/ads131m08/sensor/ads131m08_sensor.cpp

108 lines
3.4 KiB
C++

#include "ads131m08_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ads131m08 {
static const char *const TAG = "ads131m08.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_, !this->dc_block_);
this->parent_->set_channel_enable(this->channel_, true);
this->first_reading_ = false;
}
}
}
float Channel::sample()
{
int32_t raw_value = -1000;
return sample(raw_value);
}
float Channel::sample(int32_t& raw_value)
{
return (this->parent_ == nullptr) ? NAN : this->parent_->get_sample(raw_value, this->channel_/*, this->gain_, this->input_,
this->normalised_phase_calibration(), this->normalised_offset_calibration(), this->normalised_gain_calibration(), !this->dc_block_
*/);
}
//void Channel::update() {
// //int32_t raw_value = -1000;
// //float v = this->sample(raw_value);
// //if (!std::isnan(v)) {
// // if(this->count_ % 10 == 0) {
// // ESP_LOGI(TAG, "Ch%d: Raw=% 6ld %fV", this->channel_, raw_value, v);
// // }
// // this->count_++;
// //// ESP_LOGD(TAG, "Ch%d: Got Voltage=%fV", this->channel_, v);
// //// this->publish_state(v);
// //}
//}
// 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, " 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");
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, " DC block: %s", this->dc_block_ ? "YES" : "NO");
}
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;
}
}
}
}
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