ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
esphome::modbus::PackedBits Class Reference

Read-only view of Modbus-packed bits: bit 0 of byte 0 is the first bit (LSB first), the layout coil/discrete-input values use on the wire. More...

#include <modbus_definitions.h>

Public Member Functions

 PackedBits (std::span< const uint8_t > data, uint16_t count)
 
bool operator[] (size_t bit) const
 Value of the given bit; bit must be < size().
 
uint16_t size () const
 Number of bits in the view.
 
std::span< const uint8_t > bytes () const
 The underlying packed bytes: exactly ceil(size() / 8) bytes, even when the view was constructed over a larger buffer - forwarding this span onto the wire can never leak trailing buffer content.
 

Detailed Description

Read-only view of Modbus-packed bits: bit 0 of byte 0 is the first bit (LSB first), the layout coil/discrete-input values use on the wire.

Bundles the bit count with the packed bytes so the two cannot desynchronize. The view does not own the bytes - it is only valid while they are. Reads (operator[]) are unchecked by design - the caller owns the bit < size() precondition, as with any subscript. Writes and forwarding are defensive: set() drops out-of-range bits and bytes() clamps to the real span, because those paths touch buffers and the wire directly.

Definition at line 151 of file modbus_definitions.h.

Constructor & Destructor Documentation

◆ PackedBits()

esphome::modbus::PackedBits::PackedBits ( std::span< const uint8_t > data,
uint16_t count )
inline

Definition at line 153 of file modbus_definitions.h.

Member Function Documentation

◆ bytes()

std::span< const uint8_t > esphome::modbus::PackedBits::bytes ( ) const
inline

The underlying packed bytes: exactly ceil(size() / 8) bytes, even when the view was constructed over a larger buffer - forwarding this span onto the wire can never leak trailing buffer content.

Clamped to the actual span so a view over a too-short buffer stays detectable instead of UB.

Definition at line 161 of file modbus_definitions.h.

◆ operator[]()

bool esphome::modbus::PackedBits::operator[] ( size_t bit) const
inline

Value of the given bit; bit must be < size().

Definition at line 155 of file modbus_definitions.h.

◆ size()

uint16_t esphome::modbus::PackedBits::size ( ) const
inline

Number of bits in the view.

Definition at line 157 of file modbus_definitions.h.


The documentation for this class was generated from the following file: