ESPHome  2022.9.1
modbus_number.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace modbus_controller {
9 
10 using value_to_data_t = std::function<float>(float);
11 
12 class ModbusNumber : public number::Number, public Component, public SensorItem {
13  public:
15  SensorValueType value_type, int register_count, uint8_t skip_updates, bool force_new_range) {
16  this->register_type = register_type;
17  this->start_address = start_address;
18  this->offset = offset;
19  this->bitmask = bitmask;
20  this->sensor_value_type = value_type;
21  this->register_count = register_count;
22  this->skip_updates = skip_updates;
23  this->force_new_range = force_new_range;
24  };
25 
26  void dump_config() override;
27  void parse_and_publish(const std::vector<uint8_t> &data) override;
28  float get_setup_priority() const override { return setup_priority::HARDWARE; }
29  void set_parent(ModbusController *parent) { this->parent_ = parent; }
30  void set_write_multiply(float factor) { multiply_by_ = factor; }
31 
32  using transform_func_t = std::function<optional<float>(ModbusNumber *, float, const std::vector<uint8_t> &)>;
33  using write_transform_func_t = std::function<optional<float>(ModbusNumber *, float, std::vector<uint16_t> &)>;
36  void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
37 
38  protected:
39  void control(float value) override;
43  float multiply_by_{1.0};
44  bool use_write_multiple_{false};
45 };
46 
47 } // namespace modbus_controller
48 } // namespace esphome
void set_parent(ModbusController *parent)
Definition: modbus_number.h:29
void set_write_template(write_transform_func_t &&f)
Definition: modbus_number.h:35
optional< write_transform_func_t > write_transform_func_
Definition: modbus_number.h:41
void set_template(transform_func_t &&f)
Definition: modbus_number.h:34
std::function< float >(float) value_to_data_t
Definition: modbus_number.h:10
std::function< optional< float >(ModbusNumber *, float, const std::vector< uint8_t > &)> transform_func_t
Definition: modbus_number.h:32
void parse_and_publish(const std::vector< uint8_t > &data) override
Base-class for all numbers.
Definition: number.h:29
void set_use_write_mutiple(bool use_write_multiple)
Definition: modbus_number.h:36
ModbusNumber(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset, uint32_t bitmask, SensorValueType value_type, int register_count, uint8_t skip_updates, bool force_new_range)
Definition: modbus_number.h:14
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
float get_setup_priority() const override
Definition: modbus_number.h:28
optional< transform_func_t > transform_func_
Definition: modbus_number.h:40
Definition: a4988.cpp:4
std::function< optional< float >(ModbusNumber *, float, std::vector< uint16_t > &)> write_transform_func_t
Definition: modbus_number.h:33
void control(float value) override