ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tx20.cpp
Go to the documentation of this file.
1#include "tx20.h"
3#include "esphome/core/log.h"
5
6#include <array>
7
8namespace esphome::tx20 {
9
10ESPHOME_LOG_TAG(TAG, "tx20");
11static const uint8_t MAX_BUFFER_SIZE = 41;
12static const uint16_t TX20_MAX_TIME = MAX_BUFFER_SIZE * 1200 + 5000;
13static const uint16_t TX20_BIT_TIME = 1200;
14PROGMEM_STRING_TABLE(DirectionStrings, "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W",
15 "WNW", "NW", "NNW");
16
17void Tx20Component::setup() {
18 this->pin_->setup();
19
20 this->store_.buffer = new uint16_t[MAX_BUFFER_SIZE];
21 this->store_.pin = this->pin_->to_isr();
22 this->store_.reset();
23
25}
26void Tx20Component::dump_config() {
27 ESP_LOGCONFIG(TAG, "Tx20:");
28
29 LOG_SENSOR(" ", "Wind speed:", this->wind_speed_sensor_);
30 LOG_SENSOR(" ", "Wind direction degrees:", this->wind_direction_degrees_sensor_);
31
32 LOG_PIN(" Pin: ", this->pin_);
33}
34void Tx20Component::loop() {
35 if (this->store_.tx20_available) {
36 this->decode_and_publish_();
37 this->store_.reset();
38 }
39}
40
41std::string Tx20Component::get_wind_cardinal_direction() const {
42 if (this->wind_direction_index_ >= DirectionStrings::COUNT)
43 return {};
44 return progmem_string(DirectionStrings::get_progmem_str(this->wind_direction_index_, 0));
45}
46
48 ESP_LOGVV(TAG, "Decode Tx20");
49
50 std::array<bool, MAX_BUFFER_SIZE> bit_buffer{};
51 size_t bit_pos = 0;
52 bool current_bit = true;
53 // Cap at MAX_BUFFER_SIZE - 1 to prevent out-of-bounds access (buffer_index can exceed MAX_BUFFER_SIZE in ISR)
54 const int max_buffer_index =
55 std::min(static_cast<int>(this->store_.buffer_index), static_cast<int>(MAX_BUFFER_SIZE - 1));
56
57 for (int i = 1; i <= max_buffer_index; i++) {
58 uint8_t repeat = this->store_.buffer[i] / TX20_BIT_TIME;
59 // ignore segments at the end that were too short
60 for (uint8_t j = 0; j < repeat && bit_pos < MAX_BUFFER_SIZE; j++) {
61 bit_buffer[bit_pos++] = current_bit;
62 }
63 current_bit = !current_bit;
64 }
65 current_bit = !current_bit;
66 size_t bits_before_padding = bit_pos;
67 while (bit_pos < MAX_BUFFER_SIZE) {
68 bit_buffer[bit_pos++] = current_bit;
69 }
70
71 uint8_t tx20_sa = 0;
72 uint8_t tx20_sb = 0;
73 uint8_t tx20_sd = 0;
74 uint8_t tx20_se = 0;
75 uint16_t tx20_sc = 0;
76 uint16_t tx20_sf = 0;
77 uint8_t tx20_wind_direction = 0;
78 float tx20_wind_speed_kmh = 0;
79 uint8_t bit_count = 0;
80
81 for (int i = 41; i > 0; i--) {
82 uint8_t bit = bit_buffer.at(bit_count);
83 bit_count++;
84 if (i > 41 - 5) {
85 // start, inverted
86 tx20_sa = (tx20_sa << 1) | (bit ^ 1);
87 } else if (i > 41 - 5 - 4) {
88 // wind dir, inverted
89 tx20_sb = tx20_sb >> 1 | ((bit ^ 1) << 3);
90 } else if (i > 41 - 5 - 4 - 12) {
91 // windspeed, inverted
92 tx20_sc = tx20_sc >> 1 | ((bit ^ 1) << 11);
93 } else if (i > 41 - 5 - 4 - 12 - 4) {
94 // checksum, inverted
95 tx20_sd = tx20_sd >> 1 | ((bit ^ 1) << 3);
96 } else if (i > 41 - 5 - 4 - 12 - 4 - 4) {
97 // wind dir
98 tx20_se = tx20_se >> 1 | (bit << 3);
99 } else {
100 // windspeed
101 tx20_sf = tx20_sf >> 1 | (bit << 11);
102 }
103 }
104
105 uint8_t chk = (tx20_sb + (tx20_sc & 0xf) + ((tx20_sc >> 4) & 0xf) + ((tx20_sc >> 8) & 0xf));
106 chk &= 0xf;
107 bool value_set = false;
108 // checks:
109 // 1. Check that the start frame is 00100 (0x04)
110 // 2. Check received checksum matches calculated checksum
111 // 3. Check that Wind Direction matches Wind Direction (Inverted)
112 // 4. Check that Wind Speed matches Wind Speed (Inverted)
113#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
114 // Build debug strings from completed data
115 char debug_buf[320]; // buffer values: max 40 entries * 7 chars each
116 size_t debug_pos = 0;
117 for (int i = 1; i <= max_buffer_index; i++) {
118 debug_pos = buf_append_printf(debug_buf, sizeof(debug_buf), debug_pos, "%u, ", this->store_.buffer[i]);
119 }
120 if (bits_before_padding < MAX_BUFFER_SIZE) {
121 buf_append_printf(debug_buf, sizeof(debug_buf), debug_pos, "%zu, ", MAX_BUFFER_SIZE - bits_before_padding);
122 }
123 char bits_buf[MAX_BUFFER_SIZE + 1];
124 for (size_t i = 0; i < MAX_BUFFER_SIZE; i++) {
125 bits_buf[i] = bit_buffer[i] ? '1' : '0';
126 }
127 bits_buf[MAX_BUFFER_SIZE] = '\0';
128 ESP_LOGVV(TAG, "BUFFER %s", debug_buf);
129 ESP_LOGVV(TAG, "Decoded bits %s", bits_buf);
130#endif
131
132 if (tx20_sa == 4) {
133 if (chk == tx20_sd) {
134 if (tx20_sf == tx20_sc) {
135 tx20_wind_speed_kmh = float(tx20_sc) * 0.36f;
136 ESP_LOGV(TAG, "WindSpeed %f", tx20_wind_speed_kmh);
137 PUBLISH_STATE(this->wind_speed_sensor_, tx20_wind_speed_kmh);
138 value_set = true;
139 }
140 if (tx20_se == tx20_sb) {
141 tx20_wind_direction = tx20_se;
142 if (tx20_wind_direction < DirectionStrings::COUNT)
143 this->wind_direction_index_ = tx20_wind_direction;
144 ESP_LOGV(TAG, "WindDirection %d", tx20_wind_direction);
145 PUBLISH_STATE(this->wind_direction_degrees_sensor_, float(tx20_wind_direction) * 22.5f);
146 value_set = true;
147 }
148 if (!value_set) {
149 ESP_LOGW(TAG, "No value set!");
150 }
151 } else {
152 ESP_LOGW(TAG, "Checksum wrong!");
153 }
154 } else {
155 ESP_LOGW(TAG, "Start wrong!");
156 }
157}
158
160 arg->pin_state = arg->pin.digital_read();
161 const uint32_t now = micros();
162 if (!arg->start_time) {
163 // only detect a start if the bit is high
164 if (!arg->pin_state) {
165 return;
166 }
167 arg->buffer[arg->buffer_index] = 1;
168 arg->start_time = now;
169 arg->buffer_index += 1;
170 return;
171 }
172 const uint32_t delay = now - arg->start_time;
173 const uint8_t index = arg->buffer_index;
174
175 // first delay has to be ~2400
176 if (index == 1 && (delay > 3000 || delay < 2400)) {
177 arg->reset();
178 return;
179 }
180 // second delay has to be ~1200
181 if (index == 2 && (delay > 1500 || delay < 1200)) {
182 arg->reset();
183 return;
184 }
185 // third delay has to be ~2400
186 if (index == 3 && (delay > 3000 || delay < 2400)) {
187 arg->reset();
188 return;
189 }
190
191 if (arg->tx20_available || ((arg->spent_time + delay > TX20_MAX_TIME) && arg->start_time)) {
192 arg->tx20_available = true;
193 return;
194 }
195 if (index < MAX_BUFFER_SIZE) {
196 arg->buffer[index] = delay;
197 }
198 arg->spent_time += delay;
199 arg->start_time = now;
200 arg->buffer_index += 1;
201}
203 tx20_available = false;
204 buffer_index = 0;
205 spent_time = 0;
206 // rearm it!
207 start_time = 0;
208}
209
210} // namespace esphome::tx20
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:29
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:96
Tx20ComponentStore store_
Definition tx20.h:43
InternalGPIOPin * pin_
Definition tx20.h:42
uint8_t wind_direction_index_
Definition tx20.h:41
@ INTERRUPT_ANY_EDGE
Definition gpio_pin.h:54
ESPHOME_LOG_TAG(TAG, "tx20")
PROGMEM_STRING_TABLE(DirectionStrings, "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW")
std::string progmem_string(ProgmemStr str)
Copies a string stored with ESPHOME_F into a std::string.
Definition helpers.cpp:24
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
Store data in a class that doesn't use multiple-inheritance (vtables in flash)
Definition tx20.h:10
ISRInternalGPIOPin pin
Definition tx20.h:17
volatile uint32_t start_time
Definition tx20.h:12
volatile uint32_t spent_time
Definition tx20.h:14
static void gpio_intr(Tx20ComponentStore *arg)
Definition tx20.cpp:159
volatile uint16_t * buffer
Definition tx20.h:11
volatile uint8_t buffer_index
Definition tx20.h:13
volatile bool tx20_available
Definition tx20.h:15