Skip to content

Commit 3811b71

Browse files
committed
[math] Remove Tolerance class in favor of function
1 parent 1fb1339 commit 3811b71

File tree

4 files changed

+18
-58
lines changed

4 files changed

+18
-58
lines changed

src/modm/architecture/interface/peripheral.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class PeripheralDriver
8888
static void
8989
assertBaudrateInTolerance()
9090
{
91-
static_assert(modm::Tolerance::isValueInTolerance(requested, available, tolerance),
91+
static_assert(modm::isValueInTolerance(requested, available, tolerance),
9292
"The closest available baudrate exceeds the tolerance of the requested baudrate!");
9393
}
9494
};

src/modm/math/tolerance.hpp

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2015, 2018, Niklas Hauser
2+
* Copyright (c) 2013-2015, 2018, 2024, Niklas Hauser
33
*
44
* This file is part of the modm project.
55
*
@@ -9,60 +9,20 @@
99
*/
1010
// ----------------------------------------------------------------------------
1111

12-
#ifndef MODM_MATH_TOLERANCE_HPP
13-
#define MODM_MATH_TOLERANCE_HPP
12+
#pragma once
1413

15-
#include <stdint.h>
1614
#include <modm/math/units.hpp>
15+
#include <cmath>
1716

1817
namespace modm
1918
{
2019

21-
/**
22-
* This class checks if values are within a certain tolerance.
23-
*
24-
* This can be used to guarantee the quality of certain parameters,
25-
* mostly baudrate or datarate.
26-
*
27-
* @ingroup modm_math
28-
*/
29-
class
30-
Tolerance
20+
/// @ingroup modm_math
21+
template< typename T >
22+
constexpr bool
23+
isValueInTolerance(T reference, T actual, percent_t tolerance)
3124
{
32-
public:
33-
static constexpr int64_t
34-
absoluteError(uint32_t reference, uint32_t actual)
35-
{
36-
return (int64_t(reference) - actual);
37-
}
38-
39-
static constexpr float
40-
relativeError(uint32_t reference, uint32_t actual)
41-
{
42-
return (1.f - float(actual) / reference);
43-
}
44-
45-
static constexpr bool
46-
isErrorInTolerance(float error, percent_t tolerance)
47-
{
48-
return (error == 0) or (((error > 0) ? error : -error) <= pct2f(tolerance));
49-
}
50-
51-
static constexpr bool
52-
isValueInTolerance(uint32_t reference, uint32_t actual, percent_t tolerance)
53-
{
54-
return isErrorInTolerance(relativeError(reference, actual), tolerance);
55-
}
56-
57-
template< uint32_t reference, uint32_t actual, percent_t tolerance >
58-
static void
59-
assertValueInTolerance()
60-
{
61-
static_assert(isValueInTolerance(reference, actual, tolerance),
62-
"The actual value exceeds the tolerance of reference!");
63-
}
64-
};
25+
return std::abs(1.0 - double(actual) / double(reference)) <= std::abs(double(tolerance));
26+
}
6527

6628
} // namespace modm
67-
68-
#endif // MODM_MATH_TOLERANCE_HPP

src/modm/platform/usb/sam/usb.hpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public:
3030
{
3131
%% if target["family"] == "g5x"
3232
// only the USB device mode is supported for now
33-
static_assert(modm::Tolerance::isValueInTolerance(48_MHz, SystemClock::Usb, 0.25_pct),
33+
static_assert(modm::isValueInTolerance(48_MHz, SystemClock::Usb, 0.25_pct),
3434
"The USB clock frequency must be within 0.25\% of 48MHz!");
3535
// Select DM/DP function for PA21/22
3636
MATRIX->CCFG_SYSIO &= ~(CCFG_SYSIO_SYSIO11 | CCFG_SYSIO_SYSIO10);

test/modm/ui/color/color_test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ void ColorTest::testRgbHsvPingPongConvertion_8bit()
9393

9494
// Convertion can distort - allow some tolerance.
9595
using namespace modm;
96-
TEST_ASSERT_TRUE(modm::Tolerance::isValueInTolerance(rgb8.red, rgb8_b.red, 1_pct));
97-
TEST_ASSERT_TRUE(modm::Tolerance::isValueInTolerance(rgb8.green, rgb8_b.green, 1_pct));
98-
TEST_ASSERT_TRUE(modm::Tolerance::isValueInTolerance(rgb8.blue, rgb8_b.blue, 1_pct));
96+
TEST_ASSERT_TRUE(modm::isValueInTolerance(rgb8.red, rgb8_b.red, 1_pct));
97+
TEST_ASSERT_TRUE(modm::isValueInTolerance(rgb8.green, rgb8_b.green, 1_pct));
98+
TEST_ASSERT_TRUE(modm::isValueInTolerance(rgb8.blue, rgb8_b.blue, 1_pct));
9999
}
100100

101101
// TODO 16bit convertion not yet working
@@ -109,7 +109,7 @@ void ColorTest::testRgbHsvPingPongConvertion_8bit()
109109

110110
// // Convertion can distort - allow some tolerance.
111111
// using namespace modm;
112-
// TEST_ASSERT_TRUE(modm::Tolerance::isValueInTolerance(rgb.red, rgb16_b.red, 1_pct));
113-
// TEST_ASSERT_TRUE(modm::Tolerance::isValueInTolerance(rgb.green, rgb16_b.green, 1_pct));
114-
// TEST_ASSERT_TRUE(modm::Tolerance::isValueInTolerance(rgb.blue, rgb16_b.blue, 1_pct));
115-
// }
112+
// TEST_ASSERT_TRUE(modm::isValueInTolerance(rgb.red, rgb16_b.red, 1_pct));
113+
// TEST_ASSERT_TRUE(modm::isValueInTolerance(rgb.green, rgb16_b.green, 1_pct));
114+
// TEST_ASSERT_TRUE(modm::isValueInTolerance(rgb.blue, rgb16_b.blue, 1_pct));
115+
// }

0 commit comments

Comments
 (0)