mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 10:12:52 +00:00
[tx20] Keep the wind direction table in flash
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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};
|
||||
|
||||
Reference in New Issue
Block a user