mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00:00
bot review
This commit is contained in:
@@ -159,16 +159,13 @@ constexpr uint16_t CAPABILITY_BITMASKS[] = {
|
|||||||
compute_capability_bitmask(ColorCapability::RGB), // 1 << 5
|
compute_capability_bitmask(ColorCapability::RGB), // 1 << 5
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Check if any mode in the bitmask has a specific capability
|
/// Convert a power-of-2 ColorCapability value to an array index
|
||||||
/// Used for checking if a light supports a capability (e.g., BRIGHTNESS, RGB)
|
/// ColorCapability values: 1, 2, 4, 8, 16, 32 -> array indices: 0, 1, 2, 3, 4, 5
|
||||||
inline bool has_capability(const ColorModeMask &mask, ColorCapability capability) {
|
inline int capability_to_index(ColorCapability capability) {
|
||||||
// Lookup the pre-computed bitmask for this capability and check intersection with our mask
|
|
||||||
// ColorCapability values: 1, 2, 4, 8, 16, 32 -> array indices: 0, 1, 2, 3, 4, 5
|
|
||||||
// We need to convert the power-of-2 value to an index
|
|
||||||
uint8_t cap_val = static_cast<uint8_t>(capability);
|
uint8_t cap_val = static_cast<uint8_t>(capability);
|
||||||
#if defined(__GNUC__) || defined(__clang__)
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
// Use compiler intrinsic for efficient bit position lookup (O(1) vs O(log n))
|
// Use compiler intrinsic for efficient bit position lookup (O(1) vs O(log n))
|
||||||
int index = __builtin_ctz(cap_val);
|
return __builtin_ctz(cap_val);
|
||||||
#else
|
#else
|
||||||
// Fallback for compilers without __builtin_ctz
|
// Fallback for compilers without __builtin_ctz
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -176,8 +173,15 @@ inline bool has_capability(const ColorModeMask &mask, ColorCapability capability
|
|||||||
cap_val >>= 1;
|
cap_val >>= 1;
|
||||||
++index;
|
++index;
|
||||||
}
|
}
|
||||||
|
return index;
|
||||||
#endif
|
#endif
|
||||||
return (mask.get_mask() & CAPABILITY_BITMASKS[index]) != 0;
|
}
|
||||||
|
|
||||||
|
/// Check if any mode in the bitmask has a specific capability
|
||||||
|
/// Used for checking if a light supports a capability (e.g., BRIGHTNESS, RGB)
|
||||||
|
inline bool has_capability(const ColorModeMask &mask, ColorCapability capability) {
|
||||||
|
// Lookup the pre-computed bitmask for this capability and check intersection with our mask
|
||||||
|
return (mask.get_mask() & CAPABILITY_BITMASKS[capability_to_index(capability)]) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace light
|
} // namespace light
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ template<typename EnumType, int MaxBits = 16> class EnumBitmask {
|
|||||||
// Must be provided by template specialization
|
// Must be provided by template specialization
|
||||||
// These convert between enum values and bit positions (0, 1, 2, ...)
|
// These convert between enum values and bit positions (0, 1, 2, ...)
|
||||||
static constexpr int enum_to_bit(EnumType value);
|
static constexpr int enum_to_bit(EnumType value);
|
||||||
static EnumType bit_to_enum(int bit); // Not constexpr due to static array limitation in C++20
|
static EnumType bit_to_enum(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