diff --git a/esphome/components/seeed_mr60bha2/__init__.py b/esphome/components/seeed_mr60bha2/__init__.py deleted file mode 100644 index 9febf9f65a..0000000000 --- a/esphome/components/seeed_mr60bha2/__init__.py +++ /dev/null @@ -1,57 +0,0 @@ -import esphome.codegen as cg -import esphome.config_validation as cv -from esphome.components import uart -from esphome.const import CONF_ID - -DEPENDENCIES = ["uart"] -# is the code owner of the relevant code base -CODEOWNERS = ["@limengdu"] -# The current component or platform can be configured or defined multiple times in the same configuration file. -MULTI_CONF = True - -# This line of code creates a new namespace called mr60fda2_ns. -# This namespace will be used as a prefix for all classes, functions and variables associated with the mr60fda2_ns component, ensuring that they do not conflict with the names of other components. -mr60fda2_ns = cg.esphome_ns.namespace("seeed_mr60bha2") -# This MR24HPC1Component class will be a periodically polled UART device -MR60BHA2Component = mr60fda2_ns.class_( - "MR60BHA2Component", cg.Component, uart.UARTDevice -) - -CONF_MR60BHA2_ID = "mr60bha2_id" - -CONFIG_SCHEMA = ( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(MR60BHA2Component), - } - ) - .extend(uart.UART_DEVICE_SCHEMA) - .extend(cv.COMPONENT_SCHEMA) -) - -# This code extends the current CONFIG_SCHEMA by adding all the configuration parameters for the UART device and components. -# This means that in the YAML configuration file, the user can use these parameters to configure this component. -CONFIG_SCHEMA = cv.All( - CONFIG_SCHEMA.extend(uart.UART_DEVICE_SCHEMA).extend(cv.COMPONENT_SCHEMA) -) - -# A verification mode was created to verify the configuration parameters of a UART device named "seeed_mr60bha2". -# This authentication mode requires that the device must have transmit and receive functionality, a parity mode of "NONE", and a stop bit of one. -FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( - "seeed_mr60bha2", - require_tx=True, - require_rx=True, - parity="NONE", - stop_bits=1, -) - - -# The async def keyword is used to define a concurrent function. -# Concurrent functions are special functions designed to work with Python's asyncio library to support asynchronous I/O operations. -async def to_code(config): - # This line of code creates a new Pvariable (a Python object representing a C++ variable) with the variable's ID taken from the configuration. - var = cg.new_Pvariable(config[CONF_ID]) - # This line of code registers the newly created Pvariable as a component so that ESPHome can manage it at runtime. - await cg.register_component(var, config) - # This line of code registers the newly created Pvariable as a device. - await uart.register_uart_device(var, config) diff --git a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp b/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp deleted file mode 100644 index 5b97b22eda..0000000000 --- a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp +++ /dev/null @@ -1,248 +0,0 @@ -#include "esphome/core/log.h" -#include "seeed_mr60bha2.h" - -#include - -namespace esphome { -namespace seeed_mr60bha2 { - -static const char *const TAG = "seeed_mr60bha2"; - -// Prints the component's configuration data. dump_config() prints all of the component's configuration -// items in an easy-to-read format, including the configuration key-value pairs. -void MR60BHA2Component::dump_config() { - ESP_LOGCONFIG(TAG, "MR60BHA2:"); -#ifdef USE_SENSOR - LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_); - LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_); - LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_); -#endif -} - -// Initialisation functions -void MR60BHA2Component::setup() { - ESP_LOGCONFIG(TAG, "Setting up MR60BHA2..."); - this->check_uart_settings(115200); - - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - this->current_frame_id_ = 0; - this->current_frame_len_ = 0; - this->current_data_frame_len_ = 0; - this->current_frame_type_ = 0; - this->current_breath_rate_int_ = 0; - this->current_heart_rate_int_ = 0; - this->current_distance_int_ = 0; - - memset(this->current_frame_buf, 0, FRAME_BUF_MAX_SIZE); - memset(this->current_data_buf, 0, DATA_BUF_MAX_SIZE); - - ESP_LOGCONFIG(TAG, "Set up MR60BHA2 complete"); -} - -// main loop -void MR60BHA2Component::loop() { - uint8_t byte; - - // Is there data on the serial port - while (this->available()) { - this->read_byte(&byte); - this->splitFrame(byte); // split data frame - } -} - -/** - * @brief Calculate the checksum for a byte array. - * - * This function calculates the checksum for the provided byte array using an - * XOR-based checksum algorithm. - * - * @param data The byte array to calculate the checksum for. - * @param len The length of the byte array. - * @return The calculated checksum. - */ -uint8_t MR60BHA2Component::calculateChecksum(const uint8_t *data, size_t len) { - uint8_t checksum = 0; - for (size_t i = 0; i < len; i++) { - checksum ^= data[i]; - } - checksum = ~checksum; - return checksum; -} - -/** - * @brief Validate the checksum of a byte array. - * - * This function validates the checksum of the provided byte array by comparing - * it to the expected checksum. - * - * @param data The byte array to validate. - * @param len The length of the byte array. - * @param expected_checksum The expected checksum. - * @return True if the checksum is valid, false otherwise. - */ -bool MR60BHA2Component::validateChecksum(const uint8_t *data, size_t len, uint8_t expected_checksum) { - return calculateChecksum(data, len) == expected_checksum; -} - -void MR60BHA2Component::splitFrame(uint8_t buffer) { - switch (this->current_frame_locate_) { - case LOCATE_FRAME_HEADER: // starting buffer - if (buffer == FRAME_HEADER_BUFFER) { - this->current_frame_len_ = 1; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - } - break; - case LOCATE_ID_FRAME1: - this->current_frame_id_ = buffer << 8; - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - break; - case LOCATE_ID_FRAME2: - this->current_frame_id_ += buffer; - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - break; - case LOCATE_LENGTH_FRAME_H: - this->current_data_frame_len_ = buffer << 8; - if (this->current_data_frame_len_ == 0x00) { - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - } else { - // ESP_LOGD(TAG, "DATA_FRAME_LEN_H: 0x%02x", buffer); - // ESP_LOGD(TAG, "CURRENT_FRAME_LEN_H: 0x%04x", this->current_data_frame_len_); - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - } - break; - case LOCATE_LENGTH_FRAME_L: - this->current_data_frame_len_ += buffer; - if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) { - // ESP_LOGD(TAG, "DATA_FRAME_LEN_L: 0x%02x", buffer); - // ESP_LOGD(TAG, "CURRENT_FRAME_LEN: 0x%04x", this->current_data_frame_len_); - // ESP_LOGD(TAG, "DATA_FRAME_LEN ERROR: %d", this->current_data_frame_len_); - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - } else { - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - } - break; - case LOCATE_TYPE_FRAME1: - this->current_frame_type_ = buffer << 8; - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - // ESP_LOGD(TAG, "GET LOCATE_TYPE_FRAME1: 0x%02x", this->current_frame_buf[this->current_frame_len_ - 1]); - break; - case LOCATE_TYPE_FRAME2: - this->current_frame_type_ += buffer; - if ((this->current_frame_type_ == BREATH_RATE_TYPE_BUFFER) || - (this->current_frame_type_ == HEART_RATE_TYPE_BUFFER) || - (this->current_frame_type_ == DISTANCE_TYPE_BUFFER)) { - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - // ESP_LOGD(TAG, "GET CURRENT_FRAME_TYPE: 0x%02x 0x%02x", this->current_frame_buf[this->current_frame_len_ - 2], - // this->current_frame_buf[this->current_frame_len_ - 1]); - } else { - // ESP_LOGD(TAG, "CURRENT_FRAME_TYPE NOT FOUND: 0x%02x 0x%02x", - // this->current_frame_buf[this->current_frame_len_ - 2], - // this->current_frame_buf[this->current_frame_len_ - 1]); - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - } - break; - case LOCATE_HEAD_CKSUM_FRAME: - if (this->validateChecksum(this->current_frame_buf, this->current_frame_len_, buffer)) { - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - } else { - ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer); - ESP_LOGD(TAG, "GET CURRENT_FRAME:"); - for (size_t i = 0; i < this->current_frame_len_; i++) { - ESP_LOGD(TAG, " 0x%02x", current_frame_buf[i]); - } - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - } - break; - case LOCATE_DATA_FRAME: - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_data_buf[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer; - if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) { - this->current_frame_locate_++; - } - if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) { - ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM); - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - } - break; - case LOCATE_DATA_CKSUM_FRAME: - if (this->validateChecksum(this->current_data_buf, this->current_data_frame_len_, buffer)) { - this->current_frame_len_++; - this->current_frame_buf[this->current_frame_len_ - 1] = buffer; - this->current_frame_locate_++; - this->processFrame(); - } else { - ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer); - ESP_LOGD(TAG, "GET CURRENT_FRAME:"); - for (size_t i = 0; i < this->current_frame_len_; i++) { - ESP_LOGD(TAG, " 0x%02x", current_frame_buf[i]); - } - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - } - break; - default: - break; - } -} - -void MR60BHA2Component::processFrame() { - switch (this->current_frame_type_) { - case BREATH_RATE_TYPE_BUFFER: - if (this->breath_rate_sensor_ != nullptr) { - this->current_breath_rate_int_ = - (static_cast(current_data_buf[3]) << 24) | (static_cast(current_data_buf[2]) << 16) | - (static_cast(current_data_buf[1]) << 8) | static_cast(current_data_buf[0]); - float breath_rate_float; - memcpy(&breath_rate_float, ¤t_breath_rate_int_, sizeof(float)); - this->breath_rate_sensor_->publish_state(breath_rate_float); - } - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - break; - case HEART_RATE_TYPE_BUFFER: - if (this->heart_rate_sensor_ != nullptr) { - this->current_heart_rate_int_ = - (static_cast(current_data_buf[3]) << 24) | (static_cast(current_data_buf[2]) << 16) | - (static_cast(current_data_buf[1]) << 8) | static_cast(current_data_buf[0]); - float heart_rate_float; - memcpy(&heart_rate_float, ¤t_heart_rate_int_, sizeof(float)); - this->heart_rate_sensor_->publish_state(heart_rate_float); - } - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - break; - case DISTANCE_TYPE_BUFFER: - if (!current_data_buf[0]) { - // ESP_LOGD(TAG, "Successfully set the mounting height"); - if (this->distance_sensor_ != nullptr) { - this->current_distance_int_ = - (static_cast(current_data_buf[7]) << 24) | (static_cast(current_data_buf[6]) << 16) | - (static_cast(current_data_buf[5]) << 8) | static_cast(current_data_buf[4]); - float distance_float; - memcpy(&distance_float, ¤t_distance_int_, sizeof(float)); - this->distance_sensor_->publish_state(distance_float); - } - } else - ESP_LOGD(TAG, "Distance information is not output"); - this->current_frame_locate_ = LOCATE_FRAME_HEADER; - break; - default: - break; - } -} - -} // namespace seeed_mr60bha2 -} // namespace esphome diff --git a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h b/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h deleted file mode 100644 index bfd86247e0..0000000000 --- a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once -#include "esphome/core/defines.h" -#include "esphome/core/component.h" -#ifdef USE_SENSOR -#include "esphome/components/sensor/sensor.h" -#endif -#include "esphome/components/uart/uart.h" -#include "esphome/core/automation.h" -#include "esphome/core/helpers.h" - -#include - -namespace esphome { -namespace seeed_mr60bha2 { - -static const uint8_t DATA_BUF_MAX_SIZE = 12; -static const uint8_t FRAME_BUF_MAX_SIZE = 21; -static const uint8_t LEN_TO_HEAD_CKSUM = 8; -static const uint8_t LEN_TO_DATA_FRAME = 9; - -static const uint8_t FRAME_HEADER_BUFFER = 0x01; -static const uint16_t BREATH_RATE_TYPE_BUFFER = 0x0A14; -static const uint16_t HEART_RATE_TYPE_BUFFER = 0x0A15; -static const uint16_t DISTANCE_TYPE_BUFFER = 0x0A16; - -enum FrameLocation { - LOCATE_FRAME_HEADER, - LOCATE_ID_FRAME1, - LOCATE_ID_FRAME2, - LOCATE_LENGTH_FRAME_H, - LOCATE_LENGTH_FRAME_L, - LOCATE_TYPE_FRAME1, - LOCATE_TYPE_FRAME2, - LOCATE_HEAD_CKSUM_FRAME, // Header checksum: [from the first byte to the previous byte of the HEAD_CKSUM bit] - LOCATE_DATA_FRAME, - LOCATE_DATA_CKSUM_FRAME, // Data checksum: [from the first to the previous byte of the DATA_CKSUM bit] - LOCATE_PROCESS_FRAME, -}; - -class MR60BHA2Component : public Component, - public uart::UARTDevice { // The class name must be the name defined by text_sensor.py -#ifdef USE_SENSOR - SUB_SENSOR(breath_rate); - SUB_SENSOR(heart_rate); - SUB_SENSOR(distance); -#endif - - protected: - uint8_t current_frame_locate_; - uint8_t current_frame_buf[FRAME_BUF_MAX_SIZE]; - uint8_t current_data_buf[DATA_BUF_MAX_SIZE]; - uint16_t current_frame_id_; - size_t current_frame_len_; - size_t current_data_frame_len_; - uint16_t current_frame_type_; - uint32_t current_breath_rate_int_; - uint32_t current_heart_rate_int_; - uint32_t current_distance_int_; - - bool validateChecksum(const uint8_t *data, size_t len, uint8_t expected_checksum); - uint8_t calculateChecksum(const uint8_t *data, size_t len); - void splitFrame(uint8_t buffer); - void processFrame(); - - public: - float get_setup_priority() const override { return esphome::setup_priority::LATE; } - void setup() override; - void dump_config() override; - void loop() override; -}; - -} // namespace seeed_mr60bha2 -} // namespace esphome diff --git a/esphome/components/seeed_mr60bha2/sensor.py b/esphome/components/seeed_mr60bha2/sensor.py deleted file mode 100644 index 1ba18296e9..0000000000 --- a/esphome/components/seeed_mr60bha2/sensor.py +++ /dev/null @@ -1,47 +0,0 @@ -import esphome.codegen as cg -from esphome.components import sensor -import esphome.config_validation as cv -from esphome.const import ( - DEVICE_CLASS_DISTANCE, - UNIT_CENTIMETER, - CONF_DISTANCE, -) -from . import CONF_MR60BHA2_ID, MR60BHA2Component - -AUTO_LOAD = ["seeed_mr60bha2"] - -CONF_BREATH_RATE = "breath_rate" -CONF_HEART_RATE = "heart_rate" - -CONFIG_SCHEMA = cv.Schema( - { - cv.GenerateID(CONF_MR60BHA2_ID): cv.use_id(MR60BHA2Component), - cv.Optional(CONF_BREATH_RATE): sensor.sensor_schema( - accuracy_decimals=2, - icon="mdi:counter", - ), - cv.Optional(CONF_HEART_RATE): sensor.sensor_schema( - accuracy_decimals=2, - icon="mdi:counter", - ), - cv.Optional(CONF_DISTANCE): sensor.sensor_schema( - device_class=DEVICE_CLASS_DISTANCE, - unit_of_measurement=UNIT_CENTIMETER, - accuracy_decimals=2, # Specify the number of decimal places - icon="mdi:signal-distance-variant", - ), - } -) - - -async def to_code(config): - mr60bha2_component = await cg.get_variable(config[CONF_MR60BHA2_ID]) - if breath_rate_config := config.get(CONF_BREATH_RATE): - sens = await sensor.new_sensor(breath_rate_config) - cg.add(mr60bha2_component.set_breath_rate_sensor(sens)) - if heart_rate_config := config.get(CONF_HEART_RATE): - sens = await sensor.new_sensor(heart_rate_config) - cg.add(mr60bha2_component.set_heart_rate_sensor(sens)) - if distance_config := config.get(CONF_DISTANCE): - sens = await sensor.new_sensor(distance_config) - cg.add(mr60bha2_component.set_distance_sensor(sens))