Skip to content

Commit

Permalink
DSTherm::getTemp2(), MAX31850::getTempInternal2() added
Browse files Browse the repository at this point in the history
  • Loading branch information
pstolarz committed Jan 28, 2024
1 parent e8ecb9d commit 126eab8
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 70 deletions.
12 changes: 3 additions & 9 deletions examples/arduino/DallasTemperature/DallasTemperature.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022 Piotr Stolarz
* Copyright (c) 2019-2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -114,15 +114,9 @@ static void printScratchpad(const DSTherm::Scratchpad& scrpd)
Serial.print("; Resolution:");
Serial.print(9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));

long temp = scrpd.getTemp();
long temp = scrpd.getTemp2();
Serial.print("; Temp:");
if (temp < 0) {
temp = -temp;
Serial.print('-');
}
Serial.print(temp / 1000);
Serial.print('.');
Serial.print(temp % 1000);
Serial.print((float)temp / 16);
Serial.print(" C");

Serial.println();
Expand Down
6 changes: 3 additions & 3 deletions examples/esp-idf/DallasTemperature/main/DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Piotr Stolarz
* Copyright (c) 2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -78,13 +78,13 @@ static void printScratchpad(const DSTherm::Scratchpad& scrpd)
scrpd.getTh(), scrpd.getTl(),
9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));

long temp = scrpd.getTemp();
long temp = scrpd.getTemp2();
printf("; Temp:");
if (temp < 0) {
temp = -temp;
printf("-");
}
printf("%d.%d C\n", (int)temp / 1000, (int)temp % 1000);
printf("%d.%04d C\n", (int)temp / 16, (10000 * ((int)temp % 16)) / 16);
}

void setup()
Expand Down
6 changes: 3 additions & 3 deletions examples/mbed-os/DallasTemperature/DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Piotr Stolarz
* Copyright (c) 2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -83,13 +83,13 @@ static void printScratchpad(const DSTherm::Scratchpad& scrpd)
scrpd.getTh(), scrpd.getTl(),
9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));

long temp = scrpd.getTemp();
long temp = scrpd.getTemp2();
printf("; Temp:");
if (temp < 0) {
temp = -temp;
printf("-");
}
printf("%d.%d C\n", (int)temp / 1000, (int)temp % 1000);
printf("%d.%04d C\n", (int)temp / 16, (10000 * ((int)temp % 16)) / 16);
}

void setup()
Expand Down
6 changes: 3 additions & 3 deletions examples/pico-sdk/DallasTemperature/DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Piotr Stolarz
* Copyright (c) 2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -109,13 +109,13 @@ static void printScratchpad(const DSTherm::Scratchpad& scrpd)
scrpd.getTh(), scrpd.getTl(),
9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));

long temp = scrpd.getTemp();
long temp = scrpd.getTemp2();
printf("; Temp:");
if (temp < 0) {
temp = -temp;
printf("-");
}
printf("%d.%d C\n", (int)temp / 1000, (int)temp % 1000);
printf("%d.%04d C\n", (int)temp / 16, (10000 * ((int)temp % 16)) / 16);
}

void setup()
Expand Down
56 changes: 28 additions & 28 deletions extras/test/t03_DSTherm_Test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021,2022 Piotr Stolarz
* Copyright (c) 2021,2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -78,69 +78,69 @@ class DSTherm_Test: OneWireNg
scrpd.setResolution(DSTherm::RES_12_BIT);

scrpd._scrpd[0] = 0xd0; scrpd._scrpd[1] = 0x07;
assert(scrpd.getTemp() == 125000);
assert(scrpd.getTemp() == 125000 && scrpd.getTemp2() == 2000);
scrpd._scrpd[0] = 0x50; scrpd._scrpd[1] = 0x05;
assert(scrpd.getTemp() == 85000);
assert(scrpd.getTemp() == 85000 && scrpd.getTemp2() == 1360);
scrpd._scrpd[0] = 0x91; scrpd._scrpd[1] = 0x01;
assert(scrpd.getTemp() == 25062);
assert(scrpd.getTemp() == 25062 && scrpd.getTemp2() == 401);
scrpd._scrpd[0] = 0xa2; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 10125);
assert(scrpd.getTemp() == 10125 && scrpd.getTemp2() == 162);
scrpd._scrpd[0] = 0x08; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 500);
assert(scrpd.getTemp() == 500 && scrpd.getTemp2() == 8);
scrpd._scrpd[0] = 0x00; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 0);
assert(scrpd.getTemp() == 0 && scrpd.getTemp2() == 0);
scrpd._scrpd[0] = 0xf8; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -500);
assert(scrpd.getTemp() == -500 && scrpd.getTemp2() == -8);
scrpd._scrpd[0] = 0x5e; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -10125);
assert(scrpd.getTemp() == -10125 && scrpd.getTemp2() == -162);
scrpd._scrpd[0] = 0x6f; scrpd._scrpd[1] = 0xfe;
assert(scrpd.getTemp() == -25062);
assert(scrpd.getTemp() == -25062 && scrpd.getTemp2() == -401);
scrpd._scrpd[0] = 0x90; scrpd._scrpd[1] = 0xfc;
assert(scrpd.getTemp() == -55000);
assert(scrpd.getTemp() == -55000 && scrpd.getTemp2() == -880);

