ESPHome  2024.12.2
one_wire.cpp
Go to the documentation of this file.
1 #include "one_wire.h"
2 
3 namespace esphome {
4 namespace one_wire {
5 
6 static const char *const TAG = "one_wire";
7 
8 const 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 
14 std::string OneWireDevice::unique_id() { return "dallas-" + str_lower_case(format_hex(this->address_)); }
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  if (devices.empty()) {
28  ESP_LOGE(TAG, "No devices, can't auto-select address");
29  return false;
30  }
31  if (devices.size() > 1) {
32  ESP_LOGE(TAG, "More than one device, can't auto-select address");
33  return false;
34  }
35  this->address_ = devices[0];
36  return true;
37 }
38 
39 } // namespace one_wire
40 } // namespace esphome
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:357
bool select(uint64_t address)
Select a specific address on the bus for the following command.
const std::vector< uint64_t > & get_devices()
Return the list of found devices.
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition: helpers.cpp:287
virtual void write8(uint8_t val)=0
Write a word to the bus. LSB first.
bool check_address_()
find an address if necessary should be called from setup
Definition: one_wire.cpp:23
const std::string & get_address_name()
Helper to create (and cache) the name for this sensor. For example "0xfe0000031f1eaf29".
Definition: one_wire.cpp:8
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
OneWireBus * bus_
pointer to OneWireBus instance
Definition: one_wire.h:31
bool send_command_(uint8_t cmd)
send command on the bus
Definition: one_wire.cpp:16
stm32_cmd_t * cmd
Definition: stm32flash.h:96