Skip to content

Commit 30f15de

Browse files
committed
Extend temperature conversion API
Add a stateful millisTowaitForConversion (class knows the resolution) Make millisToWaitForConversion(uint8_t) static Add celsiusToRaw Return signed int from celsiusToRaw Add a description to celsiusToRaw
1 parent 95566e8 commit 30f15de

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

DallasTemperature.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ uint16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
469469

470470
}
471471

472+
// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
473+
uint16_t DallasTemperature::millisToWaitForConversion() {
474+
return millisToWaitForConversion(bitResolution);
475+
}
476+
472477
// Sends command to one device to save values from scratchpad to EEPROM by index
473478
// Returns true if no errors were encountered, false indicates failure
474479
bool DallasTemperature::saveScratchPadByIndex(uint8_t deviceIndex) {
@@ -740,6 +745,19 @@ float DallasTemperature::rawToCelsius(int16_t raw) {
740745

741746
}
742747

748+
// Convert from Celsius to raw returns temperature in raw integer format.
749+
// The rounding error in the conversion is smaller than 0.01°C
750+
// where the resolution of the sensor is at best 0.0625°C (in 12 bit mode).
751+
// Rounding error can be verified by running:
752+
// for (float t=-55.; t<125.; t+=0.01)
753+
// {
754+
// Serial.println( DallasTemperature::rawToCelsius(DallasTemperature::celsiusToRaw(t))-t, 4 );
755+
// }
756+
int16_t DallasTemperature::celsiusToRaw(float celsius) {
757+
758+
return static_cast<uint16_t>( celsius * 128.f );
759+
}
760+
743761
// convert from raw to Fahrenheit
744762
float DallasTemperature::rawToFahrenheit(int16_t raw) {
745763

DallasTemperature.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ class DallasTemperature {
158158
// Is a conversion complete on the wire? Only applies to the first sensor on the wire.
159159
bool isConversionComplete(void);
160160

161-
int16_t millisToWaitForConversion(uint8_t);
161+
static uint16_t millisToWaitForConversion(uint8_t);
162+
163+
uint16_t millisToWaitForConversion();
162164

163165
// Sends command to one device to save values from scratchpad to EEPROM by index
164166
// Returns true if no errors were encountered, false indicates failure
@@ -244,6 +246,9 @@ class DallasTemperature {
244246
// convert from raw to Celsius
245247
static float rawToCelsius(int16_t);
246248

249+
// convert from Celsius to raw
250+
static int16_t celsiusToRaw(float);
251+
247252
// convert from raw to Fahrenheit
248253
static float rawToFahrenheit(int16_t);
249254

0 commit comments

Comments
 (0)