ESPHome  2022.9.1
modbus_select.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 
8 
9 namespace esphome {
10 namespace modbus_controller {
11 
12 class ModbusSelect : public Component, public select::Select, public SensorItem {
13  public:
15  bool force_new_range, std::vector<int64_t> mapping) {
16  this->register_type = ModbusRegisterType::HOLDING; // not configurable
17  this->sensor_value_type = sensor_value_type;
18  this->start_address = start_address;
19  this->offset = 0; // not configurable
20  this->bitmask = 0xFFFFFFFF; // not configurable
21  this->register_count = register_count;
22  this->response_bytes = 0; // not configurable
23  this->skip_updates = skip_updates;
24  this->force_new_range = force_new_range;
25  this->mapping_ = std::move(mapping);
26  }
27 
28  using transform_func_t =
29  std::function<optional<std::string>(ModbusSelect *const, int64_t, const std::vector<uint8_t> &)>;
31  std::function<optional<int64_t>(ModbusSelect *const, const std::string &, int64_t, std::vector<uint16_t> &)>;
32 
33  void set_parent(ModbusController *const parent) { this->parent_ = parent; }
34  void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
35  void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
37  void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
38 
39  void dump_config() override;
40  void parse_and_publish(const std::vector<uint8_t> &data) override;
41  void control(const std::string &value) override;
42 
43  protected:
44  std::vector<int64_t> mapping_;
46  bool use_write_multiple_{false};
47  bool optimistic_{false};
50 };
51 
52 } // namespace modbus_controller
53 } // namespace esphome
void set_write_template(write_transform_func_t &&f)
Definition: modbus_select.h:37
ModbusSelect(SensorValueType sensor_value_type, uint16_t start_address, uint8_t register_count, uint8_t skip_updates, bool force_new_range, std::vector< int64_t > mapping)
Definition: modbus_select.h:14
optional< transform_func_t > transform_func_
Definition: modbus_select.h:48
std::function< optional< std::string >(ModbusSelect *const, int64_t, const std::vector< uint8_t > &)> transform_func_t
Definition: modbus_select.h:29
void set_template(transform_func_t &&f)
Definition: modbus_select.h:36
void control(const std::string &value) override
void set_parent(ModbusController *const parent)
Definition: modbus_select.h:33
optional< write_transform_func_t > write_transform_func_
Definition: modbus_select.h:49
void parse_and_publish(const std::vector< uint8_t > &data) override
Base-class for all selects.
Definition: select.h:24
Definition: a4988.cpp:4
std::function< optional< int64_t >(ModbusSelect *const, const std::string &, int64_t, std::vector< uint16_t > &)> write_transform_func_t
Definition: modbus_select.h:31
void set_use_write_mutiple(bool use_write_multiple)
Definition: modbus_select.h:34