43 ESP_LOGVV(TAG,
"Decode Tx20");
45 std::array<bool, MAX_BUFFER_SIZE> bit_buffer{};
47 bool current_bit =
true;
49 const int max_buffer_index =
50 std::min(
static_cast<int>(this->
store_.
buffer_index),
static_cast<int>(MAX_BUFFER_SIZE - 1));
52 for (
int i = 1; i <= max_buffer_index; i++) {
55 for (uint8_t j = 0; j < repeat && bit_pos < MAX_BUFFER_SIZE; j++) {
56 bit_buffer[bit_pos++] = current_bit;
58 current_bit = !current_bit;
60 current_bit = !current_bit;
61 size_t bits_before_padding = bit_pos;
62 while (bit_pos < MAX_BUFFER_SIZE) {
63 bit_buffer[bit_pos++] = current_bit;
72 uint8_t tx20_wind_direction = 0;
73 float tx20_wind_speed_kmh = 0;
74 uint8_t bit_count = 0;
76 for (
int i = 41; i > 0; i--) {
77 uint8_t bit = bit_buffer.at(bit_count);
81 tx20_sa = (tx20_sa << 1) | (bit ^ 1);
82 }
else if (i > 41 - 5 - 4) {
84 tx20_sb = tx20_sb >> 1 | ((bit ^ 1) << 3);
85 }
else if (i > 41 - 5 - 4 - 12) {
87 tx20_sc = tx20_sc >> 1 | ((bit ^ 1) << 11);
88 }
else if (i > 41 - 5 - 4 - 12 - 4) {
90 tx20_sd = tx20_sd >> 1 | ((bit ^ 1) << 3);
91 }
else if (i > 41 - 5 - 4 - 12 - 4 - 4) {
93 tx20_se = tx20_se >> 1 | (bit << 3);
96 tx20_sf = tx20_sf >> 1 | (bit << 11);
100 uint8_t chk = (tx20_sb + (tx20_sc & 0xf) + ((tx20_sc >> 4) & 0xf) + ((tx20_sc >> 8) & 0xf));
102 bool value_set =
false;
108#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
111 size_t debug_pos = 0;
112 for (
int i = 1; i <= max_buffer_index; i++) {
113 debug_pos = buf_append_printf(debug_buf,
sizeof(debug_buf), debug_pos,
"%u, ", this->
store_.
buffer[i]);
115 if (bits_before_padding < MAX_BUFFER_SIZE) {
116 buf_append_printf(debug_buf,
sizeof(debug_buf), debug_pos,
"%zu, ", MAX_BUFFER_SIZE - bits_before_padding);
118 char bits_buf[MAX_BUFFER_SIZE + 1];
119 for (
size_t i = 0; i < MAX_BUFFER_SIZE; i++) {
120 bits_buf[i] = bit_buffer[i] ?
'1' :
'0';
122 bits_buf[MAX_BUFFER_SIZE] =
'\0';
123 ESP_LOGVV(TAG,
"BUFFER %s", debug_buf);
124 ESP_LOGVV(TAG,
"Decoded bits %s", bits_buf);
128 if (chk == tx20_sd) {
129 if (tx20_sf == tx20_sc) {
130 tx20_wind_speed_kmh = float(tx20_sc) * 0.36f;
131 ESP_LOGV(TAG,
"WindSpeed %f", tx20_wind_speed_kmh);
136 if (tx20_se == tx20_sb) {
137 tx20_wind_direction = tx20_se;
138 if (tx20_wind_direction < 16) {
141 ESP_LOGV(TAG,
"WindDirection %d", tx20_wind_direction);
147 ESP_LOGW(TAG,
"No value set!");
150 ESP_LOGW(TAG,
"Checksum wrong!");
153 ESP_LOGW(TAG,
"Start wrong!");