7 namespace seeed_mr60bha2 {
9 static const char *
const TAG =
"seeed_mr60bha2";
14 ESP_LOGCONFIG(TAG,
"MR60BHA2:");
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_);
46 static uint8_t calculate_checksum(
const uint8_t *data,
size_t len) {
48 for (
size_t i = 0; i <
len; i++) {
66 static bool validate_checksum(
const uint8_t *data,
size_t len, uint8_t expected_checksum) {
67 return calculate_checksum(data, len) == expected_checksum;
73 uint8_t new_byte = data[at];
76 return new_byte == FRAME_HEADER_BUFFER;
96 if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
97 frame_type != DISTANCE_TYPE_BUFFER) {
101 uint8_t header_checksum = new_byte;
104 if (!validate_checksum(data, 7, header_checksum)) {
105 ESP_LOGE(TAG,
"HEAD_CKSUM_FRAME ERROR: 0x%02x", header_checksum);
113 if (at - 8 < length) {
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);
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,
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, ¤t_breath_rate_int,
sizeof(
float));
143 this->breath_rate_sensor_->publish_state(breath_rate_float);
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, ¤t_heart_rate_int,
sizeof(
float));
153 this->heart_rate_sensor_->publish_state(heart_rate_float);
157 case DISTANCE_TYPE_BUFFER:
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, ¤t_distance_int,
sizeof(
float));
163 this->distance_sensor_->publish_state(distance_float);
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.
void process_frame_(uint16_t frame_id, uint16_t frame_type, const uint8_t *data, size_t length)
std::vector< uint8_t > rx_message_
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.
void dump_config() override
bool read_byte(uint8_t *data)
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Implementation of SPI Controller mode.