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