ESPHome  2024.12.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 <utility>
5 
6 namespace esphome {
7 namespace seeed_mr60bha2 {
8 
9 static const char *const TAG = "seeed_mr60bha2";
10 
11 // Prints the component's configuration data. dump_config() prints all of the component's configuration
12 // items in an easy-to-read format, including the configuration key-value pairs.
14  ESP_LOGCONFIG(TAG, "MR60BHA2:");
15 #ifdef USE_SENSOR
16  LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_);
17  LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_);
18  LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_);
19 #endif
20 }
21 
22 // main loop
24  uint8_t byte;
25 
26  // Is there data on the serial port
27  while (this->available()) {
28  this->read_byte(&byte);
29  this->rx_message_.push_back(byte);
30  if (!this->validate_message_()) {
31  this->rx_message_.clear();
32  }
33  }
34 }
35 
46 static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
47  uint8_t checksum = 0;
48  for (size_t i = 0; i < len; i++) {
49  checksum ^= data[i];
50  }
51  checksum = ~checksum;
52  return checksum;
53 }
54 
66 static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
67  return calculate_checksum(data, len) == expected_checksum;
68 }
69 
71  size_t at = this->rx_message_.size() - 1;
72  auto *data = &this->rx_message_[0];
73  uint8_t new_byte = data[at];
74 
75  if (at == 0) {
76  return new_byte == FRAME_HEADER_BUFFER;
77  }
78 
79  if (at <= 2) {
80  return true;
81  }
82  uint16_t frame_id = encode_uint16(data[1], data[2]);
83 
84  if (at <= 4) {
85  return true;
86  }
87 
88  uint16_t length = encode_uint16(data[3], data[4]);
89 
90  if (at <= 6) {
91  return true;
92  }
93 
94  uint16_t frame_type = encode_uint16(data[5], data[6]);
95 
96  if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
97  frame_type != DISTANCE_TYPE_BUFFER) {
98  return false;
99  }
100 
101  uint8_t header_checksum = new_byte;
102 
103  if (at == 7) {
104  if (!validate_checksum(data, 7, header_checksum)) {
105  ESP_LOGE(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", header_checksum);
106  ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty(data, 8).c_str());
107  return false;
108  }
109  return true;
110  }
111 
112  // Wait until all data is read
113  if (at - 8 < length) {
114  return true;
115  }
116 
117  uint8_t data_checksum = new_byte;
118  if (at == 8 + length) {
119  if (!validate_checksum(data + 8, length, data_checksum)) {
120  ESP_LOGE(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", data_checksum);
121  ESP_LOGV(TAG, "GET FRAME: %s", format_hex_pretty(data, 8 + length).c_str());
122  return false;
123  }
124  }
125 
126  const uint8_t *frame_data = data + 8;
127  ESP_LOGV(TAG, "Received Frame: ID: 0x%04x, Type: 0x%04x, Data: [%s] Raw Data: [%s]", frame_id, frame_type,
128  format_hex_pretty(frame_data, length).c_str(), format_hex_pretty(this->rx_message_).c_str());
129  this->process_frame_(frame_id, frame_type, data + 8, length);
130 
131  // Return false to reset rx buffer
132  return false;
133 }
134 
135 void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length) {
136  switch (frame_type) {
137  case BREATH_RATE_TYPE_BUFFER:
138  if (this->breath_rate_sensor_ != nullptr && length >= 4) {
139  uint32_t current_breath_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
140  if (current_breath_rate_int != 0) {
141  float breath_rate_float;
142  memcpy(&breath_rate_float, &current_breath_rate_int, sizeof(float));
143  this->breath_rate_sensor_->publish_state(breath_rate_float);
144  }
145  }
146  break;
147  case HEART_RATE_TYPE_BUFFER:
148  if (this->heart_rate_sensor_ != nullptr && length >= 4) {
149  uint32_t current_heart_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
150  if (current_heart_rate_int != 0) {
151  float heart_rate_float;
152  memcpy(&heart_rate_float, &current_heart_rate_int, sizeof(float));
153  this->heart_rate_sensor_->publish_state(heart_rate_float);
154  }
155  }
156  break;
157  case DISTANCE_TYPE_BUFFER:
158  if (!data[0]) {
159  if (this->distance_sensor_ != nullptr && length >= 8) {
160  uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]);
161  float distance_float;
162  memcpy(&distance_float, &current_distance_int, sizeof(float));
163  this->distance_sensor_->publish_state(distance_float);
164  }
165  }
166  break;
167  default:
168  break;
169  }
170 }
171 
172 } // namespace seeed_mr60bha2
173 } // 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:369
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:187
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:183
std::string size_t len
Definition: helpers.h:293
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7