diff --git a/esphome/components/tx20/tx20.cpp b/esphome/components/tx20/tx20.cpp index ce4c20ede41..2819323d69f 100644 --- a/esphome/components/tx20/tx20.cpp +++ b/esphome/components/tx20/tx20.cpp @@ -1,6 +1,7 @@ #include "tx20.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" +#include "esphome/core/progmem.h" #include @@ -10,8 +11,8 @@ ESPHOME_LOG_TAG(TAG, "tx20"); static const uint8_t MAX_BUFFER_SIZE = 41; static const uint16_t TX20_MAX_TIME = MAX_BUFFER_SIZE * 1200 + 5000; static const uint16_t TX20_BIT_TIME = 1200; -static const char *const DIRECTIONS[] = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", - "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"}; +PROGMEM_STRING_TABLE(DirectionStrings, "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", + "WNW", "NW", "NNW"); void Tx20Component::setup() { this->pin_->setup(); @@ -37,7 +38,11 @@ void Tx20Component::loop() { } } -std::string Tx20Component::get_wind_cardinal_direction() const { return this->wind_cardinal_direction_; } +std::string Tx20Component::get_wind_cardinal_direction() const { + if (this->wind_direction_index_ >= DirectionStrings::COUNT) + return {}; + return progmem_string(DirectionStrings::get_progmem_str(this->wind_direction_index_, 0)); +} void Tx20Component::decode_and_publish_() { ESP_LOGVV(TAG, "Decode Tx20"); @@ -135,9 +140,8 @@ void Tx20Component::decode_and_publish_() { } if (tx20_se == tx20_sb) { tx20_wind_direction = tx20_se; - if (tx20_wind_direction < 16) { - wind_cardinal_direction_ = DIRECTIONS[tx20_wind_direction]; - } + if (tx20_wind_direction < 16) + this->wind_direction_index_ = tx20_wind_direction; ESP_LOGV(TAG, "WindDirection %d", tx20_wind_direction); if (this->wind_direction_degrees_sensor_ != nullptr) this->wind_direction_degrees_sensor_->publish_state(float(tx20_wind_direction) * 22.5f); diff --git a/esphome/components/tx20/tx20.h b/esphome/components/tx20/tx20.h index e4dc7dfab01..0d5b123ab64 100644 --- a/esphome/components/tx20/tx20.h +++ b/esphome/components/tx20/tx20.h @@ -39,7 +39,7 @@ class Tx20Component final : public Component { protected: void decode_and_publish_(); - std::string wind_cardinal_direction_; + uint8_t wind_direction_index_{0xFF}; // 0xFF until the first reading InternalGPIOPin *pin_; sensor::Sensor *wind_speed_sensor_{nullptr}; sensor::Sensor *wind_direction_degrees_sensor_{nullptr};