From c1fd7c01d6d710bc93a82679fabd796f8e016093 Mon Sep 17 00:00:00 2001 From: blah18 Date: Fri, 26 Jun 2026 23:25:04 +0700 Subject: [PATCH] Add optional addressBook output to begin() to capture discovered ROMs begin() already enumerates the bus but drops the ROM addresses it finds. Callers that need the addresses must run a second SearchROM via getAddress(), which can fail intermittently on long or marginal 1-Wire buses even though begin()'s own search just succeeded. Add two optional, defaulted parameters so begin() can copy each valid ROM found during enumeration into a caller-provided buffer (in discovery order, up to maxDeviceCount). Fully backward compatible: with no arguments begin() behaves exactly as before. --- DallasTemperature.cpp | 10 +++++++++- DallasTemperature.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/DallasTemperature.cpp b/DallasTemperature.cpp index 29b7bc5..a7a0af8 100644 --- a/DallasTemperature.cpp +++ b/DallasTemperature.cpp @@ -83,7 +83,7 @@ void DallasTemperature::setPullupPin(uint8_t _pullupPin) { deactivateExternalPullup(); } -void DallasTemperature::begin(void) { +void DallasTemperature::begin(uint8_t* addressBook, uint8_t maxDeviceCount) { DeviceAddress deviceAddress; for (uint8_t retry = 0; retry < MAX_INITIALIZATION_RETRIES; retry++) { @@ -95,6 +95,14 @@ void DallasTemperature::begin(void) { while (_wire->search(deviceAddress)) { if (validAddress(deviceAddress)) { + // Optionally capture the ROM discovered during enumeration into the + // caller-provided address book, so callers don't have to re-discover + // the same devices with getAddress()/search() afterwards — a second + // search pass that can fail intermittently on long or marginal buses. + if (addressBook && devices < maxDeviceCount) { + memcpy(addressBook + (size_t)devices * sizeof(DeviceAddress), + deviceAddress, sizeof(DeviceAddress)); + } devices++; if (validFamily(deviceAddress)) { diff --git a/DallasTemperature.h b/DallasTemperature.h index 49cb3f5..e7c6c13 100644 --- a/DallasTemperature.h +++ b/DallasTemperature.h @@ -77,7 +77,7 @@ class DallasTemperature { // Setup & Configuration void setOneWire(OneWire*); void setPullupPin(uint8_t); - void begin(void); + void begin(uint8_t* addressBook = nullptr, uint8_t maxDeviceCount = 0); bool verifyDeviceCount(void); // Device Information