64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
#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,
|
|
public voltage_sampler::VoltageSampler,
|
|
public Parented<ADS131M08Hub> {
|
|
public:
|
|
void loop() override;
|
|
float sample() override;
|
|
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; }
|
|
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;
|
|
|
|
protected:
|
|
uint8_t channel_;
|
|
int phase_cal_;
|
|
int offset_cal_;
|
|
float gain_cal_;
|
|
bool calc_rms_;
|
|
uint8_t gain_;
|
|
ADC_INPUT_CHANNEL_MUX input_;
|
|
bool first_reading_{true};
|
|
|
|
};
|
|
|
|
class RMS_Sensor : public sensor::Sensor,
|
|
public Component,
|
|
public Parented<ads131m08::Channel> {
|
|
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
|