10 static const char *
const TAG =
"tx20";
11 static const uint8_t MAX_BUFFER_SIZE = 41;
12 static const uint16_t TX20_MAX_TIME = MAX_BUFFER_SIZE * 1200 + 5000;
13 static const uint16_t TX20_BIT_TIME = 1200;
14 static const char *
const DIRECTIONS[] = {
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
15 "S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW"};
18 ESP_LOGCONFIG(TAG,
"Setting up Tx20");
28 ESP_LOGCONFIG(TAG,
"Tx20:");
33 LOG_PIN(
" Pin: ", this->
pin_);
47 ESP_LOGVV(TAG,
"Decode Tx20...");
49 std::string string_buffer;
50 std::string string_buffer_2;
51 std::vector<bool> bit_buffer;
52 bool current_bit =
true;
58 string_buffer.append(repeat, current_bit ?
'1' :
'0');
59 bit_buffer.insert(bit_buffer.end(), repeat, current_bit);
60 current_bit = !current_bit;
62 current_bit = !current_bit;
63 if (string_buffer.length() < MAX_BUFFER_SIZE) {
64 uint8_t remain = MAX_BUFFER_SIZE - string_buffer.length();
65 string_buffer_2 +=
to_string(remain) +
", ";
66 string_buffer.append(remain, current_bit ?
'1' :
'0');
67 bit_buffer.insert(bit_buffer.end(), remain, current_bit);
76 uint8_t tx20_wind_direction = 0;
77 float tx20_wind_speed_kmh = 0;
78 uint8_t bit_count = 0;
80 for (
int i = 41; i > 0; i--) {
81 uint8_t bit = bit_buffer.at(bit_count);
85 tx20_sa = (tx20_sa << 1) | (bit ^ 1);
86 }
else if (i > 41 - 5 - 4) {
88 tx20_sb = tx20_sb >> 1 | ((bit ^ 1) << 3);
89 }
else if (i > 41 - 5 - 4 - 12) {
91 tx20_sc = tx20_sc >> 1 | ((bit ^ 1) << 11);
92 }
else if (i > 41 - 5 - 4 - 12 - 4) {
94 tx20_sd = tx20_sd >> 1 | ((bit ^ 1) << 3);
95 }
else if (i > 41 - 5 - 4 - 12 - 4 - 4) {
97 tx20_se = tx20_se >> 1 | (bit << 3);
100 tx20_sf = tx20_sf >> 1 | (bit << 11);
104 uint8_t chk = (tx20_sb + (tx20_sc & 0xf) + ((tx20_sc >> 4) & 0xf) + ((tx20_sc >> 8) & 0xf));
106 bool value_set =
false;
112 ESP_LOGVV(TAG,
"BUFFER %s", string_buffer_2.c_str());
113 ESP_LOGVV(TAG,
"Decoded bits %s", string_buffer.c_str());
116 if (chk == tx20_sd) {
117 if (tx20_sf == tx20_sc) {
118 tx20_wind_speed_kmh = float(tx20_sc) * 0.36f;
119 ESP_LOGV(TAG,
"WindSpeed %f", tx20_wind_speed_kmh);
124 if (tx20_se == tx20_sb) {
125 tx20_wind_direction = tx20_se;
126 if (tx20_wind_direction >= 0 && tx20_wind_direction < 16) {
129 ESP_LOGV(TAG,
"WindDirection %d", tx20_wind_direction);
135 ESP_LOGW(TAG,
"No value set!");
138 ESP_LOGW(TAG,
"Checksum wrong!");
141 ESP_LOGW(TAG,
"Start wrong!");
147 const uint32_t now =
micros();
162 if (index == 1 && (delay > 3000 || delay < 2400)) {
167 if (index == 2 && (delay > 1500 || delay < 1200)) {
172 if (index == 3 && (delay > 3000 || delay < 2400)) {
181 if (index <= MAX_BUFFER_SIZE) {
189 tx20_available =
false;
volatile uint8_t buffer_index
const float DATA
For components that import data from directly connected sensors like DHT.
void decode_and_publish_()
volatile uint32_t spent_time
static void gpio_intr(Tx20ComponentStore *arg)
sensor::Sensor * wind_direction_degrees_sensor_
uint32_t IRAM_ATTR HOT micros()
float get_setup_priority() const override
volatile uint32_t start_time
void dump_config() override
void publish_state(float state)
Publish a new state to the front-end.
sensor::Sensor * wind_speed_sensor_
virtual ISRInternalGPIOPin to_isr() const =0
Store data in a class that doesn't use multiple-inheritance (vtables in flash)
std::string to_string(int value)
volatile bool tx20_available
std::string wind_cardinal_direction_
volatile uint16_t * buffer
Implementation of SPI Controller mode.
Tx20ComponentStore store_
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
std::string get_wind_cardinal_direction() const
Get the textual representation of the wind direction ('N', 'SSE', ..).
void IRAM_ATTR HOT delay(uint32_t ms)