ESPHome 2026.2.1
Loading...
Searching...
No Matches
one_wire.cpp
Go to the documentation of this file.
1#include "one_wire.h"
2
3namespace esphome {
4namespace one_wire {
5
6static const char *const TAG = "one_wire";
7
8const std::string &OneWireDevice::get_address_name() {
9 if (this->address_name_.empty()) {
10 char hex_buf[19]; // "0x" + 16 hex chars + null
11 this->address_name_ = format_hex_prefixed_to(hex_buf, this->address_);
12 }
13 return this->address_name_;
14}
15
17 if (!this->bus_->select(this->address_))
18 return false;
19 this->bus_->write8(cmd);
20 return true;
21}
22
24 if (this->address_ != 0)
25 return true;
26 auto devices = this->bus_->get_devices();
27
28 if (this->index_ != INDEX_NOT_SET) {
29 if (this->index_ >= devices.size()) {
30 ESP_LOGE(TAG, "Index %d out of range, only %d devices found", this->index_, devices.size());
31 return false;
32 }
33 this->address_ = devices[this->index_];
34 return true;
35 }
36
37 if (devices.empty()) {
38 ESP_LOGE(TAG, "No devices, can't auto-select address");
39 return false;
40 }
41 if (devices.size() > 1) {
42 ESP_LOGE(TAG, "More than one device, can't auto-select address");
43 return false;
44 }
45 this->address_ = devices[0];
46 return true;
47}
48
49} // namespace one_wire
50} // namespace esphome
const std::vector< uint64_t > & get_devices()
Return the list of found devices.
bool select(uint64_t address)
Select a specific address on the bus for the following command.
virtual void write8(uint8_t val)=0
Write a word to the bus. LSB first.
static constexpr uint8_t INDEX_NOT_SET
Definition one_wire.h:30
OneWireBus * bus_
pointer to OneWireBus instance
Definition one_wire.h:34
bool send_command_(uint8_t cmd)
send command on the bus
Definition one_wire.cpp:16
const std::string & get_address_name()
Helper to create (and cache) the name for this sensor. For example "0xfe0000031f1eaf29".
Definition one_wire.cpp:8
bool check_address_or_index_()
find an address if necessary should be called from setup
Definition one_wire.cpp:23
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
char * format_hex_prefixed_to(char(&buffer)[N], T val)
Format an unsigned integer as "0x" prefixed lowercase hex to buffer.
Definition helpers.h:959