mirror of
https://github.com/esphome/esphome.git
synced 2025-11-18 07:45:56 +00:00
[core] Add helper functions for clamp_at_... (#10387)
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <concepts>
|
||||
|
||||
#include "esphome/core/optional.h"
|
||||
|
||||
@@ -1169,7 +1170,20 @@ template<class T> class RAMAllocator {
|
||||
|
||||
template<class T> using ExternalRAMAllocator = RAMAllocator<T>;
|
||||
|
||||
/// @}
|
||||
/**
|
||||
* Functions to constrain the range of arithmetic values.
|
||||
*/
|
||||
|
||||
template<std::totally_ordered T> T clamp_at_least(T value, T min) {
|
||||
if (value < min)
|
||||
return min;
|
||||
return value;
|
||||
}
|
||||
template<std::totally_ordered T> T clamp_at_most(T value, T max) {
|
||||
if (value > max)
|
||||
return max;
|
||||
return value;
|
||||
}
|
||||
|
||||
/// @name Internal functions
|
||||
///@{
|
||||
|
||||
13
tests/components/esp32/common.yaml
Normal file
13
tests/components/esp32/common.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
logger:
|
||||
level: VERBOSE
|
||||
|
||||
esphome:
|
||||
on_boot:
|
||||
- lambda: |-
|
||||
int x = 100;
|
||||
x = clamp(x, 50, 90);
|
||||
assert(x == 90);
|
||||
x = clamp_at_least(x, 95);
|
||||
assert(x == 95);
|
||||
x = clamp_at_most(x, 40);
|
||||
assert(x == 40);
|
||||
13
tests/components/esp8266/test.esp8266-ard.yaml
Normal file
13
tests/components/esp8266/test.esp8266-ard.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
logger:
|
||||
level: VERBOSE
|
||||
|
||||
esphome:
|
||||
on_boot:
|
||||
- lambda: |-
|
||||
int x = 100;
|
||||
x = clamp(x, 50, 90);
|
||||
assert(x == 90);
|
||||
x = clamp_at_least(x, 95);
|
||||
assert(x == 95);
|
||||
x = clamp_at_most(x, 40);
|
||||
assert(x == 40);
|
||||
@@ -15,3 +15,10 @@ esphome:
|
||||
static const uint8_t my_addr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
if (!mac_address_is_valid(my_addr))
|
||||
ESP_LOGD("test", "Invalid mac address %X", my_addr[0]); // etc.
|
||||
int x = 100;
|
||||
x = clamp(x, 50, 90);
|
||||
assert(x == 90);
|
||||
x = clamp_at_least(x, 95);
|
||||
assert(x == 95);
|
||||
x = clamp_at_most(x, 40);
|
||||
assert(x == 40);
|
||||
|
||||
13
tests/components/libretiny/test.bk72xx-ard.yaml
Normal file
13
tests/components/libretiny/test.bk72xx-ard.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
logger:
|
||||
level: VERBOSE
|
||||
|
||||
esphome:
|
||||
on_boot:
|
||||
- lambda: |-
|
||||
int x = 100;
|
||||
x = clamp(x, 50, 90);
|
||||
assert(x == 90);
|
||||
x = clamp_at_least(x, 95);
|
||||
assert(x == 95);
|
||||
x = clamp_at_most(x, 40);
|
||||
assert(x == 40);
|
||||
@@ -1,3 +1,13 @@
|
||||
esphome:
|
||||
on_boot:
|
||||
- lambda: |-
|
||||
int x = 100;
|
||||
x = clamp(x, 50, 90);
|
||||
assert(x == 90);
|
||||
x = clamp_at_least(x, 95);
|
||||
assert(x == 95);
|
||||
x = clamp_at_most(x, 40);
|
||||
assert(x == 40);
|
||||
nrf52:
|
||||
dfu:
|
||||
reset_pin:
|
||||
|
||||
13
tests/components/rp2040/test.rp2040-ard.yaml
Normal file
13
tests/components/rp2040/test.rp2040-ard.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
logger:
|
||||
level: VERBOSE
|
||||
|
||||
esphome:
|
||||
on_boot:
|
||||
- lambda: |-
|
||||
int x = 100;
|
||||
x = clamp(x, 50, 90);
|
||||
assert(x == 90);
|
||||
x = clamp_at_least(x, 95);
|
||||
assert(x == 95);
|
||||
x = clamp_at_most(x, 40);
|
||||
assert(x == 40);
|
||||
Reference in New Issue
Block a user