ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ds248x_one_wire_bus.cpp
Go to the documentation of this file.
2#include "ds248x.h"
4#include "esphome/core/log.h"
5
6namespace esphome::ds248x {
7
8ESPHOME_LOG_TAG(TAG, "ds248x.one_wire");
9
11 ESP_LOGCONFIG(TAG, "Setting up DS248x 1-Wire Bus (Channel %d)...", this->channel_);
12
13 // Parent setup happens in DS248xComponent::setup()
14 // We just need to scan for devices on this channel
15 if (!this->ensure_channel_()) {
16 ESP_LOGE(TAG, "Failed to select channel %d during setup", this->channel_);
17 this->mark_failed();
18 return;
19 }
20
21 // Perform device search on this channel
22 this->search();
23
24 ESP_LOGCONFIG(TAG, "Found %zu devices on channel %d", this->devices_.size(), this->channel_);
25}
26
28 ESP_LOGCONFIG(TAG, "DS248x 1-Wire Bus (Channel %d):", this->channel_);
29 this->dump_devices_(TAG);
30}
31
33 if (this->parent_ == nullptr) {
34 ESP_LOGE(TAG, "Parent not set!");
35 return false;
36 }
37 return this->parent_->select_channel(this->channel_);
38}
39
41 if (!this->ensure_channel_()) {
42 return -1;
43 }
44
45 bool presence = false;
46 if (!this->parent_->ow_reset(presence)) {
47 return -1;
48 }
49 return presence ? 1 : 0;
50}
51
53 if (!this->ensure_channel_()) {
54 return;
55 }
56 if (!this->parent_->ow_write_byte(val)) {
57 ESP_LOGE(TAG, "Failed to write byte 0x%02X on channel %d", val, this->channel_);
58 }
59}
60
62 if (!this->ensure_channel_()) {
63 return;
64 }
65 for (uint8_t i = 0; i < 8; i++) {
66 uint8_t byte = static_cast<uint8_t>(val >> (i * 8));
67 if (!this->parent_->ow_write_byte(byte)) {
68 ESP_LOGE(TAG, "Failed to write byte %d/8 (0x%02X) on channel %d - aborting write64", i + 1, byte, this->channel_);
69 return; // Stop writing to prevent sending corrupted data
70 }
71 }
72}
73
75 if (!this->ensure_channel_()) {
76 return 0;
77 }
78 uint8_t value = 0;
79 if (!this->parent_->ow_read_byte(value)) {
80 ESP_LOGE(TAG, "Failed to read byte on channel %d", this->channel_);
81 }
82 return value;
83}
84
86 if (!this->ensure_channel_()) {
87 return 0;
88 }
89 uint64_t value = 0;
90 for (uint8_t i = 0; i < 8; i++) {
91 uint8_t byte = 0;
92 if (!this->parent_->ow_read_byte(byte)) {
93 ESP_LOGE(TAG, "Failed to read byte %d/8 on channel %d - returning partial data", i + 1, this->channel_);
94 return value; // Return partial data to avoid blocking, caller should validate
95 }
96 value |= (static_cast<uint64_t>(byte) << (i * 8));
97 }
98 return value;
99}
100
102 this->search_last_discrepancy_ = 0;
103 this->search_last_device_flag_ = false;
104 this->search_address_ = 0;
105}
106
108 if (!this->ensure_channel_()) {
109 return 0;
110 }
111
112 if (this->search_last_device_flag_) {
113 return 0;
114 }
115
116 uint8_t last_zero = 0;
117 uint64_t address = this->search_address_;
118
119 // Iterate through all 64 bits
120 for (uint8_t bit_number = 1; bit_number <= 64; bit_number++) {
121 uint64_t bit_mask = 1ULL << (bit_number - 1);
122
123 // Determine search direction
124 bool search_direction;
125 if (bit_number < this->search_last_discrepancy_) {
126 search_direction = (address & bit_mask) != 0;
127 } else {
128 search_direction = (bit_number == this->search_last_discrepancy_);
129 }
130
131 // Perform triplet operation
132 uint8_t status = 0;
133 if (!this->parent_->search_triplet(search_direction, status)) {
134 ESP_LOGW(TAG, "1-Wire triplet failed at bit %d on channel %d - aborting search", bit_number, this->channel_);
135 this->reset_search();
136 return 0;
137 }
138
139 bool id_bit = (status & DS248X_STATUS_SBR) != 0;
140 bool cmp_id_bit = (status & DS248X_STATUS_TSB) != 0;
141 bool dir_taken = (status & DS248X_STATUS_DIR) != 0;
142
143 if (id_bit && cmp_id_bit) {
144 // No devices participating
145 this->reset_search();
146 return 0;
147 }
148
149 if (!id_bit && !cmp_id_bit && !dir_taken) {
150 // Discrepancy, went 0 - record position
151 last_zero = bit_number;
152 }
153
154 // Update address based on direction taken
155 assign_bits(address, bit_mask, dir_taken);
156 }
157
158 // Search successful
159 this->search_last_discrepancy_ = last_zero;
160 if (last_zero == 0) {
161 this->search_last_device_flag_ = true;
162 }
163 this->search_address_ = address;
164
165 return address;
166}
167
168} // namespace esphome::ds248x
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool select_channel(uint8_t channel)
Definition ds248x.cpp:194
bool ow_reset(bool &presence)
Definition ds248x.cpp:229
bool ow_read_byte(uint8_t &byte)
Definition ds248x.cpp:280
bool ow_write_byte(uint8_t byte)
Definition ds248x.cpp:257
bool search_triplet(bool search_direction, uint8_t &status)
Definition ds248x.cpp:300
void write64(uint64_t val) override
bool ensure_channel_()
Select the channel on the DS248x before any 1-Wire operation.
std::vector< uint64_t > devices_
void dump_devices_(const char *tag)
log the found devices
void search()
Search for 1-Wire devices on the bus.
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "ds248x")
uint32_t value
Definition hal.cpp:41
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150