48 ESP_LOGVV(TAG,
"Decode Tx20");
50 std::array<bool, MAX_BUFFER_SIZE> bit_buffer{};
52 bool current_bit =
true;
54 const int max_buffer_index =
55 std::min(
static_cast<int>(this->
store_.
buffer_index),
static_cast<int>(MAX_BUFFER_SIZE - 1));
57 for (
int i = 1; i <= max_buffer_index; i++) {
60 for (uint8_t j = 0; j < repeat && bit_pos < MAX_BUFFER_SIZE; j++) {
61 bit_buffer[bit_pos++] = current_bit;
63 current_bit = !current_bit;
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;
77 uint8_t tx20_wind_direction = 0;
78 float tx20_wind_speed_kmh = 0;
79 uint8_t bit_count = 0;
81 for (
int i = 41; i > 0; i--) {
82 uint8_t bit = bit_buffer.at(bit_count);
86 tx20_sa = (tx20_sa << 1) | (bit ^ 1);
87 }
else if (i > 41 - 5 - 4) {
89 tx20_sb = tx20_sb >> 1 | ((bit ^ 1) << 3);
90 }
else if (i > 41 - 5 - 4 - 12) {
92 tx20_sc = tx20_sc >> 1 | ((bit ^ 1) << 11);
93 }
else if (i > 41 - 5 - 4 - 12 - 4) {
95 tx20_sd = tx20_sd >> 1 | ((bit ^ 1) << 3);
96 }
else if (i > 41 - 5 - 4 - 12 - 4 - 4) {
98 tx20_se = tx20_se >> 1 | (bit << 3);
101 tx20_sf = tx20_sf >> 1 | (bit << 11);
105 uint8_t chk = (tx20_sb + (tx20_sc & 0xf) + ((tx20_sc >> 4) & 0xf) + ((tx20_sc >> 8) & 0xf));
107 bool value_set =
false;
113#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
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]);
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);
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';
127 bits_buf[MAX_BUFFER_SIZE] =
'\0';
128 ESP_LOGVV(TAG,
"BUFFER %s", debug_buf);
129 ESP_LOGVV(TAG,
"Decoded bits %s", bits_buf);
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);
140 if (tx20_se == tx20_sb) {
141 tx20_wind_direction = tx20_se;
142 if (tx20_wind_direction < DirectionStrings::COUNT)
144 ESP_LOGV(TAG,
"WindDirection %d", tx20_wind_direction);
145 PUBLISH_STATE(this->wind_direction_degrees_sensor_,
float(tx20_wind_direction) * 22.5f);
149 ESP_LOGW(TAG,
"No value set!");
152 ESP_LOGW(TAG,
"Checksum wrong!");
155 ESP_LOGW(TAG,
"Start wrong!");