44 ESP_LOGVV(TAG,
"Decode Tx20");
46 std::array<bool, MAX_BUFFER_SIZE> bit_buffer{};
48 bool current_bit =
true;
50 const int max_buffer_index =
51 std::min(
static_cast<int>(this->
store_.
buffer_index),
static_cast<int>(MAX_BUFFER_SIZE - 1));
53 for (
int i = 1; i <= max_buffer_index; i++) {
56 for (uint8_t j = 0; j < repeat && bit_pos < MAX_BUFFER_SIZE; j++) {
57 bit_buffer[bit_pos++] = current_bit;
59 current_bit = !current_bit;
61 current_bit = !current_bit;
62 size_t bits_before_padding = bit_pos;
63 while (bit_pos < MAX_BUFFER_SIZE) {
64 bit_buffer[bit_pos++] = current_bit;
73 uint8_t tx20_wind_direction = 0;
74 float tx20_wind_speed_kmh = 0;
75 uint8_t bit_count = 0;
77 for (
int i = 41; i > 0; i--) {
78 uint8_t bit = bit_buffer.at(bit_count);
82 tx20_sa = (tx20_sa << 1) | (bit ^ 1);
83 }
else if (i > 41 - 5 - 4) {
85 tx20_sb = tx20_sb >> 1 | ((bit ^ 1) << 3);
86 }
else if (i > 41 - 5 - 4 - 12) {
88 tx20_sc = tx20_sc >> 1 | ((bit ^ 1) << 11);
89 }
else if (i > 41 - 5 - 4 - 12 - 4) {
91 tx20_sd = tx20_sd >> 1 | ((bit ^ 1) << 3);
92 }
else if (i > 41 - 5 - 4 - 12 - 4 - 4) {
94 tx20_se = tx20_se >> 1 | (bit << 3);
97 tx20_sf = tx20_sf >> 1 | (bit << 11);
101 uint8_t chk = (tx20_sb + (tx20_sc & 0xf) + ((tx20_sc >> 4) & 0xf) + ((tx20_sc >> 8) & 0xf));
103 bool value_set =
false;
109#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
112 size_t debug_pos = 0;
113 for (
int i = 1; i <= max_buffer_index; i++) {
114 debug_pos = buf_append_printf(debug_buf,
sizeof(debug_buf), debug_pos,
"%u, ", this->
store_.
buffer[i]);
116 if (bits_before_padding < MAX_BUFFER_SIZE) {
117 buf_append_printf(debug_buf,
sizeof(debug_buf), debug_pos,
"%zu, ", MAX_BUFFER_SIZE - bits_before_padding);
119 char bits_buf[MAX_BUFFER_SIZE + 1];
120 for (
size_t i = 0; i < MAX_BUFFER_SIZE; i++) {
121 bits_buf[i] = bit_buffer[i] ?
'1' :
'0';
123 bits_buf[MAX_BUFFER_SIZE] =
'\0';
124 ESP_LOGVV(TAG,
"BUFFER %s", debug_buf);
125 ESP_LOGVV(TAG,
"Decoded bits %s", bits_buf);
129 if (chk == tx20_sd) {
130 if (tx20_sf == tx20_sc) {
131 tx20_wind_speed_kmh = float(tx20_sc) * 0.36f;
132 ESP_LOGV(TAG,
"WindSpeed %f", tx20_wind_speed_kmh);
137 if (tx20_se == tx20_sb) {
138 tx20_wind_direction = tx20_se;
139 if (tx20_wind_direction >= 0 && tx20_wind_direction < 16) {
142 ESP_LOGV(TAG,
"WindDirection %d", tx20_wind_direction);
148 ESP_LOGW(TAG,
"No value set!");
151 ESP_LOGW(TAG,
"Checksum wrong!");
154 ESP_LOGW(TAG,
"Start wrong!");