[tx20] Keep the wind direction table in flash

This commit is contained in:
J. Nick Koston
2026-10-13 10:03:22 -10:00
parent 918b7d2553
commit 2d93446439
2 changed files with 11 additions and 7 deletions
+10 -6
View File
@@ -1,6 +1,7 @@
#include "tx20.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#include <array>
@@ -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);
+1 -1
View File
@@ -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};