Skip to content

Commit 03099b7

Browse files
committed
[test] Test for TMP12x driver
1 parent 98367b0 commit 03099b7

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

test/modm/driver/module.lb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def prepare(module, options):
2828
"modm:driver:drv832x_spi",
2929
"modm:driver:mcp2515",
3030
"modm:driver:block.allocator",
31+
"modm:driver:tmp12x",
3132
"modm:platform:gpio",
3233
":mock:spi.device",
3334
":mock:spi.master")
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2020, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include "tmp12x_test.hpp"
12+
13+
void Tmp12xTest::testTmp123TemperatureData()
14+
{
15+
// Test TMP121, TMP123 temperature conversion (12+1 bit, 1/16 °C resolution)
16+
// Testcases are example values from the TMP123 datasheet
17+
18+
// 0xE480, -55 °C
19+
const modm::Tmp123Temperature temp0{0xE480};
20+
TEST_ASSERT_EQUALS(temp0.getTemperatureInteger(), -55);
21+
TEST_ASSERT_EQUALS(temp0.getTemperatureFractional(), -55 * 16);
22+
TEST_ASSERT_EQUALS_FLOAT(temp0.getTemperatureFloat(), -55.f);
23+
// 0x4B00, 150 °C
24+
const modm::Tmp123Temperature temp1{0x4B00};
25+
TEST_ASSERT_EQUALS(temp1.getTemperatureInteger(), 150);
26+
TEST_ASSERT_EQUALS(temp1.getTemperatureFractional(), 150 * 16);
27+
TEST_ASSERT_EQUALS_FLOAT(temp1.getTemperatureFloat(), 150.f);
28+
// 0x0000, 0 °C
29+
const modm::Tmp123Temperature temp2{0x0000};
30+
TEST_ASSERT_EQUALS(temp2.getTemperatureInteger(), 0);
31+
TEST_ASSERT_EQUALS(temp2.getTemperatureFractional(), 0);
32+
TEST_ASSERT_EQUALS_FLOAT(temp2.getTemperatureFloat(), 0.f);
33+
// 0x0000, 0 °C, test 3 invalid lsb are ignored
34+
const modm::Tmp123Temperature temp3{0x0007};
35+
TEST_ASSERT_EQUALS(temp3.getTemperatureInteger(), 0);
36+
TEST_ASSERT_EQUALS(temp3.getTemperatureFractional(), 0);
37+
TEST_ASSERT_EQUALS_FLOAT(temp3.getTemperatureFloat(), 0.f);
38+
// 0xFFF8, -0.0625 °C
39+
const modm::Tmp123Temperature temp4{0xFFF8};
40+
TEST_ASSERT_EQUALS(temp4.getTemperatureInteger(), 0);
41+
TEST_ASSERT_EQUALS(temp4.getTemperatureFractional(), -1);
42+
TEST_ASSERT_EQUALS_FLOAT(temp4.getTemperatureFloat(), -0.0625f);
43+
}
44+
45+
void Tmp12xTest::testTmp125TemperatureData()
46+
{
47+
// Test TMP125 temperature conversion (10 bit, 1/4 °C resolution)
48+
// Testcases are example values from the TMP125 datasheet
49+
50+
// 0x6480, -55 °C
51+
const modm::Tmp125Temperature temp5{0x6480};
52+
TEST_ASSERT_EQUALS(temp5.getTemperatureInteger(), -55);
53+
TEST_ASSERT_EQUALS(temp5.getTemperatureFractional(), -55 * 4);
54+
TEST_ASSERT_EQUALS_FLOAT(temp5.getTemperatureFloat(), -55.f);
55+
// 0x3F80, 127 °C
56+
const modm::Tmp125Temperature temp6{0x3F80};
57+
TEST_ASSERT_EQUALS(temp6.getTemperatureInteger(), 127);
58+
TEST_ASSERT_EQUALS(temp6.getTemperatureFractional(), 127 * 4);
59+
TEST_ASSERT_EQUALS_FLOAT(temp6.getTemperatureFloat(), 127.f);
60+
// 0x0000, 0 °C
61+
const modm::Tmp125Temperature temp7{0x0000};
62+
TEST_ASSERT_EQUALS(temp7.getTemperatureInteger(), 0);
63+
TEST_ASSERT_EQUALS(temp7.getTemperatureFractional(), 0);
64+
TEST_ASSERT_EQUALS_FLOAT(temp7.getTemperatureFloat(), 0.f);
65+
// 0x0000, 0 °C, test bits 15,4-0 are ignored
66+
const modm::Tmp125Temperature temp8{uint16_t((1 << 15) | 0b11111)};
67+
TEST_ASSERT_EQUALS(temp8.getTemperatureInteger(), 0);
68+
TEST_ASSERT_EQUALS(temp8.getTemperatureFractional(), 0);
69+
TEST_ASSERT_EQUALS_FLOAT(temp8.getTemperatureFloat(), 0.f);
70+
// 0xFFF8, -0.0625 °C
71+
const modm::Tmp125Temperature temp9{0x7FE0};
72+
TEST_ASSERT_EQUALS(temp9.getTemperatureInteger(), 0);
73+
TEST_ASSERT_EQUALS(temp9.getTemperatureFractional(), -1);
74+
TEST_ASSERT_EQUALS_FLOAT(temp9.getTemperatureFloat(), -0.25f);
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2020, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <unittest/testsuite.hpp>
12+
#include <modm/driver/temperature/tmp12x.hpp>
13+
14+
/// @ingroup modm_test_test_driver
15+
class Tmp12xTest : public unittest::TestSuite
16+
{
17+
public:
18+
void
19+
testTmp123TemperatureData();
20+
21+
void
22+
testTmp125TemperatureData();
23+
};

0 commit comments

Comments
 (0)