ESPHome  2024.10.2
ezo_pmp.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
7 
8 #ifdef USE_BINARY_SENSOR
10 #endif
11 
12 #ifdef USE_SENSOR
14 #endif
15 
16 #ifdef USE_TEXT_SENSOR
18 #endif
19 
20 namespace esphome {
21 namespace ezo_pmp {
22 
23 class EzoPMP : public PollingComponent, public i2c::I2CDevice {
24  public:
25  void dump_config() override;
26  float get_setup_priority() const override { return setup_priority::DATA; };
27 
28  void loop() override;
29  void update() override;
30 
31 #ifdef USE_SENSOR
32  void set_current_volume_dosed(sensor::Sensor *current_volume_dosed) { current_volume_dosed_ = current_volume_dosed; }
33  void set_total_volume_dosed(sensor::Sensor *total_volume_dosed) { total_volume_dosed_ = total_volume_dosed; }
34  void set_absolute_total_volume_dosed(sensor::Sensor *absolute_total_volume_dosed) {
35  absolute_total_volume_dosed_ = absolute_total_volume_dosed;
36  }
37  void set_pump_voltage(sensor::Sensor *pump_voltage) { pump_voltage_ = pump_voltage; }
38  void set_last_volume_requested(sensor::Sensor *last_volume_requested) {
39  last_volume_requested_ = last_volume_requested;
40  }
41  void set_max_flow_rate(sensor::Sensor *max_flow_rate) { max_flow_rate_ = max_flow_rate; }
42 #endif
43 
44 #ifdef USE_BINARY_SENSOR
45  void set_is_dosing(binary_sensor::BinarySensor *is_dosing) { is_dosing_ = is_dosing; }
46  void set_is_paused(binary_sensor::BinarySensor *is_paused) { is_paused_ = is_paused; }
47 #endif
48 
49 #ifdef USE_TEXT_SENSOR
50  void set_dosing_mode(text_sensor::TextSensor *dosing_mode) { dosing_mode_ = dosing_mode; }
51  void set_calibration_status(text_sensor::TextSensor *calibration_status) { calibration_status_ = calibration_status; }
52 #endif
53 
54  // Actions for EZO-PMP
55  void find();
56  void dose_continuously();
57  void dose_volume(double volume);
58  void dose_volume_over_time(double volume, int duration);
59  void dose_with_constant_flow_rate(double volume, int duration);
60  void set_calibration_volume(double volume);
62  void clear_calibration();
63  void pause_dosing();
64  void stop_dosing();
65  void change_i2c_address(int address);
66  void exec_arbitrary_command(const std::basic_string<char> &command);
67 
68  protected:
69  uint32_t start_time_ = 0;
70  uint32_t wait_time_ = 0;
71  bool is_waiting_ = false;
72  bool is_first_read_ = true;
73 
74  uint16_t next_command_ = 0;
75  double next_command_volume_ = 0; // might be negative
77 
78  uint16_t next_command_queue_[10];
84 
85  uint16_t current_command_ = 0;
86  bool is_paused_flag_ = false;
87  bool is_dosing_flag_ = false;
88 
89  const char *arbitrary_command_{nullptr};
90 
91  void send_next_command_();
92  void read_command_result_();
94  void queue_command_(uint16_t command, double volume, int duration, bool should_schedule);
95  void pop_next_command_();
96  uint16_t peek_next_command_();
97 
98 #ifdef USE_SENSOR
105 #endif
106 
107 #ifdef USE_BINARY_SENSOR
110 #endif
111 
112 #ifdef USE_TEXT_SENSOR
115 #endif
116 };
117 
118 // Action Templates
119 template<typename... Ts> class EzoPMPFindAction : public Action<Ts...> {
120  public:
121  EzoPMPFindAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
122 
123  void play(Ts... x) override { this->ezopmp_->find(); }
124 
125  protected:
127 };
128 
129 template<typename... Ts> class EzoPMPDoseContinuouslyAction : public Action<Ts...> {
130  public:
131  EzoPMPDoseContinuouslyAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
132 
133  void play(Ts... x) override { this->ezopmp_->dose_continuously(); }
134 
135  protected:
137 };
138 
139 template<typename... Ts> class EzoPMPDoseVolumeAction : public Action<Ts...> {
140  public:
141  EzoPMPDoseVolumeAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
142 
143  void play(Ts... x) override { this->ezopmp_->dose_volume(this->volume_.value(x...)); }
144  TEMPLATABLE_VALUE(double, volume)
145 
146  protected:
147  EzoPMP *ezopmp_;
148 };
149 
150 template<typename... Ts> class EzoPMPDoseVolumeOverTimeAction : public Action<Ts...> {
151  public:
152  EzoPMPDoseVolumeOverTimeAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
153 
154  void play(Ts... x) override {
155  this->ezopmp_->dose_volume_over_time(this->volume_.value(x...), this->duration_.value(x...));
156  }
157  TEMPLATABLE_VALUE(double, volume)
158  TEMPLATABLE_VALUE(int, duration)
159 
160  protected:
162 };
163 
164 template<typename... Ts> class EzoPMPDoseWithConstantFlowRateAction : public Action<Ts...> {
165  public:
166  EzoPMPDoseWithConstantFlowRateAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
167 
168  void play(Ts... x) override {
169  this->ezopmp_->dose_with_constant_flow_rate(this->volume_.value(x...), this->duration_.value(x...));
170  }
171  TEMPLATABLE_VALUE(double, volume)
172  TEMPLATABLE_VALUE(int, duration)
173 
174  protected:
176 };
177 
178 template<typename... Ts> class EzoPMPSetCalibrationVolumeAction : public Action<Ts...> {
179  public:
180  EzoPMPSetCalibrationVolumeAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
181 
182  void play(Ts... x) override { this->ezopmp_->set_calibration_volume(this->volume_.value(x...)); }
183  TEMPLATABLE_VALUE(double, volume)
184 
185  protected:
186  EzoPMP *ezopmp_;
187 };
188 
189 template<typename... Ts> class EzoPMPClearTotalVolumeDispensedAction : public Action<Ts...> {
190  public:
191  EzoPMPClearTotalVolumeDispensedAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
192 
193  void play(Ts... x) override { this->ezopmp_->clear_total_volume_dosed(); }
194 
195  protected:
197 };
198 
199 template<typename... Ts> class EzoPMPClearCalibrationAction : public Action<Ts...> {
200  public:
201  EzoPMPClearCalibrationAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
202 
203  void play(Ts... x) override { this->ezopmp_->clear_calibration(); }
204 
205  protected:
207 };
208 
209 template<typename... Ts> class EzoPMPPauseDosingAction : public Action<Ts...> {
210  public:
211  EzoPMPPauseDosingAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
212 
213  void play(Ts... x) override { this->ezopmp_->pause_dosing(); }
214 
215  protected:
217 };
218 
219 template<typename... Ts> class EzoPMPStopDosingAction : public Action<Ts...> {
220  public:
221  EzoPMPStopDosingAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
222 
223  void play(Ts... x) override { this->ezopmp_->stop_dosing(); }
224 
225  protected:
227 };
228 
229 template<typename... Ts> class EzoPMPChangeI2CAddressAction : public Action<Ts...> {
230  public:
231  EzoPMPChangeI2CAddressAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
232 
233  void play(Ts... x) override { this->ezopmp_->change_i2c_address(this->address_.value(x...)); }
234  TEMPLATABLE_VALUE(int, address)
235 
236  protected:
237  EzoPMP *ezopmp_;
238 };
239 
240 template<typename... Ts> class EzoPMPArbitraryCommandAction : public Action<Ts...> {
241  public:
242  EzoPMPArbitraryCommandAction(EzoPMP *ezopmp) : ezopmp_(ezopmp) {}
243 
244  void play(Ts... x) override { this->ezopmp_->exec_arbitrary_command(this->command_.value(x...)); }
245  TEMPLATABLE_VALUE(std::string, command)
246 
247  protected:
248  EzoPMP *ezopmp_;
249 };
250 
251 } // namespace ezo_pmp
252 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
uint16_t peek_next_command_()
Definition: ezo_pmp.cpp:446
double next_command_volume_queue_[10]
Definition: ezo_pmp.h:79
sensor::Sensor * absolute_total_volume_dosed_
Definition: ezo_pmp.h:101
int next_command_duration_queue_[10]
Definition: ezo_pmp.h:80
void set_last_volume_requested(sensor::Sensor *last_volume_requested)
Definition: ezo_pmp.h:38
void set_max_flow_rate(sensor::Sensor *max_flow_rate)
Definition: ezo_pmp.h:41
uint16_t x
Definition: tt21100.cpp:17
uint16_t current_command_
Definition: ezo_pmp.h:85
EzoPMPFindAction(EzoPMP *ezopmp)
Definition: ezo_pmp.h:121
sensor::Sensor * last_volume_requested_
Definition: ezo_pmp.h:104
void update() override
Definition: ezo_pmp.cpp:49
void change_i2c_address(int address)
Definition: ezo_pmp.cpp:533
void set_dosing_mode(text_sensor::TextSensor *dosing_mode)
Definition: ezo_pmp.h:50
void set_total_volume_dosed(sensor::Sensor *total_volume_dosed)
Definition: ezo_pmp.h:33
text_sensor::TextSensor * dosing_mode_
Definition: ezo_pmp.h:113
binary_sensor::BinarySensor * is_paused_
Definition: ezo_pmp.h:109
This class simplifies creating components that periodically check a state.
Definition: component.h:283
sensor::Sensor * total_volume_dosed_
Definition: ezo_pmp.h:100
const char * arbitrary_command_
Definition: ezo_pmp.h:89
void exec_arbitrary_command(const std::basic_string< char > &command)
Definition: ezo_pmp.cpp:537
float get_setup_priority() const override
Definition: ezo_pmp.h:26
text_sensor::TextSensor * calibration_status_
Definition: ezo_pmp.h:114
void play(Ts... x) override
Definition: ezo_pmp.h:123
void set_is_dosing(binary_sensor::BinarySensor *is_dosing)
Definition: ezo_pmp.h:45
void queue_command_(uint16_t command, double volume, int duration, bool should_schedule)
Definition: ezo_pmp.cpp:454
void set_pump_voltage(sensor::Sensor *pump_voltage)
Definition: ezo_pmp.h:37
uint16_t next_command_
Definition: ezo_pmp.h:74
void set_absolute_total_volume_dosed(sensor::Sensor *absolute_total_volume_dosed)
Definition: ezo_pmp.h:34
void set_is_paused(binary_sensor::BinarySensor *is_paused)
Definition: ezo_pmp.h:46
void dose_volume_over_time(double volume, int duration)
Definition: ezo_pmp.cpp:495
void dose_volume(double volume)
Definition: ezo_pmp.cpp:489
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
void dose_with_constant_flow_rate(double volume, int duration)
Definition: ezo_pmp.cpp:501
sensor::Sensor * pump_voltage_
Definition: ezo_pmp.h:102
void set_calibration_volume(double volume)
Definition: ezo_pmp.cpp:507
uint16_t next_command_queue_[10]
Definition: ezo_pmp.h:78
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
sensor::Sensor * current_volume_dosed_
Definition: ezo_pmp.h:99
binary_sensor::BinarySensor * is_dosing_
Definition: ezo_pmp.h:108
Base-class for all sensors.
Definition: sensor.h:57
void loop() override
Definition: ezo_pmp.cpp:81
void set_calibration_status(text_sensor::TextSensor *calibration_status)
Definition: ezo_pmp.h:51
void dump_config() override
Definition: ezo_pmp.cpp:41
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
sensor::Sensor * max_flow_rate_
Definition: ezo_pmp.h:103
void set_current_volume_dosed(sensor::Sensor *current_volume_dosed)
Definition: ezo_pmp.h:32
double next_command_volume_
Definition: ezo_pmp.h:75