mirror of
https://github.com/esphome/esphome.git
synced 2026-10-10 05:23:07 +00:00
[dallas_temp] Use a numeric scheduler id and drop the cached address string (#20361)
This commit is contained in:
@@ -5,6 +5,8 @@ namespace esphome::dallas_temp {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "dallas.temp.sensor");
|
||||
|
||||
static constexpr uint32_t CONVERSION_TIMEOUT_ID = 0;
|
||||
|
||||
static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
|
||||
static const uint8_t DALLAS_MODEL_DS18B20 = 0x28;
|
||||
static const uint8_t DALLAS_COMMAND_START_CONVERSION = 0x44;
|
||||
@@ -44,7 +46,7 @@ void DallasTemperatureSensor::update() {
|
||||
|
||||
this->send_command_(DALLAS_COMMAND_START_CONVERSION);
|
||||
|
||||
this->set_timeout(this->get_address_name().c_str(), this->millis_to_wait_for_conversion_(), [this] {
|
||||
this->set_timeout(CONVERSION_TIMEOUT_ID, this->millis_to_wait_for_conversion_(), [this] {
|
||||
if (!this->read_scratch_pad_() || !this->check_scratch_pad_()) {
|
||||
this->publish_state(NAN);
|
||||
return;
|
||||
|
||||
@@ -4,19 +4,6 @@ namespace esphome::one_wire {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "one_wire");
|
||||
|
||||
const std::string &OneWireDevice::get_address_name() {
|
||||
if (this->address_name_.empty()) {
|
||||
char hex_buf[19]; // "0x" + 16 hex chars + null
|
||||
this->address_name_ = format_hex_prefixed_to(hex_buf, this->address_);
|
||||
}
|
||||
return this->address_name_;
|
||||
}
|
||||
|
||||
void OneWireDevice::set_address(uint64_t address) {
|
||||
this->address_ = address;
|
||||
this->address_name_.clear();
|
||||
}
|
||||
|
||||
bool OneWireDevice::send_command_(uint8_t cmd) {
|
||||
if (!this->bus_->select(this->address_))
|
||||
return false;
|
||||
|
||||
@@ -7,14 +7,17 @@
|
||||
namespace esphome::one_wire {
|
||||
|
||||
#define LOG_ONE_WIRE_DEVICE(this) \
|
||||
ESP_LOGCONFIG(TAG, " Address: %s (%s)", this->get_address_name().c_str(), \
|
||||
LOG_STR_ARG(this->bus_->get_model_str(this->address_ & 0xff)));
|
||||
do { \
|
||||
char address_buf[format_hex_prefixed_size(sizeof(uint64_t))]; \
|
||||
ESP_LOGCONFIG(TAG, " Address: %s (%s)", format_hex_prefixed_to(address_buf, (this)->address_), \
|
||||
LOG_STR_ARG((this)->bus_->get_model_str((this)->address_ & 0xff))); \
|
||||
} while (0)
|
||||
|
||||
class OneWireDevice {
|
||||
public:
|
||||
/// @brief store the address of the device
|
||||
/// @param address of the device
|
||||
void set_address(uint64_t address);
|
||||
void set_address(uint64_t address) { this->address_ = address; }
|
||||
|
||||
void set_index(uint8_t index) { this->index_ = index; }
|
||||
|
||||
@@ -22,16 +25,12 @@ class OneWireDevice {
|
||||
/// @param bus pointer to the OneWireBus object
|
||||
void set_one_wire_bus(OneWireBus *bus) { this->bus_ = bus; }
|
||||
|
||||
/// Helper to create (and cache) the name for this sensor. For example "0xfe0000031f1eaf29".
|
||||
const std::string &get_address_name();
|
||||
|
||||
protected:
|
||||
static constexpr uint8_t INDEX_NOT_SET = 255;
|
||||
|
||||
uint64_t address_{0};
|
||||
uint8_t index_{INDEX_NOT_SET};
|
||||
OneWireBus *bus_{nullptr}; ///< pointer to OneWireBus instance
|
||||
std::string address_name_;
|
||||
|
||||
/// @brief find an address if necessary
|
||||
/// should be called from setup
|
||||
|
||||
Reference in New Issue
Block a user