ESPHome 2026.1.0-dev
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 uint8_t byte;
34
35 // Is there data on the serial port
36 while (this->available()) {
37 this->read_byte(&byte);
38 this->rx_message_.push_back(byte);
39 if (!this->validate_message_()) {
40 this->rx_message_.clear();
41 }
42 }
43}
44
55static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
56 uint8_t checksum = 0;
57 for (size_t i = 0; i < len; i++) {
58 checksum ^= data[i];
59 }
60 checksum = ~checksum;
61 return checksum;
62}
63
75static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
76 return calculate_checksum(data, len) == expected_checksum;
77}
78
80 size_t at = this->rx_message_.size() - 1;
81 auto *data = &this->rx_message_[0];
82 uint8_t new_byte = data[at];
83
84 if (at == 0) {
85 return new_byte == FRAME_HEADER_BUFFER;
86 }
87
88 if (at <= 2) {
89 return true;
90 }
91 uint16_t frame_id = encode_uint16(data[1], data[2]);
92
93 if (at <= 4) {
94 return true;
95 }
96
97 uint16_t length = encode_uint16(data[3], data[4]);
98
99 if (at <= 6) {
100 return true;
101 }
102
103 uint16_t frame_type = encode_uint16(data[5], data[6]);
104
105 if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
106 frame_type != DISTANCE_TYPE_BUFFER && frame_type != PEOPLE_EXIST_TYPE_BUFFER &&
107 frame_type != PRINT_CLOUD_BUFFER) {
108 return false;
109 }
110
111 uint8_t header_checksum = new_byte;
112
113 if (at == 7) {
114 if (!validate_checksum(data, 7, header_checksum)) {
115 ESP_LOGE(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", header_checksum);
116#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
117 char hex_buf[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
118#endif
119 ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data, 8));
120 return false;
121 }
122 return true;
123 }
124
125 // Wait until all data is read
126 if (at - 8 < length) {
127 return true;
128 }
129
130 uint8_t data_checksum = new_byte;
131 if (at == 8 + length) {
132 if (!validate_checksum(data + 8, length, data_checksum)) {
133 ESP_LOGE(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", data_checksum);
134#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
135 char hex_buf[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
136#endif
137 ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty_to(hex_buf, sizeof(hex_buf), data, 8 + length));
138 return false;
139 }
140 }
141
142 const uint8_t *frame_data = data + 8;
143#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
144 char hex_buf1[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
145 char hex_buf2[format_hex_pretty_size(MR60BHA2_MAX_LOG_BYTES)];
146#endif
147 ESP_LOGV(TAG, "Received Frame: ID: 0x%04x, Type: 0x%04x, Data: [%s] Raw Data: [%s]", frame_id, frame_type,
148 format_hex_pretty_to(hex_buf1, sizeof(hex_buf1), frame_data, length),
149 format_hex_pretty_to(hex_buf2, sizeof(hex_buf2), this->rx_message_.data(), this->rx_message_.size()));
150 this->process_frame_(frame_id, frame_type, data + 8, length);
151
152 // Return false to reset rx buffer
153 return false;
154}
155
156void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length) {
157 switch (frame_type) {
158 case BREATH_RATE_TYPE_BUFFER:
159 if (this->breath_rate_sensor_ != nullptr && length >= 4) {
160 uint32_t current_breath_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
161 if (current_breath_rate_int != 0) {
162 float breath_rate_float;
163 memcpy(&breath_rate_float, &current_breath_rate_int, sizeof(float));
164 this->breath_rate_sensor_->publish_state(breath_rate_float);
165 }
166 }
167 break;
168 case PEOPLE_EXIST_TYPE_BUFFER:
169 if (this->has_target_binary_sensor_ != nullptr && length >= 2) {
170 uint16_t has_target_int = encode_uint16(data[1], data[0]);
171 this->has_target_binary_sensor_->publish_state(has_target_int);
172 if (has_target_int == 0) {
173 this->breath_rate_sensor_->publish_state(0.0);
174 this->heart_rate_sensor_->publish_state(0.0);
175 this->distance_sensor_->publish_state(0.0);
176 this->num_targets_sensor_->publish_state(0);
177 }
178 }
179 break;
180 case HEART_RATE_TYPE_BUFFER:
181 if (this->heart_rate_sensor_ != nullptr && length >= 4) {
182 uint32_t current_heart_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
183 if (current_heart_rate_int != 0) {
184 float heart_rate_float;
185 memcpy(&heart_rate_float, &current_heart_rate_int, sizeof(float));
186 this->heart_rate_sensor_->publish_state(heart_rate_float);
187 }
188 }
189 break;
190 case DISTANCE_TYPE_BUFFER:
191 if (data[0] != 0) {
192 if (this->distance_sensor_ != nullptr && length >= 8) {
193 uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]);
194 float distance_float;
195 memcpy(&distance_float, &current_distance_int, sizeof(float));
196 this->distance_sensor_->publish_state(distance_float);
197 }
198 }
199 break;
200 case PRINT_CLOUD_BUFFER:
201 if (this->num_targets_sensor_ != nullptr && length >= 4) {
202 uint32_t current_num_targets_int = encode_uint32(data[3], data[2], data[1], data[0]);
203 this->num_targets_sensor_->publish_state(current_num_targets_int);
204 }
205 break;
206 default:
207 break;
208 }
209}
210
211} // namespace seeed_mr60bha2
212} // 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)
bool read_byte(uint8_t *data)
Definition uart.h:34
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:533
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:331
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:735
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:428
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:420
uint16_t length
Definition tt21100.cpp:0