#pragma once #include "esphome/core/component.h" #include "esphome/core/helpers.h" #include "esphome/components/sensor/sensor.h" #include "esphome/components/voltage_sampler/voltage_sampler.h" #include "../ads131m08.h" namespace esphome { namespace ads131m08 { class Channel : public sensor::Sensor, public Component, //PollingComponent, public voltage_sampler::VoltageSampler, public Parented { public: void loop() override; float sample() override; // void update() override; float get_setup_priority() const override { // After the base sensor has been initialized return setup_priority::DATA - 1.0f; } void set_gain(uint8_t value) { this->gain_ = value; } void dump_config() override; void set_channel_number(uint8_t channel) { this->channel_= channel; } void set_gain_calibration(float value) { this->gain_cal_ = value; } void set_offset_calibration(int value) { this->offset_cal_ = value; } void set_phase_calibration(int value) { this->phase_cal_ = value; } void set_dc_block(bool enable) { this->dc_block_ = enable; } bool set_calc_rms(bool enable) { this->calc_rms_ = enable; return this->parent_ != nullptr ? this->parent_->set_measure_rms(this->channel_, enable) : false; } //float get_average(bool read_ac) { return this->parent_->get_average(this->channel_, read_ac); } void set_mux_input(ADC_INPUT_CHANNEL_MUX value) { this->input_ = value; } uint32_t normalised_gain_calibration() const; uint32_t normalised_offset_calibration() const; int16_t normalised_phase_calibration() const; float sample(int32_t& raw_value); protected: uint8_t channel_; int phase_cal_; int offset_cal_; float gain_cal_; bool calc_rms_; bool dc_block_; uint8_t gain_; ADC_INPUT_CHANNEL_MUX input_; bool first_reading_{true}; uint64_t count_{0}; }; class RMS_Sensor : public sensor::Sensor, public Component, public Parented { public: void loop() override; void dump_config() override; void set_calc_rms(bool enable) { this->calc_rms_ = enable; } protected: bool calc_rms_{false}; bool set_calc_rms_{false}; }; } // namespace ads131m08 } // namespace esphome