platisd / smartcar_shield
1
/**
2
 * \class Utilities
3
 * Utilitiy functions commonly used within the Smartcar library
4
 */
5
#pragma once
6

7
namespace smartcarlib
8
{
9
namespace utils
10
{
11
/**
12
 * Gets the absolute of the supplied number
13
 * @param  number The number to get the absolute of
14
 * @return        The absolute value
15
 *
16
 * **Example:**
17
 * \code
18
 * int num = -5;
19
 * num = smartcarlib::utils::getAbsolute(num);
20
 * // `num` is now `5`
21
 * \endcode
22
 */
23
template <typename AnyNumber>
24 1
constexpr AnyNumber getAbsolute(AnyNumber number)
25
{
26 1
    return number < 0 ? -number : number;
27
}
28

29
/**
30
 * Limit the number between a range
31
 * @param  number The number to limit
32
 * @param  min    The minimum limit of the range
33
 * @param  max    The maximum limit of the range
34
 * @return        The number unchanged if it was within the range, otherwise the
35
 *                lower limit of the range if it was smaller or the higher limit
36
 *                if it was larger
37
 *
38
 * **Example:**
39
 * \code
40
 * int num = -5;
41
 * num = smartcarlib::utils::getConstrain(num, 0, 100);
42
 * // `num` is now `0`
43
 * \endcode
44
 */
45
template <typename AnyNumber>
46 1
constexpr AnyNumber getConstrain(AnyNumber number, AnyNumber min, AnyNumber max)
47
{
48 1
    return number < min ? min : (number > max ? max : number);
49
}
50

51
/**
52
 * Gets the median value out of the supplied number array
53
 * @param  unsortedNumbers   An array containing numbers
54
 * @param  arraySize         Amount of numbers contained in the array
55
 * @return                   Median number of the supplied array
56
 *
57
 * **Example:**
58
 * \code
59
 * const int ARRAY_SIZE = 5;
60
 * int array[ARRAY_SIZE] = {0, 3, 4, 1, 15};
61
 * int median = smartcarlib::utils::getMedian(array, ARRAY_SIZE);
62
 * // `median` is `3`
63
 * \endcode
64
 */
65
template <typename AnyNumber>
66 1
AnyNumber getMedian(AnyNumber unsortedNumbers[], const unsigned int& arraySize)
67
{
68
    // Sort the array using bubble sort
69 1
    for (unsigned int i = 0; i < arraySize - 1; i++)
70
    {
71 1
        for (unsigned int j = 0; j < arraySize - 1 - i; j++)
72
        {
73 1
            if (unsortedNumbers[j] > unsortedNumbers[j + 1])
74
            {
75 1
                auto swappedValue      = unsortedNumbers[j];
76 1
                unsortedNumbers[j]     = unsortedNumbers[j + 1];
77 1
                unsortedNumbers[j + 1] = swappedValue;
78
            }
79
        }
80
    }
81
    // Return the Median value now that the array is sorted
82 1
    return unsortedNumbers[arraySize / 2];
83
}
84

85
/**
86
 * Maps a value from a range to another. Implemented as a templetized `map` of Arduino.
87
 * @param  valueToMap The value to be mapped
88
 * @param  fromLow    The lower limit of the initial range
89
 * @param  fromHigh   The upper limit of the initial range
90
 * @param  toLow      The lower limit of the final range
91
 * @param  toHigh     The higher limit of the final range
92
 * @return            The mapped value if initial range valid otherwise the lower limit
93
 *                    of the target range
94
 *
95
 * **Example:**
96
 * \code
97
 * int num = 3;
98
 *  // Scale a `num` from 0-10 to 0-100
99
 *  num = smartcarlib::utils::getMap(num, 0, 10, 0, 100);
100
 *  // `num` is now `30`
101
 * \endcode
102
 */
103
template <typename AnyNumber>
104 1
constexpr AnyNumber getMap(
105
    AnyNumber valueToMap, AnyNumber fromLow, AnyNumber fromHigh, AnyNumber toLow, AnyNumber toHigh)
106
{
107
    return fromHigh == fromLow
108 1
               ? toLow
109 1
               : (valueToMap - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
110
}
111

112
/**
113
 * Compares two floating point numbers
114
 * @param  a The first floating point number to compare
115
 * @param  b The second floating point number to compare
116
 * @return   `true` if the two numbers are amost equal, false otherwise
117
 *
118
 * **Example:**
119
 * \code
120
 * bool result = smartcarlib::utils::areAlmostEqual(0.000001, 0,0000012);
121
 * // `result` is `true`
122
 * \endcode
123
 */
124 1
constexpr bool areAlmostEqual(float a, float b)
125
{
126
    // C++11 does not allow us to declare the delta as a variable within the function body
127
    // NOLINTNEXTLINE(readability-magic-numbers)
128 1
    return getAbsolute(getAbsolute(a) - getAbsolute(b)) <= 0.001F;
129
}
130
} // namespace utils
131
} // namespace smartcarlib

Read our documentation on viewing source code .

Loading