Skip to content

Commit d621339

Browse files
author
Miles Burton
committed
fix: Resolved merge conflicts - take two
1 parent 7a4a5a6 commit d621339

File tree

2 files changed

+334
-546
lines changed

2 files changed

+334
-546
lines changed

DallasTemperature.cpp

+151-206
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#ifndef DallasTemperature_h
2-
#define DallasTemperature_h
3-
41
/*
52
MIT License
63
@@ -25,208 +22,156 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2522
SOFTWARE.
2623
*/
2724

28-
#define DALLASTEMPLIBVERSION "4.0.0"
29-
30-
// Configuration
31-
#ifndef REQUIRESNEW
32-
#define REQUIRESNEW false
33-
#endif
34-
35-
#ifndef REQUIRESALARMS
36-
#define REQUIRESALARMS true
37-
#endif
38-
39-
// Includes
40-
#include <inttypes.h>
41-
#ifdef __STM32F1__
42-
#include <OneWireSTM.h>
43-
#else
44-
#include <OneWire.h>
45-
#endif
46-
47-
// Constants for device models
48-
#define DS18S20MODEL 0x10 // also DS1820
49-
#define DS18B20MODEL 0x28 // also MAX31820
50-
#define DS1822MODEL 0x22
51-
#define DS1825MODEL 0x3B // also MAX31850
52-
#define DS28EA00MODEL 0x42
53-
54-
// Error Codes
55-
#define DEVICE_DISCONNECTED_C -127
56-
#define DEVICE_DISCONNECTED_F -196.6
57-
#define DEVICE_DISCONNECTED_RAW -7040
58-
59-
#define DEVICE_FAULT_OPEN_C -254
60-
#define DEVICE_FAULT_OPEN_F -425.199982
61-
#define DEVICE_FAULT_OPEN_RAW -32512
62-
63-
#define DEVICE_FAULT_SHORTGND_C -253
64-
#define DEVICE_FAULT_SHORTGND_F -423.399994
65-
#define DEVICE_FAULT_SHORTGND_RAW -32384
66-
67-
#define DEVICE_FAULT_SHORTVDD_C -252
68-
#define DEVICE_FAULT_SHORTVDD_F -421.599976
69-
#define DEVICE_FAULT_SHORTVDD_RAW -32256
70-
71-
// Configuration Constants
72-
#define MAX_CONVERSION_TIMEOUT 750
73-
#define MAX_INITIALIZATION_RETRIES 3
74-
#define INITIALIZATION_DELAY_MS 50
75-
76-
// nullptr definition for older C++
77-
#if __cplusplus < 201103L
78-
const class {
79-
public:
80-
template<class T>
81-
operator T*() const { return 0; }
82-
template<class C, class T>
83-
operator T C::*() const { return 0; }
84-
private:
85-
void operator&() const;
86-
} nullptr = {};
87-
#endif
88-
89-
// Type definitions
90-
typedef uint8_t DeviceAddress[8];
91-
92-
class DallasTemperature {
93-
public:
94-
// Constructors
95-
DallasTemperature();
96-
DallasTemperature(OneWire*);
97-
DallasTemperature(OneWire*, uint8_t);
98-
99-
// Setup & Configuration
100-
void setOneWire(OneWire*);
101-
void setPullupPin(uint8_t);
102-
void begin(void);
103-
bool verifyDeviceCount(void); // New method for device count verification
104-
105-
// Device Information
106-
uint8_t getDeviceCount(void);
107-
uint8_t getDS18Count(void);
108-
bool validAddress(const uint8_t*);
109-
bool validFamily(const uint8_t* deviceAddress);
110-
bool getAddress(uint8_t*, uint8_t);
111-
bool isConnected(const uint8_t*);
112-
bool isConnected(const uint8_t*, uint8_t*);
113-
114-
// Scratchpad Operations
115-
bool readScratchPad(const uint8_t*, uint8_t*);
116-
void writeScratchPad(const uint8_t*, const uint8_t*);
117-
bool readPowerSupply(const uint8_t* deviceAddress = nullptr);
118-
119-
// Resolution Control
120-
uint8_t getResolution();
121-
void setResolution(uint8_t);
122-
uint8_t getResolution(const uint8_t*);
123-
bool setResolution(const uint8_t*, uint8_t, bool skipGlobalBitResolutionCalculation = false);
124-
125-
// Conversion Configuration
126-
void setWaitForConversion(bool);
127-
bool getWaitForConversion(void);
128-
void setCheckForConversion(bool);
129-
bool getCheckForConversion(void);
130-
131-
// Temperature Request Structure
132-
struct request_t {
133-
bool result;
134-
unsigned long timestamp;
135-
operator bool() { return result; }
136-
};
137-
138-
// Temperature Operations
139-
request_t requestTemperatures(void);
140-
request_t requestTemperaturesByAddress(const uint8_t*);
141-
request_t requestTemperaturesByIndex(uint8_t);
142-
int32_t getTemp(const uint8_t*);
143-
float getTempC(const uint8_t*);
144-
float getTempF(const uint8_t*);
145-
float getTempCByIndex(uint8_t);
146-
float getTempFByIndex(uint8_t);
147-
148-
// Conversion Status
149-
bool isParasitePowerMode(void);
150-
bool isConversionComplete(void);
151-
uint16_t millisToWaitForConversion(uint8_t);
152-
uint16_t millisToWaitForConversion();
153-
154-
// EEPROM Operations
155-
bool saveScratchPadByIndex(uint8_t);
156-
bool saveScratchPad(const uint8_t* = nullptr);
157-
bool recallScratchPadByIndex(uint8_t);
158-
bool recallScratchPad(const uint8_t* = nullptr);
159-
void setAutoSaveScratchPad(bool);
160-
bool getAutoSaveScratchPad(void);
161-
162-
// Alarm Functionality
163-
#if REQUIRESALARMS
164-
typedef void AlarmHandler(const uint8_t*);
165-
void setHighAlarmTemp(const uint8_t*, int8_t);
166-
void setLowAlarmTemp(const uint8_t*, int8_t);
167-
int8_t getHighAlarmTemp(const uint8_t*);
168-
int8_t getLowAlarmTemp(const uint8_t*);
169-
void resetAlarmSearch(void);
170-
bool alarmSearch(uint8_t*);
171-
bool hasAlarm(const uint8_t*);
172-
bool hasAlarm(void);
173-
void processAlarms(void);
174-
void setAlarmHandler(const AlarmHandler*);
175-
bool hasAlarmHandler();
176-
#endif
177-
178-
// User Data Operations
179-
void setUserData(const uint8_t*, int16_t);
180-
void setUserDataByIndex(uint8_t, int16_t);
181-
int16_t getUserData(const uint8_t*);
182-
int16_t getUserDataByIndex(uint8_t);
183-
184-
// Temperature Conversion Utilities
185-
static float toFahrenheit(float);
186-
static float toCelsius(float);
187-
static float rawToCelsius(int32_t);
188-
static int16_t celsiusToRaw(float);
189-
static float rawToFahrenheit(int32_t);
190-
191-
// Memory Management
192-
#if REQUIRESNEW
193-
void* operator new(unsigned int);
194-
void operator delete(void*);
195-
#endif
196-
197-
// Conversion Completion Blocking
198-
void blockTillConversionComplete(uint8_t);
199-
void blockTillConversionComplete(uint8_t, unsigned long);
200-
void blockTillConversionComplete(uint8_t, request_t);
201-
202-
private:
203-
typedef uint8_t ScratchPad[9];
204-
205-
// Internal State
206-
bool parasite;
207-
bool useExternalPullup;
208-
uint8_t pullupPin;
209-
uint8_t bitResolution;
210-
bool waitForConversion;
211-
bool checkForConversion;
212-
bool autoSaveScratchPad;
213-
uint8_t devices;
214-
uint8_t ds18Count;
215-
OneWire* _wire;
216-
217-
// Internal Methods
218-
int32_t calculateTemperature(const uint8_t*, uint8_t*);
219-
bool isAllZeros(const uint8_t* const scratchPad, const size_t length = 9);
220-
void activateExternalPullup(void);
221-
void deactivateExternalPullup(void);
222-
223-
// Alarm Search Variables
25+
#include "DallasTemperature.h"
26+
27+
// OneWire commands
28+
#define STARTCONVO 0x44 // Tells device to take a temperature reading
29+
#define COPYSCRATCH 0x48 // Copy scratchpad to EEPROM
30+
#define READSCRATCH 0xBE // Read from scratchpad
31+
#define WRITESCRATCH 0x4E // Write to scratchpad
32+
#define RECALLSCRATCH 0xB8 // Recall from EEPROM to scratchpad
33+
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
34+
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
35+
36+
// Scratchpad locations
37+
#define TEMP_LSB 0
38+
#define TEMP_MSB 1
39+
#define HIGH_ALARM_TEMP 2
40+
#define LOW_ALARM_TEMP 3
41+
#define CONFIGURATION 4
42+
#define INTERNAL_BYTE 5
43+
#define COUNT_REMAIN 6
44+
#define COUNT_PER_C 7
45+
#define SCRATCHPAD_CRC 8
46+
47+
// Device resolution
48+
#define TEMP_9_BIT 0x1F // 9 bit
49+
#define TEMP_10_BIT 0x3F // 10 bit
50+
#define TEMP_11_BIT 0x5F // 11 bit
51+
#define TEMP_12_BIT 0x7F // 12 bit
52+
53+
DallasTemperature::DallasTemperature() {
22454
#if REQUIRESALARMS
225-
uint8_t alarmSearchAddress[8];
226-
int8_t alarmSearchJunction;
227-
uint8_t alarmSearchExhausted;
228-
AlarmHandler* _AlarmHandler;
55+
setAlarmHandler(nullptr);
22956
#endif
230-
};
231-
232-
#endif // DallasTemperature_h
57+
useExternalPullup = false;
58+
}
59+
60+
DallasTemperature::DallasTemperature(OneWire* _oneWire) : DallasTemperature() {
61+
setOneWire(_oneWire);
62+
}
63+
64+
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
65+
setPullupPin(_pullupPin);
66+
}
67+
68+
void DallasTemperature::setOneWire(OneWire* _oneWire) {
69+
_wire = _oneWire;
70+
devices = 0;
71+
ds18Count = 0;
72+
parasite = false;
73+
bitResolution = 9;
74+
waitForConversion = true;
75+
checkForConversion = true;
76+
autoSaveScratchPad = true;
77+
}
78+
79+
void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
80+
useExternalPullup = true;
81+
pullupPin = _pullupPin;
82+
pinMode(pullupPin, OUTPUT);
83+
deactivateExternalPullup();
84+
}
85+
86+
// Initialize the bus with retry logic
87+
void DallasTemperature::begin(void) {
88+
DeviceAddress deviceAddress;
89+
90+
for (uint8_t retry = 0; retry < MAX_INITIALIZATION_RETRIES; retry++) {
91+
_wire->reset_search();
92+
devices = 0;
93+
ds18Count = 0;
94+
95+
// Add delay for bus stabilization
96+
delay(INITIALIZATION_DELAY_MS);
97+
98+
while (_wire->search(deviceAddress)) {
99+
if (validAddress(deviceAddress)) {
100+
devices++;
101+
102+
if (validFamily(deviceAddress)) {
103+
ds18Count++;
104+
105+
if (!parasite && readPowerSupply(deviceAddress)) {
106+
parasite = true;
107+
}
108+
109+
uint8_t b = getResolution(deviceAddress);
110+
if (b > bitResolution) {
111+
bitResolution = b;
112+
}
113+
}
114+
}
115+
}
116+
117+
// If we found at least one device, exit retry loop
118+
if (devices > 0) {
119+
break;
120+
}
121+
}
122+
}
123+
124+
// Alternative device count verification method
125+
bool DallasTemperature::verifyDeviceCount(void) {
126+
uint8_t actualCount = 0;
127+
float temp;
128+
129+
requestTemperatures();
130+
131+
do {
132+
temp = getTempCByIndex(actualCount);
133+
if (temp > DEVICE_DISCONNECTED_C) {
134+
actualCount++;
135+
}
136+
} while (temp > DEVICE_DISCONNECTED_C && actualCount < 255);
137+
138+
if (actualCount > devices) {
139+
devices = actualCount;
140+
begin();
141+
return true;
142+
}
143+
144+
return false;
145+
}
146+
147+
// Returns the number of devices found on the bus
148+
uint8_t DallasTemperature::getDeviceCount(void) {
149+
return devices;
150+
}
151+
152+
uint8_t DallasTemperature::getDS18Count(void) {
153+
return ds18Count;
154+
}
155+
156+
bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
157+
switch (deviceAddress[0]) {
158+
case DS18S20MODEL:
159+
case DS18B20MODEL:
160+
case DS1822MODEL:
161+
case DS1825MODEL:
162+
case DS28EA00MODEL:
163+
return true;
164+
default:
165+
return false;
166+
}
167+
}
168+
169+
bool DallasTemperature::validAddress(const uint8_t* deviceAddress) {
170+
return (_wire->crc8(const_cast<uint8_t*>(deviceAddress), 7) == deviceAddress[7]);
171+
}
172+
173+
bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) {
174+
if (index < devices) {
175+
uint8_t depth = 0;
176+
177+
_wire->reset_

0 commit comments

Comments
 (0)