-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Resolved merge conflicts - take one million
- Loading branch information
Miles Burton
committed
Jan 4, 2025
1 parent
f68e828
commit f72579e
Showing
1 changed file
with
64 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,69 @@ | ||
#include "DallasTemperature.h" | ||
#pragma once | ||
|
||
namespace Dallas { | ||
|
||
// Constructors | ||
DallasTemperature::DallasTemperature() = default; | ||
|
||
DallasTemperature::DallasTemperature(OneWire* oneWire) : oneWire(oneWire) {} | ||
|
||
DallasTemperature::DallasTemperature(OneWire* oneWire, uint8_t pullupPin) | ||
: oneWire(oneWire), pullupPin(pullupPin) {} | ||
|
||
// Configuration | ||
void DallasTemperature::begin() { | ||
if (!oneWire) return; | ||
oneWire->reset_search(); | ||
parasitePowerMode = false; | ||
bitResolution = 9; | ||
} | ||
|
||
void DallasTemperature::setOneWire(OneWire* oneWire) { | ||
this->oneWire = oneWire; | ||
} | ||
|
||
void DallasTemperature::setPullupPin(uint8_t pullupPin) { | ||
this->pullupPin = pullupPin; | ||
pinMode(pullupPin, OUTPUT); | ||
deactivateExternalPullup(); | ||
} | ||
|
||
// Device Management | ||
uint8_t DallasTemperature::getDeviceCount() const { | ||
uint8_t count = 0; | ||
DeviceAddress address; | ||
oneWire->reset_search(); | ||
while (oneWire->search(address.data())) { | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
uint8_t DallasTemperature::getDS18Count() const { | ||
uint8_t count = 0; | ||
DeviceAddress address; | ||
oneWire->reset_search(); | ||
while (oneWire->search(address.data())) { | ||
if (address[0] == 0x28) { // Match DS18B20 family code | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
bool DallasTemperature::isConnected(const DeviceAddress& address) const { | ||
ScratchPad scratchPad; | ||
return isConnected(address, scratchPad); | ||
} | ||
|
||
bool DallasTemperature::isConnected(const DeviceAddress& address, ScratchPad& scratchPad) const { | ||
return readScratchPad(address, scratchPad); | ||
} | ||
#include <stdint.h> | ||
#include <Arduino.h> | ||
#ifdef __STM32F1__ | ||
#include <OneWireSTM.h> | ||
#else | ||
#include <OneWire.h> | ||
#endif | ||
|
||
// Temperature Reading | ||
DallasTemperature::Request DallasTemperature::requestTemperatures() { | ||
Request req = {true, millis()}; | ||
oneWire->reset(); | ||
oneWire->skip(); | ||
oneWire->write(0x44, parasitePowerMode); // Start temperature conversion | ||
delay(750); // Wait for conversion | ||
return req; | ||
} | ||
|
||
float DallasTemperature::getTempC(const DeviceAddress& address, uint8_t retryCount) const { | ||
ScratchPad scratchPad; | ||
if (!isConnected(address, scratchPad)) { | ||
return -127.0f; // Disconnected | ||
} | ||
return rawToCelsius(calculateTemperature(address, scratchPad)); | ||
} | ||
|
||
float DallasTemperature::getTempF(const DeviceAddress& address, uint8_t retryCount) const { | ||
return toFahrenheit(getTempC(address, retryCount)); | ||
} | ||
|
||
// Utility Methods | ||
float DallasTemperature::toFahrenheit(float celsius) { | ||
return (celsius * 1.8f) + 32.0f; | ||
} | ||
|
||
float DallasTemperature::toCelsius(float fahrenheit) { | ||
return (fahrenheit - 32.0f) * 0.555555556f; | ||
} | ||
|
||
float DallasTemperature::rawToCelsius(int32_t raw) { | ||
return raw * 0.0078125f; | ||
} | ||
|
||
float DallasTemperature::rawToFahrenheit(int32_t raw) { | ||
return toFahrenheit(rawToCelsius(raw)); | ||
} | ||
|
||
// Internal Methods | ||
bool DallasTemperature::readScratchPad(const DeviceAddress& address, ScratchPad& scratchPad) const { | ||
oneWire->reset(); | ||
oneWire->select(address.data()); | ||
oneWire->write(0xBE); // Read Scratchpad | ||
for (auto& byte : scratchPad) { | ||
byte = oneWire->read(); | ||
} | ||
return true; | ||
} | ||
|
||
bool DallasTemperature::readPowerSupply(const DeviceAddress& address) const { | ||
oneWire->reset(); | ||
oneWire->select(address.data()); | ||
oneWire->write(0xB4); // Read Power Supply | ||
return oneWire->read_bit(); | ||
} | ||
|
||
void DallasTemperature::activateExternalPullup() const { | ||
if (pullupPin) { | ||
digitalWrite(pullupPin, LOW); | ||
} | ||
} | ||
|
||
void DallasTemperature::deactivateExternalPullup() const { | ||
if (pullupPin) { | ||
digitalWrite(pullupPin, HIGH); | ||
} | ||
} | ||
namespace Dallas { | ||
|
||
int32_t DallasTemperature::calculateTemperature(const DeviceAddress& address, const ScratchPad& scratchPad) const { | ||
int32_t rawTemp = ((static_cast<int16_t>(scratchPad[1]) << 8) | scratchPad[0]); | ||
return rawTemp; | ||
} | ||
class DallasTemperature { | ||
public: | ||
static constexpr const char* LIB_VERSION = "4.0.0"; | ||
|
||
using DeviceAddress = uint8_t[8]; | ||
using ScratchPad = uint8_t[9]; | ||
|
||
struct Request { | ||
bool result; | ||
unsigned long timestamp; | ||
operator bool() const { return result; } | ||
}; | ||
|
||
// Constructors | ||
DallasTemperature(); | ||
explicit DallasTemperature(OneWire* oneWire); | ||
DallasTemperature(OneWire* oneWire, uint8_t pullupPin); | ||
|
||
// Configuration | ||
void begin(); | ||
void setOneWire(OneWire* oneWire); | ||
void setPullupPin(uint8_t pullupPin); | ||
|
||
// Device Management | ||
uint8_t getDeviceCount() const; | ||
uint8_t getDS18Count() const; | ||
bool isConnected(const DeviceAddress& address) const; | ||
bool isConnected(const DeviceAddress& address, ScratchPad& scratchPad) const; | ||
|
||
// Temperature Reading | ||
Request requestTemperatures(); | ||
Request requestTemperaturesByAddress(const DeviceAddress& address); | ||
Request requestTemperaturesByIndex(uint8_t index); | ||
float getTempC(const DeviceAddress& address, uint8_t retryCount = 0) const; | ||
float getTempF(const DeviceAddress& address, uint8_t retryCount = 0) const; | ||
|
||
// Utility Methods | ||
static float toFahrenheit(float celsius); | ||
static float toCelsius(float fahrenheit); | ||
static float rawToCelsius(int32_t raw); | ||
static float rawToFahrenheit(int32_t raw); | ||
|
||
private: | ||
bool parasitePowerMode = false; | ||
uint8_t bitResolution = 9; | ||
OneWire* oneWire = nullptr; | ||
uint8_t pullupPin = 0; | ||
|
||
bool readScratchPad(const DeviceAddress& address, ScratchPad& scratchPad) const; | ||
bool readPowerSupply(const DeviceAddress& address) const; | ||
void activateExternalPullup() const; | ||
void deactivateExternalPullup() const; | ||
|
||
int32_t calculateTemperature(const DeviceAddress& address, const ScratchPad& scratchPad) const; | ||
}; | ||
|
||
} // namespace Dallas |