espHome-NBS-files/external_components/analog_orp/analog_orp.cpp

139 lines
3.6 KiB
C++
Raw Permalink Normal View History

2024-05-10 10:37:19 +00:00
#include "analog_orp.h"
namespace esphome {
namespace analog_orp {
static const char *const TAG = "sensor.analog_orp";
2024-05-11 08:54:14 +00:00
FloatAverager::FloatAverager(size_t size): size_(size), vector_(size_){
this->index_ = 0;
}
void FloatAverager::add_value(float value){
vector_.at(index_++) = value;
if(index_ == size_){
index_ = 0;
has_filled_once_ = true;
}
}
float FloatAverager::create_average(){
float sum = 0;
for(auto point : vector_){
sum += point;
}
return sum / (float) size_;
}
bool FloatAverager::isFull(){
return index_ == 0;
}
bool FloatAverager::has_filled_once(){
return has_filled_once_;
}
2024-05-10 10:37:19 +00:00
void ChlorineSensor::set_pin(InternalGPIOPin *pin){
2024-05-11 08:54:14 +00:00
this->pin_ = pin;
2024-05-10 10:37:19 +00:00
}
void ChlorineSensor::set_zero_point(int zero_point){
2024-05-11 08:54:14 +00:00
this->zero_point_int_ = zero_point;
this->zero_point_ = ((float) zero_point / 1024.0f) * 3.3f; // Base Value for Clorine in Volt
2024-05-10 10:37:19 +00:00
}
void ChlorineSensor::set_print_raw(bool print_raw){
2024-05-11 08:54:14 +00:00
this->print_raw_ = print_raw;
2024-05-10 10:37:19 +00:00
}
void ChlorineSensor::set_inverted(bool inverted){
2024-05-11 08:54:14 +00:00
this->inverted_ = inverted;
}
void ChlorineSensor::init_averager(int mesurements, int send_average_every){
this->averager_ = new FloatAverager(mesurements);
this->send_averager_every_ = send_average_every;
}
void ChlorineSensor::add_read_callback(std::function<void(float)> &&callback){
this->on_value_read_.add(std::move(callback));
}
void ChlorineSensor::add_average_change_callback(std::function<void(float)> &&callback){
this->on_average_changed_.add(std::move(callback));
}
void ChlorineSensor::add_new_value_callback(std::function<void(float)> &&callback){
this->new_value_callback_.add(std::move(callback));
2024-05-10 10:37:19 +00:00
}
void ChlorineSensor::setup(){
ESP_LOGCONFIG(TAG, "Setting up AnalogOrp '%s'...", this->get_name().c_str());
2024-05-11 08:54:14 +00:00
pin_->setup();
2024-05-10 10:37:19 +00:00
ESP_LOGCONFIG(TAG, "AnalogOrp '%s' setup finished!", this->get_name().c_str());
}
void ChlorineSensor::update() {
float value_v = this->sample();
ESP_LOGV(TAG, "'%s': Got orp value=%.4fmV", this->get_name().c_str(), value_v);
2024-05-11 08:54:14 +00:00
if(averager_ != nullptr){
averager_->add_value(value_v);
on_value_read_.call(value_v);
if(++averager_send_counter_ >= send_averager_every_) averager_send_counter_ = 0;
if(!averager_->has_filled_once()) return;
float average = averager_->create_average();
on_average_changed_.call(average);
if(send_averager_every_ == -1){
if(averager_->isFull()){
this->publish_state(average);
}
}
else{
if(averager_->isFull()){
this->new_value_callback_.call(average);
}
if(averager_send_counter_ == 0){
this->publish_state(average);
}
}
return;
}
2024-05-10 10:37:19 +00:00
this->publish_state(value_v);
}
float ChlorineSensor::get_setup_priority() const { return setup_priority::DATA; }
void ChlorineSensor::dump_config(){
LOG_SENSOR("", "AnalogOrp Sensor", this);
LOG_UPDATE_INTERVAL(this);
2024-05-11 08:54:14 +00:00
LOG_PIN(" Pin: ", pin_);
ESP_LOGCONFIG(TAG, " Zero Point: %d", zero_point_int_);
ESP_LOGCONFIG(TAG, " Inverted: %s", inverted_ ? "true" : "false");
ESP_LOGCONFIG(TAG, " Print Raw: %s", print_raw_ ? "true" : "false");
2024-05-10 10:37:19 +00:00
}
bool ChlorineSensor::has_averager(){
return averager_ != nullptr;
}
2024-05-10 10:37:19 +00:00
/*
Nullwert : 680
*/
float ChlorineSensor::sample(){
2024-05-11 08:54:14 +00:00
float raw_read = (float) analogRead(pin_->get_pin());
if(print_raw_){
2024-05-10 10:37:19 +00:00
ESP_LOGD(TAG, "analogRead read a raw value of: %.0f", raw_read);
}
float raw_voltage = (raw_read / 1024.0f) * 3.3f;
2024-05-11 08:54:14 +00:00
float messured_voltage = raw_voltage - zero_point_;
2024-05-10 10:37:19 +00:00
2024-05-11 08:54:14 +00:00
if(inverted_){
2024-05-10 10:37:19 +00:00
messured_voltage = messured_voltage * -1.0f;
}
float milli_volt = messured_voltage * 1000.0f;
return milli_volt;
}
}
}