mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 06:04:01 +00:00
reduce
This commit is contained in:
@@ -18,13 +18,8 @@ constexpr int CLIMATE_PRESET_BITMASK_SIZE = 8; // 8 values (NONE, HOME, AWA
|
|||||||
// MUST be declared before any instantiation of FiniteSetMask<ClimateMode>, etc.
|
// MUST be declared before any instantiation of FiniteSetMask<ClimateMode>, etc.
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
// ClimateMode specialization (7 values: 0-6)
|
// ClimateMode uses 1:1 mapping (value_to_bit is just a cast)
|
||||||
template<>
|
// Only bit_to_value needs specialization
|
||||||
constexpr int FiniteSetMask<climate::ClimateMode, climate::CLIMATE_MODE_BITMASK_SIZE>::value_to_bit(
|
|
||||||
climate::ClimateMode mode) {
|
|
||||||
return static_cast<int>(mode); // Direct mapping: enum value = bit position
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline climate::ClimateMode FiniteSetMask<climate::ClimateMode, climate::CLIMATE_MODE_BITMASK_SIZE>::bit_to_value(
|
inline climate::ClimateMode FiniteSetMask<climate::ClimateMode, climate::CLIMATE_MODE_BITMASK_SIZE>::bit_to_value(
|
||||||
int bit) {
|
int bit) {
|
||||||
@@ -42,13 +37,8 @@ inline climate::ClimateMode FiniteSetMask<climate::ClimateMode, climate::CLIMATE
|
|||||||
return (bit >= 0 && bit < MODE_COUNT) ? MODES[bit] : climate::CLIMATE_MODE_OFF;
|
return (bit >= 0 && bit < MODE_COUNT) ? MODES[bit] : climate::CLIMATE_MODE_OFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClimateFanMode specialization (10 values: 0-9)
|
// ClimateFanMode uses 1:1 mapping (value_to_bit is just a cast)
|
||||||
template<>
|
// Only bit_to_value needs specialization
|
||||||
constexpr int FiniteSetMask<climate::ClimateFanMode, climate::CLIMATE_FAN_MODE_BITMASK_SIZE>::value_to_bit(
|
|
||||||
climate::ClimateFanMode mode) {
|
|
||||||
return static_cast<int>(mode); // Direct mapping: enum value = bit position
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline climate::ClimateFanMode FiniteSetMask<climate::ClimateFanMode,
|
inline climate::ClimateFanMode FiniteSetMask<climate::ClimateFanMode,
|
||||||
climate::CLIMATE_FAN_MODE_BITMASK_SIZE>::bit_to_value(int bit) {
|
climate::CLIMATE_FAN_MODE_BITMASK_SIZE>::bit_to_value(int bit) {
|
||||||
@@ -68,13 +58,8 @@ inline climate::ClimateFanMode FiniteSetMask<climate::ClimateFanMode,
|
|||||||
return (bit >= 0 && bit < MODE_COUNT) ? MODES[bit] : climate::CLIMATE_FAN_ON;
|
return (bit >= 0 && bit < MODE_COUNT) ? MODES[bit] : climate::CLIMATE_FAN_ON;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClimateSwingMode specialization (4 values: 0-3)
|
// ClimateSwingMode uses 1:1 mapping (value_to_bit is just a cast)
|
||||||
template<>
|
// Only bit_to_value needs specialization
|
||||||
constexpr int FiniteSetMask<climate::ClimateSwingMode, climate::CLIMATE_SWING_MODE_BITMASK_SIZE>::value_to_bit(
|
|
||||||
climate::ClimateSwingMode mode) {
|
|
||||||
return static_cast<int>(mode); // Direct mapping: enum value = bit position
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline climate::ClimateSwingMode FiniteSetMask<climate::ClimateSwingMode,
|
inline climate::ClimateSwingMode FiniteSetMask<climate::ClimateSwingMode,
|
||||||
climate::CLIMATE_SWING_MODE_BITMASK_SIZE>::bit_to_value(int bit) {
|
climate::CLIMATE_SWING_MODE_BITMASK_SIZE>::bit_to_value(int bit) {
|
||||||
@@ -88,13 +73,8 @@ inline climate::ClimateSwingMode FiniteSetMask<climate::ClimateSwingMode,
|
|||||||
return (bit >= 0 && bit < MODE_COUNT) ? MODES[bit] : climate::CLIMATE_SWING_OFF;
|
return (bit >= 0 && bit < MODE_COUNT) ? MODES[bit] : climate::CLIMATE_SWING_OFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClimatePreset specialization (8 values: 0-7)
|
// ClimatePreset uses 1:1 mapping (value_to_bit is just a cast)
|
||||||
template<>
|
// Only bit_to_value needs specialization
|
||||||
constexpr int FiniteSetMask<climate::ClimatePreset, climate::CLIMATE_PRESET_BITMASK_SIZE>::value_to_bit(
|
|
||||||
climate::ClimatePreset preset) {
|
|
||||||
return static_cast<int>(preset); // Direct mapping: enum value = bit position
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline climate::ClimatePreset FiniteSetMask<climate::ClimatePreset, climate::CLIMATE_PRESET_BITMASK_SIZE>::bit_to_value(
|
inline climate::ClimatePreset FiniteSetMask<climate::ClimatePreset, climate::CLIMATE_PRESET_BITMASK_SIZE>::bit_to_value(
|
||||||
int bit) {
|
int bit) {
|
||||||
|
|||||||
@@ -17,17 +17,27 @@ namespace esphome {
|
|||||||
///
|
///
|
||||||
/// Requirements:
|
/// Requirements:
|
||||||
/// - ValueType must have a bounded discrete range that maps to bit positions
|
/// - ValueType must have a bounded discrete range that maps to bit positions
|
||||||
/// - Specialization must provide value_to_bit() and bit_to_value() static methods
|
/// - Specialization must provide bit_to_value() static method
|
||||||
|
/// - For 1:1 mappings (enum value = bit position), default value_to_bit() is used
|
||||||
|
/// - For custom mappings (like ColorMode), specialize value_to_bit() as well
|
||||||
/// - MaxBits must be sufficient to hold all possible values
|
/// - MaxBits must be sufficient to hold all possible values
|
||||||
///
|
///
|
||||||
/// Example usage:
|
/// Example usage (1:1 mapping - climate enums):
|
||||||
|
/// // For enums with contiguous values starting at 0, only bit_to_value() needs specialization
|
||||||
|
/// template<>
|
||||||
|
/// inline ClimateMode FiniteSetMask<ClimateMode, 8>::bit_to_value(int bit) {
|
||||||
|
/// static constexpr ClimateMode MODES[] = {CLIMATE_MODE_OFF, CLIMATE_MODE_HEAT, ...};
|
||||||
|
/// return (bit >= 0 && bit < 7) ? MODES[bit] : CLIMATE_MODE_OFF;
|
||||||
|
/// }
|
||||||
|
///
|
||||||
/// using ClimateModeMask = FiniteSetMask<ClimateMode, 8>;
|
/// using ClimateModeMask = FiniteSetMask<ClimateMode, 8>;
|
||||||
/// ClimateModeMask modes({CLIMATE_MODE_HEAT, CLIMATE_MODE_COOL});
|
/// ClimateModeMask modes({CLIMATE_MODE_HEAT, CLIMATE_MODE_COOL});
|
||||||
/// if (modes.count(CLIMATE_MODE_HEAT)) { ... }
|
/// if (modes.count(CLIMATE_MODE_HEAT)) { ... }
|
||||||
/// for (auto mode : modes) { ... } // Iterate over set bits
|
/// for (auto mode : modes) { ... } // Iterate over set bits
|
||||||
///
|
///
|
||||||
/// For complete usage examples with template specializations, see:
|
/// Example usage (custom mapping - ColorMode):
|
||||||
/// - esphome/components/light/color_mode.h (ColorMode enum example)
|
/// // For custom mappings, specialize both value_to_bit() and bit_to_value()
|
||||||
|
/// // See esphome/components/light/color_mode.h for complete example
|
||||||
///
|
///
|
||||||
/// Design notes:
|
/// Design notes:
|
||||||
/// - Uses compile-time type selection for optimal size (uint8_t/uint16_t/uint32_t)
|
/// - Uses compile-time type selection for optimal size (uint8_t/uint16_t/uint32_t)
|
||||||
@@ -150,9 +160,13 @@ template<typename ValueType, int MaxBits = 16> class FiniteSetMask {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// Default implementation for 1:1 mapping (enum value = bit position)
|
||||||
|
// For enums with contiguous values starting at 0, this is all you need.
|
||||||
|
// If you need custom mapping (like ColorMode), provide a specialization.
|
||||||
|
static constexpr int value_to_bit(ValueType value) { return static_cast<int>(value); }
|
||||||
|
|
||||||
// Must be provided by template specialization
|
// Must be provided by template specialization
|
||||||
// These convert between values and bit positions (0, 1, 2, ...)
|
// Converts bit positions (0, 1, 2, ...) to actual values
|
||||||
static constexpr int value_to_bit(ValueType value);
|
|
||||||
static ValueType bit_to_value(int bit); // Not constexpr: array indexing with runtime bounds checking
|
static ValueType bit_to_value(int bit); // Not constexpr: array indexing with runtime bounds checking
|
||||||
|
|
||||||
bitmask_t mask_{0};
|
bitmask_t mask_{0};
|
||||||
|
|||||||
Reference in New Issue
Block a user