Skip to content

Commit

Permalink
resetAddressSearch and addressSearch functions
Browse files Browse the repository at this point in the history
Introduced addressSearch and resetAddressSearch to speed up iteration of devices. Discussed in milesburton#154.
  • Loading branch information
gitkomodo committed May 22, 2020
1 parent 2f3ab6e commit 3605800
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 36 deletions.
32 changes: 25 additions & 7 deletions DallasTemperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void DallasTemperature::setOneWire(OneWire* _oneWire) {
bitResolution = 9;
waitForConversion = true;
checkForConversion = true;
autoSaveScratchPad = true;
autoSaveScratchPad = true;

}

Expand Down Expand Up @@ -153,7 +153,26 @@ bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) {
}

return false;
}

// resets internal variables used for the address search
void DallasTemperature::resetAddressSearch(void) {

_wire->reset_search();

}

// searches for the next device address
// if an additional device is found it's address is loaded into deviceAddress
// returns true if an additional device is found, false if there are no additional devices
bool DallasTemperature::addressSearch(uint8_t* deviceAddress) {

while (_wire->search(deviceAddress)) {
if (validAddress(deviceAddress)) return true;
}

return false;

}

// attempt to determine if the device at the given address is connected to the bus
Expand Down Expand Up @@ -250,10 +269,9 @@ void DallasTemperature::setResolution(uint8_t newResolution) {

bitResolution = constrain(newResolution, 9, 12);
DeviceAddress deviceAddress;
for (uint8_t i = 0; i < devices; i++) {
getAddress(deviceAddress, i);
resetAddressSearch();
while (addressSearch(deviceAddress))
setResolution(deviceAddress, bitResolution, true);
}
}

/* PROPOSAL */
Expand Down Expand Up @@ -315,11 +333,11 @@ bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
bitResolution = newResolution;
if (devices > 1)
{
for (uint8_t i = 0; i < devices; i++)
DeviceAddress deviceAddr;
resetAddressSearch();
while (addressSearch(deviceAddr))
{
if (bitResolution == 12) break;
DeviceAddress deviceAddr;
getAddress(deviceAddr, i);
uint8_t b = getResolution(deviceAddr);
if (b > bitResolution) bitResolution = b;
}
Expand Down
64 changes: 35 additions & 29 deletions DallasTemperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DallasTemperature {

void setOneWire(OneWire*);

void setPullupPin(uint8_t);
void setPullupPin(uint8_t);

// initialise bus
void begin(void);
Expand All @@ -68,11 +68,17 @@ class DallasTemperature {
bool validAddress(const uint8_t*);

// returns true if address is of the family of sensors the lib supports.
bool validFamily(const uint8_t* deviceAddress);
bool validFamily(const uint8_t*);

// finds an address at a given index on the bus
bool getAddress(uint8_t*, uint8_t);

// resets internal variables used for the address search
void resetAddressSearch(void);

// searches for the next device address
bool addressSearch(uint8_t*);

// attempt to determine if the device at the given address is connected to the bus
bool isConnected(const uint8_t*);

Expand Down Expand Up @@ -140,29 +146,29 @@ class DallasTemperature {
// Is a conversion complete on the wire? Only applies to the first sensor on the wire.
bool isConversionComplete(void);

int16_t millisToWaitForConversion(uint8_t);

// Sends command to one device to save values from scratchpad to EEPROM by index
// Returns true if no errors were encountered, false indicates failure
bool saveScratchPadByIndex(uint8_t);

// Sends command to one or more devices to save values from scratchpad to EEPROM
// Returns true if no errors were encountered, false indicates failure
bool saveScratchPad(const uint8_t* = nullptr);

// Sends command to one device to recall values from EEPROM to scratchpad by index
// Returns true if no errors were encountered, false indicates failure
bool recallScratchPadByIndex(uint8_t);

// Sends command to one or more devices to recall values from EEPROM to scratchpad
// Returns true if no errors were encountered, false indicates failure
bool recallScratchPad(const uint8_t* = nullptr);
int16_t millisToWaitForConversion(uint8_t);

// Sets the autoSaveScratchPad flag
void setAutoSaveScratchPad(bool);

// Gets the autoSaveScratchPad flag
bool getAutoSaveScratchPad(void);
// Sends command to one device to save values from scratchpad to EEPROM by index
// Returns true if no errors were encountered, false indicates failure
bool saveScratchPadByIndex(uint8_t);

// Sends command to one or more devices to save values from scratchpad to EEPROM
// Returns true if no errors were encountered, false indicates failure
bool saveScratchPad(const uint8_t* = nullptr);

// Sends command to one device to recall values from EEPROM to scratchpad by index
// Returns true if no errors were encountered, false indicates failure
bool recallScratchPadByIndex(uint8_t);

// Sends command to one or more devices to recall values from EEPROM to scratchpad
// Returns true if no errors were encountered, false indicates failure
bool recallScratchPad(const uint8_t* = nullptr);

// Sets the autoSaveScratchPad flag
void setAutoSaveScratchPad(bool);

// Gets the autoSaveScratchPad flag
bool getAutoSaveScratchPad(void);

#if REQUIRESALARMS

Expand Down Expand Up @@ -259,8 +265,8 @@ class DallasTemperature {
// used to requestTemperature to dynamically check if a conversion is complete
bool checkForConversion;

// used to determine if values will be saved from scratchpad to EEPROM on every scratchpad write
bool autoSaveScratchPad;
// used to determine if values will be saved from scratchpad to EEPROM on every scratchpad write
bool autoSaveScratchPad;

// count of devices on the bus
uint8_t devices;
Expand All @@ -279,9 +285,9 @@ class DallasTemperature {
// Returns true if all bytes of scratchPad are '\0'
bool isAllZeros(const uint8_t* const scratchPad, const size_t length = 9);

// External pullup control
void activateExternalPullup(void);
void deactivateExternalPullup(void);
// External pullup control
void activateExternalPullup(void);
void deactivateExternalPullup(void);

#if REQUIRESALARMS

Expand Down

0 comments on commit 3605800

Please sign in to comment.