23 using ReadLambda = std::function<optional<int64_t>()>;
24 using WriteLambda = std::function<bool(int64_t value)>;
36 this->
read_lambda = [
this, user_read_lambda]() -> optional<int64_t> {
37 const optional<T> user_value = user_read_lambda(this->address);
38 if (!user_value.has_value()) {
41 if constexpr (std::is_same_v<T, float>) {
44 return static_cast<int64_t
>(*user_value);
51 this->
write_lambda = [
this, user_write_lambda](int64_t number) {
52 if constexpr (std::is_same_v<T, float>) {
54 return user_write_lambda(this->address, float_value);
56 return user_write_lambda(this->address,
static_cast<T
>(number));
66 const char *
format_value(int64_t value,
char *buf,
size_t buf_size)
const {
74 buf_append_printf(buf, buf_size, 0,
"%" PRIu64,
static_cast<uint64_t
>(value));
82 buf_append_printf(buf, buf_size, 0,
"%" PRId64, value);
86 buf_append_printf(buf, buf_size, 0,
"%.1f", bit_cast<float>(
static_cast<uint32_t>(value)));
89 buf_append_printf(buf, buf_size, 0,
"%" PRId64, value);
111 using ReadLambda = std::function<optional<bool>(uint16_t
address)>;
112 using WriteLambda = std::function<bool(uint16_t
address,
bool value)>;
160 .enabled =
false, .register_last_address = 0xFFFF, .register_value = 0};
Minimal static vector - saves memory by avoiding std::vector overhead.
Mutable counterpart of PackedBits: set() writes bits in place (deliberately no proxy operator[]=).
Read-only view of Modbus-packed bits: bit 0 of byte 0 is the first bit (LSB first),...
void dump_config() override
modbus::ResponseStatus on_write_registers(uint16_t start_address, const modbus::RegisterValues ®isters) final
called when a modbus request (function code 0x06 or 0x10) was parsed without errors
modbus::ResponseStatus on_read_bits(uint16_t start_address, modbus::MutablePackedBits bits) final
called when a modbus request (function code 0x01 or 0x02) was parsed without errors; both are served ...
void set_server_courtesy_response(const ServerCourtesyResponse &server_courtesy_response)
Called by esphome generated code to set the server courtesy response object.
ServerCourtesyResponse get_server_courtesy_response() const
Get the server courtesy response object.
void add_server_bit(ServerBit *server_bit)
Registers a server bit with the controller. Called by esphomes code generator.
ServerRegister * find_containing_register_(uint32_t address) const
Find the registered value whose register span contains address, or nullptr if none does.
std::vector< ServerBit * > server_bits_
Collection of all server bits (coils/discrete inputs) for this component.
std::vector< ServerRegister * > server_registers_
Collection of all server registers for this component.
modbus::ResponseStatus on_read_registers(uint16_t start_address, uint16_t number_of_registers, modbus::RegisterValues ®isters) final
called when a modbus request (function code 0x03 or 0x04) was parsed without errors
ServerBit * find_bit_(uint16_t address) const
Find the registered bit at address, or nullptr if none is.
modbus::ResponseStatus on_write_coils(uint16_t start_address, modbus::PackedBits bits) final
called when a modbus request (function code 0x05 or 0x0F) was parsed without errors
void add_server_register(ServerRegister *server_register)
Registers a server register with the controller. Called by esphomes code generator.
ServerCourtesyResponse server_courtesy_response_
Server courtesy response.
A single bit in the server's coil/discrete-input table.
void set_write_lambda(WriteLambda &&write_lambda)
ServerBit(uint16_t address)
void set_read_lambda(ReadLambda &&read_lambda)
void set_allow_partial_read(bool allow_partial_read)
void set_write_lambda(const std::function< bool(uint16_t address, const T v)> &&user_write_lambda)
SensorValueType value_type
const char * format_value(int64_t value, char *buf, size_t buf_size) const
ServerRegister(uint16_t address, SensorValueType value_type, uint8_t register_count)
void set_read_lambda(const std::function< optional< T >(uint16_t address)> &&user_read_lambda)
The user lambda returns optional<T>: an empty optional declines the read, answering the whole request...
static constexpr size_t FORMAT_VALUE_BUF_SIZE
std::optional< ExceptionCode > ResponseStatus
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
uint16_t register_last_address