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