Skip to content

Commit

Permalink
Merge pull request #40 from orgua/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
orgua authored Jul 11, 2017
2 parents 8162fca + 9924e97 commit f143795
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=OneWireHub
version=2.1.1
version=2.1.2
author=Ingmar Splitt, orgua, MarkusLange, Shagrat2
maintainer=orgua
sentence=OneWire slave device emulator with support for up to 32 simultaneous 1wire devices.
Expand Down
30 changes: 30 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
#include <iostream>
#include <vector>
#include <array>

using namespace std;

Expand Down Expand Up @@ -40,6 +41,11 @@ constexpr int8_t operator "" _i8(const unsigned long long int value)
return static_cast<int8_t>(value);
}

constexpr int16_t operator "" _i16(const unsigned long long int value)
{
return static_cast<int16_t>(value);
}

constexpr int32_t operator "" _i32(const unsigned long long int value)
{
return static_cast<int32_t>(value);
Expand Down Expand Up @@ -183,6 +189,30 @@ int main()
}
}

{
// DS18B20 & DS1822 Datasheet examples, both same
const std::array<float,10> temp_degC { 125.0f, 85.0f, 25.0625f, 10.125f, 0.5f, 0.0f, -0.5f, -10.125f, -25.0625f, -55.0f };
const std::array<int16_t,10> temp_raw { 0x07D0, 0x0550, 0x0191, 0x00A2, 0x0008, 0x0000, 0xFFF8_i16, 0xFF5E_i16, 0xFE6F_i16, 0xFC90_i16 };

for (size_t index {0}; index < temp_degC.size(); ++index)
{
ds18B20.setTemperature(temp_degC[index]);
test_eq(ds18B20.getTemperatureRaw(), temp_raw[index], "DS18B22 datasheet test" + to_string(index));
}
}

{
// DS18S20 Datasheet examples
const std::array<float,7> temp_degC { 85.0f, 25.0f, 0.5f, 0.0f, -0.5f, -25.0f, -55.0f };
const std::array<int16_t,7> temp_raw { 0x00AA, 0x0032, 0x0001, 0x0000, 0xFFFF_i16, 0xFFCE_i16, 0xFF92_i16 };

for (size_t index {0}; index < temp_degC.size(); ++index)
{
ds18S20.setTemperature(temp_degC[index]);
test_eq(ds18S20.getTemperatureRaw(), temp_raw[index], "DS18S22 datasheet test" + to_string(index));
}
}

// TODO: look through src code, for now just converted ino-examples

{
Expand Down
26 changes: 10 additions & 16 deletions src/DS18B20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,24 @@ void DS18B20::setTemperatureRaw(const int16_t value_raw)
if (ds18s20_mode)
{
value /= 8; // deg*16/8 = deg*2 ...
if (value > 0)
if (value >= 0)
{
value &= 0x00FF; // upper byte is signum (0)
}
else
{
value = -value;
value |= 0xFF00; // upper byte is signum (1)
}
}
else
{
// normal 18b20, uses always 12bit mode! also 9,10,11,12 bit possible bitPosition seems to stay the same
if (value > 0)
if (value >= 0)
{
value &= 0x07FF; // upper 5 bits are signum
} else
}
else
{
value = -value;
value |= 0xF800;
}
}
Expand All @@ -116,26 +115,21 @@ void DS18B20::setTemperatureRaw(const int16_t value_raw)

int DS18B20::getTemperature(void) const
{
int16_t value = (scratchpad[1] << 8) | scratchpad[0];
int16_t value = getTemperatureRaw();

if (ds18s20_mode)
{
if ((scratchpad[1] & 0xF0) != 0)
{
value &= 0x00FF;
value = -value;
}
value /= 2;
}
else
{
if ((scratchpad[1] & 0xF0) != 0)
{
value &= 0x07FF;
value = -value;
}
value /= 16;
}

return value;
}

int16_t DS18B20::getTemperatureRaw() const
{
return static_cast<int16_t>((scratchpad[1] << 8) | scratchpad[0]);
}
5 changes: 3 additions & 2 deletions src/DS18B20.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class DS18B20 : public OneWireItem

void setTemperature(float value_degC); // -55 to +125 degC
void setTemperature(int8_t value_degC); // -55 to +125 degC
int getTemperature(void) const;
int getTemperature() const;

void setTemperatureRaw(int16_t value_raw);
void setTemperatureRaw(int16_t value_raw);
int16_t getTemperatureRaw() const;

};

Expand Down

0 comments on commit f143795

Please sign in to comment.