/* as above but with 9-bits resolution */
scrpd.setResolution(DSTherm::RES_9_BIT);

scrpd._scrpd[0] = 0xd0; scrpd._scrpd[1] = 0x07;
assert(scrpd.getTemp() == 125000);
assert(scrpd.getTemp() == 125000 && scrpd.getTemp2() == 2000);
scrpd._scrpd[0] = 0x50; scrpd._scrpd[1] = 0x05;
assert(scrpd.getTemp() == 85000);
assert(scrpd.getTemp() == 85000 && scrpd.getTemp2() == 1360);
scrpd._scrpd[0] = 0x91; scrpd._scrpd[1] = 0x01;
assert(scrpd.getTemp() == 25000);
assert(scrpd.getTemp() == 25000 && scrpd.getTemp2() == 400);
scrpd._scrpd[0] = 0xa2; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 10000);
assert(scrpd.getTemp() == 10000 && scrpd.getTemp2() == 160);
scrpd._scrpd[0] = 0x08; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 500);
assert(scrpd.getTemp() == 500 && scrpd.getTemp2() == 8);
scrpd._scrpd[0] = 0x00; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 0);
assert(scrpd.getTemp() == 0 && scrpd.getTemp2() == 0);
scrpd._scrpd[0] = 0xf8; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -500);
assert(scrpd.getTemp() == -500 && scrpd.getTemp2() == -8);
scrpd._scrpd[0] = 0x5e; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -10500);
assert(scrpd.getTemp() == -10500 && scrpd.getTemp2() == -168);
scrpd._scrpd[0] = 0x6f; scrpd._scrpd[1] = 0xfe;
assert(scrpd.getTemp() == -25500);
assert(scrpd.getTemp() == -25500 && scrpd.getTemp2() == -408);
scrpd._scrpd[0] = 0x90; scrpd._scrpd[1] = 0xfc;
assert(scrpd.getTemp() == -55000);
assert(scrpd.getTemp() == -55000 && scrpd.getTemp2() == -880);

/* DS18S20 (const 9-bits resolution) */
scrpd._id[0] = DSTherm::DS18S20;
scrpd._scrpd[7] = 0x10;
scrpd._scrpd[6] = 0x0c;

scrpd._scrpd[0] = 0xaa; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 85000);
assert(scrpd.getTemp() == 85000 && scrpd.getTemp2() == 1360);
scrpd._scrpd[0] = 0x32; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 25000);
assert(scrpd.getTemp() == 25000 && scrpd.getTemp2() == 400);
scrpd._scrpd[0] = 0x01; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 500);
assert(scrpd.getTemp() == 500 && scrpd.getTemp2() == 8);
scrpd._scrpd[0] = 0x00; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 0);
assert(scrpd.getTemp() == 0 && scrpd.getTemp2() == 0);
scrpd._scrpd[0] = 0xff; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -500);
assert(scrpd.getTemp() == -500 && scrpd.getTemp2() == -8);
scrpd._scrpd[0] = 0xce; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -25000);
assert(scrpd.getTemp() == -25000 && scrpd.getTemp2() == -400);
scrpd._scrpd[0] = 0x92; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -55000);
assert(scrpd.getTemp() == -55000 && scrpd.getTemp2() == -880);

TEST_SUCCESS();
}
Expand Down
48 changes: 32 additions & 16 deletions extras/test/t04_MAX31850_Test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021,2022 Piotr Stolarz
* Copyright (c) 2021,2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand All @@ -26,21 +26,29 @@ class MAX31850_Test: OneWireNg
/* values taken from sensors data sheet */

