ESPHome 2025.12.0-dev
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 this->address_name_ = std::string("0x") + format_hex(this->address_);
11 return this->address_name_;
12}
13
15 if (!this->bus_->select(this->address_))
16 return false;
17 this->bus_->write8(cmd);
18 return true;
19}
20
22 if (this->address_ != 0)
23 return true;
24 auto devices = this->bus_->get_devices();
25
26 if (this->index_ != INDEX_NOT_SET) {
27 if (this->index_ >= devices.size()) {
28 ESP_LOGE(TAG, "Index %d out of range, only %d devices found", this->index_, devices.size());
29 return false;
30 }
31 this->address_ = devices[this->index_];
32 return true;
33 }
34
35 if (devices.empty()) {
36 ESP_LOGE(TAG, "No devices, can't auto-select address");
37 return false;
38 }
39 if (devices.size() > 1) {
40 ESP_LOGE(TAG, "More than one device, can't auto-select address");
41 return false;
42 }
43 this->address_ = devices[0];
44 return true;
45}
46
47} // namespace one_wire
48} // 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:14
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:21
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition helpers.cpp:288