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

136 lines
3.5 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "analog_orp.h"
namespace esphome {
namespace analog_orp {
static const char *const TAG = "sensor.analog_orp";
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_;
}
void ChlorineSensor::set_pin(InternalGPIOPin *pin){
this->pin_ = pin;
}
void ChlorineSensor::set_zero_point(int zero_point){
this->zero_point_int_ = zero_point;
this->zero_point_ = ((float) zero_point / 1024.0f) * 3.3f; // Base Value for Clorine in Volt
}
void ChlorineSensor::set_print_raw(bool print_raw){
this->print_raw_ = print_raw;
}
void ChlorineSensor::set_inverted(bool inverted){
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));
}
void ChlorineSensor::setup(){
ESP_LOGCONFIG(TAG, "Setting up AnalogOrp '%s'...", this->get_name().c_str());
pin_->setup();
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);
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;
}
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);
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");
}
/*
Nullwert : 680
*/
float ChlorineSensor::sample(){
float raw_read = (float) analogRead(pin_->get_pin());
if(print_raw_){
ESP_LOGD(TAG, "analogRead read a raw value of: %.0f", raw_read);
}
float raw_voltage = (raw_read / 1024.0f) * 3.3f;
float messured_voltage = raw_voltage - zero_point_;
if(inverted_){
messured_voltage = messured_voltage * -1.0f;
}
float milli_volt = messured_voltage * 1000.0f;
return milli_volt;
}
}
}