scrpd._scrpd[0] = 0x00; scrpd._scrpd[1] = 0x64;
assert(scrpd.getTemp() == 1600000 && !scrpd.getFaultStatus());
assert(scrpd.getTemp() == 1600000 && scrpd.getTemp2() == 25600 &&
!scrpd.getFaultStatus());
scrpd._scrpd[0] = 0x81; scrpd._scrpd[1] = 0x3e;
assert(scrpd.getTemp() == 1000000 && scrpd.getFaultStatus());
assert(scrpd.getTemp() == 1000000 && scrpd.getTemp2() == 16000 &&
scrpd.getFaultStatus());
scrpd._scrpd[0] = 0x4e; scrpd._scrpd[1] = 0x06;
assert(scrpd.getTemp() == 100750 && !scrpd.getFaultStatus());
assert(scrpd.getTemp() == 100750 && scrpd.getTemp2() == 1612 &&
!scrpd.getFaultStatus());
scrpd._scrpd[0] = 0x93; scrpd._scrpd[1] = 0x01;
assert(scrpd.getTemp() == 25000 && scrpd.getFaultStatus());
assert(scrpd.getTemp() == 25000 && scrpd.getTemp2() == 400 &&
scrpd.getFaultStatus());
scrpd._scrpd[0] = 0x02; scrpd._scrpd[1] = 0x00;
assert(scrpd.getTemp() == 0 && !scrpd.getFaultStatus());
assert(scrpd.getTemp() == 0 && scrpd.getTemp2() == 0 &&
!scrpd.getFaultStatus());
scrpd._scrpd[0] = 0xfc; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -250 && !scrpd.getFaultStatus());
assert(scrpd.getTemp() == -250 && scrpd.getTemp2() == -4 &&
!scrpd.getFaultStatus());
scrpd._scrpd[0] = 0xf1; scrpd._scrpd[1] = 0xff;
assert(scrpd.getTemp() == -1000 && scrpd.getFaultStatus());
assert(scrpd.getTemp() == -1000 && scrpd.getTemp2() == -16 &&
scrpd.getFaultStatus());
scrpd._scrpd[0] = 0x63; scrpd._scrpd[1] = 0xf0;
assert(scrpd.getTemp() == -250000 && scrpd.getFaultStatus());
assert(scrpd.getTemp() == -250000 && scrpd.getTemp2() == -4000 &&
scrpd.getFaultStatus());

assert(!scrpd.getAddr());
for (size_t i = 0; i < sizeof(OneWireNg::Id); i++)
Expand All @@ -59,30 +67,38 @@ class MAX31850_Test: OneWireNg
/* values taken from sensors data sheet */

scrpd._scrpd[3] = 0x7f; scrpd._scrpd[2] = 0x00;
assert(scrpd.getTempInternal() == 127000 && !scrpd.getInputState());
assert(scrpd.getTempInternal() == 127000 &&
scrpd.getTempInternal2() == 2032 &&
!scrpd.getInputState());
scrpd._scrpd[3] = 0x64; scrpd._scrpd[2] = 0x91;
assert(scrpd.getTempInternal() == 100562 &&
scrpd.getTempInternal2() == 1609 &&
scrpd.getInputState() == MAX31850::INPUT_OC);
scrpd._scrpd[3] = 0x19; scrpd._scrpd[2] = 0x02;
assert(scrpd.getTempInternal() == 25000 &&
scrpd.getTempInternal2() == 400 &&
scrpd.getInputState() == MAX31850::INPUT_SCG);
scrpd._scrpd[3] = 0x00; scrpd._scrpd[2] = 0x04;
assert(scrpd.getTempInternal() == 0 &&
scrpd.getTempInternal2() == 0 &&
scrpd.getInputState() == MAX31850::INPUT_SCV);
scrpd._scrpd[3] = 0xff; scrpd._scrpd[2] = 0xff;
assert(scrpd.getTempInternal() == -62 &&
scrpd.getTempInternal2() == -1 &&
scrpd.getInputState() ==
(MAX31850::INPUT_OC | MAX31850::INPUT_SCG | MAX31850::INPUT_SCV));
(MAX31850::INPUT_OC | MAX31850::INPUT_SCG | MAX31850::INPUT_SCV));
scrpd._scrpd[3] = 0xff; scrpd._scrpd[2] = 0x00;
assert(scrpd.getTempInternal() == -1000 && !scrpd.getInputState());
assert(scrpd.getTempInternal() == -1000 &&
scrpd.getTempInternal2() == -16 &&
!scrpd.getInputState());
scrpd._scrpd[3] = 0xec; scrpd._scrpd[2] = 0x06;
assert(scrpd.getTempInternal() == -20000 &&
scrpd.getInputState() ==
(MAX31850::INPUT_SCG | MAX31850::INPUT_SCV));
scrpd.getTempInternal2() == -320 &&
scrpd.getInputState() == (MAX31850::INPUT_SCG | MAX31850::INPUT_SCV));
scrpd._scrpd[3] = 0xc9; scrpd._scrpd[2] = 0x03;
assert(scrpd.getTempInternal() == -55000 &&
scrpd.getInputState() ==
(MAX31850::INPUT_OC | MAX31850::INPUT_SCG));
scrpd.getTempInternal2() == -880 &&
scrpd.getInputState() == (MAX31850::INPUT_OC | MAX31850::INPUT_SCG));

