ESPHome 2026.5.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 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 this->address_ = address;
18 this->address_name_.clear();
19}
20
22 if (!this->bus_->select(this->address_))
23 return false;
24 this->bus_->write8(cmd);
25 return true;
26}
27
29 if (this->address_ != 0)
30 return true;
31 auto devices = this->bus_->get_devices();
32
33 if (this->index_ != INDEX_NOT_SET) {
34 if (this->index_ >= devices.size()) {
35 ESP_LOGE(TAG, "Index %d out of range, only %d devices found", this->index_, devices.size());
36 return false;
37 }
38 this->address_ = devices[this->index_];
39 return true;
40 }
41
42 if (devices.empty()) {
43 ESP_LOGE(TAG, "No devices, can't auto-select address");
44 return false;
45 }
46 if (devices.size() > 1) {
47 ESP_LOGE(TAG, "More than one device, can't auto-select address");
48 return false;
49 }
50 this->address_ = devices[0];
51 return true;
52}
53
54} // namespace one_wire
55} // namespace esphome
uint8_t address
Definition bl0906.h:4
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
void set_address(uint64_t address)
store the address of the device
Definition one_wire.cpp:16
bool send_command_(uint8_t cmd)
send command on the bus
Definition one_wire.cpp:21
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:28
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:1349