ESPHome  2022.8.0
modbus_output.cpp
Go to the documentation of this file.
1 #include <vector>
2 #include "modbus_output.h"
3 #include "esphome/core/log.h"
4 #include "esphome/core/helpers.h"
5 
6 namespace esphome {
7 namespace modbus_controller {
8 
9 static const char *const TAG = "modbus_controller.output";
10 
14 void ModbusFloatOutput::write_state(float value) {
15  std::vector<uint16_t> data;
16  auto original_value = value;
17  // Is there are lambda configured?
18  if (this->write_transform_func_.has_value()) {
19  // data is passed by reference
20  // the lambda can fill the empty vector directly
21  // in that case the return value is ignored
22  auto val = (*this->write_transform_func_)(this, value, data);
23  if (val.has_value()) {
24  ESP_LOGV(TAG, "Value overwritten by lambda");
25  value = val.value();
26  } else {
27  ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
28  return;
29  }
30  } else {
31  value = multiply_by_ * value;
32  }
33  // lambda didn't set payload
34  if (data.empty()) {
35  data = float_to_payload(value, this->sensor_value_type);
36  }
37 
38  ESP_LOGD(TAG, "Updating register: start address=0x%X register count=%d new value=%.02f (val=%.02f)",
39  this->start_address, this->register_count, value, original_value);
40 
41  // Create and send the write command
42  ModbusCommandItem write_cmd;
43  if (this->register_count == 1 && !this->use_write_multiple_) {
45  } else {
47  this->register_count, data);
48  }
49  parent_->queue_command(write_cmd);
50 }
51 
53  ESP_LOGCONFIG(TAG, "Modbus Float Output:");
54  LOG_FLOAT_OUTPUT(this);
55  ESP_LOGCONFIG(TAG, " Device start address: 0x%X", this->start_address);
56  ESP_LOGCONFIG(TAG, " Register count: %d", this->register_count);
57  ESP_LOGCONFIG(TAG, " Value type: %d", static_cast<int>(this->sensor_value_type));
58 }
59 
60 // ModbusBinaryOutput
62  // This will be called every time the user requests a state change.
64  std::vector<uint8_t> data;
65 
66  // Is there are lambda configured?
67  if (this->write_transform_func_.has_value()) {
68  // data is passed by reference
69  // the lambda can fill the empty vector directly
70  // in that case the return value is ignored
71  auto val = (*this->write_transform_func_)(this, state, data);
72  if (val.has_value()) {
73  ESP_LOGV(TAG, "Value overwritten by lambda");
74  state = val.value();
75  } else {
76  ESP_LOGV(TAG, "Communication handled by lambda - exiting control");
77  return;
78  }
79  }
80  if (!data.empty()) {
81  ESP_LOGV(TAG, "Modbus binary output write raw: %s", format_hex_pretty(data).c_str());
83  this->parent_, data,
84  [this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
85  this->parent_->on_write_register_response(cmd.register_type, this->start_address, data);
86  });
87  } else {
88  ESP_LOGV(TAG, "Write new state: value is %s, type is %d address = %X, offset = %x", ONOFF(state),
89  (int) this->register_type, this->start_address, this->offset);
90 
91  // offset for coil and discrete inputs is the coil/register number not bytes
92  if (this->use_write_multiple_) {
93  std::vector<bool> states{state};
95  } else {
97  }
98  }
99  this->parent_->queue_command(cmd);
100 }
101 
103  ESP_LOGCONFIG(TAG, "Modbus Binary Output:");
104  LOG_BINARY_OUTPUT(this);
105  ESP_LOGCONFIG(TAG, " Device start address: 0x%X", this->start_address);
106  ESP_LOGCONFIG(TAG, " Register count: %d", this->register_count);
107  ESP_LOGCONFIG(TAG, " Value type: %d", static_cast<int>(this->sensor_value_type));
108 }
109 
110 } // namespace modbus_controller
111 } // namespace esphome
void queue_command(const ModbusCommandItem &command)
queues a modbus command in the send queue
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition: helpers.cpp:199
std::vector< uint16_t > float_to_payload(float value, SensorValueType value_type)
void on_write_register_response(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)
default delegate called by process_modbus_data when a response for a write response has retrieved fro...
static ModbusCommandItem create_write_single_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t value)
Create modbus write multiple registers command Function 16 (10hex) Write Multiple Registers...
bool has_value() const
Definition: optional.h:87
static ModbusCommandItem create_write_single_coil(ModbusController *modbusdevice, uint16_t address, bool value)
Create modbus write single registers command Function 05 (05hex) Write Single Coil.
void write_state(float value) override
Write a value to the device.
optional< write_transform_func_t > write_transform_func_
Definition: modbus_output.h:36
static ModbusCommandItem create_write_multiple_coils(ModbusController *modbusdevice, uint16_t start_address, const std::vector< bool > &values)
Create modbus write multiple registers command Function 15 (0Fhex) Write Multiple Coils...
static ModbusCommandItem create_write_multiple_command(ModbusController *modbusdevice, uint16_t start_address, uint16_t register_count, const std::vector< uint16_t > &values)
Create modbus read command Function code 02-04.
Definition: a4988.cpp:4
uint32_t val
Definition: datatypes.h:85
static ModbusCommandItem create_custom_command(ModbusController *modbusdevice, const std::vector< uint8_t > &values, std::function< void(ModbusRegisterType register_type, uint16_t start_address, const std::vector< uint8_t > &data)> &&handler=nullptr)
Create custom modbus command.
stm32_cmd_t * cmd
Definition: stm32flash.h:96
bool state
Definition: fan.h:34