ESPHome 2025.7.4
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// Prints the component's configuration data. dump_config() prints all of the component's configuration
14// items in an easy-to-read format, including the configuration key-value pairs.
16 ESP_LOGCONFIG(TAG, "MR60BHA2:");
17#ifdef USE_BINARY_SENSOR
18 LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->has_target_binary_sensor_);
19#endif
20#ifdef USE_SENSOR
21 LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_);
22 LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_);
23 LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_);
24 LOG_SENSOR(" ", "Target Number Sensor", this->num_targets_sensor_);
25#endif
26}
27
28// main loop
30 uint8_t byte;
31
32 // Is there data on the serial port
33 while (this->available()) {
34 this->read_byte(&byte);
35 this->rx_message_.push_back(byte);
36 if (!this->validate_message_()) {
37 this->rx_message_.clear();
38 }
39 }
40}
41
52static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
53 uint8_t checksum = 0;
54 for (size_t i = 0; i < len; i++) {
55 checksum ^= data[i];
56 }
57 checksum = ~checksum;
58 return checksum;
59}
60
72static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
73 return calculate_checksum(data, len) == expected_checksum;
74}
75
77 size_t at = this->rx_message_.size() - 1;
78 auto *data = &this->rx_message_[0];
79 uint8_t new_byte = data[at];
80
81 if (at == 0) {
82 return new_byte == FRAME_HEADER_BUFFER;
83 }
84
85 if (at <= 2) {
86 return true;
87 }
88 uint16_t frame_id = encode_uint16(data[1], data[2]);
89
90 if (at <= 4) {
91 return true;
92 }
93
94 uint16_t length = encode_uint16(data[3], data[4]);
95
96 if (at <= 6) {
97 return true;
98 }
99
100 uint16_t frame_type = encode_uint16(data[5], data[6]);
101
102 if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
103 frame_type != DISTANCE_TYPE_BUFFER && frame_type != PEOPLE_EXIST_TYPE_BUFFER &&
104 frame_type != PRINT_CLOUD_BUFFER) {
105 return false;
106 }
107
108 uint8_t header_checksum = new_byte;
109
110 if (at == 7) {
111 if (!validate_checksum(data, 7, header_checksum)) {
112 ESP_LOGE(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", header_checksum);
113 ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty(data, 8).c_str());
114 return false;
115 }
116 return true;
117 }
118
119 // Wait until all data is read
120 if (at - 8 < length) {
121 return true;
122 }
123
124 uint8_t data_checksum = new_byte;
125 if (at == 8 + length) {
126 if (!validate_checksum(data + 8, length, data_checksum)) {
127 ESP_LOGE(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", data_checksum);
128 ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty(data, 8 + length).c_str());
129 return false;
130 }
131 }
132
133 const uint8_t *frame_data = data + 8;
134 ESP_LOGV(TAG, "Received Frame: ID: 0x%04x, Type: 0x%04x, Data: [%s] Raw Data: [%s]", frame_id, frame_type,
135 format_hex_pretty(frame_data, length).c_str(), format_hex_pretty(this->rx_message_).c_str());
136 this->process_frame_(frame_id, frame_type, data + 8, length);
137
138 // Return false to reset rx buffer
139 return false;
140}
141
142void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length) {
143 switch (frame_type) {
144 case BREATH_RATE_TYPE_BUFFER:
145 if (this->breath_rate_sensor_ != nullptr && length >= 4) {
146 uint32_t current_breath_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
147 if (current_breath_rate_int != 0) {
148 float breath_rate_float;
149 memcpy(&breath_rate_float, &current_breath_rate_int, sizeof(float));
150 this->breath_rate_sensor_->publish_state(breath_rate_float);
151 }
152 }
153 break;
154 case PEOPLE_EXIST_TYPE_BUFFER:
155 if (this->has_target_binary_sensor_ != nullptr && length >= 2) {
156 uint16_t has_target_int = encode_uint16(data[1], data[0]);
157 this->has_target_binary_sensor_->publish_state(has_target_int);
158 if (has_target_int == 0) {
159 this->breath_rate_sensor_->publish_state(0.0);
160 this->heart_rate_sensor_->publish_state(0.0);
161 this->distance_sensor_->publish_state(0.0);
162 this->num_targets_sensor_->publish_state(0);
163 }
164 }
165 break;
166 case HEART_RATE_TYPE_BUFFER:
167 if (this->heart_rate_sensor_ != nullptr && length >= 4) {
168 uint32_t current_heart_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
169 if (current_heart_rate_int != 0) {
170 float heart_rate_float;
171 memcpy(&heart_rate_float, &current_heart_rate_int, sizeof(float));
172 this->heart_rate_sensor_->publish_state(heart_rate_float);
173 }
174 }
175 break;
176 case DISTANCE_TYPE_BUFFER:
177 if (data[0] != 0) {
178 if (this->distance_sensor_ != nullptr && length >= 8) {
179 uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]);
180 float distance_float;
181 memcpy(&distance_float, &current_distance_int, sizeof(float));
182 this->distance_sensor_->publish_state(distance_float);
183 }
184 }
185 break;
186 case PRINT_CLOUD_BUFFER:
187 if (this->num_targets_sensor_ != nullptr && length >= 4) {
188 uint32_t current_num_targets_int = encode_uint32(data[3], data[2], data[1], data[0]);
189 this->num_targets_sensor_->publish_state(current_num_targets_int);
190 }
191 break;
192 default:
193 break;
194 }
195}
196
197} // namespace seeed_mr60bha2
198} // 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:29
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:232
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:261
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:134
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:126
uint16_t length
Definition tt21100.cpp:0