Add: Seeed Studio mr60fda2 mmwave sensor (#7576)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Spencer Yan <spencer@spenyan.com>
This commit is contained in:
Citric Lee 2024-11-26 08:53:21 +08:00 committed by GitHub
parent d9d368d38e
commit c0dcecc465
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 845 additions and 0 deletions

View File

@ -354,6 +354,7 @@ esphome/components/sdl/* @clydebarrow
esphome/components/sdm_meter/* @jesserockz @polyfaces
esphome/components/sdp3x/* @Azimath
esphome/components/seeed_mr24hpc1/* @limengdu
esphome/components/seeed_mr60fda2/* @limengdu
esphome/components/selec_meter/* @sourabhjaiswal
esphome/components/select/* @esphome/core
esphome/components/sen0321/* @notjj

View File

@ -0,0 +1,41 @@
import esphome.codegen as cg
from esphome.components import uart
import esphome.config_validation as cv
from esphome.const import CONF_ID
CODEOWNERS = ["@limengdu"]
DEPENDENCIES = ["uart"]
MULTI_CONF = True
mr60fda2_ns = cg.esphome_ns.namespace("seeed_mr60fda2")
MR60FDA2Component = mr60fda2_ns.class_(
"MR60FDA2Component", cg.Component, uart.UARTDevice
)
CONF_MR60FDA2_ID = "mr60fda2_id"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(MR60FDA2Component),
}
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"seeed_mr60fda2",
require_tx=True,
require_rx=True,
baud_rate=115200,
parity="NONE",
stop_bits=1,
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

View File

@ -0,0 +1,33 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import DEVICE_CLASS_OCCUPANCY, DEVICE_CLASS_SAFETY
from . import CONF_MR60FDA2_ID, MR60FDA2Component
DEPENDENCIES = ["seeed_mr60fda2"]
CONF_PEOPLE_EXIST = "people_exist"
CONF_FALL_DETECTED = "fall_detected"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_MR60FDA2_ID): cv.use_id(MR60FDA2Component),
cv.Optional(CONF_PEOPLE_EXIST): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_OCCUPANCY, icon="mdi:motion-sensor"
),
cv.Optional(CONF_FALL_DETECTED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_SAFETY, icon="mdi:emergency"
),
}
async def to_code(config):
mr60fda2_component = await cg.get_variable(config[CONF_MR60FDA2_ID])
if people_exist_config := config.get(CONF_PEOPLE_EXIST):
sens = await binary_sensor.new_binary_sensor(people_exist_config)
cg.add(mr60fda2_component.set_people_exist_binary_sensor(sens))
if is_fall_config := config.get(CONF_FALL_DETECTED):
sens = await binary_sensor.new_binary_sensor(is_fall_config)
cg.add(mr60fda2_component.set_fall_detected_binary_sensor(sens))

View File

@ -0,0 +1,45 @@
import esphome.codegen as cg
from esphome.components import button
import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_RESTART,
DEVICE_CLASS_UPDATE,
ENTITY_CATEGORY_DIAGNOSTIC,
ENTITY_CATEGORY_NONE,
CONF_FACTORY_RESET,
)
from .. import CONF_MR60FDA2_ID, MR60FDA2Component, mr60fda2_ns
DEPENDENCIES = ["seeed_mr60fda2"]
GetRadarParametersButton = mr60fda2_ns.class_("GetRadarParametersButton", button.Button)
ResetRadarButton = mr60fda2_ns.class_("ResetRadarButton", button.Button)
CONF_GET_RADAR_PARAMETERS = "get_radar_parameters"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_MR60FDA2_ID): cv.use_id(MR60FDA2Component),
cv.Optional(CONF_GET_RADAR_PARAMETERS): button.button_schema(
GetRadarParametersButton,
device_class=DEVICE_CLASS_UPDATE,
entity_category=ENTITY_CATEGORY_NONE,
),
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
ResetRadarButton,
device_class=DEVICE_CLASS_RESTART,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
async def to_code(config):
mr60fda2_component = await cg.get_variable(config[CONF_MR60FDA2_ID])
if get_radar_parameters_config := config.get(CONF_GET_RADAR_PARAMETERS):
b = await button.new_button(get_radar_parameters_config)
await cg.register_parented(b, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_get_radar_parameters_button(b))
if factory_reset_config := config.get(CONF_FACTORY_RESET):
b = await button.new_button(factory_reset_config)
await cg.register_parented(b, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_factory_reset_button(b))

View File

@ -0,0 +1,9 @@
#include "get_radar_parameters_button.h"
namespace esphome {
namespace seeed_mr60fda2 {
void GetRadarParametersButton::press_action() { this->parent_->get_radar_parameters(); }
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/button/button.h"
#include "../seeed_mr60fda2.h"
namespace esphome {
namespace seeed_mr60fda2 {
class GetRadarParametersButton : public button::Button, public Parented<MR60FDA2Component> {
public:
GetRadarParametersButton() = default;
protected:
void press_action() override;
};
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,9 @@
#include "reset_radar_button.h"
namespace esphome {
namespace seeed_mr60fda2 {
void ResetRadarButton::press_action() { this->parent_->factory_reset(); }
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/button/button.h"
#include "../seeed_mr60fda2.h"
namespace esphome {
namespace seeed_mr60fda2 {
class ResetRadarButton : public button::Button, public Parented<MR60FDA2Component> {
public:
ResetRadarButton() = default;
protected:
void press_action() override;
};
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,368 @@
#include "seeed_mr60fda2.h"
#include "esphome/core/log.h"
#include <cinttypes>
#include <utility>
namespace esphome {
namespace seeed_mr60fda2 {
static const char *const TAG = "seeed_mr60fda2";
// 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 MR60FDA2Component::dump_config() {
ESP_LOGCONFIG(TAG, "MR60FDA2:");
#ifdef USE_BINARY_SENSOR
LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->people_exist_binary_sensor_);
LOG_BINARY_SENSOR(" ", "Is Fall Binary Sensor", this->fall_detected_binary_sensor_);
#endif
#ifdef USE_BUTTON
LOG_BUTTON(" ", "Get Radar Parameters Button", this->get_radar_parameters_button_);
LOG_BUTTON(" ", "Reset Radar Button", this->factory_reset_button_);
#endif
#ifdef USE_SELECT
LOG_SELECT(" ", "Install Height Select", this->install_height_select_);
LOG_SELECT(" ", "Height Threshold Select", this->height_threshold_select_);
LOG_SELECT(" ", "Sensitivity Select", this->sensitivity_select_);
#endif
}
// Initialisation functions
void MR60FDA2Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up MR60FDA2...");
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->get_radar_parameters();
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 MR60FDA2 complete");
}
// main loop
void MR60FDA2Component::loop() {
uint8_t byte;
// Is there data on the serial port
while (this->available()) {
this->read_byte(&byte);
this->split_frame_(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.
*/
static uint8_t calculate_checksum(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.
*/
static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
return calculate_checksum(data, len) == expected_checksum;
}
static uint8_t find_nearest_index(float value, const float *arr, int size) {
int nearest_index = 0;
float min_diff = std::abs(value - arr[0]);
for (int i = 1; i < size; ++i) {
float diff = std::abs(value - arr[i]);
if (diff < min_diff) {
min_diff = diff;
nearest_index = i;
}
}
return nearest_index;
}
/**
* @brief Convert a float value to a byte array.
*
* This function converts a float value to a byte array.
*
* @param value The float value to convert.
* @param bytes The byte array to store the converted value.
*/
static void float_to_bytes(float value, unsigned char *bytes) {
union {
float float_value;
unsigned char byte_array[4];
} u;
u.float_value = value;
memcpy(bytes, u.byte_array, 4);
}
/**
* @brief Convert a 32-bit unsigned integer to a byte array.
*
* This function converts a 32-bit unsigned integer to a byte array.
*
* @param value The 32-bit unsigned integer to convert.
* @param bytes The byte array to store the converted value.
*/
static void int_to_bytes(uint32_t value, unsigned char *bytes) {
bytes[0] = value & 0xFF;
bytes[1] = (value >> 8) & 0xFF;
bytes[2] = (value >> 16) & 0xFF;
bytes[3] = (value >> 24) & 0xFF;
}
void MR60FDA2Component::split_frame_(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 {
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) {
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_++;
break;
case LOCATE_TYPE_FRAME2:
this->current_frame_type_ += buffer;
if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
(this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
(this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
(this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
this->current_frame_len_++;
this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
this->current_frame_locate_++;
} else {
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
case LOCATE_HEAD_CKSUM_FRAME:
if (validate_checksum(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_LOGV(TAG, "CURRENT_FRAME: %s %s",
format_hex_pretty(this->current_frame_buf_, this->current_frame_len_).c_str(),
format_hex_pretty(&buffer, 1).c_str());
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 (validate_checksum(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->process_frame_();
} else {
ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
format_hex_pretty(this->current_frame_buf_, this->current_frame_len_).c_str(),
format_hex_pretty(&buffer, 1).c_str());
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
default:
break;
}
}
void MR60FDA2Component::process_frame_() {
switch (this->current_frame_type_) {
case IS_FALL_TYPE_BUFFER:
if (this->fall_detected_binary_sensor_ != nullptr) {
this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case PEOPLE_EXIST_TYPE_BUFFER:
if (this->people_exist_binary_sensor_ != nullptr)
this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_INSTALL_HEIGHT:
if (this->current_data_buf_[0]) {
ESP_LOGD(TAG, "Successfully set the mounting height");
} else {
ESP_LOGD(TAG, "Failed to set the mounting height");
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_HEIGHT_THRESHOLD:
if (this->current_data_buf_[0]) {
ESP_LOGD(TAG, "Successfully set the height threshold");
} else {
ESP_LOGD(TAG, "Failed to set the height threshold");
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_SENSITIVITY:
if (this->current_data_buf_[0]) {
ESP_LOGD(TAG, "Successfully set the sensitivity");
} else {
ESP_LOGD(TAG, "Failed to set the sensitivity");
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_PARAMETERS: {
float install_height_float = 0;
float height_threshold_float = 0;
uint32_t current_sensitivity = 0;
if (this->install_height_select_ != nullptr) {
uint32_t current_install_height_int =
encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
install_height_float = bit_cast<float>(current_install_height_int);
uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
}
if (this->height_threshold_select_ != nullptr) {
uint32_t current_height_threshold_int =
encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
height_threshold_float = bit_cast<float>(current_height_threshold_int);
size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
}
if (this->sensitivity_select_ != nullptr) {
current_sensitivity =
encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
}
ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
height_threshold_float, current_sensitivity);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
}
default:
break;
}
}
// Send Heartbeat Packet Command
void MR60FDA2Component::set_install_height(uint8_t index) {
uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
send_data[12] = calculate_checksum(send_data + 8, 4);
this->write_array(send_data, 13);
ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty(send_data, 13).c_str());
}
void MR60FDA2Component::set_height_threshold(uint8_t index) {
uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
send_data[12] = calculate_checksum(send_data + 8, 4);
this->write_array(send_data, 13);
ESP_LOGV(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty(send_data, 13).c_str());
}
void MR60FDA2Component::set_sensitivity(uint8_t index) {
uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
int_to_bytes(SENSITIVITY[index], &send_data[8]);
send_data[12] = calculate_checksum(send_data + 8, 4);
this->write_array(send_data, 13);
ESP_LOGV(TAG, "SEND SET SENSITIVITY: %s", format_hex_pretty(send_data, 13).c_str());
}
void MR60FDA2Component::get_radar_parameters() {
uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
this->write_array(send_data, 8);
ESP_LOGV(TAG, "SEND GET PARAMETERS: %s", format_hex_pretty(send_data, 8).c_str());
}
void MR60FDA2Component::factory_reset() {
uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
this->write_array(send_data, 8);
ESP_LOGV(TAG, "SEND RESET: %s", format_hex_pretty(send_data, 8).c_str());
this->get_radar_parameters();
}
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,101 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#ifdef USE_BUTTON
#include "esphome/components/button/button.h"
#endif
#ifdef USE_SELECT
#include "esphome/components/select/select.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/helpers.h"
#include <map>
namespace esphome {
namespace seeed_mr60fda2 {
static const uint8_t DATA_BUF_MAX_SIZE = 28;
static const uint8_t FRAME_BUF_MAX_SIZE = 37;
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 IS_FALL_TYPE_BUFFER = 0x0E02;
static const uint16_t PEOPLE_EXIST_TYPE_BUFFER = 0x0F09;
static const uint16_t RESULT_INSTALL_HEIGHT = 0x0E04;
static const uint16_t RESULT_PARAMETERS = 0x0E06;
static const uint16_t RESULT_HEIGHT_THRESHOLD = 0x0E08;
static const uint16_t RESULT_SENSITIVITY = 0x0E0A;
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,
};
static const float INSTALL_HEIGHT[7] = {2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f, 3.0f};
static const float HEIGHT_THRESHOLD[7] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
static const float SENSITIVITY[3] = {3, 15, 30};
static const char *const INSTALL_HEIGHT_STR[7] = {"2.4m", "2.5m", "2.6", "2.7m", "2.8", "2.9m", "3.0m"};
static const char *const HEIGHT_THRESHOLD_STR[7] = {"0.0m", "0.1m", "0.2m", "0.3m", "0.4m", "0.5m", "0.6m"};
static const char *const SENSITIVITY_STR[3] = {"1", "2", "3"};
class MR60FDA2Component : public Component,
public uart::UARTDevice { // The class name must be the name defined by text_sensor.py
#ifdef USE_BINARY_SENSOR
SUB_BINARY_SENSOR(people_exist)
SUB_BINARY_SENSOR(fall_detected)
#endif
#ifdef USE_BUTTON
SUB_BUTTON(get_radar_parameters)
SUB_BUTTON(factory_reset)
#endif
#ifdef USE_SELECT
SUB_SELECT(install_height)
SUB_SELECT(height_threshold)
SUB_SELECT(sensitivity)
#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_;
void split_frame_(uint8_t buffer);
void process_frame_();
public:
float get_setup_priority() const override { return esphome::setup_priority::LATE; }
void setup() override;
void dump_config() override;
void loop() override;
void set_install_height(uint8_t index);
void set_height_threshold(uint8_t index);
void set_sensitivity(uint8_t index);
void get_radar_parameters();
void factory_reset();
};
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,59 @@
import esphome.codegen as cg
from esphome.components import select
import esphome.config_validation as cv
from esphome.const import CONF_SENSITIVITY, ENTITY_CATEGORY_CONFIG, ICON_ACCELERATION_Z
from .. import CONF_MR60FDA2_ID, MR60FDA2Component, mr60fda2_ns
DEPENDENCIES = ["seeed_mr60fda2"]
InstallHeightSelect = mr60fda2_ns.class_("InstallHeightSelect", select.Select)
HeightThresholdSelect = mr60fda2_ns.class_("HeightThresholdSelect", select.Select)
SensitivitySelect = mr60fda2_ns.class_("SensitivitySelect", select.Select)
CONF_INSTALL_HEIGHT = "install_height"
CONF_HEIGHT_THRESHOLD = "height_threshold"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_MR60FDA2_ID): cv.use_id(MR60FDA2Component),
cv.Optional(CONF_INSTALL_HEIGHT): select.select_schema(
InstallHeightSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_ACCELERATION_Z,
),
cv.Optional(CONF_HEIGHT_THRESHOLD): select.select_schema(
HeightThresholdSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_ACCELERATION_Z,
),
cv.Optional(CONF_SENSITIVITY): select.select_schema(
SensitivitySelect,
entity_category=ENTITY_CATEGORY_CONFIG,
),
}
async def to_code(config):
mr60fda2_component = await cg.get_variable(config[CONF_MR60FDA2_ID])
if install_height_config := config.get(CONF_INSTALL_HEIGHT):
s = await select.new_select(
install_height_config,
options=["2.4m", "2.5m", "2.6m", "2.7m", "2.8m", "2.9m", "3.0m"],
)
await cg.register_parented(s, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_install_height_select(s))
if height_threshold_config := config.get(CONF_HEIGHT_THRESHOLD):
s = await select.new_select(
height_threshold_config,
options=["0.0m", "0.1m", "0.2m", "0.3m", "0.4m", "0.5m", "0.6m"],
)
await cg.register_parented(s, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_height_threshold_select(s))
if sensitivity_config := config.get(CONF_SENSITIVITY):
s = await select.new_select(
sensitivity_config,
options=["1", "2", "3"],
)
await cg.register_parented(s, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_sensitivity_select(s))

View File

@ -0,0 +1,15 @@
#include "height_threshold_select.h"
namespace esphome {
namespace seeed_mr60fda2 {
void HeightThresholdSelect::control(const std::string &value) {
this->publish_state(value);
auto index = this->index_of(value);
if (index.has_value()) {
this->parent_->set_height_threshold(index.value());
}
}
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../seeed_mr60fda2.h"
namespace esphome {
namespace seeed_mr60fda2 {
class HeightThresholdSelect : public select::Select, public Parented<MR60FDA2Component> {
public:
HeightThresholdSelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,15 @@
#include "install_height_select.h"
namespace esphome {
namespace seeed_mr60fda2 {
void InstallHeightSelect::control(const std::string &value) {
this->publish_state(value);
auto index = this->index_of(value);
if (index.has_value()) {
this->parent_->set_install_height(index.value());
}
}
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../seeed_mr60fda2.h"
namespace esphome {
namespace seeed_mr60fda2 {
class InstallHeightSelect : public select::Select, public Parented<MR60FDA2Component> {
public:
InstallHeightSelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,15 @@
#include "sensitivity_select.h"
namespace esphome {
namespace seeed_mr60fda2 {
void SensitivitySelect::control(const std::string &value) {
this->publish_state(value);
auto index = this->index_of(value);
if (index.has_value()) {
this->parent_->set_sensitivity(index.value());
}
}
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../seeed_mr60fda2.h"
namespace esphome {
namespace seeed_mr60fda2 {
class SensitivitySelect : public select::Select, public Parented<MR60FDA2Component> {
public:
SensitivitySelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace seeed_mr60fda2
} // namespace esphome

View File

@ -0,0 +1,34 @@
uart:
- id: seeed_mr60fda2_uart
tx_pin: ${uart_tx_pin}
rx_pin: ${uart_rx_pin}
baud_rate: 115200
parity: NONE
stop_bits: 1
seeed_mr60fda2:
id: my_seeed_mr60fda2
uart_id: seeed_mr60fda2_uart
binary_sensor:
- platform: seeed_mr60fda2
people_exist:
name: "Person Information"
fall_detected:
name: "Falling Information"
button:
- platform: seeed_mr60fda2
get_radar_parameters:
name: "Get Radar Parameters"
factory_reset:
name: "Reset"
select:
- platform: seeed_mr60fda2
install_height:
name: "Set Install Height"
height_threshold:
name: "Set Height Threshold"
sensitivity:
name: "Set Sensitivity"

View File

@ -0,0 +1,5 @@
substitutions:
uart_tx_pin: GPIO5
uart_rx_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
uart_tx_pin: GPIO5
uart_rx_pin: GPIO4
<<: !include common.yaml