assert(!scrpd.getAddr());
for (size_t i = 0; i < sizeof(OneWireNg::Id); i++)
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ getFamilyName KEYWORD2
getConversionTime KEYWORD2

getTemp KEYWORD2
getTemp2 KEYWORD2
getTh KEYWORD2
getTl KEYWORD2
setThl KEYWORD2
Expand All @@ -94,6 +95,7 @@ getRaw KEYWORD2

getFaultStatus KEYWORD2
getTempInternal KEYWORD2
getTempInternal2 KEYWORD2
getInputState KEYWORD2


Expand Down
26 changes: 24 additions & 2 deletions src/drivers/DSTherm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021,2022 Piotr Stolarz
* Copyright (c) 2021,2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -196,10 +196,32 @@ long DSTherm::Scratchpad::getTemp() const
if (_scrpd[7]) {
/* truncate fractional part */
temp = rsh(temp, 1) * 1000;
temp += (1000L * (int8_t)(_scrpd[7] - _scrpd[6]) / _scrpd[7]) - 250;
temp += ((1000L * (long)(int8_t)(_scrpd[7] - _scrpd[6])) / _scrpd[7]) - 250;
} else
#endif
temp = div2(temp * 1000, 1);
}
return temp;
}

long DSTherm::Scratchpad::getTemp2() const
{
long temp = ((long)(int8_t)_scrpd[1] << 8) | _scrpd[0];

if (_id[0] != DS18S20) {
unsigned res = (_scrpd[4] >> 5) & 3;

/* mask unused bits to zeroes */
temp &= ~(long)((1 << (3 - res)) - 1);
} else {
#if CONFIG_DS18S20_EXT_RES
if (_scrpd[7]) {
/* truncate fractional part, 16-scaled */
temp = (temp & ~1L) << 3;
temp += (((long)(int8_t)(_scrpd[7] - _scrpd[6]) << 4) / _scrpd[7]) - 4;
} else
#endif
temp <<= 3;
}
return temp;
}
21 changes: 18 additions & 3 deletions src/drivers/DSTherm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021,2022 Piotr Stolarz
* Copyright (c) 2021,2022,2024 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -45,13 +45,28 @@ class DSTherm
static const size_t LENGTH = 9;

/**
* Get temperature.
* Get temperature (1000 scaled).
*
* @return Temperature in Celsius degrees returned as fixed-point integer
* with multiplier 1000 , e.g. 20.125 C is returned as 20125.
* with multiplier 1000, e.g. 20.125 C is returned as 20125.
*/
long getTemp() const;

/**
* Get temperature (16 scaled).
*
* @return Temperature in Celsius degrees returned as fixed-point integer
* with multiplier 16, e.g. 20.125 C is returned as 322.
*
* @note Advantages using this routine over @c getTemp() are twofold:
* - It's computational faster.
* - It's more precise for temperatures with fractional part finer
* than 1/1000. For example 0.4375 will be truncated to 0.437 by
* @c getTemp(), whereas @c getTemp2() will return fractional part
* in full extend.
*/
long getTemp2() const;

/**
* Get Th.
* This is singed 1-byte integer (no fractional part) representing high
Expand Down
Loading

0 comments on commit 126eab8

Please sign in to comment.