83 [[nodiscard]]
constexpr uint64_t
raw()
const {
return _raw; }
93 [[nodiscard]]
constexpr uint8_t
family()
const {
return static_cast<uint8_t
>(_raw & 0xFF); }
118[[nodiscard]] uint8_t
crc8(std::span<const uint8_t> data);
133[[nodiscard]] uint16_t
crc16(std::span<const uint8_t> data, uint16_t crc_iv = 0);
145check_crc16(std::span<const uint8_t> data, std::span<const uint8_t, 2> inverted_crc, uint16_t crc_iv = 0);
171#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
211 return _mux->try_lock();
223 [[nodiscard]]
gpio pin() const noexcept {
return _pin; }
239#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
287#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
325#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
362#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
395#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
415 [[nodiscard]] std::vector<address>
search(uint8_t family_code,
size_t max_devices = 8) {
443 , _mux(std::
make_unique<std::recursive_mutex>()) {}
447 mutable std::unique_ptr<std::recursive_mutex> _mux;
465[[nodiscard]]
inline std::string
to_string(onewire::address addr) {
467 return "ONEWIRE_ANY";
470 return "ONEWIRE_NONE";
472 static constexpr char hex[] =
"0123456789ABCDEF";
475 auto raw = addr.raw();
476 for (
int i = 0; i < 8; ++i) {
480 auto byte =
static_cast<uint8_t
>((raw >> (i * 8)) & 0xFF);
482 result += hex[
byte & 0xF];
489#include "sdkconfig.h"
490#ifdef CONFIG_IDFXX_STD_FORMAT
496struct formatter<
idfxx::onewire::address> {
497 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
499 template<
typename FormatContext>
502 return std::copy(s.begin(), s.end(), ctx.out());
constexpr address()=default
Constructs an address with value 0 (equivalent to any()).
constexpr uint8_t family() const
Extracts the family code from the address.
constexpr uint64_t raw() const
Returns the underlying 64-bit ROM address.
constexpr bool operator==(const address &) const noexcept=default
Compares two addresses for equality.
constexpr auto operator<=>(const address &) const noexcept=default
Default three-way comparison for ordering.
constexpr address(uint64_t raw)
Constructs an address from a raw 64-bit value.
static constexpr address any()
Returns the wildcard address for single-device buses.
static constexpr address none()
Returns an invalid sentinel address indicating no device.
1-Wire bus controller with thread-safe access.
bool try_lock() const noexcept
Tries to acquire exclusive access without blocking.
void unlock() const
Releases exclusive access to the bus.
result< void > try_skip_rom()
Selects all devices on the bus.
result< void > try_read(std::span< uint8_t > buf)
Reads multiple bytes from the bus into a buffer.
result< std::vector< address > > try_search(uint8_t family_code, size_t max_devices=8)
Searches for devices with a specific family code.
void power()
Actively drives the bus high for parasitic power.
std::vector< address > search(size_t max_devices=8)
Searches for all devices on the bus.
static result< bus > make(gpio pin)
Creates a new 1-Wire bus controller.
uint8_t read()
Reads a single byte from the bus.
result< void > try_write(uint8_t value)
Writes a single byte to the bus.
void read(std::span< uint8_t > buf)
Reads multiple bytes from the bus into a buffer.
void lock() const
Acquires exclusive access to the bus.
void write(std::span< const uint8_t > data)
Writes multiple bytes to the bus.
gpio pin() const noexcept
Returns the GPIO pin.
result< void > try_power()
Actively drives the bus high for parasitic power.
result< void > try_select(address addr)
Selects a specific device by ROM address.
result< uint8_t > try_read()
Reads a single byte from the bus.
bus(gpio pin)
Creates a new 1-Wire bus controller.
void depower() noexcept
Stops driving power onto the bus.
bool reset()
Performs a 1-Wire reset cycle.
result< std::vector< address > > try_search(size_t max_devices=8)
Searches for all devices on the bus.
bus & operator=(bus &&other) noexcept=default
std::vector< address > search(uint8_t family_code, size_t max_devices=8)
Searches for devices with a specific family code.
bus & operator=(const bus &)=delete
void select(address addr)
Selects a specific device by ROM address.
result< void > try_write(std::span< const uint8_t > data)
Writes multiple bytes to the bus.
void write(uint8_t value)
Writes a single byte to the bus.
void skip_rom()
Selects all devices on the bus.
bus(bus &&other) noexcept=default
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
bool check_crc16(std::span< const uint8_t > data, std::span< const uint8_t, 2 > inverted_crc, uint16_t crc_iv=0)
Verifies a 16-bit CRC against received data.
uint8_t crc8(std::span< const uint8_t > data)
Computes a Dallas Semiconductor 8-bit CRC.
uint16_t crc16(std::span< const uint8_t > data, uint16_t crc_iv=0)
Computes a Dallas Semiconductor 16-bit CRC.
1-Wire bus protocol classes and utilities.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
result< std::unique_ptr< T > > make_unique(Args &&... args)
Constructs a T via its make() factory and wraps it in a std::unique_ptr.
std::expected< T, std::error_code > result
result type wrapping a value or error code.