diff --git a/esphome/core/color.cpp b/esphome/core/color.cpp index 58d995db2f..7e390b2354 100644 --- a/esphome/core/color.cpp +++ b/esphome/core/color.cpp @@ -2,10 +2,8 @@ namespace esphome { -const Color Color::BLACK(0, 0, 0, 0); -const Color Color::WHITE(255, 255, 255, 255); - -const Color COLOR_BLACK(0, 0, 0, 0); -const Color COLOR_WHITE(255, 255, 255, 255); +// C++20 constinit ensures compile-time initialization (stored in ROM) +constinit const Color Color::BLACK(0, 0, 0, 0); +constinit const Color Color::WHITE(255, 255, 255, 255); } // namespace esphome diff --git a/esphome/core/color.h b/esphome/core/color.h index 1c43fd9d3e..2b307bb438 100644 --- a/esphome/core/color.h +++ b/esphome/core/color.h @@ -5,7 +5,9 @@ namespace esphome { -inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) { return (uint16_t(i) * (1 + uint16_t(scale))) / 256; } +inline static constexpr uint8_t esp_scale8(uint8_t i, uint8_t scale) { + return (uint16_t(i) * (1 + uint16_t(scale))) / 256; +} struct Color { union { @@ -31,17 +33,20 @@ struct Color { uint32_t raw_32; }; - inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT - inline Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {} + inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(0) {} // NOLINT + inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), + g(green), + b(blue), + w(0) {} - inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red), - g(green), - b(blue), - w(white) {} - inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), - g((colorcode >> 8) & 0xFF), - b((colorcode >> 0) & 0xFF), - w((colorcode >> 24) & 0xFF) {} + inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red), + g(green), + b(blue), + w(white) {} + inline explicit constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF), + g((colorcode >> 8) & 0xFF), + b((colorcode >> 0) & 0xFF), + w((colorcode >> 24) & 0xFF) {} inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; } @@ -169,9 +174,4 @@ struct Color { static const Color WHITE; }; -ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") -extern const Color COLOR_BLACK; -ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21") -extern const Color COLOR_WHITE; - } // namespace esphome diff --git a/esphome/core/time.cpp b/esphome/core/time.cpp index 672f5b98bf..f9652b5329 100644 --- a/esphome/core/time.cpp +++ b/esphome/core/time.cpp @@ -226,11 +226,11 @@ int32_t ESPTime::timezone_offset() { return offset; } -bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; } -bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; } -bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; } -bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; } -bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; } +bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; } +bool ESPTime::operator<=(const ESPTime &other) const { return this->timestamp <= other.timestamp; } +bool ESPTime::operator==(const ESPTime &other) const { return this->timestamp == other.timestamp; } +bool ESPTime::operator>=(const ESPTime &other) const { return this->timestamp >= other.timestamp; } +bool ESPTime::operator>(const ESPTime &other) const { return this->timestamp > other.timestamp; } template bool increment_time_value(T ¤t, uint16_t begin, uint16_t end) { current++; diff --git a/esphome/core/time.h b/esphome/core/time.h index 5cbd9369fb..a53fca2346 100644 --- a/esphome/core/time.h +++ b/esphome/core/time.h @@ -109,10 +109,10 @@ struct ESPTime { void increment_second(); /// Increment this clock instance by one day. void increment_day(); - bool operator<(ESPTime other); - bool operator<=(ESPTime other); - bool operator==(ESPTime other); - bool operator>=(ESPTime other); - bool operator>(ESPTime other); + bool operator<(const ESPTime &other) const; + bool operator<=(const ESPTime &other) const; + bool operator==(const ESPTime &other) const; + bool operator>=(const ESPTime &other) const; + bool operator>(const ESPTime &other) const; }; } // namespace esphome