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