ESPHome 2026.2.1
Loading...
Searching...
No Matches
seeed_mr60bha2.cpp
Go to the documentation of this file.
1#include "seeed_mr60bha2.h"
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6#include <utility>
7
8namespace esphome {
9namespace seeed_mr60bha2 {
10
11static const char *const TAG = "seeed_mr60bha2";
12
13// Maximum bytes to log in verbose hex output
14static constexpr size_t MR60BHA2_MAX_LOG_BYTES = 64;
15
16// Prints the component's configuration data. dump_config() prints all of the component's configuration
17// items in an easy-to-read format, including the configuration key-value pairs.
19 ESP_LOGCONFIG(TAG, "MR60BHA2:");
20#ifdef USE_BINARY_SENSOR
21 LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->has_target_binary_sensor_);
22#endif
23#ifdef USE_SENSOR
24 LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_);
25 LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_);
26 LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_);
27 LOG_SENSOR(" ", "Target Number Sensor", this->num_targets_sensor_);
28#endif
29}
30
31// main loop
33 // Read all available bytes in batches to reduce UART call overhead.
34 size_t avail = this->available();
35 uint8_t buf[64];
36 while (avail > 0) {
37 size_t to_read = std::min(avail, sizeof(buf));
38 if (!this->read_array(buf, to_read)) {
39 break;
40 }
41 avail -= to_read;
42
43 for (size_t i = 0; i < to_read; i++) {
44 this->rx_message_.push_back(buf[i]);
45 if (!this->validate_message_()) {
46 this->rx_message_.clear();
47 }
48 }
49 }
50}
51
62static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
63 uint8_t checksum = 0;
64 for (size_t i = 0; i < len; i++) {
65 checksum ^= data[i];
66 }
67 checksum = ~checksum;
68 return checksum;
69}
70
82static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
83 return calculate_checksum(data, len) == expected_checksum;
84}
85
87 size_t at = this->rx_message_.size() - 1;
88 auto *data = &this->rx_message_[0];
89 uint8_t new_byte = data[at];
90
91 if (at == 0) {
92 return new_byte == FRAME_HEADER_BUFFER;
93 }
94
95 if (at <= 2) {
96 return true;
97 }
98 uint16_t frame_id = encode_uint16(data[1], data[2]);
99
100 if (at <= 4) {
101 return true;
102 }
103
104 uint16_t length = encode_uint16(data[3], data[4]);
105
106 if (at <= 6) {
107 return true;
108 }
109
110 uint16_t frame_type = encode_uint16(data[5], data[6]);
111
112 if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
113 frame_type != DISTANCE_TYPE_BUFFER && frame_type != PEOPLE_EXIST_TYPE_BUFFER &&
114 frame_type != PRINT_CLOUD_BUFFER) {
115 return false;
116 }
117
118 uint8_t header_checksum = new_byte;
119
120 if (at == 7) {
121 if (!validate_checksum(data, 7, header_checksum)) {
122 ESP_LOGE(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", header_checksum);
123#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
124 char hex_buf[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
125#endif
126 ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data, 8));
127 return false;
128 }
129 return true;
130 }
131
132 // Wait until all data is read
133 if (at - 8 < length) {
134 return true;
135 }
136
137 uint8_t data_checksum = new_byte;
138 if (at == 8 + length) {
139 if (!validate_checksum(data + 8, length, data_checksum)) {
140 ESP_LOGE(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", data_checksum);
141#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
142 char hex_buf[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
143#endif
144 ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data, 8 + length));
145 return false;
146 }
147 }
148
149 const uint8_t *frame_data = data + 8;
150#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
151 char hex_buf1[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
152 char hex_buf2[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
153#endif
154 ESP_LOGV(TAG, "Received Frame: ID: 0x%04x, Type: 0x%04x, Data: [%s] Raw Data: [%s]", frame_id, frame_type,
155 format_hex_pretty_to(hex_buf1, sizeof(hex_buf1), frame_data, length),
156 format_hex_pretty_to(hex_buf2, sizeof(hex_buf2), this->rx_message_.data(), this->rx_message_.size()));
157 this->process_frame_(frame_id, frame_type, data + 8, length);
158
159 // Return false to reset rx buffer
160 return false;
161}
162
163void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length) {
164 switch (frame_type) {
165 case BREATH_RATE_TYPE_BUFFER:
166 if (this->breath_rate_sensor_ != nullptr && length >= 4) {
167 uint32_t current_breath_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
168 if (current_breath_rate_int != 0) {
169 float breath_rate_float;
170 memcpy(&breath_rate_float, &current_breath_rate_int, sizeof(float));
171 this->breath_rate_sensor_->publish_state(breath_rate_float);
172 }
173 }
174 break;
175 case PEOPLE_EXIST_TYPE_BUFFER:
176 if (this->has_target_binary_sensor_ != nullptr && length >= 2) {
177 uint16_t has_target_int = encode_uint16(data[1], data[0]);
178 this->has_target_binary_sensor_->publish_state(has_target_int);
179 if (has_target_int == 0) {
180 this->breath_rate_sensor_->publish_state(0.0);
181 this->heart_rate_sensor_->publish_state(0.0);
182 this->distance_sensor_->publish_state(0.0);
183 this->num_targets_sensor_->publish_state(0);
184 }
185 }
186 break;
187 case HEART_RATE_TYPE_BUFFER:
188 if (this->heart_rate_sensor_ != nullptr && length >= 4) {
189 uint32_t current_heart_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
190 if (current_heart_rate_int != 0) {
191 float heart_rate_float;
192 memcpy(&heart_rate_float, &current_heart_rate_int, sizeof(float));
193 this->heart_rate_sensor_->publish_state(heart_rate_float);
194 }
195 }
196 break;
197 case DISTANCE_TYPE_BUFFER:
198 if (data[0] != 0) {
199 if (this->distance_sensor_ != nullptr && length >= 8) {
200 uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]);
201 float distance_float;
202 memcpy(&distance_float, &current_distance_int, sizeof(float));
203 this->distance_sensor_->publish_state(distance_float);
204 }
205 }
206 break;
207 case PRINT_CLOUD_BUFFER:
208 if (this->num_targets_sensor_ != nullptr && length >= 4) {
209 uint32_t current_num_targets_int = encode_uint32(data[3], data[2], data[1], data[0]);
210 this->num_targets_sensor_->publish_state(current_num_targets_int);
211 }
212 break;
213 default:
214 break;
215 }
216}
217
218} // namespace seeed_mr60bha2
219} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
void process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length)
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:692
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:353
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:978
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:536
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:528
uint16_t length
Definition tt21100.cpp:0