mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 05:54:56 +01:00
Convert clamp() helper to backport of std::clamp() (#3010)
This commit is contained in:
parent
a4931f5d78
commit
5c339d4597
2 changed files with 12 additions and 20 deletions
|
@ -215,17 +215,6 @@ void HighFrequencyLoopRequester::stop() {
|
||||||
}
|
}
|
||||||
bool HighFrequencyLoopRequester::is_high_frequency() { return high_freq_num_requests > 0; }
|
bool HighFrequencyLoopRequester::is_high_frequency() { return high_freq_num_requests > 0; }
|
||||||
|
|
||||||
template<typename T> T clamp(const T val, const T min, const T max) {
|
|
||||||
if (val < min)
|
|
||||||
return min;
|
|
||||||
if (val > max)
|
|
||||||
return max;
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
template uint8_t clamp(uint8_t, uint8_t, uint8_t);
|
|
||||||
template float clamp(float, float, float);
|
|
||||||
template int clamp(int, int, int);
|
|
||||||
|
|
||||||
float lerp(float completion, float start, float end) { return start + (end - start) * completion; }
|
float lerp(float completion, float start, float end) { return start + (end - start) * completion; }
|
||||||
|
|
||||||
bool str_startswith(const std::string &full, const std::string &start) { return full.rfind(start, 0) == 0; }
|
bool str_startswith(const std::string &full, const std::string &start) { return full.rfind(start, 0) == 0; }
|
||||||
|
|
|
@ -58,15 +58,6 @@ class HighFrequencyLoopRequester {
|
||||||
bool started_{false};
|
bool started_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Clamp the value between min and max.
|
|
||||||
*
|
|
||||||
* @param val The value.
|
|
||||||
* @param min The minimum value.
|
|
||||||
* @param max The maximum value.
|
|
||||||
* @return val clamped in between min and max.
|
|
||||||
*/
|
|
||||||
template<typename T> T clamp(T val, T min, T max);
|
|
||||||
|
|
||||||
/** Linearly interpolate between end start and end by completion.
|
/** Linearly interpolate between end start and end by completion.
|
||||||
*
|
*
|
||||||
* @tparam T The input/output typename.
|
* @tparam T The input/output typename.
|
||||||
|
@ -277,6 +268,18 @@ using std::is_trivially_copyable;
|
||||||
template<typename T> struct is_trivially_copyable : public std::integral_constant<bool, true> {};
|
template<typename T> struct is_trivially_copyable : public std::integral_constant<bool, true> {};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// std::clamp from C++17
|
||||||
|
#if __cpp_lib_clamp >= 201603
|
||||||
|
using std::clamp;
|
||||||
|
#else
|
||||||
|
template<typename T, typename Compare> constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) {
|
||||||
|
return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
|
||||||
|
}
|
||||||
|
template<typename T> constexpr const T &clamp(const T &v, const T &lo, const T &hi) {
|
||||||
|
return clamp(v, lo, hi, std::less<T>{});
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// std::bit_cast from C++20
|
// std::bit_cast from C++20
|
||||||
#if __cpp_lib_bit_cast >= 201806
|
#if __cpp_lib_bit_cast >= 201806
|
||||||
using std::bit_cast;
|
using std::bit_cast;
|
||||||
|
|
Loading…
Reference in a new issue