espHome-NBS-files/external_components/ezo_orp_i2c/ezo_orp.h

73 lines
2.0 KiB
C
Raw Normal View History

#pragma once
#include "esphome/components/sensor/sensor.h"
#include "esphome/core/component.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/automation.h"
#include "Arduino.h"
namespace esphome {
namespace ezo_orp_i2c {
enum SensorAction: int{
NOTHING = 0,
READ = 10,
CALIBRATE = 20,
IS_CALIBRATED = 21,
EXPORT_CALIBRATION = 30,
FACTORY_RESET = 40,
INFORMATION = 50,
};
class EzoOrpSensor: public PollingComponent, public sensor::Sensor, public i2c::I2CDevice {
public:
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const;
void calibrate_to_target(int target);
void print_info();
bool is_busy();
protected:
SensorAction scheduledAction_ = SensorAction::NOTHING; // THIS ACTS AS A SORT OF QUEUE
uint8_t scheduledActionData_[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int scheduledActionDataSize_ = 0;
int scheduledActionTime_ = 0;
bool busy_ = false;
void read_response(int maxLength, SensorAction action);
void runScheduledAction();
void scheduleNextAction(SensorAction action, uint8_t* data, int size, int expected_response_time);
bool send_command(const uint8_t* data, size_t dataSize);
bool send_command(const uint8_t* data, size_t dataSize, const uint8_t* parameterData, size_t parameterSize);
//bool read_data_(u_int8_t[] * read_data);
};
template<typename... Ts> class EzoCalibrateAction : public Action<Ts...> {
public:
EzoCalibrateAction(EzoOrpSensor *sensor) : sensor_(sensor) {}
TEMPLATABLE_VALUE(float, value)
void play(Ts... x) override {
this->sensor_->calibrate_to_target((int) this->value_.value(x...));
}
protected:
EzoOrpSensor *sensor_;
};
template<typename... Ts> class DeviceInfoAction : public Action<Ts...> {
public:
DeviceInfoAction(EzoOrpSensor *sensor) : sensor_(sensor) {}
TEMPLATABLE_VALUE(float, value)
void play(Ts... x) override {
this->sensor_->print_info();
}
protected:
EzoOrpSensor *sensor_;
};
}
}